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

LAB 6 IoT - Send Sensors Data to Cloud BIW33803

NAME: YONG HUEI JEAN


MATRIC NO: AI210405
SECTION: 06

BIW33803 – IoT
Semester 2 SESSION 2023/2024
LAB 6
Title : LAB-IoT - Send Sensors Data to Cloud

Objectives : By the end of this lab activity, students should be able


to:
• Understand the use of Arduino WeMos D1 WiFi UNO
ESP8266 IOT IDE Compatible Board.
• Utilize Arduino libraries effectively.
• Configure Wi-Fi Settings.
• Set up IoT cloud services with ThingSpeak.

Materials : • Arduino board


• Temperature and humidity sensor / light sensor
 USB cable
• Personal computer with Arduino IDE installed

Part I : IoT-Arduino WeMos /NODEMCU with ESP8266


Exercises:

1
LAB 6 IoT - Send Sensors Data to Cloud BIW33803

Q1 Given the following Arduino- code, answer Q1(a)-Q1(c):

#define DHT11PIN D3 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)
{
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature (oC): ");
Serial.println((float)DHT11.temperature, 2); delay(2000);
}

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

Figure 1: Serial monitor shows the sensor data

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".

int Temp_Check(float temperature) {


if (temperature > 20) {
return 1;
} else {
return 0;
}
}
Table 2: Function to pass the temperature sensor value through it

#include <dht11.h>

#define DHT11PIN 7

dht11 DHT11;

int Temp_Check(float temperature) {


if (temperature > 20) {
return 1;
} else {
return 0;

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

Figure 2: Serial monitor after adding temperature check

Part II : IoT- Send Sensors Data To Cloud

#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

DHT dht(DHTPIN, DHTYPE);

WiFiClient client;

const char* server = "api.thingspeak.com";


const int postingInterval = 1 * 1000; //post data every x seconds

void setup() {
Serial.begin(115200);
Serial.println("DHT11 TEST PROGRAM");
Serial.println();
dht.begin();
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


delay(500);
}
ThingSpeak.begin(client);
}

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

// wait and then post again


delay(postingInterval);
}
Table 3: Sketch of uploading sensor data to cloud using ESP32-CAM with CH340

Figure 3: Data of temperature and humidity uploaded to Thingspeak

Figure 4: ThingSpeak graph (channel id: 2552682)

8
LAB 6 IoT - Send Sensors Data to Cloud BIW33803

Exercise: Add Additional Sensors


• Objective: Expand the system to handle multiple types of sensors.
• Task:
1. Integrate an additional sensor (e.g., light sensor) to your existing setup.
2. Modify the code to send this new sensor data to another field in your ThingSpeak
channel.
#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

int trigPin = 13;


int echoPin = 15;
int pingTravelTime;

// WIFI settings
const char* ssid = "UR NETWORK";
const char* password = "URPASSWORD!";

DHT dht(DHTPIN, DHTYPE);

WiFiClient client;

const char* server = "api.thingspeak.com";


const int postingInterval = 1 * 1000; //post data every x seconds

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);

while (WiFi.status() != WL_CONNECTED) {


delay(500);
}
ThingSpeak.begin(client);
}

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);

// wait and then post again


delay(postingInterval);
}
Table 4: Sketch of adding new sensor (Ultrasonic sensor) to the existing code

10
LAB 6 IoT - Send Sensors Data to Cloud BIW33803

Figure 5: Sensor data uploaded to Thingspeak

Figure 6: ThingSpeak graph (channel id: 2552682)

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

Summary of the report:

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 7: ESP32 CAM connected with humidity sensor

Figure 8: ESP32 CAM connected with humidity sensor and ultrasonic sensor

12

You might also like