Introduction: IoT Temperature Sensor With ESP8266
I was inspired to create a temperature sensor that I can monitor over wifi. This project is not new, but I like the idea of having intermediate layer for security and not having to open port in your router to your IoT device.
The basic idea will allow me to check up on the temperature at home while I was at work. Later it can be expanded to control the aircon and turning it on and off.
Step 1: Concept and Components Required
First the concept. The picture explain what I am trying to achieve. The temperature sensor is connected to the analog input of the ESP8266 WIFI module, which will periodically post the temperature to a cloud based hosting (in this example I am only using webserver with php server side scripting). Then you can access the temperature anywhere from the cloud (webserver) to check what is the current temperature.
Now components required for this are below, please not that the link are affiliate link, if you don't want, just go directly to the website.
- NodeMcu Lua ESP8266 dev board. I get mine from banggood.
- LM35 temperature sensor. I get mine from here.
- Prototype board and some wires (optional if you prefer to solder the temperature sensor directly)
- Arduino IDE, you can download it here.
- Working webserver with php server scripting installed (not required if you just want to view the temperature in your local area network)
Step 2: Get Arduino IDE to Work
Install the Arduino IDE, it support varying platform, windows, Linux and Mac. Just use the one you are comfortable with.
I am assuming you are comfortable to install and get this going.
Once Arduino is installed it is time to test your ESP8266 wifi board. You can connect the board using USB cable to your computer. The computer should detect this automatically.
- Select the correct board from Tools->Board-> NodeMCU 1.0 (ESP-12E module)
- The next step is to select the port where your ESP8266 is connected to, so go to Tools->Port-> mine come up as /dev/cu.wchusbserial14750, (you could have something else)
Once you have this connected you can test the connection and the board by loading the example sketch
- Select File->Examples->ESP8266->Blink
This will open the Blink sketch, you can click on the "Upload" button to upload your sketch. If all goes well the Led on your ESP8266 should start blinking.
Step 3: Connect the Temperature Sensor
The temperature sensor LM35 have 3 legs, the first leg is VCC, you can connect this to the 5V (ESP8266 board's output is 3.3V). The middle leg is Vout (where the temperature is read from, you can connect this to the analog input of the ESP8266 pin AD0, this is located at the top right hand side of the board as shown in picture. And the right leg should be connected to the ground. Now your circuit is complete.
Step 4: Setting Up the Cloud Webserver (optional)
You can skip this step if you want to upload the temperature to another webserver.
Assumption:
You will have a working webserver, and you are familiar with transferring files to your webserver.
Upload the attached zip file to the root of your website lets say the website is "http://arduinotestbed.com"
You can also put the files in a folder but make sure you modify the arduino sketch to point to the correct location of the "data_store.php"
In this example we are assuming the data_store.php file is located in the root of the website, accessible from http://arduinotestbed.com/data_store.php
Once uploaded you can test that it is working by pointing to http://arduinotestbed.com/ArduinoData.php
You should hopefully see the temperature dial showing the dummy temperature. Now that the webserver is ready we can proceed to the next step loading the sketch to our ESP8266.
One thing to note you will need to make sure the file temp.txt in the webserver have the write permission "666".
Attachments
Step 5: Upload the Temperature Sensor Sketch
We are using the ESP8266 as a webserver an monitor the temperature and posting the reading to its internal webserver as well as the cloud.
- Copy the attached 3 files into a folder and open the main file "ESP8266TempSensor.ino" in Arduino IDE
- Modify the location of the webserver on the line String webserver = "arduinotestbed.com" (your webserver root)
- Modify the weburi on the line String weburi = "/data_store.php" (if you use the stepbefore)
- Upload the sketch to the ESP8266
If all goes well it should get uploaded successfully and the first time the ESP will goes into AP mode. You can use your laptop or mobile phone to connect to it. You should be able to find the AP by the name of "ESP-TEMP".
- Try to connect to ESP-TEMP using your laptop of mobile phone
- Find out what is the IP address that you are being assigned to, by doing the "ipconfig" command in windows or "ifconfig" command in linux or mac.
- If you are using iphone click on the i button next to ESP-TEMP that you are connected to
- Open up your browser and point to the ESP-TEMP, if you are assigned with 192.168.4.10 as your ip, the ESP-TEMP is having the ip of 192.168.4.1, so you can just go to http://192.168.4.1 and you should be presented with the setting page where you can enter your wifi router ssid and the psk key. once you entered that both and tick the "Update Wifi Config" check box, click on "update" to update the setting to your ESP8266.
The ESP8266 will now restart and trying to connect to your wifi router. You can monitor this progress in the Serial monitor by clicking on Tools->Serial Monitor. The serial monitor window will show you the progress of the connection.
The serial monitor will also tell you what is the IP address of your ESP8266 once is it connected to your wifi router. The LED will blink once every couple of minute when the temperature reading is take place. And you should be able to see the temperature by pointing to the address of the ESP8266.
Optional step: if you are setting up the webserver to save the temperature to, you can now point to the webserver that you had setup in the previous step, in this example is http://arduinotestbed.com/ArduinoData.php
Congratulations!!! you can now brag to your friends that you have IoT temperature sensor.
You can check out my temperature reading in the following url http://effendy.net.au/arduino/ArduinoData.php
Drop me a line if you manage to make this. If you like this, tell your friends, and you can follow me for more extension to the IoT temperature sensor. You can also check my personal blog for more projects related to microcontroller.
Step 6: Additional Step: TimeLib (Optional)
If you don't have the TimeLib, you will need to install the attached library in your library folder.
For more information about library you can find in the following link,https://www.arduino.cc/en/Guide/Libraries.
For windows users:
The library is located in C:\Users\<your username>\Documents\Arduino
For Mac users:
The library is located in Documents\Arduino\Libraries
You can manually extract the above zip file in the location mentioned.
55 Comments
Question 2 years ago
My temperature is showing as 303.0ºC, heating, and cooling the sensor seems to make no difference in the output. Never mind, using a DHT11, got the libs and am integrating that with your code. BTW nice project!
Answer 2 years ago
Thanks. Yes you will have to adapt a different library. Sometimes the sensor can be broken too. I have one that are giving me an intermittent temperature.
Reply 2 years ago
I got the D11 working no problem and I also made some changes to the web code so it displays Temp and relativity humidity and includes updates w/o reloading the page using ajax with animation. If you are interested let me know and I will send the changes over to you. Thanks! Again nice project.
Reply 2 years ago
Hi raybotokyo, Thank you for upgrading the web code. Please send it over, so that I can share this updated version for the benefit of everyone.
Reply 2 years ago
I will try to attach it as an image here, if that does not work then let me know an email address to send it to. All the code compressed is about 11k, Temp_Probe.tar.gz
Cheers
Reply 2 years ago
Thanks Greatly appreciated
3 years ago
For those following this excellent Instructable, have it working, BUT would like to change the gauge image used have a look at ArduinoData line 43 onwards where Google Charts is used, for example change wording 'Temperature' to say 'Celcius' https://developers.google.com/chart/interactive/do...
Looking at the downloadable files in the Instructable I'd be interested in any thoughts on how to open the SQL file as it looks to be configured for multiple sensors which would be applicable for other applications. I'm using 000webhost as my WWW Server.
Enjoy....
3 years ago
Code works well but where do I change the timezone ?? I'v tried all of the lines of code like "timezone = 180; " but so far no luck.
Reply 3 years ago
Hi, You can edit the default settings in file "Settings.h" line 42
#define DEFAULT_TIMEZONE 180.
When the code runs, it should allow you to change the setting through the web interface and save it in the EPROM.
Reply 3 years ago
Thanks for prompt response.... I have returned the previous line of code to = 360 as that seemed to make no change BUT have now found in the php code for ArduinoData.php a line of code which I changed to suit my city as follows and when I refresh my browser tab shows my correct local time, as follows: date_default_timezone_set('Australia/Perth');
4 years ago
When I try to verify the code, on line 98 DebugLn("handleIO"); I am getting the error
exit status 1
expected constructor, destructor, or type conversion before '(' token.
I've also tried uploading it to the ESP8266 and I'm getting the same error.
I'm not an expert at all in Arduino programming and any help is appreciated.
Thanks in advance!!!!
Steve
Reply 4 years ago
Hi Steve,
You will need to make sure the definition of DebugLn is in the code, at the top you will need to make sure you have the following line
#define DebugLn(s) Serial.println((s))
This is to define DebugLn function the same as Serial.println function in Arduino. If this is not working still just replace "DebugLn" with "Serial.println".
I hope this helps.
4 years ago
Great, thanks a lot for this tutorial, flashing and connecting to a Wemos D1 mini lite worked at first attempt.
I've got one question. You say: "- Working webserver with php server scripting installed (not required if you just want to view the temperature in your local area network)". Actually, I'm only looking for a nice visualization of the A0 value (in my case a pressure sensor for measuring the level in a tank) inside my LAN. How can I achieve this? The only page I can open is the setting page.
Thanks in advance & best regards
Martin
Reply 4 years ago
Hi dadock, you can check out my other instructables https://www.instructables.com/id/Wireless-Temperature-and-Motion-Sensor/, in Step 4-6 is the instruction on how to setup a database in the webserver that holds your data. And Step 8 is showing you how the visualisation looks like. Hopefully that is something that you are after. Otherwise, just let me know, I might have to simplify the visualisation instruction.
Question 4 years ago
Hi, I've been battling with this but cannot get it to work.
I have a ESP8266-12F and a LM35 sensor. I wire it together and run some code and the code works great, but the readings I'm getting back form the sensor is very odd.
1) It's all over the place. It goes up and down all the time, pretty wildly.
2) The raw analog value being returned on the AD0 pin varies from 0 to 16, but no higher, does not really respond to temperature changes and seems a bit random in it's variance.
I've confirmed the AD0 pin seems to be working, but checking it returns a full 1024 value when 3.3v are applied, which is does. But when the sensor in 'in the middle' it does not really behave itself.
In the pictures:
* Purple = 3.3v
* Green = GND
* Blue = AD0
Any help would be very welcome. I just cannot work out what I'm doing wrong.
Answer 4 years ago
Hi Richard, couple of things you can try.
- can you use a prototype board or check the connectivity of the wire you are using to make sure that it is wired property. Sometimes the wire can be broken too. Or it might not connect property when you try to slot 3 into the small spacing between the sensor legs.
- if you have a multimeter this comes handy for trouble shooting the connection
- try just a simple wiring of lm35 sensor with power supply of 5v and gnd, and check the output (middle pin) voltage to see whether it produce the voltage responsive to temperature changes to make sure the sensor is not broken.
- if all good, check the 3.3 v and gnd from esp8266 that it is actually 3.3v
Please let me know how did you go.
Reply 4 years ago
Thanks. I bought a new set of sensors from a different place and these are now working :-)
Reply 4 years ago
Good to know that you get it working, Congrats!
4 years ago
The next step would be to choose any temperature and interact with a boil or A/C
5 years ago
I am getting a crash report when I try to access the IP of the AP
https://pastebin.com/1CMT9qTZ