Tag Archives: ESP8266 Arduino

Testing ESP8266 WIFI

Amateur night out radiation shieldHaving had mixed results with both the ESP-Arduino environment and the normal SDK (1.3.0 with latest patch) and C, I decided this morning to up the game and do some focussed testing.

So – I made a simple loop with ESP-Arduino, connecting to WIFI, connecting to MQTT and publishing a message to which I’m subscribed, every second. This failed after a while the other day. Someone suggested I needed a “delay()” call in the loop – this despite the client.loop() call. I wasn’t buying it but decided to give it a whirl anyway.

So here is my test ESP-Arduino code with the delay() call added. https://bitbucket.org/snippets/scargill/7RAg4

At 10:26am I set it off running using a nodeMCU board (only because I had it handy – don’t confuse this with Lua code) and every second, sure enough the message went out and came back in… I occasionally used MQTT spy to inject something in there at random.

In true Hackitt & Bodgitt style and sick of getting it in the neck from my wife Maureen for turning the WIFI off, I made a simple shield to test the boards to see what happens when they lose WIFI. See picture above. I used a plastic glass surrounded with foil so as not to short the board while it is inside.

And so it was that by 11:20am, nearly an hour after starting this all off, the board was sitting working perfectly – maybe there IS something to using delay(). My us of 10ms was completely arbitrary incidentally.

Armed this this – the next step was to dump the board in my poor man’s faraday cage. Not one of my more successful ideas. Even with the mesh on the top it worked perfectly – so more silver foil, I covered the top completely with it… and that didn’t work either. Bugger – a lifetime of theory out of the window.  I tried grounding the aluminium foil…  for a second that didn’t seem to work… and then the every-second signal stopped – constant blue light on the nodeMcu board…Nothing – no error messages – just dead. I removed the board from the cup, careful not to short it against any metal…. still dead. Still a constant blue light.

I pressed reset – sure enough – the board came up and started responding again. I repeated the experiment…I’m amazed at how hard it was to stop the signal even with grounding – but eventually,stop it I did. Took the board out – started up again, put it back in – it stopped, took it out… no blue light this time – and no output (again no error messages either)… the only way to bring it back up was a reset. I waited a minute – nothing. Pressed reset – it started up.

Clearly I need to go back to school on shielding so for my next set of experiments, it just so happens I have another wIFI router sitting doing nothing – so I altered the SSID to this one – knowing I could shut it off as needed without upsetting the wife!

Booted up – no bother – messages sending… .turned the WIFI off – the unit stopped – dead. No error messages. Turned the WIFI back on – nothing – not a sausage. I waited – nothing. Pressed reset – worked perfectly.

So there is something here about losing the WIFI signal. I realised at this point that I’d put a forever loop in with no checking for WIFI connectivity – and so made a slight alteration..

while(1)
  {
    client.loop();
    if (mytimer<millis()) { client.publish("inTopic","hello world"); mytimer=millis()+1000; }
    delay(10);

    if (WiFi.status() != WL_CONNECTED)  break;        
  }

This in theory would notice the connection break – and reconnect again to the MQTT.

And sure enough – a series of warnings about no WIFI – turn the WIFI back on – and voila.

So it seems that with the delay() function in the loop – and with the right checks for failed WIFI – this all DOES work.

That leaves the question – what if the MQTT server fails!! I disconnected the MQTT server internet connection and sure enough – the program sat doing nothing. I then sensibly added the next line…

while(1)
  {
    client.loop();
    if (mytimer<millis()) { client.publish("inTopic","hello world"); mytimer=millis()+1000; }
    delay(10);

    if (WiFi.status() != WL_CONNECTED)  break;        
    if (!client.connected()) break;
  }

 

Sure enough, as soon as I disconnected my MQTT server, the board started to gripe about lack of MQTT. Connected the MQTT and… again everything started up.

So having satisfied myself that this all works – all that remains to be done is a longer term test. I’m off out for a spot of swimming and will leave this running, firing out auto incrementing messages once a second….

mytimer=millis()+1000;
  while(1)
    {
      client.loop();
      if (mytimer<millis()) {
          sprintf(str,"My counter %ld",mycounter++);
          client.publish("inTopic",str); mytimer=millis()+1000;
          }
      delay(10);
      if (WiFi.status() != WL_CONNECTED)  break;        
      if (!client.connected()) break;
    }

 

SADLY – after waiting some time – this was the result..

 

Incoming: My counter 2520
Incoming: My counter 2521
Incoming: My counter 2522
Incoming: My counter 2523
Incoming: My counter 2524
Incoming: My counter 2525
Connecting to MQTT server
Could not connect to MQTT server
Connecting to MQTT server
Could not connect to MQTT server
Connecting to MQTT server
Could not connect to MQTT server
Connecting to MQTT server
Could not connect to MQTT server
Connecting to MQTT server
Could not connect to MQTT server
Connecting to MQTT server
Could not connect to MQTT server
Connecting to MQTT server
Could not connect to MQTT server
Connecting to MQTT server
Could not connect to MQTT server

This despite tests showing the MQTT server is working perfectly…

A quick RESET and the count started again. It got as far as 2525 last time…the second attempt it has climbed to 16070 and still going – beats me why it packed in the first time.

 

For instant gratification (or more regular than the blog, if you’re in Facebook – like my ESP8266WIFI page here. https://www.facebook.com/esp8266wifi

Facebooktwittergoogle_pluspinterestlinkedin