# LNU project - Table of Content [ToC] ## Tutorial: Arduino temp and humidity sensor ### Overview By Alexandra Gripenhoftner From / ag223nw In this tutorial you will learn to build an Arduino based sensor that tracks temperature and humidity and then sends the data via WiFi to Thingspeak. It is a rather basic project that is great for beginners and the approximate time estimation is 2 hours. ### Objective I decided on this project due to my interest in rare plants, many of which require pretty exact temperatures and humidity to thrive. I debated using a Raspberry Pi or LoRa but settled on Arduino as I already owned some parts. The project functions both as an introduction to the WiFi module and sensors, meaning that one can use the information from this tutorial and apply it on other builds with different sensors. The data gathered from my project will be logged and compared to the overall health of my plants which will give me an indication of what a perfect environment would be. ### Material | Part | Cost | | ----------------- |:----------------------- | | Arduino Uno | 270kr | | ESP8266 (Wifi module) | 120kr | | DHT11 (temp. & humidity sensor) | 100kr | | Breadboard | 50kr | | Wires | 80kr (for a bunch) | Total cost: 620kr I bought all my parts att Kjell and Company (Kjell.com) as I live close to one of their physical stores. Kjell and Company stocks several sensors and Arduino components and I've found that the staff is very knowledgeable which is a perk. ![](https://i.imgur.com/9t88iZJ.jpg) As mentioned earlier, I used the Arduino Uno board as I had previously bought one. I like Arduino as there are hundreds of tutorials online and there are pretty much unlimited possibilites for a creative mind. ![](https://i.imgur.com/dg655vk.jpg) The ESP8266, the WiFi module. I opted to by this basic version of the ESP8266 as it is rather cheap compared to other WiFi modules, while still fulfilling its purpose. ![](https://i.imgur.com/QmuYOXW.jpg) DHT11, temperature and humidity sensor. I originally started of by using a analog temperature sensor but in the end I bought this one as there are several tutorials for it and it is a cheap sensor that works great. ### Computer setup As I chose Arduino as my board, I also chose to write code in the Arduino IDE. The IDE contains several examples that I used to get to know the components, e.g. the blink example to get the blue led on the ESP8266 to blink. To be able to use the ESP, one has to include the proper board. I did so by following the steps of this YouTube tutorial https://www.youtube.com/watch?v=Tv2RMlIxBXs , in which we can learn how to import the board and set it up for using with the blink example. I recommend watching the first 4 minutes as they are of key importance to using the component. After finishing the flashing and "installation", one more download is needed. The DHT sensor library which can be found here https://github.com/adafruit/DHT-sensor-library and are imported into Arduino IDE through Sketch --> Include Library --> Add .Zip library. This library is neccesary to run the code for the temperature and humidity sensor. ### Putting everything together ![](https://i.imgur.com/JOsnBc5.png) In this circut diagram, the top grey square represents the ESP8266 with its 8 pins and the lower blue square the DHT11 with its 3 legs. Start by making sure that your Arduino board isnt connected to a power source. !!IMPORTANT!! The two components require different power inputs (5V and 3.3V), make sure that the 5V ONLY goes to the DHT11 and the 3.3V ONLY goes to the ESP to ensure that the ESP doesn't get fried. In order to do so, connect the 3.3V to the upper side of the breadboard and the 5V to the lower side. Connect the ground to the lower side as well. The DHT is very straight forward in its connections, right leg to ground, middle leg to power, left leg to digital pin no. 2. The ESP might seem confusing but if you followed the youtube tutorial, you know a bit of what goes where. On the back side of the components, there are indicators for each pin as well. 3V3, RTS and EN goes to the power. Again, remember to connect it ONLY to the top side power. The TX goes into the TX pin on the board and RX goes to RX on the board next to TX. The final wire (GND) goes to ground and as we have chosen to only connect the lower ground, the wire must go in the lower section. ### Platform I chose between the mobile application Blynk and Thingspeak as a platform for my data. In the end I decided on ThingSpeak as I found it to be a bit more straight forward and would allow me to analyze the data over a longer period of time compared to Blynk which is more of a data presentation tool. ThingSpeak offers channels for free after one creates an account which is perfect for my project. The channel settings allows you to decide how many fields you wish to have, in my case two (Temperature and Humidity) and how they are going to look. After setting up a channel, ThingSpeak automatically generates a Write API key and a Read API key. The Write API key will be included in the code to signal that I want the data to be sent to this "adress". ThingSpeak is free for non commercial use for small projects which is great for projects like this. ### The code This part of the code shows how we tell the Arduino which sensor we are using as well as which network the ESP should connect to and what the ThingSpeak API is. ``` #include <stdlib.h> #include <DHT.h> // Here goes the library that we downloaded earlier #define DHTPIN 2 // The pin where the left DHT leg is connected to #define DHTTYPE DHT11 // There are more than one type of the DHT, I used the DHT11 but one could change it here DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor #define SSID "Mount Doom" // SSID is the name of your WiFi, mine is Mount Doom. #define PASS "0010010000" // Here you put the passowrd for your WiFI #define IP "184.106.153.149"// Not your IP but ThingSpeaks IP. Google it if needed. String msg = "GET /update?key=AKXYFX4KTH722SPL"; // Put your own Write key form ThingSpeak after key= ``` To get the value of the sensor, a loop is needed: ``` void loop(){ start: error=0; temp = dht.readTemperature(); // The temperature hum = dht.readHumidity(); // The humidity tempC = dtostrf(temp, 4, 1, buffer); // dtostrf converts the float to string updateTemp(); if (error==1){ goto start; //if the data isnt sent, go back to start } delay(60000); //Update once every minute, change the number to whatever timeframe you want } ``` To send the data to ThingSpeak, we need to tell the components to write the data in two fields and send them to ThingSpeaks IP. ``` void updateTemp(){ String cmd = "AT+CIPSTART=\"TCP\",\""; // begin cmd += IP; // ThingSpeaks IP that we defined earlier cmd += "\",80"; } cmd = msg ; cmd += "&field1="; //One field for temperature cmd += tempC; cmd += "&field2="; //One field for humidity cmd += String(hum); cmd += "\r\n"; Serial.print("AT+CIPSEND="); // If everything is OK, send Serial.println(cmd.length()); if(Serial.find("=")){ Serial.print(cmd); } else{ Serial.println("AT+CIPCLOSE"); // If not, return to previous error=1; } } ``` As for the final section of code, we need to connect the ESP to the Wifi using the credentials we declared in the beginning of the code. ``` boolean connectWiFi(){ cmd+=SSID; // The wifi name cmd+="\",\""; cmd+=PASS; // the wifi password cmd+="\""; Serial.println(cmd); delay(5000); if(Serial.find("OK")){ //if/else to declare what happens if the wifi is unaccessable return true; }else{ return false; } } ``` ### Transmitting the data As mentioned in the comments in the code, I opted for refreshing the temperature once every minute to be able to clearly track if the code was working or not and thus refreshing my channel once every minute. Once a minute is not needed for my use of this project, however I find it interesting to be presented with a large amount of data so I decided to stick with it for now. The data is sent through WiFi, using the ESP8266 and serval wires. I chose the protocol mainly because I wanted to learn to implement WiFi in projects as I think that it very beneficial for other editions of similar projects, e.g. a similar setup but with other sensors. As seen on my circuit diagram, I used the RX and TX ports on my Arduino board. Through these ports, I made use of the transport protocol UART to transport and convert data. ### Presenting the data ThingSpeak allows you to customize your fields for your channel based on what information you wish to gather, settings include bar graph, linear graph, timescale etc. For this project only two fields are neccesary, however I decided to use two fields per data type and display the data both through a linear graph and through a gauge widget. Currently the data is saved once a minute, resulting in the thick line. ![](https://i.imgur.com/6ANkvh3.jpg) ![](https://i.imgur.com/5Xg4Ktl.jpg) ### Finalizing the design I think my project came out okay, I ended up putting way too many hours into it as I had to buy a new temperature sensor and my code didn't want to upload to the ESP multiple times. If I were to start over I would probably buy the more expenside WiFi module as it seems more stable or simply buy the Arduino Uno Wifi board. Included in this tutorial is mainly the base code as I also think that it could be improved in many ways, e.g. using strings for all the parts of the ThingSpeak connection. *String API = "my key"; String HOST = "api.thingspeak.com";* All in all I feel happy with my first IoT project! ![](https://i.imgur.com/1pjK0XR.jpg) A colorful little project!