Professional Documents
Culture Documents
6.lab6iotsendsensorsdatatocloud
6.lab6iotsendsensorsdatatocloud
BIW33803 – IoT
Semester 2 SESSION 2023/2024
LAB 6
Title : LAB-IoT - Send Sensors Data to Cloud
1
LAB 6 IoT - Send Sensors Data to Cloud BIW33803
a) What is the purpose of this code? Describe the input/output pins used.
The purpose of the code is to read the humidity and temperature data from humidity
sensor (DHT11 sensor). The input pins used is D3 which is a digital pin.
b) Try to use Arduino-IDE to compile/check the code. Write down your observations.
DHT11 is not declared. This is due to the dht11 is actually a library and to use the
humidity sensor, we need to create object which is DHT11 to use the library code. The
code also has a switch case but the switch case is not complete and lack of ‘}’. The
switch case do not have the statement (case) to pass through so that it can execute the
corresponding code.
c) Based on your answer in Q1(b), explain how to resolve the code errors (If any).
2
LAB 6 IoT - Send Sensors Data to Cloud BIW33803
In this lab, ESP32-CAM with CH340 is used. To use the sensor, we must include the
library of that sensor. The library in this case is DHT11 library. To use the library, we
need to create an object, and declare, in this case DHT11. The chk variable is to check
the sensor is reading the data or not, so the statement (case) should be -1 which the data
is read and execute the block of the corresponding code. If -2 means the data not read.
By default , if it returns anything other than –1 and -2, it will execute the default block
which indicate that the sensor data not read.
#include <dht11.h>
#define DHT11PIN 7
dht11 DHT11;
void setup()
{
Serial.begin(9600);
Serial.println("DHT11 TEST PROGRAM ");
Serial.println();
}
void loop()
{
Serial.println("\n");
int chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: ");
switch (chk)
{
case -1:
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature (oC): ");
Serial.println((float)DHT11.temperature, 2);
break;
case -2:
Serial.println("Data not read");
break;
default:
Serial.println("Failed!");
}
delay(2000);
}
Table 1: Sketch of reading humidity sensor data
3
LAB 6 IoT - Send Sensors Data to Cloud BIW33803
Q2 Write a new function called Temp_Check() to pass the temperature sensor value through it.
If the temperature sensor value is greater than 20, it returns "1", otherwise, it returns "0".
#include <dht11.h>
#define DHT11PIN 7
dht11 DHT11;
4
LAB 6 IoT - Send Sensors Data to Cloud BIW33803
}
}
void setup()
{
Serial.begin(9600);
Serial.println("DHT11 TEST PROGRAM ");
Serial.println();
}
void loop()
{
Serial.println("\n");
int chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: ");
switch (chk)
{
case -1:
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature (oC): ");
Serial.println((float)DHT11.temperature, 2);
break;
case -2:
Serial.println("Data not read");
break;
default:
Serial.println("Failed!");
}
int result = Temp_Check(float DHT11.temperature);
Serial.println(result);
delay(2000);
}
Table 2.1: Sketch after adding Temp_Check function
5
LAB 6 IoT - Send Sensors Data to Cloud BIW33803
#include <ThingSpeak.h>
#include <DHT.h>
#include <WiFi.h>
// Thingspeak settings
#define CHANNEL_ID 2552682
#define WRITE_API_KEY "BE66CYDEJKGDNW0T"
// humidity sensor
#define DHTPIN 12
#define DHTYPE 11
// WIFI settings
const char* ssid = "URNETWORK";
const char* password = "URPASSWORD";
6
LAB 6 IoT - Send Sensors Data to Cloud BIW33803
WiFiClient client;
void setup() {
Serial.begin(115200);
Serial.println("DHT11 TEST PROGRAM");
Serial.println();
dht.begin();
WiFi.begin(ssid, password);
void loop() {
if (client.connect(server, 80)) {
float h = dht.readHumidity();
float t = dht.readTemperature();
ThingSpeak.setField(1, t);
ThingSpeak.setField(2, h);
ThingSpeak.writeFields(CHANNEL_ID, WRITE_API_KEY);
7
LAB 6 IoT - Send Sensors Data to Cloud BIW33803
8
LAB 6 IoT - Send Sensors Data to Cloud BIW33803
#include <DHT.h>
#include <WiFi.h>
// Thingspeak settings
#define CHANNEL_ID 2552682
#define WRITE_API_KEY "BE66CYDEJKGDNW0T"
// humidity sensor
#define DHTPIN 12
#define DHTYPE 11
// WIFI settings
const char* ssid = "UR NETWORK";
const char* password = "URPASSWORD!";
WiFiClient client;
void setup() {
Serial.begin(115200);
Serial.println("DHT11 TEST PROGRAM");
Serial.println();
dht.begin();
9
LAB 6 IoT - Send Sensors Data to Cloud BIW33803
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
WiFi.begin(ssid, password);
void loop() {
if (client.connect(server, 80)) {
float h = dht.readHumidity();
float t = dht.readTemperature();
digitalWrite(trigPin, LOW);
delayMicroseconds(10);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pingTravelTime = pulseIn(echoPin, HIGH);
delay(25);
// Serial.println(pingTravelTime);
ThingSpeak.setField(1, t);
ThingSpeak.setField(2, h);
ThingSpeak.setField(3, pingTravelTime);
ThingSpeak.writeFields(CHANNEL_ID, WRITE_API_KEY);
10
LAB 6 IoT - Send Sensors Data to Cloud BIW33803
The new sensor added to the existing circuit is an ultrasonic sensor which in this case uploads the time the
pulse travels from the signal transmitter to the receiver. The pulseIn() output is in microseconds.
11
LAB 6 IoT - Send Sensors Data to Cloud BIW33803
In this lab, I have learnt how to use library to read the sensor data and the wifi functionality which enables
the board to have network connectivity and ability to upload the data to the cloud. The challenge in this lab
is the board itself. Because of the lack of the board, the ESP32-CAM with CH340 is used.
Figure 8: ESP32 CAM connected with humidity sensor and ultrasonic sensor
12