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

12 thoughts on “Testing ESP8266 WIFI

  1. I am making a mailbox (the physical world type) notifier using the ESP8266 running NodeMCU and am going to have to put it inside my all-metal box. So I loaded up a basic Lua program to establish a connection to my WiFi AP, started the 8266, set my desktop to pinging it, and then shut it into the box. The ping never missed a beat. That box is watertight but there’s apparently enough RF leakage to let the 8266 get thru no problem. That’s a relief, I thought I might have to make provisions for an external antenna and put a hole in the box.

    1. Brilliant, John. Yes, my pet theories about Faraday cages went out of the window recently. I was trying to see what effect lack of WIFI would have on the board and put it in a glass container surrounded by grounded aluminium foil, thoroughly expecting the board to stop sending and receiving data – no difference – kept working a treat. Strange thing is, put the board near a breeze-block wall and the signal level drops markedly!

  2. I can’t tell you how to use it – depends what you want to do – and there are several blog entries in here showing the ESP-12. The pin spacings are not 0.1″ but if that is important there are back-boards available. Alternatively, get a Node-MCU board, that has a micro-USB connector so you don’t even need a usb-serial convertor – just plug it into your computer and go. The Node-MCU board is a little more espensive but then once you know how to use it you can just get the ESP-12 and do your own thing.

  3. Erm why can you only have 2 io ports on the ESP – I take it you’re using the ESP-01 ? Try the ESP-12 – same price if not cheaper (try AliExpress – what, 3 dollars?) there’s an A/D convertor, several IO pins…

    1. Right, ESP01. So can I avoid using the Arduino with the ESP-12? Can you share a link or example on how to use that board version? Thanks for your help!

  4. Hi Pete! I’ve been following your blog since a couple of months and now I have all the tools to start working on my own IoT project (RPi B, ESP -02, Arduino Nano, etc.). What I have so far is a working RPi with Mosquitto. I will use MQTT as a transfer protocol btw RPi (controller) and Arduino+ESP (sensors, actuators). I’m still lost on what should I do now: according to TuanMP’s instructions, I need to flash the ESP modules and then connect them to Arduino, load the code, and start communications between them though MQTT messages. Is that the right path to follow? Information is sometimes overwhelming :/

    1. So first of all – TuanPM has 3 related projects – I know he does the MQTT for NodeMCU – that’s an area I’m not interested in as it involves Lua and I prefer working in C… also he has a project that interfaces with Arduino and the MQTT code is on the Arduino. Finally he has a project where the MQTT is on the ESP8266 and this has nothing to do with Arduinos whatsoever. Best figure out which one is which….

      1. Thanks for your prompt reply! To be honest, I’m not yet sure about using Arduino+ESP or just the ESP. The first option has its advantages in terms of a more rich language, I/O ports, memory and processing power. On the other hand, it requires more power, space and it is more expensive. ESP stand-alone approach is better if you consider that you are only using one device with extremely low power consumption, but, you can only have one sensor or actuator per board (AFAIK). If I could have at least 2 I/O ports on the ESP, then I’d definitely go that way. Does that make sense?

  5. Hi Peter,

    Isn’t the whole idea of the Arduino “loop”-function that you let the Arduino code create an endless loop? In your examples above you use “while(1)” to create an endless loop within the Arduino endless loop.
    I would remove the “while(1)” and depend on the Arduino library to call the loop function every time. I’m almost sure you can leave out the sleep-function when you do it like that.

    Keep up the good work!

    1. Oh yes that was just a quick test… although it really doesn’t matter because the “delay()” function does the background stuff… but yes, you’re probably right.

  6. I remember a similar problem trying to block out GPS signal for some testing we were doing, it was flippin difficult.

  7. I remember to have read in the API documents that the ESP takes care of background tasks when you call delay – probably everything that’s done by polling. Without delays this stuff would be blockend by your main loop.

    For shielding, you could try a metallised potato chips bag. When you close it, make sure to fold it over as to block the Wifi completely. The wavelength of Wifi is pretty small, so it tends to escape through small openings.

Comments are closed.