Concise ESP8266 Code

The below code uses a nice mechanism to send commands to ESP8266 and wait for Response and print it. The example uses a temperature sensor (connected to A0) to upload the data to sparkfun cloud using TCP protocol.

#include "SoftwareSerial.h"
SoftwareSerial Serial1(2,3);

void sendToESP8266AndWaitForResponse (char *cmd, char *resp, bool waitForResponse, int duration) {
String bytes;
Serial.print ("CMD: "); Serial.println(cmd);
do {
Serial.print(".");
Serial1.println (cmd);
delay(duration);
bytes = Serial1.readString();
} while ( (waitForResponse) && (bytes.indexOf(resp)<0));
Serial.print ("RESPONSE: ");
Serial.print (bytes.c_str());
Serial.println("\n------------------------------");
}

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(9, OUTPUT);
Serial.begin(9600);
Serial1.begin(9600);

// Setup Wifi as STA and connect to AP
sendToESP8266AndWaitForResponse ("AT+CWMODE=1", "", false, 5);
sendToESP8266AndWaitForResponse ("AT+CWJAP=\"SKS Cottage\",\"kayar123\"", "OK", true, 50);
sendToESP8266AndWaitForResponse ("AT+CIPMUX=0", "OK", false, 50);
}

// the loop function runs over and over again forever
void loop() {
int rawvoltage= analogRead(A0);
float millivolts= (rawvoltage/1024.0) * 5000;
float temp= millivolts/10; //celcius

sendToESP8266AndWaitForResponse ("AT+CIPSTART=\"TCP\",\"data.sparkfun.com\",80", "Linked", true, 10);
char cmd[200],cipsend[100];
sprintf (cmd, "POST /input/QGyLQKYb71F2Q1qMqQER?private_key=JqyBXeWg9ViqeBy9yevR&temperature=%d.%04d HTTP/1.0\r\n\r\n Host: data.sparkfun.com\r\n\r\n", (int)temp,(int)trunc((temp-(int)temp)*10000)); 
sprintf (cipsend, "AT+CIPSEND=%d",strlen(cmd));

sendToESP8266AndWaitForResponse (cipsend, ">", true, 10);
sendToESP8266AndWaitForResponse (cmd, "", false, 10);
delay(10000); 
}

The below code is modified to push data to ThingSpeak cloud.

#include "SoftwareSerial.h"

#define WIFISSID "SKS Cottage" // WIFI Username
#define WIFIPASS "kayar123" // WIFI Password
#define SERVERIP "api.thingspeak.com"// Server to post the update. "data.sparkfun.com"
#define POSTURL "POST /update.json?api_key=MR00DLM5QJLAYZ9C&field1=" //"POST /input/QGyLQKYb71F2Q1qMqQER?private_key=JqyBXeWg9ViqeBy9yevR&temperature=" // Use this for

SoftwareSerial Serial1(2,3);
void sendToESP8266AndWaitForResponse (const char *cmd, const char *resp, bool waitForResponse, int duration) {
String bytes;
Serial.print ("CMD: "); Serial.println(cmd);
do {
Serial.print(".");
Serial1.println (cmd);
delay(duration);
bytes = Serial1.readString();
} while ( (waitForResponse) && (bytes.indexOf(resp)<0));
Serial.print ("RESPONSE: ");
Serial.print (bytes.c_str());
Serial.println("\n------------------------------");
}

void connectToWIFI() {
String cwjapCmd = "AT+CWJAP=\"";
cwjapCmd+=WIFISSID; cwjapCmd+="\",\""; cwjapCmd+=WIFIPASS; cwjapCmd+="\"";
sendToESP8266AndWaitForResponse (cwjapCmd.c_str(), "OK", true, 50);
}

void connectToServer() {
String cipstartCmd = "AT+CIPSTART=\"TCP\",\"";
cipstartCmd += SERVERIP; cipstartCmd += "\",80";
sendToESP8266AndWaitForResponse (cipstartCmd.c_str(), "Linked", true, 10);
}

// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(9600);
Serial1.begin(9600);

// Setup Wifi as STA and connect to AP
sendToESP8266AndWaitForResponse ("AT+CWMODE=1", "", false, 5);
connectToWIFI();
sendToESP8266AndWaitForResponse ("AT+CIPMUX=0", "OK", false, 50);
}

// the loop function runs over and over again forever
void loop() {
int rawvoltage= analogRead(A0);
float millivolts= (rawvoltage/1024.0) * 5000;
float temp= millivolts/10; //celcius

connectToServer();
char cmd[200],cipsend[100];
sprintf (cmd, "%s%d.%04d HTTP/1.0\r\n\r\n Host: %s\r\n\r\n",POSTURL, (int)temp,(int)trunc((temp-(int)temp)*10000),SERVERIP); 
sprintf (cipsend, "AT+CIPSEND=%d",strlen(cmd));

sendToESP8266AndWaitForResponse (cipsend, ">", true, 10);
sendToESP8266AndWaitForResponse (cmd, "", false, 10);
delay(10000); 
}

One thought on “Concise ESP8266 Code

Leave a Reply