/*
Geekstips.com
IoT project - Communication between two ESP8266 - Talk with Each Other
ESP8266 Arduino code example
*/
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define DHTPIN 2
// AP Wi-Fi credentials
const char* ssid = "***********";
const char* password = "**********";
// Local ESP web-server address
String serverHost = "http://192.168.4.1/feed";
String data;
// DEEP_SLEEP Timeout interval
int sleepInterval = 5;
// DEEP_SLEEP Timeout interval when connecting to AP fails
int failConnectRetryInterval = 2;
int counter = 0;
float h;
float t;
// Static network configuration
IPAddress ip(192, 168, 4, 4);
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
DHT dht(DHTPIN, DHT22);
WiFiClient client;
void setup() {
ESP.eraseConfig();
WiFi.persistent(false);
Serial.begin(74880);
Serial.println();
Serial.println("**************************");
Serial.println("**************************");
Serial.println("******** BEGIN ***********");
Serial.println("- start DHT sensor");
dht.begin();
delay(500);
Serial.println("- set ESP STA mode");
WiFi.mode(WIFI_STA);
Serial.println("- connecting to wifi");
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
if(counter > 20){
Serial.println("- can't connect, going to sleep");
hibernate(failConnectRetryInterval);
}
delay(500);
Serial.print(".");
counter++;
}
Serial.println("- wifi connected");
Serial.println("- read DHT sensor");
readDHTSensor();
Serial.println("- build DATA stream string");
buildDataStream();
Serial.println("- send GET request");
sendHttpRequest();
Serial.println();
Serial.println("- got back to sleep");
Serial.println("**************************");
Serial.println("**************************");
hibernate(sleepInterval);
}
void sendHttpRequest() {
HTTPClient http;
http.begin(serverHost);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.POST(data);
http.writeToStream(&Serial);
http.end();
}
void readDHTSensor() {
delay(200);
h = dht.readHumidity();
t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
t = 0.00;
h = 0.00;
}
Serial.println("- temperature read : "+String(t));
Serial.println("- humidity read : "+String(h));
}
void buildDataStream() {
data = "temp=";
data += String(t);
data += "&hum=";
data += String(h);
Serial.println("- data stream: "+data);
}
void hibernate(int pInterval) {
WiFi.disconnect();
ESP.deepSleep(10 * 600000 * pInterval, WAKE_RFCAL);
delay(100);
}
void loop() {}
This is AP side in which I need to insert a code. If temp is below 20 turn on relay otherwise turn off that's it
/*
Geekstips.com
IoT project - Communication between two ESP8266 - Talk with Each Other
ESP8266 Arduino code example
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
// IoT platform Credentials
String apiKey = "*************";
const char* logServer = "api.thingspeak.com";
// Internet router credentials
const char* ssid = "***********";
const char* password = "***********";
ESP8266WebServer server(80);
void setup() {
Serial.begin(74880);
WiFi.mode(WIFI_AP_STA);
setupAccessPoint();
}
// Handling the / root web page from my server
void handle_index() {
server.send(200, "text/plain", "Get the f**k out from my server!");
}
// Handling the /feed page from my server
void handle_feed() {
String t = server.arg("temp");
String h = server.arg("hum");
server.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 :");
server.on("/", handle_index);
server.on("/feed", handle_feed);
server.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("*********", "*********");
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");
/** If your ESP does not respond you can just
*** reset after each request sending
Serial.println("- trying reset");
ESP.reset();
**/
}
void loop() {
server.handleClient();
}