Hello,
I have the following sketch powering an ESP8266 Temperature / Humidity setup that works well EXCEPT that it constantly loses WiFi connection (I think)
I am not a C+ programmer.
Could you look at this sketch and see if there is a flaw that would cause the sketch to keep looping even after it loses WiFi connection? Perhaps propose a way to overcome that?
Thanks!
// #include <DHT.h>
#include <DHT_U.h>
#include <DHT.h>;
#include<Timer.h>
Timer t;
#include <SoftwareSerial.h>
SoftwareSerial Serial1(2, 3);
//#define dht_dpin 12
#define DHTPIN 12
#define heart 13
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
const char *workaround="OK";
static char postUrl[150];
float humi;
float tem;
//int tem,humi;
float temConv;
float humiConv;
void httpGet(String ip, String path, int port=80);
void setup()
{
Serial1.begin(57600);
Serial.begin(57600);
Serial.println(F("Delay: 5Min(300000ms), ESP8266-10, DHT22-2"));
Serial.println(F("Connecting Wifi...."));
connect_wifi("AT",1000);
connect_wifi("AT+CWMODE=1",1000);
connect_wifi("AT+CIPSTA=\"192.168.000.59\",\"192.168.000.002\",\"255.255.255.000\"",10000);
connect_wifi("AT+CWQAP",1000);
connect_wifi("AT+RST",5000);
connect_wifi("AT+CWJAP=\"MyWiFiSSID\",\"Password\"",10000);
Serial.println(F("Wifi Connected"));
pinMode(heart, OUTPUT);
delay(3000);
t.oscillate(heart, 1000, LOW);
t.every(300000, send2server); // 50000 = 1 minute
}
void loop()
{
humi = dht.readHumidity();
tem= dht.readTemperature();
delay(3000);
t.update();
}
void send2server()
{
char tempStr[8];
char humidStr[8];
char idStr[8];
temConv=(tem*1.8)+32;
temConv=temConv-1; //Correction for DHT22-2
humiConv=humi; //For DHT22 -2
dtostrf(temConv, 5, 3, tempStr);
dtostrf(humiConv, 5, 3, humidStr);
sprintf(postUrl, "temphumid/temphumid.cfm?humidity=%s&temperature=%s&ID=TH-1",humidStr,tempStr,idStr);
httpGet("192.168.0.102", postUrl, 80);
}
void httpGet(String ip, String path, int port)
{
int resp;
String atHttpGetCmd = "GET /";
atHttpGetCmd.concat(path);
atHttpGetCmd.concat(" HTTP/1.0\r\n\r\n");
String atTcpPortConnectCmd = "AT+CIPSTART=\"TCP\",\""+ip+"\","+port+"";
connect_wifi(atTcpPortConnectCmd,2000);
int len = atHttpGetCmd.length();
String atSendCmd = "AT+CIPSEND=";
atSendCmd+=len;
connect_wifi(atSendCmd,3000);
connect_wifi(atHttpGetCmd,3000);
}
void connect_wifi(String cmd, int t)
{
int temp=0,i=0;
while(1)
{
Serial.println(cmd);
Serial1.println(cmd);
while(Serial1.available())
{
if(Serial1.find("OK"))
i=8;
}
delay(t);
if(i>5)
break;
i++;
}
if(i==8)
{
Serial.println("OK");
}
else
{
Serial.println("Error");
}
}