IoT Lab Manual
IoT Lab Manual
IoT Lab Manual
To install the ESP32 board in your Arduino IDE, follow these next instructions:
2. Enter the following into the “Additional Board Manager URLs” field:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
4. Search for ESP32 and press install button for the “ESP32 by Espressif Systems“:
void loop()
{
digitalWrite(LED_BUILTIN, HIGH);
delay(5000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(5000); // Wait for 1000 millisecond(s)
}
2. Embedded System design using MCU – ESP32- Usage of Digital Input/ Output
Devices - Arduino Platform
A.Digital Output – (Two LEDS )
void loop() {
// put your main code here,to run repeatedly:
digitalWrite(ledpin1, HIGH);
digitalWrite(ledpin2, LOW);
delay(2000);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, HIGH);
delay(2000);
}
B.Push Button – Control LED
int led=13;
int pushbutton=2;
int inputval=0;
void setup()
{
pinMode(led,OUTPUT);
pinMode(pushbutton,INPUT);
}
void loop()
{
inputval=digitalRead(pushbutton);
if(inputval==1){
digitalWrite(led,HIGH);
}
if(inputval==0){
digitalWrite(led,LOW);
}
3. Embedded System design using MCU – ESP32- Usage of ADC and Analogue
Sensors - Arduino Platform
A.Analog Input & Serial Output
void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop()
{
int sensorValue = analogRead(A0);
Serial.print(“The sensor value is: “);
Serial.println(sensorValue);
delay(500);
}
B.POT-Controlling LED
void setup()
{
pinMode(10, OUTPUT);
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop()
{
if (sensorValue>=512) {
digitalWrite(10, HIGH);
} else {
digitalWrite(10, LOW);
}
delay(500);
}
C.POT-FADING LED
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(10, OUTPUT);
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop()
{
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
int brightness = map(sensorValue, 0, 1023, 0, 255);
analogWrite(10, brightness);
delay(50);
A.Serial Communication
void setup()
{
Serial.begin(9600);
void loop()
{
if (Serial.available()) {
char c=Serial.read();
if(c=='A')
{
digitalWrite(10, HIGH);
Serial.println("led on");
}
else if(c=='B')
{
digitalWrite(10, LOW);
Serial.println("led off");
}
}
}
Embedded System design using MCU – ESP32: Arduino Platform -5.Design of IoT
End node using MCU – ESP32 and Arduino Platform - Wireless Part - BLE -
Controlling IoT End node using Mobile Bluetooth
#include "BluetoothSerial.h"
/*
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
*/
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
pinMode(27,OUTPUT);
}
void loop() {
/*if (Serial.available()) {
SerialBT.write(Serial.read());
}*/
if (SerialBT.available()) {
char x=SerialBT.read();
//Serial.write(SerialBT.read());
if(x=='A'){
Serial.write(x);
digitalWrite(27,HIGH);
}
if(x=='B'){
Serial.write(x);
digitalWrite(27,LOW);
}
}
}
6. Design of IoT End node using MCU – ESP32 and Arduino Platform - Wireless
Part - Wi-FiScan Wifi Network - Connect Internet through Wireless Hotspot
#include <WiFi.h>
// Replace with your network credentials
const char* ssid = "Thiru";
const char* password = "thiru123";
void initWiFi();
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
initWiFi();
}
void loop() {
// put your main code here, to run repeatedly:
void initWiFi() {
Serial.println("");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("");
Serial.println(WiFi.localIP());
}
7.Embedded System design using MCU – ESP32: Arduino Platform
DHT11 Sensor Interface
#include "DHT.h"
#define DHTPIN 13
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float temp_value;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
dht.begin();
}
void loop() {
// put your main code here, to run repeatedly:
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
Serial.print("Humidity: ");
Serial.println(h);
Serial.print("Temperature: (°C) ");
Serial.println(t);
Serial.print("Temperature: (F) ");
Serial.println(f);
delay(5000);
}
Implementation of MQTT protocol using ESP32 as MQTT Client (Publish).
#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "DHT.h"
#define DHTPIN 13
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
/* Setup the MQTT client class by passing in the WiFi client and MQTT server and login
details.*/
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME,
AIO_KEY);
void MQTT_connect();
void setup() {
Serial.begin(9600);
delay(10);
Serial.println(F("DHTxx test!"));
dht.begin();
Serial.println(F("Adafruit MQTT demo"));
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
void loop() {
/* Ensure the connection to the MQTT server is alive (this will make the first
connection and automatically reconnect when disconnected). See the MQTT_connect
function definition further below.*/
MQTT_connect();
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
Serial.print("Humidity(%): ");
Serial.print(h);
Serial.print(" Temperature (°C): ");
Serial.print(t);
Serial.print(" Temperature(F): ");
Serial.println(f);
//Mqtt Publish data
if (! dht_temp.publish(t)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
delay(10000);
if (! dht_humidity.publish(h)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
delay(1000);
}
uint8_t retries = 1;
while ((ret = mqtt.connect()) != 0) {
// connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(3000);
retries--;
if (retries == 0) {
// basically die and wait for reset
while (1);
}
}
Serial.println("MQTT Connected!");
}
#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "DHT.h"
#define DHTPIN 13
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void MQTT_connect();
int led1=27;
/* Setup the MQTT client class by passing in the WiFi client and MQTT server and login
details.*/
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME,
AIO_KEY);
void setup() {
Serial.begin(9600);
delay(10);
Serial.println(F("DHTxx test!"));
dht.begin();
Serial.println(F("Adafruit MQTT demo"));
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
// digitalWrite(led1, HIGH);
void loop() {
/* Ensure the connection to the MQTT server is alive (this will make the first
connection and automatically reconnect when disconnected). See the MQTT_connect
function definition further below. */
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &onoffbutton) {
Serial.print(F("Got: "));
Serial.println((char *)onoffbutton.lastread);
if (strcmp((char *)onoffbutton.lastread, "ON") == 0) {
digitalWrite(led1, HIGH);
Serial.println("led is on");
}
if (strcmp((char *)onoffbutton.lastread, "OFF") == 0) {
digitalWrite(led1, LOW);
Serial.println("led is off");
}
}
}
// Now we can publish stuff!
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
// int r=analogRead(A6);
Serial.print("Humidity(%): ");
Serial.print(h);
Serial.print(" Temperature (°C): ");
Serial.print(t);
Serial.print(" Temperature(F): ");
Serial.println(f);
if (! dht_temp.publish(t)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
delay(10000);
if (! dht_humidity.publish(h)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
/*delay(1000);
if (! pot.publish(r)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
} */
uint8_t retries = 1;
while ((ret = mqtt.connect()) != 0) {
// connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 3 seconds...");
mqtt.disconnect();
delay(3000);
retries--;
if (retries == 0) {
// basically die and wait for reset
while (1);
}
}
Serial.println("MQTT Connected!");
}
10. Controlling of Actuator in ESP 32 based IoT End node using IFTTT.
11.Design of WebServer using ESP32 as Hardware and Arduino as Software Platform. –
Sending Data to Webserver
float temperature;
float humidity;
// Replace with your network credentials
const char* ssid = "Thiru";
const char* password = "thiru123";
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup() {
Serial.begin(115200);
dht.begin();
client.println("</style></head>");
client.println("</body></html>");
float temperature;
float humidity;
// Replace with your network credentials
const char* ssid = "Thiru";
const char* password = "thiru123";
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout (example: 2000ms = 2s)
const long timeoutTime = 2000;
float temp=10;
void setup() {
Serial.begin(115200);
dht.begin();
// Initialize the output variables as outputs
pinMode(output26, OUTPUT);
pinMode(output27, OUTPUT);
// Set outputs to LOW
digitalWrite(output26, LOW);
digitalWrite(output27, LOW);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
To build the web server we’ll use the ESPAsyncWebServer library. This library needs the AsyncTCP library to
work properly. Click the links below to download the libraries.
• ESPAsyncWebServer
• AsyncTCP
These libraries aren’t available to install through the Arduino Library Manager, so you need to copy the library
files to the Arduino Installation Libraries folder. Alternatively, in your Arduino IDE, you can go
to Sketch > Include Library > Add .zip Library and select the libraries you’ve just downloaded.
Websocket Server
// Import required libraries
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
void notifyClients() {
ws.textAll(String(ledState));
}
void initWebSocket() {
ws.onEvent(onEvent);
server.addHandler(&ws);
}
void setup(){
// Serial port for debugging purposes
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Connect to Wi-Fi
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
// Start server
server.begin();
}
void loop() {
ws.cleanupClients();
digitalWrite(ledPin, ledState);
}
WebSocket Client
http://192.168.0.103
14.IoT Cloud Platform - Working with ThingSpeak Cloud - Account Creation and
Dashboard Design
15. Integration of IoT End Node ( ESP32 based ) with ThingSpeak Cloud and ESP 32 Based
Arduino Program for Cloud Integration
#include <WiFi.h>
#include "DHT.h"
#include "ThingSpeak.h"
WiFiClient client;
unsigned long myChannelNumber = 1913557;
const char * myWriteAPIKey = "DFNMJEOPYO1HWM51";
float temp=0;
float hum=0;
int pot=0;
#define DHTPIN 13
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void send_thinkspeak(){
Wifi_Connect();
ThingSpeak.setField(1,String(temp));
ThingSpeak.setField(2,String(hum));
ThingSpeak.setField(3,String(pot));
int x= ThingSpeak.writeFields(myChannelNumber,myWriteAPIKey);
void read_sensors(){
hum = dht.readHumidity();
Serial.println("Humidity :");
Serial.println(hum);
// Read temperature as Celsius (the default)
temp = dht.readTemperature();
Serial.println("Temperature in celsius :");
Serial.println(temp);
pot = analogRead(A0);
Serial.println("Pot value :");
Serial.println(pot);
}
void Wifi_Connect() {
Serial.print("Attempting to connect");
// put your main code here, to run repeatedly:
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("\nConnected.");
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //Initialize serial
pinMode(A0,INPUT);
dht.begin();
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); //
analogReadResolution(9);
Wifi_Connect();
void loop() {
read_sensors();
send_thinkspeak();
delay(10000);
}