**Iot Tutorial ki222df** LnU Karolina Ihrfelt ki222df 1DT305 Tillämpad IoT ~ Internet of Things ~ Tutorial SUMMER HOUSE TEMPERATURE SENSOR INTRODUCTION The project is about measure temperature through a sensor. This is my first time experimenting with IoT. I tried to do my project as simple I could. I encourage anyone who is new to the field to try IoT out! This tutorial can help you go through some basic steps to set up your own device. If you’re a beginner, expect the project to take you no longer than five hours. But the learning progress goes beyond the practical steps and have taken weeks. It takes more time to understand the how everything is connected, the possible implications and some concerns with IoT. OBEJECTIVE My project is about measure temperature through a sensor and connected through wi-fi the data will be accessible even if I am not in the nearby. This can be appropriate if you have a country house to take care of all year around, but you normally only access it 2-3 months per year. The wi-fi connected temperature sensor can tell when the inside temperature reaches below a certain degree and it is time to close the country house for the season, for example turn off the water. The cold weather risks to water damage the house if the water in the pipes freezes, which should preferably be avoided! This project saves the owner time, energy and worries. MATERIAL ![](https://i.imgur.com/J0zaSfl.png) List of Material; (1,2,3 from the left) 1) ESP8266 - 69kr / piece. ESP8266 is a low cost alternative for in this case both microchip and development board, can be coded with both Arduino and Micropython. 3) Sensor : DHT22 - 41 kr / piece A low cost sensor that can measure temperatures between -40°C and +80 °C, which is suitable for this project. 2) Breadboard - 29 kr /piece It is a reusable plugboard suitable for connecting and experimenting with electronics. Couple of wires and a resistor Can be accessed and bought at pchbutik.se COMPUTER SETUP I am using Arduino IDE. Install Arduino on your computing Install Python 3 on your computer ![](https://i.imgur.com/Ap17w6N.png) Plugins: Go to the settings on the Arduino IDE and on the Additional Board managers URLs type: 
http://arduino.esp8266.com/stable/package_esp8266com_index.json ![](https://i.imgur.com/PpAN9pz.png) Install the ESP8266 by ESP8266 community from the boards manager accessed from the tools meny. ![](https://i.imgur.com/t8Gx1lq.png) Install DHT sensor library for ESPxx, accessed from the library manage. To be able to connect and communicating from the development board through the USB we need to install an extra driver, in this case I used CP210x from Silicon Labs. Download and install this to your computer. Before I start to code, Arduino has to be restarted. Plug in your development board with an USB-cable and click on the bottom Upload and wait until you see in the bottom `done uploading`. Your computer set-up is now ready. ![](https://i.imgur.com/5rlqGn0.png) PUTTING EVERYTHING TOGETHER Sensor DHT22 used pins 1=VCC, 2=data, 4=Gnd ESP8266 used pins GPIO4 (D2), GND, 3V3 ![](https://i.imgur.com/II12qNI.png) Connecting Sensor DHT22 to ESP8266 with wires; VCC pin <=> 3v3 data pin <=> GPIO4(D2), resistor is used! gnd pin <=> GND Resistor are used to reduce current flow and as a voltage divider. This is mostly for a development setup. PLATFORM I built my own web server as my own platform. The platform is based on a local installation. I did not try with using other platforms. A web server is not optimal from all points of view but it serves the purpose of this project. It allows me to display the temperatures, which is what I wanted to achieve. See more under; Presenting the data. CODE ``` =python /********* Karolina based on Rui Santos https://randomnerdtutorials.com *********/ // Load Wi-Fi library #include <ESP8266WiFi.h> #include <DHT.h>; #define DHTPIN 4 // what pin we're connected to #define DHTTYPE DHT22 // DHT 22 (AM2302) DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino //Variables int chk; float hum=0; //Stores humidity value float temp=0; //Stores temperature value // My own network id/password const char* ssid = "**"; const char* password = "**"; // Set web server port number to 80 WiFiServer server(80); // Variable to store the HTTP request String header; // Save status ok/cold for the temperature readings String input5State = "ok"; ou // Current time unsigned long currentTime = millis(); // Previous time unsigned long previousTime = 0; // Define timeout time in milliseconds (example: 2000ms = 2s) const long timeoutTime = 2000; void setup() { Serial.begin(115200); // Begin the DHT reading dht.begin(); // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } // Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server.begin(); } void loop(){ // Saves current temperature hum = dht.readHumidity(); temp= dht.readTemperature(); //prints current temperature to serial Serial.print(hum); Serial.println(temp); //converits temperature to string String temp2=String(temp); String hum2=String(hum); // if the temp is 5 deg or below, a warning will be displayed to the client when accessing the website. if (temp<=5){ input5State="cold"; } WiFiClient client = server.available(); // Listen for incoming clients if (client) { // If a new client connects, Serial.println("New Client."); // print a message out in the serial port String currentLine = ""; // make a String to hold incoming data from the client currentTime = millis(); previousTime = currentTime; while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected currentTime = millis(); if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor header += c; if (c == '\n') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); // turns the GPIOs on and off if (header.indexOf("GET /reset") >= 0) { Serial.println("reset"); input5State = "ok"; } } // Display the HTML web page client.println("<!DOCTYPE html><html>"); client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"); client.println("<link rel=\"icon\" href=\"data:,\">"); // CSS to style the warning button // background-color and font-size attributes client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}"); client.println(".cold { background-color: #ffffff;border-style: dotted; border-width: 2px; border-color:#ff0000; color: black; padding: 16px 40px;"); client.println("text-decoration: none; font-size: 14px; margin: 2px; cursor: pointer;}"); // Web Page Heading client.println("<body><h1>Summer House Temperature Sensor</h1>"); client.println("<p>Temperature - State " + input5State + "</p>"); // Display current temperatures and the reset button if (input5State=="cold") { client.println("<p><a href=\"/reset\"><button class=\"cold\">Too cold temperature detected! Call Arne (neighbour) 0707781012. Click here to reset.</button></a></p>"); } client.println("<p>Temperature: " + temp2 +"C"+ "</p>"); client.println("<p>Humidity: " + hum2 +"%" +"</p>"); // The HTTP response ends with another blank line client.println(); // Break out of the while loop break; } else { // if you got a newline, then clear currentLine currentLine = ""; } } } } // Clear the header variable header = ""; // Close the connection client.stop(); Serial.println("Client disconnected."); Serial.println(""); } ``` TRANSMITTING THE DATA / CONNECTIVITY The data is sent on request and sends every time I open the web server or refresh the page. As wireless protocol I am using Wi-fi because it match the necessities of my project. Most summer houses has wi-fi connection so it seemed appropriate. The transport protocol used is TCP (Transmission Control Protocol) to help us send data from the receiver (ESP8266) to the sender (the web server). The user sends an HTTP request that through a wi-fi router reaches my device (sensor through development board) and sends back a data (temperature for example) to the user (client). There are some comments in the code that explains the process. For example the server makes itself availble and able to eventually be sending data; WiFiClient client = server.available(); // Listen for incoming clients. PRESENTING THE DATA I built a web server to present my data. The dashboard is built up two simple displays; temperature and humidity. A built in function warns when the temperature is too low. Simpel data (true or false/temperature cold or temperature ok) is saved once the temperature reaches below a certain degree. The `temperature - State Cold` is saved displayed with the warning until the it’s reset by clicking the button (you’ve called the neighbor, or checked on the house yourself). The temperature values is not saved. I use the data from the sensor to display on request realtime temperature and humidity which allow securing the state of my summerhouse even when I’m not there. The server will warn when the temperature has reached below 5° (had to put the device in the freezer to trigger the warning), then it is time to call the neighbor to have the house closed for the season! This requires an active house owner that check upon the data for his house every now and then. ![](https://i.imgur.com/s2FTZbb.png) ![](https://i.imgur.com/LOMrKwF.png) FINALIZING THE DESIGN I have had a lot of fun doing this project! Overall I’m surprised how much I could learn about technology, coming from a total different field. I hope my participation in the course and conducting the project will help me gain tech insights that I can use in my studies that I pursue the rest of the year. I could have built a case to the hardware to make it a nice accessory laying around the summer house! Since my sensor also tracks humidity I could have built in a warning on the dashboard if the humidity raises above let’s say 75%. That would indicate that someone has broken in or opened a window (normal humidity in a season-closed house would be 30-40%). I could have used an IoT platform to better control, manage and present my data. ![](https://i.imgur.com/Wji8V2y.jpg) This is how it looks like!