0% found this document useful (0 votes)
26 views10 pages

Iot Ex Lab

Uploaded by

Anvitha m
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)
26 views10 pages

Iot Ex Lab

Uploaded by

Anvitha m
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/ 10

week 2

void setup()

pinMode(8, OUTPUT);

void loop()

digitalWrite(8,HIGH);

delay(3000);

digitalWrite(8,LOW);

delay(1000);

}
week 3

int buttonpin = 7;
int ledpin=8;
int last_state = HIGH;
void setup()
{
Serial.begin(115200);
pinMode(buttonpin, INPUT_PULLUP);
pinMode(ledpin,OUTPUT);
}
void loop()
{
int value = digitalRead(buttonpin);
if (last_state != value)
{
last_state=value;
if (value == HIGH)
{
digitalWrite(ledpin,LOW);
Serial.println(" released");
}
else
{
digitalWrite(ledpin,HIGH);
Serial.println(" pressed");
//Serial.println(“LED is OFF, Button released”);
}
}
}
week 4 and week 7

#include "dht.h"

#define dht_apin A0 // Analog Pin sensor is connected to

dht DHT;

void setup(){

Serial.begin(9600);

delay(500);//Delay to let system boot

Serial.println("DHT11 Humidity & temperature Sensor\n\n");

delay(1000);//Wait before accessing Sensor

void loop(){

DHT.read11(dht_apin);

Serial.print("humidity:");

Serial.print(DHT.humidity);

Serial.print("% ");

Serial.print("temp:");

Serial.print(DHT.temperature);

Serial.println("C");

delay(5000);

}
week 5

#include <Servo.h>
int buttonpin = 7;
int last_state = HIGH;
int relaypin=8;
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
Serial.begin(115200);
pinMode(buttonpin, INPUT_PULLUP);
pinMode(relaypin,OUTPUT);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
int value = digitalRead(buttonpin);
if (last_state != value)
{
last_state=value;
if (value == HIGH)
{
digitalWrite(relaypin,LOW);
Serial.println(" released");
for (pos = 0; pos <= 180; pos += 1)
{ // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1)
{ // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
else
{
digitalWrite(relaypin,HIGH);
Serial.println(" pressed");
}
}
}
week 6

#include "dht.h"
#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define dht_apin A0
dht DHT;
void setup(){
Serial.begin(9600);
delay(500);
Serial.println("DHT11 Humidity & temperature Sensor\n\n");
delay(1000);
lcd.init();
lcd.backlight();
}
void loop(){
DHT.read11(dht_apin);
lcd.setCursor(1,0);
lcd.print("humidity:");
lcd.print(DHT.humidity);
lcd.print("% ");
lcd.setCursor(1,1);
lcd.print("temp:");
lcd.print(DHT.temperature);
lcd.println("C");
delay(5000);
}
week 8

int led=7;
int data;
void setup() {
Serial.begin(9600);
pinMode(7, OUTPUT);
}

void loop() {
while(Serial.available()>0){
data=Serial.read();
Serial.println(data);
if(data==65)
digitalWrite(led,HIGH);

else
digitalWrite(led,LOW);
}
}
week 9

#include <WiFi.h>
#include <DHT.h>

#define DHTPIN 5 // DHT data pin connected to ESP32 pin 5


#define DHTTYPE DHT11 // DHT11 (DHT Sensor Type)
#define SSID "6005" // WiFi Name
#define PASS "12345678" // Password
#define WRITE_API_KEY "JJ98J2RWY7I3S9HX" // ThingSpeak Write API key
#define READ_API_KEY "AT8KEV6YJXCZ35SP" // ThingSpeak Read API key (for private channels)
#define CHANNEL_ID "2713998" // ThingSpeak Channel ID
#define THINGSPEAK_URL "api.thingspeak.com"

DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor

void setup() {
Serial.begin(115200); // Start Serial Monitor
dht.begin(); // Initialize DHT sensor
WiFi.begin(SSID, PASS); // Connect to WiFi

Serial.print("Connecting to WiFi");
unsigned long startAttemptTime = millis();

// Check connection with a timeout


while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 10000) {
delay(500);
Serial.print(".");
}

if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nConnected to WiFi");
} else {
Serial.println("\nFailed to connect to WiFi");
}
}

void sendDataToThingSpeak(float temp, float hum) {


// Prepare HTTP request URL for sending data
String url = "/update?api_key=" + String(WRITE_API_KEY) + "&field1=" + String(temp) +
"&field2=" + String(hum);
Serial.print("Sending data: ");
Serial.println(url);

// Send data to ThingSpeak


WiFiClient client;
if (client.connect(THINGSPEAK_URL, 80)) {
client.print("GET " + url + " HTTP/1.1\r\n");
client.print("Host: " THINGSPEAK_URL "\r\n");
client.print("User-Agent: ESP32Client\r\n");
client.print("Connection: close\r\n\r\n");

// Wait for a response from the server


while (client.available() == 0) {
delay(100);
}

// Read and display the response


while (client.available()) {
String line = client.readStringUntil('\n');
Serial.println(line);
}
} else {
Serial.println("Connection failed");
}

client.stop(); // Close the connection


}

void loop() {
// Read temperature and humidity
float temp = dht.readTemperature();
float hum = dht.readHumidity();

// Check if reading was successful


if (isnan(temp) || isnan(hum)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

// Send data to ThingSpeak


sendDataToThingSpeak(temp, hum);

// Wait for the server to process before the next request


delay(20000); // Wait 20 seconds between updates to avoid rate limits
}
week 10

#include <WiFi.h>
#include <DHT.h>

#define DHTPIN 5 // DHT data pin connected to ESP32 pin 5


#define DHTTYPE DHT11 // DHT11 (DHT Sensor Type)
#define SSID "6005" // WiFi Name
#define PASS "12345678" // Password
#define WRITE_API_KEY "JJ98J2RWY7I3S9HX" // ThingSpeak Write API key
#define READ_API_KEY "AT8KEV6YJXCZ35SP" // ThingSpeak Read API key (for private channels)
#define CHANNEL_ID "2713998" // ThingSpeak Channel ID
#define THINGSPEAK_URL "api.thingspeak.com"

DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor

void setup() {
Serial.begin(115200); // Start Serial Monitor
dht.begin(); // Initialize DHT sensor
WiFi.begin(SSID, PASS); // Connect to WiFi

Serial.print("Connecting to WiFi");
unsigned long startAttemptTime = millis();

// Check connection with a timeout


while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 10000) {
delay(500);
Serial.print(".");
}

if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nConnected to WiFi");
} else {
Serial.println("\nFailed to connect to WiFi");
}
}

