Hello everyone. I'm trying to send data from ESP01 WIFI module using Serial Port and Method POST
HARDWARE:
ESP8266 WIFI MODULE ESP01
ESP01 ADAPTER
SOFTWARE
Arduino Uno
STEPS THAT I'M FOLLOWING:
1.- First I need to send data through the Serial port from Arduino Uno to the esp8266 wifi module
2.- This module receives the data.
3.- It's connects to the WIFI.
4.- Sends the data with the METHOD POST to an external server (REST API)
I am new to Arduino. I appreciate your time and help.
This is my code:
#include <SoftwareSerial.h>
SoftwareSerial wifimodule(10, 11); /* RX, TX */
String ssid = "name";
String password = "pass";
String data; //data for the POST request
String server = "http://192.168.0.4:8080/api/v1"; // post route
String uri = "/registrar"; //server side script to call
//reset the esp8266 module
void reset() {
wifimodule.println("AT+RST");
delay(3000);
if(wifimodule.find("OK")){
Serial.println("Module Reset");
}
}
//connect to your wifi network
void connectWifi() {
String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + password + "\"";
wifimodule.println(cmd);
delay(4000);
if (wifimodule.find("OK")) {
Serial.println("Connected!");
} else {
connectWifi();
Serial.println("Cannot connect to wifi");
}
}
void setup() {
Serial.begin(9600); //start regular serial for PC -> Arduino comms
wifimodule.begin(115200); //start SoftSerial for ESP comms
reset(); //call the ESP reset function
connectWifi(); //call the ESP connection function
delay(2000);
Serial.println("begin with configuration...");
}
void loop() {
Serial.println("begin...");
savedata();
delay(2000);
Serial.println("end...");
}
void savedata() {
String ph = "2.5";
String uv = "200";
String co2 = "240";
data = "{\"ph\":\"" + ph + "\",\"uv\":\"" + uv + "\",\"co2\":\"" + co2 + "\"}";
httppost();
delay(5000);
}
void httppost() {
wifimodule.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80"); //start a TCP connection.
if (wifimodule.find("OK")) {
Serial.println("TCP connection ready");
}
delay(1000);
String postRequest = String("POST ");
postRequest = postRequest + uri + " HTTP/1.1\r\n" +
"Host: " + server + "\r\n" +
"Accept: *" + "/" + "*\r\n" +
"Content-Length: " + data.length() + "\r\n" +
"Content-Type: application/json\r\n" +
"\r\n" + data;
Serial.println("Post Request text: "); //see what the fully built POST request looks like
Serial.println(postRequest);
String sendCmd = "AT+CIPSEND="; //determine the number of caracters to be sent.
wifimodule.print(sendCmd);
wifimodule.println(postRequest.length());
delay(500);
if (wifimodule.find(">")) {
Serial.println("Sending..");
wifimodule.print(postRequest);
Serial.println("Data to send: ");
Serial.println(data);
Serial.print("Data length: ");
Serial.println(data.length());
Serial.print("Request length: ");
Serial.println(postRequest.length());
Serial.println("Request Sent:");
Serial.println(postRequest);
if ( wifimodule.find("SEND OK")) {
Serial.println("Packet sent");
while (wifimodule.available()) {
String tmpResp = wifimodule.readString();
Serial.println(tmpResp);
}
// close the connection
wifimodule.println("AT+CIPCLOSE");
}
}
}
The program compiles normally and the serial monitor is only blank but when executing the AT commands they are not executed.
I don't know what to do. Please, help me
Thanks in advance for your help.