0% found this document useful (0 votes)
6 views12 pages

Iot Lab File

The document contains various code snippets demonstrating the use of different data types and control structures in programming, including integers, floats, strings, lists, and loops. It also includes examples of Arduino sketches for controlling LEDs and reading temperature sensors, as well as connecting to WiFi and MQTT for IoT applications. Additionally, there are examples of using Raspberry Pi GPIO for output control.

Uploaded by

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

Iot Lab File

The document contains various code snippets demonstrating the use of different data types and control structures in programming, including integers, floats, strings, lists, and loops. It also includes examples of Arduino sketches for controlling LEDs and reading temperature sensors, as well as connecting to WiFi and MQTT for IoT applications. Additionally, there are examples of using Raspberry Pi GPIO for output control.

Uploaded by

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

integer_number = 10

float_number = 10.5
complex_number = 2 + 3j
string_text = "Hello, World!"
list_items = [1, 2, 3, "apple", "banana"]
tuple_items = (1, 2, 3, "apple", "banana")
range_numbers = range(6)
dictionary_items = {"name": "John", "age": 30}
set_items = {1, 2, 3}
boolean_value = True
print("Integer:", integer_number, type(integer_number))
print("Float:", float_number, type(float_number))
print("Complex:", complex_number, type(complex_number))
print("String:", string_text, type(string_text))
print("List:", list_items, type(list_items))
print("Tuple:", tuple_items, type(tuple_items))
print("Range:", range_numbers, type(range_numbers))
print("Dictionary:", dictionary_items, type(dictionary_items))
print("Set:", set_items, type(set_items))
print("Boolean:", boolean_value, type(boolean_value))
num1 = 10
num2 = 5
sum_result = num1 + num2
print("Sum:", sum_result)
difference_result = num1 - num2
print("Difference:", difference_result)
product_result = num1 * num2
print("Product:", product_result)
division_result = num1 / num2
print("Division:", division_result)
modulus_result = num1 % num2
print("Modulus:", modulus_result)
exponentiation_result = num1 ** num2
print("Exponentiation:", exponentiation_result)
print("For loop example:")
for i in range(5):
print(i)
list_example = ["apple", "banana", "cherry"]
for item in list_example:
print(item)
string_example = "Python"
for char in string_example:
print(char)
print("\nWhile loop example:")
count = 0
while count < 5:
print(count)
count += 1
print("\nLoop control statements example:")
for i in range(10):
if i == 3:
break
if i % 2 == 0:
Ex-5
int LEDpin = 13;
int delayT = 1000;
void setup() {
pinMode(LEDpin, OUTPUT);
}
void loop() {
digitalWrite(LEDpin, HIGH);
delay(delayT);
digitalWrite(LEDpin, LOW);
delay(delayT);
}
Ex-6
int redPin= 5;
int greenPin = 6;
int bluePin = 7;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
setColor(255, 0, 0); // Red Color
delay(1000);
setColor(0, 255, 0); // Green Color
delay(1000);
setColor(0, 0, 255); // Blue Color
delay(1000);
setColor(255, 255, 255); // White Color
delay(1000);
setColor(170, 0, 255); // Purple Color
delay(1000);
setColor(127, 127, 127); // Light Blue
delay(1000);
}
void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}
Ex-7
float temp;
int tempPin = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
temp = analogRead(tempPin);
temp = temp * 0.48828125;
Serial.print("TEMPERATURE = ");
Serial.print(temp); // display temperature value
Serial.print("*C");
Serial.println();
delay(1000); // update sensor reading each one second
}
Ex-8
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 5
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
SPI.begin(); // init SPI bus
rfid.PCD_Init(); // init MFRC522
Serial.println("Tap RFID/NFC Tag on reader");
}
void loop() {
if (rfid.PICC_IsNewCardPresent()) { // new tag is available
if (rfid.PICC_ReadCardSerial()) { // NUID has been readed
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.print("UID:");
for (int i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX);
}
Serial.println();
rfid.PICC_HaltA(); // halt PICC
rfid.PCD_StopCrypto1(); // stop encryption on PCD
}))
Ex-9
#include <WiFiNINA.h>
#include <ArduinoMqttClient.h>
char ssid[] = "SSID"; // Replace with WiFi SSID
char pass[] = "PASSWORD"; // Replace with WiFi Password
WiFiClient wifiHdlr;
MqttClient mqttClient(wifiHdlr);
const char brokerAddr[] = "192.168.178.20"; // Use the real broker ip address
const char topic[] = "/home/room/temperature";
const long intTimer = 5000; // Timer interval for publishing the message
int currentTemp = 25; // Simulated temperature value in °C
unsigned long prevTimer = 0; // Variable used to keep track of the previous timer
value
unsigned long actualTimer = 0; // Variable used to keep track of the current timer value
void setup()
{
Serial.begin(9600);
Serial.print("[WiFi]: Connecting to WPA SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
Serial.println("[WiFi]: Retry");
delay(3000);
} Serial.println("[WiFi]: Connection established");
Serial.println();
Serial.print("[MQTT]: Connecting to MQTT broker: ");
Serial.println(brokerAddr);
if (!mqttClient.connect(brokerAddr, 1883)) {
Serial.print("[MQTT]: MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
while (1);
}
Serial.println("[MQTT]: Connection established");
Serial.println();
}
void loop()
{
mqttClient.poll();
actualTimer = millis();
if (actualTimer - prevTimer >= intTimer) {
prevTimer = actualTimer;
Serial.print("[MQTT]: Sending message to topic: ");
Serial.println(topic);
Serial.println(currentTemp);
mqttClient.beginMessage(topic);
mqttClient.print(currentTemp);
mqttClient.endMessage();

Serial.println();
}
}
EX-11
import RPi.GPIO as IO
import time
IO.setmode (IO.BOARD)
IO.setup(40,IO.OUT)
IO.output(40,1)
time.sleep(1)
IO.cleanup()
time.sleep(1)

#loop is executed second time


IO.setmode (IO.BOARD)
IO.setup(40,IO.OUT)
IO.output(40,1)
time.sleep(1)
IO.cleanup()
time.sleep(1)
#loop is executed third time

IO.setmode (IO.BOARD)
IO.setup(40,IO.OUT)
IO.output(40,1)
time.sleep(1)
IO.cleanup()
time.sleep(1)

You might also like