Team 5 – Writing to the Web With ESP32

In this part, our team is going to guide you how to use the ESP32 to write data to the remote server. This guide assumes that you have a PHP API up and running that at least have a POST route to receive data and a ESP32 with you. Our server accepts a string of data and writes that to a text file.

This is the ESP32 that we’re using

ESP32

If you have a mini USB cable, you could plug that into the ESP32 and then plug the other end to your laptop. In order to use ESP32 with Arduino software, you should first install the ESP32 Dev Modules following this instruction.

After that we start writing code, below is the code that we’re using:

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = <your-hotspot-ssid>;
const char* password = <your-hotspot-password;

String strTemp = "25.3";
String strPressure = "0.00";
String strLon = "24.947167639512955";
String strLat = "60.16757447480902";
String strAlt = "0.00";
String numSats = "0";


const char* server = <your-api-address>;

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

  WiFi.begin(ssid, password);
  Serial.println("Connecting to WIFI…");
  
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("After 10 seconds the first reading will be displayed");
  Serial.println("");
}

void loop() {
  WriteWeb();
}

void WriteWeb() {
   if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;

    http.begin(server);
    http.addHeader("Content-Type", "text/plain");
    
    int httpResponseCode = http.POST(strLat + "," + strLon + "," + strTemp + "," + strPressure + "," + strAlt + "," + numSats);
    String response = http.getString();
    
    if (httpResponseCode > 0) {
      Serial.print("HTTP Response Code is: ");
      Serial.println(httpResponseCode);  
      Serial.println("String was written into server");  
    } else {
      Serial.print("Error sending on POST: ");
      Serial.println(httpResponseCode);  
      Serial.println(response);  
    }
    http.end();
  } else {
    Serial.println("Wifi is disconnected");  
  }

  delay(10000); 
}

It looks complicated but we could break down the code and explain it for you

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = <your-hotspot-ssid>;
const char* password = <your-hotspot-password;

String strTemp = "25.3";
String strPressure = "0.00";
String strLon = "24.947167639512955";
String strLat = "60.16757447480902";
String strAlt = "0.00";
String numSats = "0";

const char* server = <your-api-endpoint>;

First, we include the Wifi and HTTPClient libraries. These 2 come along when you install ESP32 Dev Modules and switch Arduino board to this one. After that, we have to define our hotspot ssid and password, you could use your phone hotspot for this. All of the String variables are just hard-coded data for printing out a full string to post to the server. Finally, we define also the endpoint that we’re going to post the data to.

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

  WiFi.begin(ssid, password);
  Serial.println("Connecting to WIFI…");
  
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("After 10 seconds the first reading will be displayed");
  Serial.println("");
}

The above code block is for setting up the ESP32 and checking the Wifi. When the code is uploaded to the ESP32, we can monitor it through Serial Monitor in Arduino. It starts by printing out “Connecting to Wifi…“. If there’s no Wifi or something wrong happens, it continues printing “.“. If everything is fine, it will prints the IP address and then move on from that.

The WriteWeb() function is where we utilize all of the String variables, combine them into a full string and then use HTTPClient library to post it to the server. We use this function inside a loop, because we want it to write to the server every 10 seconds. The full string looks something like this:

60.16757447480902,24.947167639512955,25.3,0.00,0.00,0

So that’s basically how we can use ESP32 to post data to the server! Thanks for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.