Author Topic: send HTTP GET request  (Read 4754 times)

alangstein

  • Newbie
  • *
  • Posts: 2
send HTTP GET request
« on: March 26, 2016, 11:47:56 am »
I would like to use the OAK to send data to a data stream on data.sparkfun.com.  I am a little confused/unsure of how to use the OAK to connect to the internet versus a standard ESP8266 board.  Does the OAK maintain a persistent connection to the wifi, do I need to #include <ESP8266WiFi.h>, do I invoke WiFiClient client; etc...  Presumably OAK already knows my SSID and password but I don't know how to invoke those functions.  Also, from my reading, Serial doesn't work.

Typically my code would include the following:


Code: [Select]

#include <ESP8266WiFi.h>

const char* ssid     = "................";
const char* password = "................";

const char* host = "data.sparkfun.com";
const char* publicKey = "................";
const char* privateKey = "................";

void setup() {
  Serial.begin(115200);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("ESP8266 Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  delay(5000);

  // Do stuff to get data to upload here

  Serial.print("connecting to ");
  Serial.println(host);
 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
 
  // We now create a URI for the request
  String url = "/input/";
  url += publicKey;
  url += "?private_key=";
  url += privateKey;
  url += "&data=";
  url += data;
 
  Serial.print("Requesting URL: ");
  Serial.println(url);
 
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(10);
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
 
  Serial.println();
  Serial.println("closing connection");
  delay(600000); // Send data every 10 minutes
}


Nevyn

  • Newbie
  • *
  • Posts: 25
Re: send HTTP GET request
« Reply #1 on: March 28, 2016, 12:23:16 am »
I managed to get this working yesterday afternoon.  My WiFi setup code is:

Code: [Select]
    WiFi.begin();
    while (WiFi.status() != WL_CONNECTED)
    {
      delay(500);
      Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

Note the WiFi credentials are not used in the WiFi.begin method as the Oak sorts that out.

My code to post the data to Sparkfun's data service is:

Code: [Select]
    strcpy(url, phantPage);
    strcat(url, "?private_key=*********************");
    strcat(url, "&airpressure=");
    strcat(url, ftoa(number, pressure / 100, 0));
    strcat(url, "&groundmoisture=0");
    strcat(url, "&groundtemperature=0");
    strcat(url, "&temperature=");
    strcat(url, ftoa(number, temperature, 2));
    strcat(url, "&humidity=");
    strcat(url, ftoa(number, humidity, 2));
    strcat(url, "&luminosity=");
    strcat(url, ftoa(number, lux, 2));
    strcat(url, "&rainfall=0");
    strcat(url, "&ultravioletlight=");
    strcat(url, ftoa(number, ultraviolet, 0));
    strcat(url, "&winddirection=0");
    strcat(url, "&windspeed=0");
    //
    //  Send the data to Phant (Sparkfun's data logging service).
    //
    Serial.print("URL: ");
    Serial.println(url);
    http.begin(phantDomain, 80, url);
    int httpCode = http.GET();
    Serial.print("Status code: ");
    Serial.println(httpCode);
    String response = http.getString();
    DebugMessage((char *) response.c_str());

Where phantPage is set to /input/StreamPublicKey and phantDomain is data.sparkfun.com

Hope this helps,
Mark
« Last Edit: March 28, 2016, 12:30:26 am by Nevyn »

alangstein

  • Newbie
  • *
  • Posts: 2
Re: send HTTP GET request
« Reply #2 on: March 28, 2016, 12:59:12 pm »
Thanks, this look promising.  I will play around with it next weekend  :)

defragster

  • Sr. Member
  • ****
  • Posts: 467
Re: send HTTP GET request
« Reply #3 on: April 12, 2016, 04:28:29 pm »
I keep seeing cool Connectivity methods in various threads.  Starting one thread to track them might work but could get polluted quickly - it could become a WIKI section.  It occurred to me putting a unique string once per Thread where stuff works could at least allow a forum Search to work.
SFUN.Phant_http.Get::OAK_CONNECT