-->
Page 1 of 1

2 ESP8266 communicating and sending data to Thingspeak

PostPosted: Sat Mar 24, 2018 12:39 pm
by V3N0MX
Hello guys!

I worked on a project where I use a DHT22 sensor to read humidity and temperature in my room. I adapted some code I found on the Internet, because I'm new to ESP8266.

I use 2 ESP8266 (NodeMCU 1.0 ESP12-E) boards. The first board (server) is connected to the DHT22 Sensor and it reads temperature & humidity in my room once every 5 minutes, sends the data to the other board (client) and then goes into deepsleep, then after 5 minutes it wakes up, makes another reading and the cycle goes on. The other board (client) listens to the server and its just sending data to Thingspeak (for now).

All works fine, I can monitor the data, see the charts online or on the mobile App. The problem I face is that I want to change the code in order to be able to control my heating system through a relay. This means that I want to set up a temperature through a HTML webpage, for example I want to type it into a Form and Submit it through a Button (of course the HTML code should be on the second ESP which is always on). I made an if statement so it compares my room temperature and if it is higher than the temperature I set, the relay goes OFF and the heating system stops, else the heating system is ON until I reach the temperature I set. I declared the variables I need for this (temp, settemp and relay), but for now I can only SET the temperature inside Arduino IDE environment, I don't know exactly how to do it through a website...

I need some help with creating the HTML Website and the Form (a simple textform & a button), to be able to update my settemp variable. I tried to adapt some code but I got some weird errors. Is it possible to actually modify my ClientCode so it's able to create a WebSite and update the temperature everytime I press the Submit button and also send the data to Thingspeak? Or i need to remake everything because the client-code is not made for this?

Thanks in advance. Any Help is appreciated ! (the Code is in the attachments)

Re: 2 ESP8266 communicating and sending data to Thingspeak

PostPosted: Sat Mar 24, 2018 8:40 pm
by rudy
The following is not fancy. I didn't want to go to much effort because I think it isn't what you really want, although it does what you asked for.

I think that you really want something that will be accessible outside of your home network. The problem with that is making it available and secure. I would think that you should be able to send a temperature setting through Thingspeak and it can be picked up by your ESP.

Code: Select all//  http://www.esp8266.com/viewtopic.php?f=8&t=4897&start=5

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

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

float settemp = 22.5;

ESP8266WebServer webserver(80);

static const char PROGMEM INDEX_HTML[] = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<title>Set Temperature</title>
</head>
<body>
<form method="post" action="/save" >
Temperature Setpoint:<br><input name="settemp" type="text" size="16" value="" ><br><br>
<input type="submit" name="clk_action" value="accept">
</form>
</body>
</html>
)rawliteral";

void handleRoot() {
  webserver.send(200, "text/html", INDEX_HTML);
}

void handleNotFound() {
  webserver.send(404, "text/plain", "Page not found ...");
}

void handleSave() {
  String str = "Settings Saved ...\r\n";
 
  Serial.print("number of arguments ");
  Serial.println(webserver.args());                    // number of arguments
 
  if (webserver.args() > 0 ) {
    for ( uint8_t i = 0; i < webserver.args(); i++ ) {
      str += webserver.argName(i) + " = " + webserver.arg(i) + "\r\n";
     
      Serial.println("Arg "+ String(i)+"="+ webserver.argName(i));     
      Serial.println("Arg "+ String(i)+"="+ webserver.arg(i));
    }
   
String inString = ""; 
      inString = (webserver.arg(0));
      settemp = inString.toFloat();
      Serial.print("settemp ");
      Serial.println(settemp);
      Serial.println();
  }
  webserver.send(200, "text/plain", str.c_str());
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

 
  webserver.on("/", handleRoot);
  webserver.on("/save", handleSave);
  webserver.onNotFound(handleNotFound);
  webserver.begin();
  Serial.println("Web server has started");
  Serial.println("----------------------");
}

void loop() {
  webserver.handleClient();
}

Re: 2 ESP8266 communicating and sending data to Thingspeak

PostPosted: Sun Mar 25, 2018 1:47 pm
by V3N0MX
rudy wrote:The following is not fancy. I didn't want to go to much effort because I think it isn't what you really want, although it does what you asked for.

I think that you really want something that will be accessible outside of your home network. The problem with that is making it available and secure. I would think that you should be able to send a temperature setting through Thingspeak and it can be picked up by your ESP.

Code: Select all//  http://www.esp8266.com/viewtopic.php?f=8&t=4897&start=5

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

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

float settemp = 22.5;

ESP8266WebServer webserver(80);

static const char PROGMEM INDEX_HTML[] = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<title>Set Temperature</title>
</head>
<body>
<form method="post" action="/save" >
Temperature Setpoint:<br><input name="settemp" type="text" size="16" value="" ><br><br>
<input type="submit" name="clk_action" value="accept">
</form>
</body>
</html>
)rawliteral";

void handleRoot() {
  webserver.send(200, "text/html", INDEX_HTML);
}

void handleNotFound() {
  webserver.send(404, "text/plain", "Page not found ...");
}

