This sketch has a problem.....do you see it?

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

Dont use delay(), note millis() and count while polling. delay is DEAD

1 Like

please post a schematic of your setup and some real pictures to see what you have.

Hi,

I have experienced a lot of problems myself with esp8266 dropping wifi until I tried the below which solved it for me

  // Stop wifi going to sleep (if enabled it can cause wifi to drop out randomly especially on esp8266 boards)
    #if defined ESP8266
      WiFi.setSleepMode(WIFI_NONE_SLEEP);
    #elif defined ESP32
      WiFi.setSleep(false);
    #endif```
1 Like

Please explain what you mean by this, because your sketch is not using the right functions to connect to WiFi with an esp8266. It's using functions that would be used if you were using, say, an Uno, with an esp-01 WiFi adapter.

Yes, the board is an UNO board and the WiFi module is an ESP8266 which I thought was the same as an esp-01 ?

Hello.
Thanks for this. This is along the lines of what I was thinking the cure might be.
How would I integrate this into my sketch?
Where would I place it and does it need to be declared such as Void NoSleep() ?

I will try this because this is exactly what I was thinking was happening. The esp8266 is losing the connection but keeps merrily looping as if it is still connected to WiFi.

Could you explain what you mean?
I just searched and Delay() is still in the Official Reference with no notification that it is no longer preferred?

Correct, esp-01 is an esp8266. But these days, if you say you are using an esp8266 without mentioning any other type of Arduino, many, if not most, people assume you are using the esp instead of an Uno or other Arduino. That is the more common way to build projects now because, well, it's easier and cheaper and better.

When combining Uno and esp-01, you need to make sure you include components to adapt between the two different voltages (Uno is 5V and esp is 3.3V). The esp is also a far more powerful beast: faster, more memory, more flash. Not as many pins, but that doesn't appear to be a problem from what we know about your project.

I would recommend a wemos d1 mini to replace your Uno and esp-01.

2 Likes

by DEAD i mean that delay() is a blocking function. NOTHING happens until delay() runs out. If you use millis() you can do timing without delay(). The blink without delay() example shows this.

1 Like

Update to this post....
After about 24 hours it seems it has gone back to it's old ways.
I'm getting hardly any data at all now.
I wonder if keeping the esp8266 in active mode full time has shortened it's life?


Hello Alanesq,

So I added your code to my sketch after the initial declarations and before any void functions.

So far it seems to have made a difference. it seems my process has gone from getting about a 30% rate of success (in data capture) to about 75% success. That's a BIG difference.
I'm thinking we're on the right track looking at the sketch code as the culprit.

The Esp8266 and Arduino UNO are about 50 feet from the AP, but there is a WiFi range extender in between the UNO and esp8266 and the WiFi AP that boosts and repeats the signal and forwards it to the AP.

Still uncertain why I still get about a 25% data loss rate but it's better than I've ever seen it so thank you!

Hi,
I have 3 calls to the Delay() in my sketch above.
After reading the official millis() tutorial and quite a few others, I am left confused. It seems all the examples use millis() outside of a void() but my delays are all inside of another.

I don't understand how I would replace any of the Delay() with an equivalent millis() in this particular code situation.

Additionally, in the sketch code above, is millis() a better choice than delay() in all of the instances where it is used?

Could someone show me an example of one replacement in the code I posted?
It would be very helpful.
Thank you in advance.

"blink without delay()" is an EXAMPLE in the IDE ...

I specifically studied that example that you cite..
In that example the Millis() seems to be moved to it's own completely independent function outside of any others.

In the sketch above, it is inside another function, which, to me appears to be a different scenario.
It is very possible I simply do not understand....and that is why I keep asking.
Again, the example you recommend makes no sense to me because it seems to be much different than my situation.

I have obtained a C++ programming book (actually two) and I'm trying to figure this out.

Thank you for your patience and understanding.

inside or outside function mox nix. The concept of noting the start time, and comparing to a computed end time, and continuing to run, is what is important. The concept works inside or outside functions.
Real time programming requires a different thought process than linear sequential programming. You cannot ever "stop and wait". You always have to use "touch and go" as your paradigm.

1 Like

Another potential problem..........
How would I fix this?

...
if(Serial1.find("OK"))
...

106:26: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
if(Serial1.find("OK"))

This is an update for any poor soul reading this thread who is having similar issues getting consistent readings from a DHT-22 using an Arduino board (probably any).

It seems the major culprit in my case was the DHT-22 sensor itself. Apparently they wear out or get old or fail or whatever. When they do, readings can be sporadic or not at all. I replaced them with new DHT-22 sensors and immediately I began getting consistent data finally.

It may or may not solve your issue but it you've tried everything else it's worth a shot.

Seems I can only solve this problem for a short time and it returns.

Question:
Is it possible that even though the Arduino UNO's that I have say Arduino on them, that they could be cheap knock offs and that is causing the problems?

How much should I pay for an authentic Arduino UNO board? Should I be using a different Arduino board?

BME280 is much better than DHT-xx

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.