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 https://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.