Making a combi project using esp8266

Hi all

I have two codes one is server the other is client. Client sends dht22 temperature information to server and there is a relay connected to server when temp is below a specified value I want relay turned on otherwise off but it doesn't work properly could you have a look at the code? Or test it yourself?

This is client side:

/*
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() {}

And this is server:

/*
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();
pinMode(2, OUTPUT) ;
}
// 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);

  long temp = t.toFloat();
  if (temp < 20.0)
    digitalWrite(2, HIGH); // Turn ON Relay on GPIO2
  else
    digitalWrite(2, LOW); // Turn OFF Relay on GPIO2
}
}
 
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();
}

I've been trying to make this work for a month so I'm about to go crazy if somebody have an idea please help me

Ok the project is now working but I don't know why? Because when I try the code below using a led it is working as supposed to. When the temperature is below 20 GPIO2 lights up the led and when it is above 20 degrees it turns it off. But what surprises me happens when I connect a relay instead of led. When the temp is below 20 it turns off the relay but when it is above 20 it turns out on, I realized this by chance I just changed the low into high and high into low and it works vice versa. Why this happens like this?

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);

long temp = t.toFloat();
if (temp < 20.0)
digitalWrite(2, HIGH); // Turn ON Relay on GPIO2
else
digitalWrite(2, LOW); // Turn OFF Relay on GPIO2
}
}

Hello!
I tried to compile, but it gives me an error - 'hibernate' was not declared in this ospege on the line - hibernate (sleepInterval);
Can I have a different library?