IoT programs for read
IoT programs for read
AURDINO 1.8.19
we use a board based on ESP32 i.e., ESP32 Dev module
STEPS BEFORE THE EXECUTION OF A PROGRAM-
open Aurdino 1.8.19
goto file--->preferences--->additional Boards Manager URL--->TYPE
https://dl.espressif.com/dl/package_esp32_index.json
CLICK OK
goto tools--->board--->boards Manager--->(wait sometime for downloads)--->in search bar type
"esp32"
esp32 by espressif ---->click and install(takes time)
After installation
goto tools--->boards--->select esp32 aurdino---->select esp32 dev module
void setup() {
pinMode(2,OUTPUT);
void loop() {
digitalWrite(2,HIGH); //on
delay(1000);// 1000ms = 1sec
digitalWrite(2,LOW); //off
delay(1000);// 1000ms = 1sec
}
in case if the "ports" file is not appearing go to the BIT-IOT-WS folder and install
CP21_WINDOWS_DRIVER
Repeat the above steps
Save the file-----------to check for errors
upload the file
OUTPUT:
Title: Program for Interfacing an External LED
void setup() {
pinMode(18,OUTPUT);
}
void loop() {
digitalWrite(18,HIGH); //on
delay(200);// 1000ms = 1sec
digitalWrite(18,LOW); //off
delay(200);// 1000ms = 1sec
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //UART---- BAUD RATE is 96000 (how many bits u can send)
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(342);
Serial.println(34.432);
Serial.println('K');
Serial.println("bangalore");
delay(2000);
}
To check ouput click on serial Monitor and change baud rate to 96000
}
void loop() {
// put your main code here, to run repeatedly:
int val;
val = digitalRead(35); // push button status pin=35
Serial.println(val);
}
void loop() {
// put your main code here, to run repeatedly:
int val;
val= digitalRead(35);
if(val==1)
digitalWrite(18,HIGH); // LED on
else
digitalWrite(18,LOW);
}
SMART LED:
sensor----- LDR(light dependent register)
For analog read to get the status of light intensity value in serial monitor
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(36,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int val;
val = analogRead(36);
Serial.println(val);
delay(500);
}
//output - in serial monitor we will get intensity of light depending on amount of light reflected on ldr
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(18,OUTPUT); // LED
pinMode(36,INPUT); //LDR pin 35
}
void loop() {
// put your main code here, to run repeatedly:
int val;
val= analogRead(36);
Serial.println(val);
if(val< 500) // less than 500 light is blocked
digitalWrite(18,HIGH); // LED on
else
digitalWrite(18,LOW); // LED off
}
TO ADD LIBRARY:
goto sketch ---> include library ---> zip library ---> browse for bit iot ---> add required library.( i.e
,adafruit library)
repeat the above step sketch ---> include library ---> zip library ---> browse for bit iot ---> add required
library(dht sensor library)
CONNECTION:
ii) To interface OLED with Arduino/Raspberry Pi and write a program to print temperature and
humidity readings on it.
#include <Arduino.h>
#include <U8x8lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
U8X8_SH1106_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 32
#define DHTTYPE DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);
void setup(void)
{
u8x8.begin();
u8x8.setPowerSave(0);
u8x8.setFont(u8x8_font_chroma48medium8_r);
dht.begin();
}
void loop(void)
{
u8x8.setCursor (0,1);
sensors_event_t event;
// get temperature and print as float
dht.temperature().getEvent(&event);
u8x8.print("Temp:");
u8x8.print(event.temperature);
u8x8.print(" Cels");
// get humidity and print as integer
u8x8.setCursor(0,3);
dht.humidity().getEvent(&event);
u8x8.print("Hum:");
u8x8.print(event.relative_humidity);
u8x8.print("%");
delay(2000);
}
3. To interface motor using relay with Arduino/Raspberry Pi and write a program to ‘turn ON’ motor
when push button is pressed.
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(35))
{
digitalWrite(13,HIGH);
}
else
{
digitalWrite(13,LOW);
}
}
NO = normally open
NC = normally close
COM=common
4. To interface Bluetooth with Arduino/Raspberry Pi and write a program to send sensor data to
smartphone using Bluetooth.
While execution
replace SerialBT.begin("ESP32test"); with SerialBT.begin("any name");
5. To interface Bluetooth with Arduino/Raspberry Pi and write a program to turn LED ON/OFF when
'1'/'0' is received from smartphone using Bluetooth.
// Load libraries
#include "BluetoothSerial.h"
// Check if Bluetooth configs are enabled
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
// Bluetooth Serial object
BluetoothSerial SerialBT;
// GPIO where LED is connected to
const int ledPin = 18;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
// Bluetooth device name
SerialBT.begin("ESP32test");
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
char val;
if (SerialBT.available()){
val = SerialBT.read();
if (val =='1')
digitalWrite(ledPin,HIGH);
if (val =='0')
digitalWrite(ledPin,LOW);
}
}
Note:
While execution:
replace SerialBT.begin("ESP32test"); with SerialBT.begin("any name");
}
Serial.print("connected ,IP address:");
Serial.println(WiFi.localIP());//display IP assigned by wifi router
digitalWrite(2,HIGH);
delay(2000);
}
Output:
connect the kit
led is on when hotspot is connected --------->check it in serial monitor
led turns off when hotspot is not connected ---------->in serial monitor the cursor blinks
6. Write a program on Arduino/Raspberry Pi to upload temperature and humidity data to thingspeak
cloud.
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 32
#define DHTTYPE DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);
#include <WiFi.h>
#include "ThingSpeak.h"
const char* ssid = "Namratha";// your network SSID (name)
const char* password = "kruthika";// your network password
WiFiClient client;
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;
float temperatureC;
float humidityP;
void setup() {
Serial.begin(115200); //Initialize serial
dht.begin();
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
lastTime = millis();
}
}
#include <WiFi.h>
#include "ThingSpeak.h"
const char* ssid = "Namratha";// your network SSID (name)
const char* password = "kruthika";// your network password
WiFiClient client;
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;
float temperatureC;
float humidityP;
float temperatureR;
float humidityR;
void setup() {
Serial.begin(115200); //Initialize serial
dht.begin();
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
temperatureR= ThingSpeak.readFloatField(channelID,firstReadFieldNumber,readAPIKey);
Serial.println(" Temperature read from ThingSpeak "+String(temperatureR));
humidityR= ThingSpeak.readFloatField(channelID,secondReadFieldNumber,readAPIKey);
Serial.println(" Humidity read from ThingSpeak "+String(humidityR));
lastTime = millis();
}
}
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
void setupMQTT() {
mqttClient.setServer(mqttServer, mqttPort);
mqttClient.setCallback(callback);
}
void reconnect() {
Serial.println("Connecting to MQTT Broker...");
while (!mqttClient.connected()) {
Serial.println("Reconnecting to MQTT Broker..");
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
if (mqttClient.connect(clientId.c_str())) {
Serial.println("Connected.");
// subscribe to topic
mqttClient.subscribe("esp32/message");
}
}
}
void setup() {
Serial.begin(9600);
Serial.println("DHT11 test!");
dht.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected to Wi-Fi");
pinMode(2, OUTPUT);
setupMQTT();
}
void loop() {
if (!mqttClient.connected())
reconnect();
mqttClient.loop();
long now = millis();
long previous_time = 0;
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
char tempString[8];
dtostrf(t, 1, 2, tempString);
Serial.print("Temperature: ");
Serial.println(tempString);
mqttClient.publish("esp32/temperature", tempString);
char humString[8];
dtostrf(h, 1, 2, humString);
Serial.print("Humidity: ");
Serial.println(humString);
mqttClient.publish("esp32/humidity", humString);
delay(4000);
}
}
#include <WiFi.h>
#include <WiFiUdp.h>
#include <DHT.h>
WiFiUDP udp;
float humidity = 0.0;
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
void loop() {
// Wait for incoming UDP packets
int packetSize = udp.parsePacket();
if (packetSize) {
// Read the incoming packet
char packetData[packetSize];
udp.read(packetData, packetSize);
String request = String(packetData);
if (request == "get_humidity") {
// Read humidity from the DHT sensor
humidity = dht.readHumidity();
11. Write a program to create TCP server on Arduino/Raspberry Pi and respond with humidity data to
TCP client when requested.
#include <WiFi.h>
#include <WiFiClient.h>
#include <DHT.h>
void setup() {
Serial.begin(115200);
delay(10);
// Connect to Wi-Fi
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
dht.begin();
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("New client connected");
if (request.indexOf("/humidity") != -1) {
// Read humidity data from DHT sensor
float humidity = dht.readHumidity();
if (isnan(humidity)) {
Serial.println("Failed to read humidity from DHT sensor");
client.println("Failed to read humidity from DHT sensor");
} else {
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
client.print("Humidity: ");
client.print(humidity);
client.println("%");
}
}
LCD PROGRAM
//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
lcd.setCursor(0,1);
lcd.print("HELLO WORLD ");
}
void loop()
{
}
FOR OUTPUT:
CONNECT THE LCD TO LCD INTERFACE OF KIT (ADD REQUIRED LIBRARIES i e wire.zip
and liquid crystal )
and upload the program