ESP8266 Temperature Logger


This is a quick post about a pretty standard Temperature (and Humidity) data logger. Yes, I’m finally jumping on the IOT bandwagon, whatever that is… 🙂

The background for this is my recent discovery that the adrenaline (epinephrine) injections that a person at risk of anaphylaxis has to carry around should normally be stored within a very tight temperature range (20-25°C with short exposures to 15-30ºC tolerated).

This came as a big surprise, especially in the context of going on a skiing holiday, so I decided to do something to at least have an idea of the temperature range that these injections experience, before going any further and thinking about how to store them better.

The by now ubiquitous ESP8266 seemed like the obvious choice for this project, and after a short search on the typical Chinese online suppliers, I found the  D1 mini board from Wemos and it’s ecosystem.

It’s tiny and modular, as one can add “shields” on it. I also grabbed a battery shield , a SD card one (the actual one I got is not the Wemos one, but still pretty similar) and a temperature/humidity sensor.

It was quite straight forward to connect the 3 shields together, and then the sensor using a ribbon cable as I wanted to make sure the readings don’t get influenced too much by eventual heat of the MCU or the battery circuitry.

The 1st thing that struck me, was how accurate and consistent the SHT30 was! I had already experienced with older versions of similar sensors (maybe even using the previous SHT20  version, can’t remember exactly ) and I remember having had a hard time getting reliable data points. Not the case with the SHT30, I’m really really pleased with it !

There were 2 things that I struggled with and took some time to get right (yes, even on such seemingly simple and straight forward project 🙂 ) :

  1. The SD card shield versus the ESP8266 boot
    1. my SD card shield is using D8 as the Chip Select pin for the SPI communication with the MCU
    2. D8 corresponds to GPIO15 which is a special Pin used for Boot: https://github.com/esp8266/esp8266-wiki/wiki/Boot-Process
    3. It needs to be LOW in order for the ESP8266 to boot “normally” from its SPI Flash, and it does have a 10K pull down resistor on the board
    4. HOWEVER, with my SD Card shield, when the SD card is inserted it takes this pin UP. Not sure exactly why, and it did take quite a while to figure this causality out…
    5. The solution was to solder an additional 1K resistor between D8 and GND, which seems to fix the problem (probably by being a lower value that whatever is on the SD card shield that takes the pin up)
  2. ESP8266 coming back from deep-sleep
    1. for it to come back to life GPIO16 needs to be connected to RST
    2. on the D1 mini Wemos board the former is D0. There’s also a nice “SLEEP” jumper on the back
    3. HOWEVER when this connection is done the ESP8266 can’t be programmed anymore
    4. So I added a SWITCH which has to be “off” when programming and “on” when I need the sleep to work. Annoying, but definitely not a huge deal, especially that this is not something that I’ll program very often once it’s configured and it works !

Once these issues were out of the way, I realised that saving the data on the SD card was nice, but quite cumbersome to actually look at, and that it would be much nicer to push it somewhere online.

I haven’t spent too much time searching, but I could vaguely remember that Sparkfun were doing something along these lines, so found Phant. Sadly it’s no longer in operation. So after quickly checking the 3 other IOT data-treaming services they recommend instead, I decided to go with ThingSpeak from MathWorks as it seems to be the more flexible / data analytics oriented one.

Another later but nonetheless cool addition was the ability to monitor the battery voltage and report that too. Using this very useful post, I added a 100Kohms resistor between VBatt and A0, and with a couple of lines of code I was able to get the voltage.

It later turned out for this feature to be quite a necessity, as the initial battery I was using, a 1 cell 300mAh LiPo would drain in less than 24 hours. I finished by replacing it with a ~1200mAh LiIon photo camera one, which seems to be enough for several days but it still convinced my of the importance of knowing in advance when the battery will run out.

Haven’t tested exactly, but it does depend on how often the MCU wakes up and, most importantly,  on how often it finds the relevant WiFi access point and uploads the data to the cloud, which is the thing that uses the most energy *by far*.

As usual, you can find all the code (there’s not that much of it, just medium sized Arduino file) on my Github account.

In a nutshell, what this IOT sensor does is:

  1. wake up from deep-sleep every 5 mins
  2. get the temperature and humidity from the SHT30 sensor (through I2C / Arduino library)
  3. try to connect to a pre-configured WiFi access point
  4. if the above is successful try and get the exact time from a NTP server. Alternatively just keep counting using the internal clock
  5. write the data points to the same .csv file on the SD card (create the file if necessarY)
  6. if WiFi is available also try to upload the latest data to the cloud (it keeps track of the last line uploaded)
  7. store the current time counting to the RTC memory (the timers will be re-initialised when waking up from deep-sleep)
  8. go to deep-sleep for 5 mins

Leave a comment