---
# System prepended metadata

title: Measuring Room Temperature

---

---
###### tags: IoT
---
# Measuring Room Temperature
#### By Karam Khatib (kk222yq at Linneaus University)
In this project we are going to build a system that allows us to measure the temperature of the room. We will be using Arduino Uno and a temperature sensor to measure the room temperature, which will then be sent to a server to visualise the data. The time required to set up such project woud vary from 8 - 30 hours depending on the knowledge and experience that you maintain. 

## Table of contents
[TOC]















## Objective
There are some reasons for me to implement this project: 

1. I was curious to understand more about the Internet of Things, its protocols, and how to set up IoT devices.
2. I wanted to be able to Measure my room temperature.
3. I wanted to learn more about using arduino, since I have already created a project using it and I got very interested in it. I also like programming in C and C++, which makes it more fun for me personally to create projects using Arduino.

## Materials 


Below is a list of materials needed to implement this project.

1. **Microcontroller:** I am using an Arduino Uno, which is an open-source microcontroller that can run Arduino, and it is the best board for learning electronics and coding.
Price: [292,30 SEK](https://se.rs-online.com/web/p/arduino/7697409?cm_mmc=SE-PLA-DS3A-_-google-_-PLA_SE_SE_Raspberry_Pi_%26_Arduino_%26_Development_Tools_Whoop-_-(SE:Whoop!)+Arduino-_-7697409&matchtype=&pla-341920527054&gclid=Cj0KCQjwn4qWBhCvARIsAFNAMij0Rx9Xi_a_5731MU1zrrVkIHn17a1fxL6aro6iPsJKNWS-U2uMsr4aAhBZEALw_wcB&gclsrc=aw.ds)

2. **BreadBoard:** I am also using a Breadboard to make the wiring easier and more neat.
Price: [52,59 SEK](https://se.rs-online.com/web/p/raspberry-pi-hats-add-ons/2222162?cm_mmc=SE-PLA-DS3A-_-google-_-CSS_SE_SE_Raspberry_Pi_%26_Arduino_%26_Development_Tools_Whoop-_-(SE:Whoop!)+Raspberry+Pi+HATs+%26+Add-ons-_-2222162&matchtype=&pla-374265978167&gclid=Cj0KCQjwn4qWBhCvARIsAFNAMig59xfkDKWMUrXwlkY9rT8DDWxGH8XEDn32nhytmhfopI1FL_Ds9ZoaAh8YEALw_wcB&gclsrc=aw.ds)

3. **Jumper Wires:** To connect the temperature sensor to the arduino I will be using Jumper wires. 
Price: [41,29 SEK](https://se.rs-online.com/web/p/breadboard-jumper-wires/7916454?cm_mmc=SE-PLA-DS3A-_-google-_-CSS_SE_SE_ESD_Control_%26_Cleanroom_%26_PCB_Prototyping_Whoop-_-(SE:Whoop!)+Breadboard+Jumper+Wires-_-7916454&matchtype=&pla-477747351836&gclid=Cj0KCQjwn4qWBhCvARIsAFNAMijcJvUj9-ulN4SYqr-6-6pNRNhwYaH7rEwzQFxKhgaWIGa4f-V74oAaAg_AEALw_wcB&gclsrc=aw.ds) 

4. **USB Cable:** I am using a USB cable to upload the code to the Arduino microcontroller. 
Price: [363,54 SEK](https://se.rs-online.com/web/p/usb-cables/8950495)

5. **Temperature Sensor:** To measure the temperature I am using a temperature sensor of type MCP9700. 
Price: [15 SEK](https://www.electrokit.com/produkt/mcp9700a-to-92-temperaturgivare/)

## Computer Setup
The procedures I followed to get my microcontroller up and running and how I uploaded code to the microcontroller are described below.
### Chosen IDE
I will use Arduino IDE since it is very simple, compatible with Arduino Uno microcontroller (obviously) and I already have some experience with it.

### Uploading The Code
The code is uploaded from the IDE to the microcontroller by connecting the microcontroller to your PC/Laptop via a USB cable and compiling the code. You will be able to fully understand how to upload your code in the following sections.

### Steps that you needed to do for your computer
Install the most recent version of the Arduino IDE via the  https://www.arduino.cc/en/software.

## Putting Everything Together

### Material Setup
I started by attaching three jumper wires to the Arduino Uno device. The first wire is linked to the 5V pin on the arduino microcontroller on one end and to VDD pin on the temperature sensor on its other end, the second wire is connected to the GND pin on arduino and to GND on the temperature sensore, and the third wire was connected to the A2 pin on arduino and VOUT on the temperature sensor.
![](https://i.imgur.com/FWapHwF.jpg)

![](https://i.imgur.com/MlqQXr1.jpg)
![](https://i.imgur.com/3soHGYb.png)


### Flashing The Microcontroller


Launch the Arduino IDE. go to boardmanger, then tool and select Ardunio Uno as your selection. 
![](https://i.imgur.com/jqOmrPs.jpg)

Connect the USB cable to your computer, and then click the arrow button in the upper right corner of the IDE. A blinking led will then turn on, indicating that your Arduino is connected properly.
![](https://i.imgur.com/2E0E4MU.png)





### The Program Code
Below is the code used to measure the room temperature and send the data to dweet.io to visualise the data, which is uploaded to the Arduino microcontroller. 
**Note:** there is a part of the code (a variable named **dweet_siteName**), which will not be the same for you and will be given to you from dweet.io once you create your database on it. This will be explained more in the [Treansmitting The Data](https://hackmd.io/5eU7TiC7R9-8XZ3j4eHrpA?both#Transmitting-the-datasection) section in this tutorial.
```
//Adding Libraries
#include <WiFi.h>
#include <WiFiServer.h>
#include <WiFiClient.h>
#include <WiFiUdp.h>
#include <avr/delay.h>
#include <avr/io.h>



// setting up dweet and wifi connection
const char* wifi_ssid = ""; //This variable holds the wifi ssid. 
                            //It is empty as it is a placeholder for now.
const char* wifi_password = ""; //This variable holds the wifi password. 
                                //It is empty as it is a placeholder for now.
const char* host  ="dweet.io"; // This is the website, where you will send your data to.
const char* dweet_siteName = "wide-drop"; // This is the site extension given by dweet
                                          //to share your data to. 
                                          
int temperature_pin = A2; // A pin that reads the sensor's output

void setup() {
  
  pinMode(temperature_pin, INPUT);// setting pin 2 as an input pin
  Serial.begin(9600); // begins the serail at 9600 and starts to read

  WiFi.begin(wifi_ssid, wifi_password);//Wifi Network Connection
  int connection_attempts = 0;
    while ((WiFi.status() != WL_CONNECTED) && (connection_attempts < 15)){
      connection_attempts++;
      delay(500);
      Serial.print("Attempting to connect to Wifi");
    }
    if(connection_attempts>=15){
      Serial.println(F("WiFi conenction FAILED"));
    }


  if(WiFi.status() == WL_CONNECTED){
    Serial.println("Connection succeded");
    Serial.println( WiFi.localIP());
    Serial.println("The Setup is ready");
  }
}


float get_temperature(){

  int reading_pin = analogRead(temperature_pin);// This variable gets the voltage 
  //reading from the temperature sensor, if it returns 0 then 0V, if it returns 1023 5V.

  float voltage = reading_pin * 5.0;// converting the reading to volt values
  voltage /= 1024.0;// percentage of input reading for 5V voltage

  // converting the value to celsius using 10 mV  per degree with 500 mV offset
  //(voltage- 500mV) times 100)

  float temperature_celsius = (voltage - 0.5) * 100 ;
  return temperature_celsius;

}

void send_to_dweet(float temperature){ // send data to your dweet page

 WiFiClient client;
  const int httpPort = 80;
  client.stop();

  if(client.connect(host, httpPort)){
   Serial.println("Connected...");
    String post_req = "POST /dweet/for/"; //Postrequest to dweet to add your data
    post_req.concat(dweet_siteName);
    post_req.concat("?A2=");
    post_req.concat(temperature);
    Serial.print(post_req);
    client.println(post_req);

    client.println("Host: dweet.io");
    client.println("Connection: close");
    client.println();

  }else{

 Serial.println("Connection Failed");

  }

}


void loop(){

 float room_temperature = get_temperature();
 Serial.println(" The room temperature is ");
 Serial.print(room_temperature);
 Serial.print(" Celsius");
 send_to_dweet(room_temperature);
 delay(4000);


}
```
After writing the code, you can compile it by pressing **verify** on the Arduino IDE software to check for if the code compilation has no errors and then you can upload it to the microcontroller by pressing **Upload** on the IDE software.
![](https://i.imgur.com/blq6fr0.png)

![](https://i.imgur.com/zNKKVqP.png)





## Transmitting The Data
WiFi, Bluetooth, and Lora are all supported by the equipment used for this project. WiFi, however, is being utilized to transfer the data since it is ideal for the project's goal of home use despite its short range and high power consumption (such as measruing the room tempearture).

### Transport Protocol
In this project the adrunio microcontroller works as client and the dweet web works as a server. The arduino makes a post request to dweet.io and then the web send back the http resposne to the screen. 

#### Create The dweet Database 
To create your database on dweet, go to dweet.io and press on **Try it Now** button

![](https://i.imgur.com/X7Djn2u.png)

dweet will give you the url to share your data to but you only need the last part of it. In this case it would be **wide-drop**![](https://i.imgur.com/OZH3mW7.png)


depending on your internet speed and the number of seconds you provide in your code, the data will then be delivered into the page; in my case, it was sent every four seconds as follows.
![](https://i.imgur.com/u735evw.png)


## Final Results
I have always loved creating arduino project, and this project made love it even more especially that it allowed me to learn more about communicating the microcontroller to a database through internet connection and showed me a real application of IOT, which I applied by myself. Although the project seems simple to implement it was a bit challenging but in a fun way.

In the future, I would like to improve this project and add features that allow me to measure the humidity in the room. It should not be easier for me now to implement. In general I was very happy with this project and gave an extra motivational boost to work more on such project, and hopfully I will post another tutorial on how to measure both the temperature and humidity in your room.

## References
1-  https://arduinogetstarted.com/tutorials/arduino-http-request 
2- https://dweet.io/
3- https://www.arduino.cc/
4- subscription.packtpub.com/book/hardware_and_creative/9781783986088/7/ch07lvl1sec45/sending-data-to-dweet-io
5- https://os.mbed.com/users/edodm85/notebook/mcp9700a-temperature-sensor/
6-https://gist.github.com/liz-miller/d17a63ca722497f4b703f57dcf1cdb48
