IoT Lab BEC657C Experiments
IoT Lab BEC657C Experiments
To interface LED/Buzzer with Arduino /Raspberry Pi and write a program to ‘turn ON’ LED
for 1 sec after every 2 seconds.
1(ii). To interface the Push button/Digital sensor (IR/LDR) with Arduino /Raspberry Pi and write
a program to ‘turn ON’ LED when a push button is pressed or at sensor detection.
2 (i). To interface the DHT11 sensor with Arduino /Raspberry Pi and write a program to print
temperature and humidity readings.
2(ii). To interface OLED with Arduino /Raspberry Pi and write a program to print its temperature
and humidity readings.
3. To interface the motor using a relay with Arduino /Raspberry Pi and write a program to ‘turn
ON’ the motor when a push button is pressed.
4(i). Write an Arduino/Raspberry Pi program to interface the Soil Moisture Sensor.
4(ii). Write an Arduino/Raspberry Pi program to interface the LDR/Photo Sensor.
5. Write a program to interface an Ultrasonic Sensor with Arduino /Raspberry Pi.
6. Write a program on Arduino/Raspberry Pi to upload temperature and humidity data
to thingspeak cloud.
7. Write a program on Arduino/Raspberry Pi to retrieve temperature and humidity data
from thingspeak cloud.
8. Write a program to interface LED using Telegram App.
9. Write a program on Arduino/Raspberry Pi to publish temperature data to the MQTT broker.
10. Write a program to create a UDP server on Arduino/Raspberry Pi and respond with humidity
data to the UDP client when requested.
11. Write a program to create a TCP server on Arduino /Raspberry Pi and respond with humidity
data to the TCP client when requested.
12. Write a program on Arduino / Raspberry Pi to subscribe to the MQTT broker for temperature
data and print it.
1(i). To interface LED/Buzzer with Arduino /Raspberry Pi and write a program to ‘turn ON’ LED
for 1 sec after every 2 seconds.
void setup() {
// initialize digital pin LED_BUILTIN as an output. Which acts as Active low. (means Not operation)
pinMode(LED_BUILTIN, OUTPUT); //NodeMCU uses GPIO02 or D4 as built in LED.
}
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(SENSOR_PIN, INPUT); // Internal pull-up for button/sensor
Serial.begin(115200);
}
void loop() {
int sensorState = digitalRead(SENSOR_PIN); // Read sensor/button state
#include <DHT.h>
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
float temperature = dht.readTemperature(); // Read temperature (Celsius)
float humidity = dht.readHumidity(); // Read humidity
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT11 sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" degree C | Humidity: ");
Serial.print(humidity);
Serial.println(" %");
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
void setup() {
Serial.begin(115200);
dht.begin();
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Display on OLED
display.clearDisplay();
display.setCursor(0, 10);
display.println("Temperature: " + String(temperature) + " degree C");
display.setCursor(0, 30);
display.println("Humidity: " + String(humidity) + " %");
display.display();
Circuit:
OLED Pin NodeMCU Pin
VCC 3.3V
GND GND
SDA D2 (GPIO4)
SCL D1 (GPIO5)
3. To interface the motor using a relay with Arduino /Raspberry Pi and write a program to ‘turn ON’
the motor when a push button is pressed.
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(SENSOR_PIN, INPUT); // Internal pull-up for button/sensor
Serial.begin(115200);
}
void loop() {
int sensorState = digitalRead(SENSOR_PIN); // Read sensor/button state
void setup() {
Serial.begin(115200);
pinMode(SOIL_SENSOR, INPUT);
}
void loop() {
int moistureValue = analogRead(SOIL_SENSOR);
delay(2000);
}
4(ii). Write an Arduino/Raspberry Pi program to interface the LDR/Photo Sensor.
void setup() {
Serial.begin(115200);
pinMode(LDR_PIN, INPUT);
}
void loop() {
int lightState = digitalRead(LDR_PIN);
delay(1000);
}
5. Write a program to interface an Ultrasonic Sensor with Arduino /Raspberry Pi.
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
long duration;
float distance;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000);
}
6. Write a program on Arduino/Raspberry Pi to upload temperature and humidity data to thingspeak
cloud.
#include <ESP8266WiFi.h>
#include <DHT.h>
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
dht.begin();
}
void loop() {
float temperature = dht.readTemperature(); // Read Temperature
float humidity = dht.readHumidity(); // Read Humidity
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" degree C, Humidity: ");
Serial.print(humidity);
Serial.println("%");
client.stop();
delay(15000); // Wait 15 seconds before sending new data
}
7. Write a program on Arduino/Raspberry Pi to retrieve temperature and humidity data from
thingspeak cloud.
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h> // Include ArduinoJson library for parsing JSON
const char* ssid = "OnePlus Nord CE 2 Lite 5G"; // Enter your WiFi Name
const char* password = "Sathish@123"; // Enter your WiFi Password
String channelID = "2861168"; // Replace with your ThingSpeak Channel ID
String readAPIKey = "FG2VQLEPL9JEG9EM"; // Optional: Replace with Read API Key if private
WiFiClient client; // Create WiFiClient object
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "http://api.thingspeak.com/channels/" + channelID + "/feeds/last.json?api_key=" +
readAPIKey;
if (!error) {
float temperature = doc["field1"].as<float>();
float humidity = doc["field2"].as<float>();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" degree C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
} else {
Serial.println("JSON Parsing Failed!");
}//
} else {
Serial.println("Error retrieving data from ThingSpeak");
}
http.end();
} else {
Serial.println("WiFi Disconnected!");
}
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
// WiFi Credentials
const char* ssid = "OnePlus Nord CE 2 Lite 5G";
const char* password = "Sathish@123";
// DHT11 Sensor
#define DHTPIN D4 // GPIO2 (D4 on NodeMCU)
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// MQTT Client
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWiFi Connected!");
dht.begin();
// Set up MQTT Server
client.setServer(mqtt_server, mqtt_port);
}
void reconnect() {
while (!client.connected()) {
Serial.print("Connecting to MQTT...");
if (client.connect(mqtt_client_id, mqtt_username, mqtt_password)) {
Serial.println("Connected to ThingSpeak MQTT broker");
} else {
Serial.print("Failed. Error Code: ");
Serial.println(client.state());
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop(); // Keep the connection alive
// Read temperature
float temperature = dht.readTemperature();
if (!isnan(temperature)) {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" degree C");
delay(20000); // ThingSpeak allows data every 15 seconds (set to 20s for stability)
}
12. Write a program on Arduino / Raspberry Pi to subscribe to the MQTT broker for temperature
data and print it.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// WiFi Credentials
const char* ssid = "OnePlus Nord CE 2 Lite 5G";
const char* password = "Sathish@123";
// MQTT Client
WiFiClient espClient;
PubSubClient client(espClient);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
attempts++;
void reconnect() {
while (!client.connected()) {
Serial.print("Connecting to MQTT...");
if (client.connect(mqtt_client_id, mqtt_username, mqtt_password)) {
Serial.println("Connected to ThingSpeak MQTT broker");
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop(); // Keep the connection alive
delay(1000);
}
10. Write a program to create a UDP server on Arduino/Raspberry Pi and respond with humidity
data to the UDP client when requested.
Server Program
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <DHT.h>
// WiFi Credentials
const char* ssid = "OnePlus Nord CE 2 Lite 5G";
const char* password = "Sathish@123";
// UDP Settings
WiFiUDP udp;
const unsigned int udpPort = 1234; // Listening port
char packetBuffer[255]; // Buffer for incoming data
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWiFi Connected!");
Serial.print("ESP8266 IP Address: ");
Serial.println(WiFi.localIP());
void loop() {
int packetSize = udp.parsePacket(); // Check if data is received
if (packetSize) {
Serial.println("UDP Request Received!");
UDP_IP = "192.168.35.219"
UDP_PORT = 1234
MESSAGE = "GET_HUMIDITY"
sock.close()
11. Write a program to create a TCP server on Arduino /Raspberry Pi and respond with humidity
data to the TCP client when requested.
Server Program
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <DHT.h>
// WiFi Credentials
const char* ssid = "OnePlus Nord CE 2 Lite 5G";
const char* password = "Sathish@123";
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWiFi Connected!");
Serial.print("ESP8266 IP Address: ");
Serial.println(WiFi.localIP());
void loop() {
WiFiClient client = server.available(); // Check for client connection
if (client) {
Serial.println("Client Connected!");
while (client.connected()) {
if (client.available()) {
String request = client.readStringUntil('\n'); // Read request
Serial.print("Received Request: ");
Serial.println(request);
def send_tcp_request(request):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((ESP8266_IP, ESP8266_PORT))
s.sendall(request.encode())
response = s.recv(1024).decode()
print("Received response:", response)
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
// WiFi Credentials
const char* ssid = "OnePlus Nord CE 2 Lite 5G";
const char* password = "Sathish@123";
// LED Configuration
#define LED_PIN D4 // GPIO2 (D4) on NodeMCU
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWiFi Connected!");
void loop() {
int newMessages = bot.getUpdates(bot.last_message_received + 1);
while (newMessages) {
for (int i = 0; i < newMessages; i++) {
String text = bot.messages[i].text;
String senderID = String(bot.messages[i].chat_id);
if (String(senderID) == chatID) {
if (text == "/on") {
digitalWrite(LED_PIN, HIGH);
bot.sendMessage(chatID, "LED is ON", "");
Serial.println("LED Turned ON");
} else if (text == "/off") {
digitalWrite(LED_PIN, LOW);
bot.sendMessage(chatID, "LED is OFF", "");
Serial.println("LED Turned OFF");
} else {
bot.sendMessage(chatID, "Invalid Command! Use /on or /off", "");
}
} else {
bot.sendMessage(senderID, "Unauthorized User! Access Denied", "");
}
}
newMessages = bot.getUpdates(bot.last_message_received + 1);
}