WiFi Enabled Temperature/Humidity Sensor Using ESP8266 (TempBuddy Sensor)
by BirdOfFire1549 in Circuits > Electronics
1998 Views, 24 Favorites, 0 Comments
WiFi Enabled Temperature/Humidity Sensor Using ESP8266 (TempBuddy Sensor)
Backstory...
I have a shed in my backyard that has been converted into some office space. I use it as my office since I mostly work from home these days and though it has insulation the AC/Heating Unit in there is kinda small and doesn't always keep things comfortable on the Hottest or Coldest days of the year. So I wanted a way I could keep an eye on what the temperature and humidity was out there without having to actually go out there to see, as I usually start my days working inside the house and move out there in the afternoon if the temperature is good in the shed.
So, I came up with a device I like to call the "TempBuddy - Sensor".
About the Device...
This device can be connected to my WiFi and then it serves a webpage that I can access from my phone or computer to check the temperature/humidity at the device's location.
The nice thing about this device is, that it can be factory reset by holding the factory-reset button while pressing the reset button, then you can connect directly to the device as an Access Point using the SSID: TempBuddy with no password, then connect to the web interface of the device using the URL: http://192.168.1.1/admin, using the default username: 'admin' and password: 'admin'. Then you can configure the device with the settings it needs to connect to your WiFi. Finally the device reboots and attaches to the configured WiFi. The WiFi password in AP mode is: 'P@ssw0rd123'
Get device IP...
To find out what IP Address the device was assigned on your network you can briefly press the factory-reset button during normal operation and the device will flash out the last octet of the IP Address using the ESP's built-in LED. If for some reason you aren't sure what the first 3 octets of the IP Address on the network it connected to are, then you can hold down the factory-reset button for more than 6 seconds and the device will flash out the entire IP Address.
You might me wondering how the IP Address is flashed out when you have an IP Address like 192.168.123.71...
Well, no... You don't have to count out 192 flashes, then 168 flashes, then 123 flashes, then 71... That would take forever and be hard to do or even tolerate. Instead I wrote the code for the chip so that it works as follows:
Each digit of each octet is flashed individually, there is a small rapid flash between each digit, there are 2 small rapid flashes between octets and when all octets have been flashed there is a long rapid flash to indicate this.
Example...
I will demonstrate how this works below by representing a single flash of the LED as a dash '-'. I will represent the post digit rapid flash with three asterisks '***', and finally I will represent the end of sequence's (end of IP) long flash using 10 asterisks '**********'.
Using the above the IP address of 192.168.123.71 would be flashed out as follows:
1 9 2 . 1 6 8 . 1 2 3 . 7 1
- *** - - - - - - - - - *** - - *** *** - *** - - - - - - *** - - - - - - - - *** *** - *** - - *** - - - *** *** - - - - - - - *** - **********
The short button press version of the above would simply be the last octet so in this case it would be:
7 1
- - - - - - - *** - **********
In the steps below I will walk you through how to create one of these devices.
Please Note:
I have discovered that the ESP chip generates a small amount of heat and it is enough to throw off the temp reading of the device by a few degrees in the initial version of the design. Since then I made a single modification where the AHT10 chip is connected to the board by a small length of wire rather than directly to the board. This simple change seems to have fixed the temperature readings so that now they are accurate.
Security Related:
The initial device didn't use SSL so interacting with its configuration page would expose the information in clear text, however the software has been rewritten to enable the device to serve its pages using SSL via HTTPS over port 443. Also there have been improvements to the Admin Login page to use a better type of Authentication and authenticated sessions. Firmware Version 2.2.1 is available via my GitHub Space, however it is setup to used with PlatformIO and the code no longer works with Arduino IDE without making modifications to it: https://github.com/birdoffire1549/TempBuddy_Sensor
I switched my IDE from Arduino IDE to PlatformIO because I kept hitting limitations with the Arduino IDE, and while PlatformIO also has its own frustrations, I feel like they are more tolerable so that's what I use now.
I have also uploaded the current files with modifications for use with Arduino IDE to this Instructable, see Step 3: Write The Code, for more information.
Supplies
- ESP8266MOD - ESP8266 ESP-12 ESP-12F NodeMcu Mini D1 Module
- AHT10 - Temperature/Humidity Sensor
- 2 x 1K Ohm Resistor
- 2 x Push Button (Normally Open)
- Wire
- PCB Prototype Board
- Solder
- Soldering Iron
Design the Circuit
The design of this device really centers around the use of the ESP8266, as it is the brains of the device. It does everything from fetching the temp/humidity from the AHT10 device and handles the WiFi for both connected and AP mode as well as hosts the web-pages the device uses to interact with the client, and ofcourse it handles the input buttons and how those are used. Really this whole device is rather simple as almost all of the functionality is derived through the device's software.
I attached a hand drawing of the circuit. Yah, sorry about that. Maybe someday I will find some software that I can use to do those instead, just that pen and paper has always worked well for me.
Reboot Button...
So, the ESP board I am using does have a reset button built into it for rebooting the device but I wanted to relocate the button so I added a button (SW1) with a 1k Ohm resistor (R1) that pulls the reset pin high while the button is unpressed (open) then once pressed (closed) the button pulls the reset pin on the device to Low. This will cause the device to reboot and that functionality is already built into the ESP so no coding is required for that.
Factory Reset Button...
I also added what I call, the Factory Reset button. The purpose of this button is more complex than simply factory resetting the device, although that was its initial intent. The idea is, that one holds down the Factory Reset button, then presses the reboot button, first releasing the reboot button then the factory reset button. This will cause all of the device's settings to be erased and set back to a predetermined factory version of the settings. Later on I decided I needed a way to find out what the device's IP Address was without having the device hooked up to a computer and reading it over the serial interface. That is when the Factory Reset button became a multi-purpose button as it can be used to cause the device to "Flash out" the IP Address, as described in the Introduction section of this instructable. Similar to the reboot button the Factory Reset button (SW2) is used with a 1k Ohm resistor (R2), only this time the resistor pulls the input pin Low, and once the button is pressed then the input is pulled to High.
AHT10 Sensor...
Finally the AHT10 sensor was added to the device. It has one pin that connects to 3.3v power, another that connects to Ground, then the two remaining pins are are connected to the ESP's SDA and SCL lines which are used for communicating with the sensor using the I2C protocol.
AHT10 Gotcha...
So it turns out there is a downside to using the AHT10 sensor. That is, even though the device CLAIMS to be I2C compliant, it really isn't. Oh sure, you can talk to it using I2C from a chip like the ESP, and that works just fine. The gotcha is that the AHT10 Sensor hogs the I2C line and doesn't allow you to use it for any other devices. So, if you want to use this sensor and display the output to a I2C driven display or pull data from multiple I2C sensors, well, in that case you are going to need a different sensor because the AHT10 won't allow it. As for me... I knew this downside when I bought the chip but I didn't care because for its application I don't need the I2C line for anything else.
Build the Circuit
Temporary Proto-Board Build...
Well, this part was pretty simple. I simply had to implement my design on a temporary proto-board. So I had a prototype of the circuit I could interact with, test, and fine tune if needed. Turns out the circuit was a pretty simple one so I really didn't need to change much, although this is actually the step where I not only decided to add the Factory Reset button and capability, but also realized through interactions that in practical use the device wouldn't be setup though the serial connection with my computer so I needed a way to get the IP Address from the device directly, and that is when the multi-purpose nature of the Factory Reset button came to be.
Write the Code
This is the part of the build that I enjoyed most being a Software Developer by trade, although I am much more used to working with Java so there was still a bit of a learning curve. Originally I wrote this code in Arduino IDE but recently I switched to using PlatformIO instead. Setup in Arduino IDE is more difficult but I will tell how to do it both ways.
Using PlatformIO:
Locate a directory where you wish to keep the project and navigate to that folder on your computer's command terminal. Once there, if you have git installed on your computer just run the command:
git clone git@github.com:birdoffire1549/TempBuddy_Sensor.git
Then open the project folder in PlatformIO.
If you don't have git installed then you will need to download the project from the repository located here: https://github.com/birdoffire1549/TempBuddy_Sensor
Then extract the files to the desired directory then open the project in PlatformIO.
Using Arduino IDE:
Things are a little more complicated using Arduino IDE. If you are using Arduino IDE then download the files attached to this step placing the files in a folder called, 'TempBuddy_Sensor', and placing that folder in the directory where Arduino stores sketches on your computer.
Note: I probably won't do an Arduino IDE release again, so if you want to stay current with my GitHub Repo you might consider switching to PlatformIO.
Now open the sketch in Arduino IDE...
You will now have some problems with missing libraries and references when you try to compile this sketch using Arduino IDE, so now we need to make sure the libraries get installed.
First install the support for the ESP8266 boards. Open Board Manager in the IDE and search for ESP8266. You should find one called: esp8266 by ESP8266 Community. Install that one.
Next open the IDE's Library Manager to search for ESP_EEPROM, and install the one by j-watson, using version: 2.2.1, then search for ‘Adafruit AH10’, it should show a library called ‘Adafruit AHT10’ by Adafruit, click install to add that library as well.
Now the not so fun one...
You need the ESP8266WiFi library, but it isn't easily available in the IDE, but it is available on github. I forked the Repository to my github account so I know you can find it located there, so if you navigate to the libraries folder located here: https://github.com/birdoffire1549/Arduino_ESP8266/tree/master/libraries
You can see all of the libraries available and in each one there is an examples folder with code samples for leveraging the library to do various things. I really recommend that you clone the repository and transfer all the folders in libraries to your Arduino libraries folder on your local machine as I am sure they may come in useful sometime in the future if you want to continue to work with ESP8266 chips.
Locate the 'ESP8266WiFi' Library folder and you will need to download the entire library folder to your Arduino Library folder. Where that library folder is on your machine can be very different for everyone so you will want to do some research on the internet regarding that folder to help locate it, but it is typically in your IDE's workspace folder where the sketches are kept.
Now, take a deep breath and if you did everything right and there are no unforeseen differences between your system and mine, then the sketch should now build, and can be uploaded to the ESP device.
Upload the Sketch...
Now, hopefully the project builds just fine regardless if you are using PlatformIO or Arduino IDE.
Once it does build then we can upload it to the ESP.
Once uploaded to the ESP you can connect to the device using Serial in the IDE, then for fun reboot the device using the reset button. You should see all kinds of logging from the device in the serial monitor telling you what the device is doing and whatnot. Maybe play around with the device while keeping it connected to serial and see all the different things that get logged out from the device.
Configure the Device
Once the device is programmed and running for the first time, you should find that it transmits a WiFi network called something like 'TempBuddy_Sensor_<DeviceID>'. The 'DeviceID' portion of the SSID is a mostly unique ID for your device that is automatically generated by the code based on your device's MAC address. This way if you have multiples of these devices their SSIDs aren't as likely to conflict. Anyhow, you can connect to the network from a computer or smartphone using the password: 'P@ssw0rd123'. Once connected you may have to tell our phone or computer to stay connected to the network despite not having Internet access. Then open a web browser and navigate to http://192.168.1.1/admin, and you will see a popup requesting a user and password. For the username use 'admin' and for the password also use 'admin' and it should take you to the settings page. Once at the settings page you can configure the device to join a local WiFi network as well as make some other adjustments and customizations. Once you save the settings, if you updated the WiFi settings then the device will reboot and join the desired network. Give the device 5 to 10 seconds after reboot to join the network and then use the Factory Reset button to either get the last octet of the IP Address it obtained using a quick press, or using a long press of 6 seconds or more you can obtain the full IP Address. Once you have the IP Address for the device, simply open a web-browser on any computer or smartphone on that network and type in the IP Address for the address and you should be greeted with the Information Page where the Temperature and Humidity are listed.
From here just pull up the web-page at the root of the IP Address whenever you wish to know the Temp and/or Humidity, or navigate to the /admin page anytime to change any settings of the device at any time.
The Soldered Board
Since I had things built on a temporary proto-board i simply reproduced that layout of the components on a board where everything was soldered into place, this is how I made the final version of the device that I would actually use for its intended purpose in an ongoing fashion.
Attached are some pictures. There is the initial version of the device where the AHT10 is soldered onto the board (don't make it like that), and the second version where the AHT10 has been moved off the board using some wires (that's the way to go).
A Case for the Device
So, I did create a case for my device using my 3D Printer.
Incase anyone wants to know, I use Blender to design 3D objects like device cases, I use Simplify3D to slice them creating the GCode for the printer, and then I print them using my Creality Ender 3 printer.
So, anyhow... If you look at my case designs you will see that I like to make cases that simply snap together when possible. The latching system I have designed seems to work well for my purposes so I have used it for multiple things I have created in the past. This case is a snug fit on the board of the TempBuddy device this was done on purpose so it could be pressed down into its resting place and then once there it wouldn't want to move, which seems to work well for the testing I have done. Only issue with that is, you will need to build yours using exactly the same dimensions as mine for this case to be of any use as-is for you. If your dimensions are different then you'll need to make your own case, but perhaps you can draw some inspiration from mine.
That being said, I think you probably won't find this case useful as-is but I do think the STL files can give you something to start with that you can modify to work for you.
Lessons Learned
Well, with any project I think it is important to reflect on what did and didn't work and what you can do better with in a future version of the project. So here is where I will reflect at where this project is headed in the future for me and perhaps it will hint at ways this instructable may become better in the near future.
Ok, so here we go...
Heat Issue...
As I hinted at very early on, version 1 of the device had a heat issue that caused the temperature the device reported to be a bit higher than it actually was in the room. (picture of device v1 attached) This was a HUGE issue for me. I made several attempts to keep the first board as it was but try to block the heat of the ESP from reaching the sensor. Sadly, all of those attempts failed and I came to the conclusion that the board was conducting the heat from the ESP to the Temp Sensor, as such the only way to solve the issue was going to be to get the sensor completely off the board. To do that I desoldered the sensor and added some wires with plugs on the ends that could connect to the pins of senor. That worked great. The temp reading became accurate and since the sensor basically plugs into the cables, the sensor is replaceable just in-case I ever need to replace it.
Attached is a picture of the device with the cable and sensor attached.
I also attached some pictures of when I made the cable. I used some instant drying glue that can be cured with a UV LED, to tack the plug ends of the wires together in the order they would need to be to plug into the sensor, and added some to the length of cable too so the individual wires would be welded together to form kinda like a ribbon cable of sorts. This worked GREAT and I highly recommend it.
SSL and WPA for WiFi...
Initially I didn't use SSL for the admin page but then later added it in. I have since learned that the SSL takes up a lot of resources on the device and as such the device's admin page cannot be expanded to be any larger in size or the device runs into memory issues and rather than throwing errors it just doesn't send the HTML page to the requestor. That was a nightmare to fix at one point, but now I have things a little bit more compact and it seems to be working fine as is.
Software...
I have no complaints in general with the way I did things in the software. Yes, I know things could have been more concise and the serial logging should probably be pulled out as that takes up space and isn't needed for production operation of the device, but at times when making updates to the code it became nice to have, so I didn't have to add it all back in later. I also added an endpoint to the device called '/api/info' that will serve up the Temperature and Humidity data in a JSON format as I plan on fetching the data from the sensor in the future using some other software I am currently working on for my own Smart Home devices. Sure I could programatically get the data from the main human-readable page but having a JSON endpoint just makes me happy.
Hardware...
I have heard tales of the AHT10 sensor going bad over time and even in one case shorting out and destroying the ESP. I don't know how wide spread those defective devices are. I got a pack of 10 for pretty cheap and haven't had any issues with them as of yet. If I do have problems with them I may need to look for a replacement in the future but I'll cross that bridge if I come to it.
On another note... I might try to come up with a battery powered version of this device and see how long it lasts powered that way. Probably not long enough to be worth it as the ESP was generating some measurable heat, but it might be fun anyways.
Case...
I thought the case design was pretty good. The venting ensures adequate exposure to the environment that is being measured, while also keeping things from touching and/or potentially shorting out the board. I will likely base my V2 Case off of this design and probably not much will change.
That's All
Well, that is all I got for this instructable. If you stuck with me until the end, thanks a lot and I really hope you enjoyed it and found at least parts of it useful and informational.