void readDataFromThingSpeak() {
// Prepare HTTP request URL for reading data
String url = "/channels/" + String(CHANNEL_ID) + "/fields/1.json?api_key=" +
String(READ_API_KEY) + "&results=1";
Serial.print("Requesting data from ThingSpeak: ");
Serial.println(url);

// Read data from ThingSpeak


WiFiClient client;
if (client.connect(THINGSPEAK_URL, 80)) {
client.print("GET " + url + " HTTP/1.1\r\n");
client.print("Host: " THINGSPEAK_URL "\r\n");
client.print("User-Agent: ESP32Client\r\n");
client.print("Connection: close\r\n\r\n");

// Wait for a response from the server


while (client.available() == 0) {
delay(100);
}

// Parse and display the response


String response = "";
while (client.available()) {
response += client.readStringUntil('\n');
}
Serial.println("Response from ThingSpeak:");
Serial.println(response);

// Optional: Parse JSON response if needed


// You can use the ArduinoJson library for more complex JSON parsing
} else {
Serial.println("Failed to connect to ThingSpeak");
}

client.stop(); // Close the connection


}
void loop() {
// Read temperature and humidity
float temp = dht.readTemperature();
float hum = dht.readHumidity();

// Check if reading was successful


if (isnan(temp) || isnan(hum)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Read data from ThingSpeak
readDataFromThingSpeak();

// Wait before the next cycle


delay(40000); // Total delay of 60 seconds
}

You might also like