void handleSave() {
  String str = "Settings Saved ...\r\n";
 
  Serial.print("number of arguments ");
  Serial.println(webserver.args());                    // number of arguments
 
  if (webserver.args() > 0 ) {
    for ( uint8_t i = 0; i < webserver.args(); i++ ) {
      str += webserver.argName(i) + " = " + webserver.arg(i) + "\r\n";
     
      Serial.println("Arg "+ String(i)+"="+ webserver.argName(i));     
      Serial.println("Arg "+ String(i)+"="+ webserver.arg(i));
    }
   
String inString = ""; 
      inString = (webserver.arg(0));
      settemp = inString.toFloat();
      Serial.print("settemp ");
      Serial.println(settemp);
      Serial.println();
  }
  webserver.send(200, "text/plain", str.c_str());
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

 
  webserver.on("/", handleRoot);
  webserver.on("/save", handleSave);
  webserver.onNotFound(handleNotFound);
  webserver.begin();
  Serial.println("Web server has started");
  Serial.println("----------------------");
}

void loop() {
  webserver.handleClient();
}



Your code is exactly what I wanted to do, it works like a charm, even though you say it's not fancy :D
I modified the client-code to send the data to Thingspeak and also create the webserver with the Setup Temperature form & button. Here is the code if someone wants to do this project:

Code: Select all//  http://www.esp8266.com/viewtopic.php?f=8&t=4897&start=5

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>


// IoT platform Credentials
String apiKey = "*****";
const char* logServer = "api.thingspeak.com";
const char* ssid = "*****";
const char* password = "*****";

float settemp = 22.5;
float temp;
int Relay = 13; // GPIO13
ESP8266WebServer webserver(80);

static const char PROGMEM INDEX_HTML[] = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<title>Set Temperature</title>
</head>
<body>
<form method="post" action="/save" >
Temperature Setpoint:<br><input name="settemp" type="text" size="16" value="" ><br><br>
<input type="submit" name="clk_action" value="accept">
</form>
</body>
</html>
)rawliteral";

void handleRoot() {
  webserver.send(200, "text/html", INDEX_HTML);
}

void handleNotFound() {
  webserver.send(404, "text/plain", "Page not found ...");
}

void handleSave() {
  String str = "Settings Saved ...\r\n";
 
  Serial.print("number of arguments ");
  Serial.println(webserver.args());                    // number of arguments
 
  if (webserver.args() > 0 ) {
    for ( uint8_t i = 0; i < webserver.args(); i++ ) {
      str += webserver.argName(i) + " = " + webserver.arg(i) + "\r\n";
     
      Serial.println("Arg "+ String(i)+"="+ webserver.argName(i));     
      Serial.println("Arg "+ String(i)+"="+ webserver.arg(i));
    }
   
String inString = ""; 
      inString = (webserver.arg(0));
      settemp = inString.toFloat();
      Serial.print("settemp ");
      Serial.println(settemp);
      Serial.println();
  }
  webserver.send(200, "text/plain", str.c_str());
}

void setup() {
  Serial.begin(115200);
    pinMode(Relay, OUTPUT);
    digitalWrite(Relay, LOW);
   
  WiFi.begin(ssid, password);
  WiFi.mode(WIFI_AP_STA);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  webserver.on("/", handleRoot);
  webserver.on("/save", handleSave);
  webserver.onNotFound(handleNotFound);
  webserver.begin();
  Serial.println("Web server has started");
  Serial.println("----------------------");
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  WiFi.mode(WIFI_AP_STA);
  setupAccessPoint();
}

// Handling the / root web page from my server
void handle_index() {
  webserver.send(200, "text/plain", "");
}

// Handling the /feed page from my server
void handle_feed() {
  String t = webserver.arg("temp");
  String h = webserver.arg("hum");
  temp=t.toFloat();
 

  webserver.send(200, "text/plain", "This is response to client");
  setupStMode(t, h);
}

void setupAccessPoint(){


  Serial.println("** SETUP ACCESS POINT **");
  Serial.println("- disconnect from any other modes");
  WiFi.disconnect();
  Serial.println("- start ap with SID: "+ String(ssid));
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("- AP IP address is :");
  Serial.print(myIP);
  setupServer();
}

void setupServer(){
  Serial.println("** SETUP SERVER **");
  Serial.println("- starting server :");
  Serial.print(WiFi.localIP());
  webserver.on("/", handle_index);
  webserver.on("/feed", handle_feed);
  webserver.begin();
};

void setupStMode(String t, String v){
  Serial.println("** SETUP STATION MODE **");
  Serial.println("- disconnect from any other modes");
  WiFi.disconnect();
  Serial.println();
  Serial.println("- connecting to Home Router SID: **********");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("- succesfully connected");
  Serial.println("- starting client");
 
  WiFiClient client;

  Serial.println("- connecting to Database server: " + String(logServer));
  if (client.connect(logServer, 80)) {
    Serial.println("- succesfully connected");
    String postStr = apiKey;
    postStr += "&field1=";
    postStr += String(t);
    postStr += "&field2=";
    postStr += String(v);
    postStr += "\r\n\r\n";
    Serial.println("- sending data...");
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
  }
  client.stop();
  Serial.println("- stopping the client");
  Serial.println("Temperature read:");
  Serial.println(temp,1);
  Serial.println("Temperature set:");
  Serial.println(settemp,1);
  if (temp < settemp)
  {
    digitalWrite(Relay, HIGH);
  }
  else
  {
  digitalWrite(Relay, LOW);
  }
  /** If your ESP does not respond you can just
  *** reset after each request sending
  Serial.println("- trying reset");
  ESP.reset();
  **/
}

void loop() {
  webserver.handleClient();
}


For now I just want to do the temperature Setup through my Wi-Fi while I'm being connected at my home network (not remotely from the outside). Maybe in the future I will want to set my temperature through an outside network. Another problem I'm facing is that sometimes after a while I get no internet access on other devices connected to the network when the ESP8266 client is online. Any thought on this?

Thank you for your help!