Author Topic: HTTP Requests from Oak  (Read 677 times)

creechonwheels

  • Newbie
  • *
  • Posts: 1
HTTP Requests from Oak
« on: May 15, 2017, 02:11:23 pm »
Trying hard to make an Oak project that allows me to sense a light level and upload it to the cloud. I am looking to just do data logging to an SQL database and so I wrote this whole API on the server end. I cannot find anywhere online explicit instructions on making a basic http request from Oak via the built-in WiFi. I have experience doing this with Arduino and various WiFi boards but every time I try to install the Particle HTTPClient library I get errors. Is there an alternative way to initialize my connection?

digi_guy

  • Jr. Member
  • **
  • Posts: 81
Re: HTTP Requests from Oak
« Reply #1 on: May 16, 2017, 08:01:03 am »
I did something similar with an online service called Carriots
http://digistump.com/board/index.php/topic,2234.0.html

From what I remember, some of the basics are:

Code: [Select]
#include "ESP8266WiFi.h"


IPAddress server(82,223,244,60);  //desired  IP Address


WiFiClient client;


void sendStream()
{

  if (client.connect(server, 80)) {   // If there's a successful connection to the IP address on port 80
   
    // Make a HTTP request
    client.println("POST /streams HTTP/1.1");
    client.println("Host: api.carriots.com");
    client.println("Accept: application/json");
    client.println("User-Agent: Arduino-Carriots");
    client.println("Content-Type: application/json");
    client.print("carriots.apikey: ");
    client.println(APIKEY);
    client.print("Content-Length: ");
    int thisLength = json.length();
    client.println(thisLength);
    client.println("Connection: close");
    client.println();

    client.println(json);
  }
  else {
    // If you didn't get a connection to the server:
    Particle.publish("Status", "connection failed");
  }
}


PeterF

  • Hero Member
  • *****
  • Posts: 805
Re: HTTP Requests from Oak
« Reply #2 on: May 16, 2017, 06:32:28 pm »
That looks to be the basics of what one of my nodes does when posting data to thingspeak... I hacked away most of the stuff that is sensor or peculiar to my coad... so should be left mostly with just the remains necessary to do the wifi talk to server bit.

Code: [Select]
WiFiClient client;

String myWriteAPIKey = "";   // Put your API key here
String fieldToPost = "field1";               // Put the field to update here
const char * server_ip = "184.106.153.149";
const char * server_dns = "api.thingspeak.com";

void setup(void)
{
  Particle.publish("oak/userrom/startup", "DS18x20_Particle_Thingspeak v0.6", 60, PRIVATE);
}

void loop(void)
{
 updateThingspeak("field1",tempC);
}

// thingspeak needs minimum 15 sec delay between updates
boolean updateThingspeak(String field, float data)
{
  //try through DNS first
  if (client.connect(server_dns, 80))
  {
    //construct ThinkSpeak POST URL
    String postStr = myWriteAPIKey;
    postStr += "&";
    postStr += field;
    postStr += "=";
    postStr += String(data);
    postStr += "\r\n\r\n";

    //process
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + myWriteAPIKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);

    client.stop();

    return true;
  } // END if(client.connect())
    else
    {
      return false; //failed to connect
    }
  }
}