0% found this document useful (0 votes)
2 views9 pages

Iot Lab Programs

The document contains a series of programming experiments involving the ESP32 and Raspberry Pi, demonstrating various functionalities such as connecting to WiFi, Bluetooth, interfacing with sensors (GPS, temperature, ultrasonic), and data logging. Each experiment includes code snippets in C++ for the ESP32 and Python for the Raspberry Pi, showcasing how to read sensor data and communicate over networks. The document serves as a practical guide for implementing IoT projects using these devices.

Uploaded by

abut64337
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Iot Lab Programs

The document contains a series of programming experiments involving the ESP32 and Raspberry Pi, demonstrating various functionalities such as connecting to WiFi, Bluetooth, interfacing with sensors (GPS, temperature, ultrasonic), and data logging. Each experiment includes code snippets in C++ for the ESP32 and Python for the Raspberry Pi, showcasing how to read sensor data and communicate over networks. The document serves as a practical guide for implementing IoT projects using these devices.

Uploaded by

abut64337
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Exp 1 : Connecting Esp with WiFi

Program:

#include <WiFi.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println(WiFi.localIP());
}

void loop() {
}
Exp 2 : Connecting Esp with Bluetooth

Program:

#include "BluetoothSerial.h"
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_BT");
Serial.println("Bluetooth Started! Waiting for messages...");
}
void loop() {
if (SerialBT.available()) {
String receivedData = "";
while (SerialBT.available()) {
char c = SerialBT.read();
receivedData += c;
}
Serial.print("Received: ");
Serial.println(receivedData);
}
delay(100);
}
Exp 3: Interfacing Gps with ESP

Program:
#include <TinyGPS++.h>
#include <HardwareSerial.h>
TinyGPSPlus gps;
HardwareSerial SerialGPS(1);
void setup() {
Serial.begin(115200);
SerialGPS.begin(9600, SERIAL_8N1, 16, 17);
}
void loop() {
while (SerialGPS.available() > 0) {
gps.encode(SerialGPS.read());
if (gps.location.isUpdated()) {
Serial.print("Latitude: ");
Serial.println(gps.location.lat(), 6);
Serial.print("Longitude: ");
Serial.println(gps.location.lng(), 6);
}
}
}
Exp 4 : Interfacing Esp with Temperature sensor

Program:

#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("% Temperature: ");
Serial.print(t);
Serial.println("°C");
delay(2000);
}
Exp 5 : Interfacing Esp with Ultrasonic sensor

Program:
#define TRIG_PIN 5
#define ECHO_PIN 18
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration * 0.0343 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Exp 6 : Interfacing Raspberry Pi with Sensor (DHT11)

Program:
import Adafruit_DHT
import time
sensor = Adafruit_DHT.DHT11
pin = 4
while True:
humidity, temperature = Adafruit_DHT.read_retry(sensor,
pin)
if humidity is not None and temperature is not None:
print(f"Temp={temperature:0.1f}C
Humidity={humidity:0.1f}%")
time.sleep(2)
Exp 7 : Interfacing Raspberry Pi with Arduino
ESP32 (Wi-Fi Server) Code – Arduino IDE
Program:
#include <WiFi.h>
const char* ssid = "Your_SSID"; // Replace with your
Wi-Fi SSID
const char* password = "Your_PASSWORD"; // Replace with
your Wi-Fi Password
WiFiServer server(80); // Server on port 80
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected.");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("Client connected.");
client.println("Hello from ESP32"); // Send message to
client
delay(100);
client.stop();
Serial.println("Client disconnected.");
}
}
Raspberry Pi (Client) Code – Python 3
import socket
esp32_ip = "192.168.1.100" # Replace with ESP32 IP shown
in Serial Monitor
port = 80
try:
with socket.socket(socket.AF_INET,
socket.SOCK_STREAM) as s:
s.connect((esp32_ip, port))
data = s.recv(1024)
print("Received from ESP32:", data.decode())
except Exception as e:
print("Connection failed:", e)
Exp 8 : Logging data using raspberry pi and uploading to
cloud (Thingspeak)
Program:
import time
import requests
import random
WRITE_API_KEY = ' '
url =
f"https://api.thingspeak.com/update?api_key={WRITE_API_
KEY}"

while True:
temperature = random.uniform(20.0, 30.0)
humidity = random.uniform(40.0, 60.0)

payload = {'field1': temperature, 'field2': humidity}


response = requests.get(url, params=payload)

print(f"Uploaded: Temp={temperature:.2f}C
Humidity={humidity:.2f}% Status={response.status_code}")
time.sleep(15)

You might also like