Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Experiment 3:

Aim:

i. To interface the temperature & humidity sensor in Ardunio-UNO board.


ii. To obtain the parameters using IOT platform and display in OLED display.

Apparatus Required:

S.No. Name of the item Type Range Quantity


1. Ardunio UNO - 1
2. OLED SSD1306 - 1
3. Temperature and Humidity Sensor DHT11 - 1
4. Breadboard - - 1
5. Power Adaptor 12 V - 1
6. Connecting Wires - - 10

Procedure:

1. Connections are given as per the circuit diagram.


2. Replace Wifi SSID and Wifi Password of the connected router.
3. Replace API write key from the newly created thingspeak channel.
4. After making the above changes, the code is uploaded into Arduino board using
Arduino IDE.
5. Temperature and Humidity values of the surroundings are displayed in the OLED
display and the same values were send to Thingspeak cloud server and the data can be
viewed in the corresponding fields graphically.
Circuit Diagram:
Code

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire,
OLED_RESET);

#include <DHT.h>
#include <Adafruit_Sensor.h>
#define DHTTYPE DHT
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
char temperature[] = "00.0 C";
char humidity[] = "00.0 %";

#include <SoftwareSerial.h>
String apiKey = "NUTMAPZYKC34DAMT";
String Host_Name = "saravanan";
String Password = "saravanan sona";
SoftwareSerial ser(9,10);
int i=1;

void setup(void) {
Serial.begin(9600);
dht.begin();
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}

display.display();

delay(1000);
display.clearDisplay();
display.drawFastHLine(0, 15, SSD1306_LCDWIDTH, WHITE);

display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.setCursor(0, 5);
display.print(" TEMPERATURE:");
display.setCursor(0, 20);
display.print(" HUMIDITY:");
display.display();
Serial.begin(115200);

ser.begin(115200);
ser.println("AT+RST");

char inv ='"';


String cmd = "AT+CWJAP";
cmd+= "=";
cmd+= inv;
cmd+= Host_Name;
cmd+= inv;
cmd+= ",";
cmd+= inv;
cmd+= Password;
cmd+= inv;
ser.println(cmd);
}
void loop()
{
sensorread();
sensorsend();
}
void sensorread()
{
// Read humidity
byte RH = dht.readHumidity();
//Read temperature in degree Celsius
byte Temp = dht.readTemperature();
temperature[0] = Temp / 10 + 48;
temperature[1] = Temp % 10 + 48;
humidity[0] = RH / 10 + 48;
humidity[1] = RH % 10 + 48;
display.setCursor(80, 5);
display.print(temperature);
display.setCursor(80,20 );
display.print(humidity);
display.drawRect(104, 5, 3, 3, WHITE);
display.display();
delay(1000);
}
void sensorsend()
{
int humidity = dht.readHumidity();
int temperature = dht.readTemperature();
String state1=String(temperature);
String state2=String(humidity);
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "184.106.153.149";
cmd += "\",80";
ser.println(cmd);
Serial.println(cmd);
if(ser.find("Error")){
Serial.println("AT+CIPSTART error");
return;
}
String getStr = "GET /update?api_key=";
getStr += apiKey;
getStr +="&field1=";
getStr += String(state1);
getStr +="&field2=";
getStr += String(state2);
getStr += "\r\n\r\n";
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
ser.println(cmd);
Serial.println(cmd);
if(ser.find(">")){
ser.print(getStr);
Serial.print(getStr);
}
else{
ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
}
delay(1000);
}

Output Inference:

Result:

You might also like