IoT Temperature Sensor With ESP8266

62,355

88

49

Introduction: IoT Temperature Sensor With ESP8266

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.

Be the First to Share

    Recommendations

    • Pocket-Sized Speed Challenge

      Pocket-Sized Speed Challenge
    • Super-Size Speed Challenge

      Super-Size Speed Challenge
    • Audio Challenge 2020

      Audio Challenge 2020

    49 Discussions

    0
    rbright
    rbright

    4 months 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....

    0
    rbright
    rbright

    4 months 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.

    0
    snorlaxprime
    snorlaxprime

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

    0
    rbright
    rbright

    Reply 4 months 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');

    0
    steve-f-dallas
    steve-f-dallas

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


    0
    snorlaxprime
    snorlaxprime

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

    0
    dadock
    dadock

    1 year 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

    0
    snorlaxprime
    snorlaxprime

    Reply 1 year 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
    RichardM505
    RichardM505

    Question 1 year 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
    0
    snorlaxprime
    snorlaxprime

    Answer 1 year 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
    RichardM505
    RichardM505

    Reply 1 year ago

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

    0
    snorlaxprime
    snorlaxprime

    Reply 1 year ago

    Good to know that you get it working, Congrats!

    0
    MarioS103
    MarioS103

    1 year ago

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

    0
    snorlaxprime
    snorlaxprime

    Reply 1 year ago

    Thank you for your suggestion on how to fix the problem

    0
    snorlaxprime
    snorlaxprime

    Reply 2 years 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
    PatricioD8
    PatricioD8

    Reply 1 year 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
    snorlaxprime
    snorlaxprime

    Reply 1 year 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
    MehdiO6
    MehdiO6

    1 year 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