IoT Temperature Sensor With ESP8266

42,749

80

43

About: I am a Dad to two kids who are constantly need attentions. I am passionate about anything related to technology, Internet of Things, Arduino and Raspberry Pi, I am good at fixing things that is broken to red...

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".

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.

Share

    Recommendations

    • Growing Beyond Earth Maker Contest

      Growing Beyond Earth Maker Contest
    • Frozen Treats Challenge

      Frozen Treats Challenge
    • Colors of the Rainbow Contest

      Colors of the Rainbow Contest

    43 Discussions

    0
    None
    dadock

    2 months 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

    1 reply
    0
    None
    snorlaxprimedadock

    Reply 2 months 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.

    0
    None
    RichardM505

    Question 2 months 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.

    IMG_0757.JPGIMG_0758.JPGIMG_0756.JPG
    3 answers
    0
    None
    snorlaxprimeRichardM505

    Answer 2 months 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.

    0
    None
    RichardM505snorlaxprime

    Reply 2 months ago

    Thanks. I bought a new set of sensors from a different place and these are now working :-)

    0
    None
    MarioS103

    4 months ago

    The next step would be to choose any temperature and interact with a boil or A/C

    0
    None
    snorlaxprimeSamW91

    Reply 4 months ago

    Thank you for your suggestion on how to fix the problem

    0
    None
    snorlaxprimeRaymondN29

    Reply 1 year ago

    Hi RaymendN29,

    Have you try just a simple wifi setup to see whether the ESP8266 is working, you can try the example code in File->Examples->ESP8266WIFI->WiFIAccessPoint and see how did you go.

    0
    None
    PatricioD8snorlaxprime

    Reply 11 months ago

    Same issue here, I get the exception after connecting to the hotspot, then the nodemcu restart and start reading the temp, but of course no wifi access.

    0
    None
    snorlaxprimePatricioD8

    Reply 11 months ago

    Hi PatricioD8,

    Have you try the simple WifiAccessPoint example to see whether the ESP8266 is working ok? I can simplify the code down so that the access point is hardcoded. Let me know how did you go.

    0
    None
    MehdiO6

    4 months ago

    i have a problem in step 5. When i open the cloud i have this msg Notice: Undefined index: temp_sensor in /storage/ssd5/070/8517070/public_html/wecg/webserver/data_store.php on line 6
    0

    Capturez.PNG
    1 reply
    0
    None
    snorlaxprimeMehdiO6

    Reply 4 months ago

    Hi,
    Can you confirm that your cloud support HTTP POST? I didn't use cloud, just a normal webserver with PHP enabled.
    Also can you check what is returned at the following:

    DebugLn(data); <======== check the data, this should say "temp_sensor=xx"
    int httpCode = http.POST(data);
    String payload = http.getString();
    DebugLn(httpCode); <======= check what this return, it should be "200" when success
    DebugLn(payload); // this will get the response.

    If your server doesn't support HTTP POST you might want to use HTTP GET, which is much easier to DEBUG.

    0
    None
    ΧάρηςΚ1

    Question 6 months ago

    I am almost done but:
    I see ESP-TEMP, can connect to it, can find the IPs 192.168.4.2 and Default Gateway is 192.168.4.1. The problem is that I can't connect to 192.168.4.1. It says "can't reach this page". I can ping 192.168.4.1 without packet loss. I have tried the example wifiaccesspoint and 192.168.4.1 opens. Any advise?
    And a 2nd question: How can I change the temp interface on the webserver? I would like to have just the current number and a graph with previous recordings.
    Thank you!

    4 answers
    0
    None
    snorlaxprimeΧάρηςΚ1

    Reply 6 months ago

    If you can see ESP-TEMP, that means it is part of the setup, you need to connect using a laptop or mobile phone, then check the gateway IP, using the browser navigate to the gateway ip. You should get presented with the Configuration page as shown in step 5.
    At this point in Arduino, it is executing the handleRoot() function, the setting page is constructed in mainPage.h file. I hope this makes sense.
    As for your second question, you will have to modify the ArduinoData.php. This page reads the data file called temp.txt which store the temperate value posted by ESP8266. You can completely write a new webpage that read the temp.txt. Another alternative is to modify the data_store.php to store the posted value into a database for further processing.

    0
    None
    ΧάρηςΚ1snorlaxprime

    Reply 6 months ago

    Thank you for reoly! I have done what you say, found assigned ip and rhe ip of access point, then try to connect with browser from my mobile or laptop to this ip (192.168.4.1) but it does not connect..

    0
    None
    snorlaxprimeΧάρηςΚ1

    Reply 6 months ago

    That is very strange.
    You might want to try the following:
    - Main PC connected to ESP via USB and open the serial connection, so that you can see the debug code.
    - use your mobile to connect to ESP access point, and use the mobile phone to browse to the ESP gateway (192.168.4.1)
    - check the serial connection debug window in main pc to see whether the handleRoot() function is executed.
    Please post the debug message if you still can't connect.

    0
    None
    ΧάρηςΚ1snorlaxprime

    Reply 6 months ago

    Here is the debug code as I try to connect (I guess something is going totally wrong):  )a⸮"I⸮⸮⸮⸮⸮⸮[⸮⸮Z{PP[^~W⸮U⸮⸮W⸮⸮⸮UU⸮[⸮u⸮[⸮ԮWv_⸮U⸮VW[⸮⸮⸮W⸮ۿ⸮⸮[⸮ZP⸮⸮⸮T⸮^⸮UV⸮⸮⸮⸮׫U{⸮⸮⸮PW^^_k⸮֪_○⸮U⸮Un(ǜ⸮~⸮⸮X⸮f⸮Y⸮  )⸮5j⸮UVW⸮{⸮VU⸮⸮⸮⸮⸮[⸮⸮Z⸮⸮W⸮]v_{⸮uW⸮⸮⸮⸮⸮⸮[⸮⸮⸮⸮⸮^^⸮TUV⸮⸮{⸮⸮U⸮⸮[⸮vZ⸮⸮_⸮UV⸮⸮o⸮׻U*⸮⸮⸮^⸮J⸮⸮⸮U⸮⸮⸮^V⸮⸮Uf⸮U⸮(⸮LA⸮U⸮⸮/g%-⸮0@ A⸮Q⸮⸮⸮UvW⸮{⸮⸮U⸮⸮⸮⸮Z⸮U⸮VU⸮⸮[⸮-⸮r⸮⸮⸮U⸮⸮[⸮R~W⸮uw⸮⸮⸮⸮⸮⸮⸮e]⸮⸮kLUU⸮⸮T⸮VU[⸮+⸮⸮ZP⸮⸮UV⸮⸮_⸮k⸮U⸮⸮ԫu⸮֪_⸮⸮⸮U⸮⸮⸮uWk⸮9⸮⸮⸮⸮