0% found this document useful (0 votes)
11 views7 pages

Iot Programs

The document provides an overview of programming and interfacing various hardware components with Arduino and Raspberry Pi. It includes example code for basic tasks such as LED blinking, communication with Zigbee, GSM, and Bluetooth modules, as well as sensor interfacing. Additionally, it describes the design of an IoT-based smart energy meter system using ESP32, incorporating energy monitoring and data display functionalities.

Uploaded by

saisandhiya31
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)
11 views7 pages

Iot Programs

The document provides an overview of programming and interfacing various hardware components with Arduino and Raspberry Pi. It includes example code for basic tasks such as LED blinking, communication with Zigbee, GSM, and Bluetooth modules, as well as sensor interfacing. Additionally, it describes the design of an IoT-based smart energy meter system using ESP32, incorporating energy monitoring and data display functionalities.

Uploaded by

saisandhiya31
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/ 7

1 .

Introduction to Arduino platform and programming:

// Basic LED Blink Program

void setup() {
pinMode(13, OUTPUT); // Set the built-in LED pin as OUTPUT
}

void loop() {
digitalWrite(13, HIGH); // Turn the LED ON
delay(1000); // Wait for 1 second
digitalWrite(13, LOW); // Turn the LED OFF
delay(1000); // Wait for 1 second
}

2 .Interfacing Arduino to Zigbee module:

// Simple Arduino Uno + Zigbee Communication Program

void setup() {
Serial.begin(9600); // Start Serial Communication at 9600 baud
}

void loop() {
// Send data
Serial.println("Hello from Arduino!");

// Check if data is available to read


if (Serial.available()) {
char receivedChar = Serial.read(); // Read incoming character
// Do something with the received data
Serial.print("Received: ");
Serial.println(receivedChar);
}

delay(1000); // Wait for 1 second


}
3 .Interfacing Arduino to GSM module:

// Simple Arduino UNO + GSM Module Program

void setup() {
Serial.begin(9600); // Start serial communication with GSM module

delay(1000); // Wait for GSM module to initialize

// Send AT commands to GSM module


Serial.println("AT"); // Check communication
delay(1000);

Serial.println("AT+CMGF=1"); // Set SMS text mode


delay(1000);

Serial.println("AT+CMGS=\"+1234567890\""); // Replace with receiver's phone number


delay(1000);

Serial.print("Hello from Arduino UNO!"); // Message to send


delay(500);

Serial.write(26); // Send Ctrl+Z (End of message)


}

void loop() {
// Nothing to do here
}

4 .Interfacing Arduino to Bluetooth Module:

void setup() {

Serial.begin(9600);

pinMode(3, OUTPUT);

void loop() {

char inChar = (char) Serial.read();

if(inChar == 'A')
{

digitalWrite(3,HIGH);

else if(inChar == 'B')

digitalWrite(3,LOW);

5. Introduction to Raspberry PI platform and python


Programming:

# LED Blinking Program for Raspberry Pi


import RPi.GPIO as GPIO
import time

# Set the pin numbering mode


GPIO.setmode(GPIO.BCM)

# Set the GPIO pin (example: GPIO 17)


led_pin = 17

# Set the LED pin as output


GPIO.setup(led_pin, GPIO.OUT)

# Blink the LED


try:
while True:
GPIO.output(led_pin, GPIO.HIGH) # Turn LED ON
time.sleep(1) # Wait 1 second
GPIO.output(led_pin, GPIO.LOW) # Turn LED OFF
time.sleep(1) # Wait 1 second
except KeyboardInterrupt:
# Clean up GPIO on Ctrl+C
GPIO.cleanup()
6 .Interfacing sensors to Raspberry PI:

# IR Sensor interfacing with LED - Raspberry Pi


import RPi.GPIO as GPIO
import time

# Set the pin numbering mode


GPIO.setmode(GPIO.BCM)

# Define pins
ir_sensor_pin = 18 # IR sensor output pin connected to GPIO18
led_pin = 17 # LED connected to GPIO17

# Setup pins
GPIO.setup(ir_sensor_pin, GPIO.IN) # IR sensor as input
GPIO.setup(led_pin, GPIO.OUT) # LED as output

try:
while True:
if GPIO.input(ir_sensor_pin) == 0: # 0 means "object detected" for many IR sensors
GPIO.output(led_pin, GPIO.HIGH) # Turn LED ON
else:
GPIO.output(led_pin, GPIO.LOW) # Turn LED OFF
time.sleep(0.1) # Small delay to avoid bouncing
except KeyboardInterrupt:
GPIO.cleanup() # Clean up GPIO when Ctrl+C pressed

7 .Communicate between Arduino and Raspberry PI using


any wireless medium:

Arduino Code (send "Hello from Arduino" repeatedly):

// Arduino Bluetooth Sender


void setup() {
Serial.begin(9600); // Start Serial communication
}

void loop() {
Serial.println("Hello from Arduino!");
delay(1000); // Send every 1 second
}

Raspberry Pi Python Code (receive and display the message):

# Raspberry Pi Bluetooth Receiver


import bluetooth

server_mac_address = '00:00:00:00:00:00' # HC-05 MAC address (replace it)


port = 1

sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((server_mac_address, port))

try:
while True:
data = sock.recv(1024) # Receive data
print("Received:", data.decode('utf-8'))
except KeyboardInterrupt:
sock.close()

10 .Design an IOT based system (Smart Energy Meter using ESP32):

#define BLYNK_TEMPLATE_ID "TMPL3VT87N9d8"


#define BLYNK_TEMPLATE_NAME "energy meter"
#define BLYNK_PRINT Serial

#include <EmonLib.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

const float vCalibration = 42.5;


const float currCalibration = 1.80;

const char auth[] = "TPefWeJfHnF_zMMSxgWh4UmTaiTPE-GC";


const char ssid[] = "Àłøñè Ķïñğ";
const char pass[] = "9487348015";

EnergyMonitor emon;
BlynkTimer timer;

float kWh = 0.0, cost = 0.0;


const float ratePerkWh = 6.5;
unsigned long lastMillis = millis();
int displayPage = 0;

void setup() {
Serial.begin(115200);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) delay(500);
Blynk.begin(auth, ssid, pass);
lcd.init();
lcd.backlight();

emon.voltage(35, vCalibration, 1.7);


emon.current(34, currCalibration);

timer.setInterval(2000L, sendData);
timer.setInterval(2000L, changeDisplay);
}

void loop() {
Blynk.run();
timer.run();
}

void sendData() {
emon.calcVI(20, 2000);
float Vrms = emon.Vrms;
float Irms = emon.Irms;
float power = emon.apparentPower;

unsigned long now = millis();


kWh += power * (now - lastMillis) / 3600000000.0;
lastMillis = now;
cost = kWh * ratePerkWh;

Blynk.virtualWrite(V0, Vrms);
Blynk.virtualWrite(V1, Irms);
Blynk.virtualWrite(V2, power);
Blynk.virtualWrite(V3, kWh);
Blynk.virtualWrite(V4, cost);

updateLCD(Vrms, Irms, power);


}

void updateLCD(float V, float I, float P) {


lcd.clear();
if (displayPage == 0) {
lcd.setCursor(0, 0);
lcd.printf("V:%.fV I:%.fA", V, I);
lcd.setCursor(0, 1);
lcd.printf("P:%.fW", P);
} else {
lcd.setCursor(0, 0);
lcd.printf("Energy:%.2fkWh", kWh);
lcd.setCursor(0, 1);
lcd.printf("Cost:%.2fRs", cost);
}
}
void changeDisplay() {
displayPage = (displayPage + 1) % 2;
}

You might also like