Introduction: ESP8266 Building Blocks: Send Data to a Web Server With GET

About: IoT - Internet of Things. Iota - small thing. Thingamajig - An object whose name can't be recalled. Iotamajig - A little unnamed internet connected gizmo!

This instructable is part of my series on introducing people to the ESP8266-01 WiFi transceiver. The goal of this series is to act as a basic code repository for easy reuse, as well as to provide some foundational building blocks for people new to the ESP. The "building blocks" series will contain just the basic code needed to complete the object of the instructable, with a (hopefully) thorough explanation of what's going on and why.

Here we are going to take a look at one way of sending data to a web server: the HTTP GET.

We'll take a look at using the POST method in another instructable.

Please Note: This code will NOT run on it's own, as there is no code included actually connecting it to the internet. See this instructable for the code to add to make that connection (building blocks!).

Step 1: The Code

#include 

//IP or name of address root: ie: google.com
//NOT google.com/nothing/after/the/dotcom.html
const char* hostGet = "mydatasite.com"; 


void postData() {

   WiFiClient clientGet;
   const int httpGetPort = 80;

   //the path and file to send the data to:
   String urlGet = "/data/collector.php";

 
  // We now create and add parameters:
  String src = "ESP";
  String typ = "flt";
  String nam = "temp";
  String vint = "92"; 

  urlGet += "?src=" + src + "&typ=" + typ + "&nam=" + nam + "&int=" + vint;
   
      Serial.print(">>> Connecting to host: ");
      Serial.println(hostGet);
      
       if (!clientGet.connect(hostGet, httpGetPort)) {
        Serial.print("Connection failed: ");
        Serial.print(hostGet);
      } else {
          clientGet.println("GET " + urlGet + " HTTP/1.1");
          clientGet.print("Host: ");
          clientGet.println(hostGet);
          clientGet.println("User-Agent: ESP8266/1.0");
          clientGet.println("Connection: close\r\n\r\n");
          
          unsigned long timeoutP = millis();
          while (clientGet.available() == 0) {
            
            if (millis() - timeoutP > 10000) {
              Serial.print(">>> Client Timeout: ");
              Serial.println(hostGet);
              clientGet.stop();
              return;
            }
          }

          //just checks the 1st line of the server response. Could be expanded if needed.
          while(clientGet.available()){
            String retLine = clientGet.readStringUntil('\r');
            Serial.println(retLine);
            break; 
          }

      } //end client connection if else
                        
      Serial.print(">>> Closing host: ");
      Serial.println(hostGet);
          
      clientGet.stop();

}

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

void loop() {

  postData();

  delay(10000);

}

Step 2: Upload Your Code

You'll need to update a number of the parameters, as well as add the WiFiCon() function from here (or something similar). Parameters that need to change are the host, the URL, and the data parameters - we'll look at these in the explanation step.

Open the serial monitor in the Arduino IDE. This is so once your code uploads, we can see the messages coming over serial from the ESP.

Use a setup like the one laid out here to upload your code.

Once the code is done uploading, you should immediately start to see some messages in the serial monitor. If you don't, power down the ESP, turn the switch off of flash mode, and repower the ESP.

After the code is uploaded, a simple wiring like the one at the top of this instructable is all you need to actually run the ESP.

Step 3: Explanation

The GET method of posting data is a bit simpler than the POST method, and will work for most of your needs. The nice thing about GET, is that you simply build your data out into the string of the URL.

Let's say you want to send data to a site called mysite.com.

It has a page that processes data called data.php.

And you have two pieces of data you want to send: name and id.

If data.php is set up to parse variables named "name" and "id", the URL you need to generate is:

mysite.com/data.php?name=Jimmy&id=52

Notice the variables are separated from the page by a ?, and from each other by an &. You can send quite a few variables this way - but the GET tends to work best for simple types of data. If you need to send long text or something more complicated, then we need to take a look at the POST method.

IoT is all about data and devices. If you need to send some data somewhere, hopefully this has helped you out!