The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by ROSLINDA BINTI ROSLAN KPM-Guru, 2023-05-10 23:23:02

Embedded System using ESP32

Embedded System using ESP32

Keywords: ESP32,EMBEDDED SYSTEM,INTERNET OF THING,IOT

1 Embedded System Using ESP32 Instructor : Mr Mahdzir Bin Jamiaan Fasilitator : Madam Munirah Mujid Training Kit and Material Hardware : ● Iot Learning Kit/Developement Board ● Sensor : ware : ● Arduino IDE ● MQTTbox Windows App ● Nodejs ● Node-Red ● Bluestack - for non android users Mobile App : Android Platform ● IoT MQTT Panel - https://play.google.com/store/apps/details?id=snr.lab.iotmqttpanel.prod&hl=en&gl=US ● MQTT dashboard - https://play.google.com/store/apps/details?id=com.lapetov.mqtt&hl=en&gl=US ● MQTT Dash - https://play.google.com/store/apps/details?id=net.routix.mqttdash&hl=en&gl=US ● Iphone IoS Platform ● IoT onoff - https://www.iot-onoff.com/ https://www.bluestacks.com/ms/index.html


2 TENTATIVE PROGRAM ONLINE SESSION - IoT Software and Data Monitoring Concept Time Topic Notes 8.30 - 9.15 Session 1 - Introduction to IoT 9.15 - 10.15 Session 2 - Introduction IoT Protokol and Data communication Using MQTT Download Arduino IDE 10.30 - 12.30 Session 3 - Online Hands On Application using MQTT Client Server Tools 2.30 - 4.00 Session 4 - Data display and control from Android Mobile Dashboard Application F2F SESSION - Interfacing Hardware devices and Data Monitoring Time Topic Notes Session 1 - Introduction IoT Hardware and Devices Session 2 - Introduction to ESP32 microcontroller ➔ IoT Evakits ESP-32 Learning Board ➔ Installing Arduino IDE for ESP32 Board Download Arduino IDE Session 3 - Exloring ESP32 GPIO Pins ➔ Digital Input and Output ◆ Output to LED, Buzzer ◆ Input fron SWITCH , Touch Sensor ◆ ESP32 PWM Session 4 - Exloring ESP32 GPIO Pins ➔ Analog Input ◆ Read from Analog Potentiometer as a a sensor ◆ Read from LDR sensor Serial Monitor Session 5 - Introduction to MQTT ➔ IOT Protocols, IOT Platforms ➔ Why MQTT ➔ MQTT - Pub/Sub, Topic and Broker ➔ Apply Mqtt in Mobile/PC Application ● Download MQTT IoT panel - Android ● Download MQTTbox - pc Session 6 - Review previous session Session 7 - Publish data and Monitoring ➔ Read 1st Sensor - analog VR and publish a topic to mosquito server ➔ Read 2nd Sensor - analog LDR and publish a topic to mosquito server Read data convert to string before publish to server


3 Session 8 - Subscribe data for Control Actuators ➔ Send a data from MQTT App to ON and OFF a LED. ➔ Send a data from MQTT App to ON and OFF another LEDs and Buzzer. Simple Application Project using MQTT and Android App. Closing Session.


4 Table of Contents Participant List 1 T r ainin g Kit a n d M a t e rial 2 H a r d w a r e : 2 S o ft w a r e : 2 M o bile A n d r oid A p p 2 T E N TAT I V E R O G R A M 2 Ta b l e o f C o n t e n t s 4 S e s s i o n 1 : 5 I n t r o d u c t i o n t o R o b o t i c 5 S e s s i o n 2 : 6 I n t r o d u c t i o n t o E S P 3 2 m i c r o c o n t r o l l e r 6


5 Session 1: Session 1 - Introduction IoT Hardware and Devices Sistem IoT Pembangunan Sistem IoT ini terbahagi kepada 3 bahagian utama iaitu ● End Node ● Gateway Server ● User Client Seksyen 1 : Pengenalan Kepada Endnode - Embedded System Hardware - Mikropengawal


6 Jadual Ringkasan Pin (30 PIN ESP 32 DoiT) Pin I O Nota PIN I O Nota EN 23 ok ok MOSI (VSPI) 36 ok x ADC1_CH0, RTC_GPIO0 Input Only 22 ok ok SCL 39 ok x ADC1_CH3, RTC_GPIO3 Input Only 1 Tx ok debug output at boot 34 ok x ADC1_CH4, RTC_GPIO4 Input Only 3 ok Rx HIGH at boot 35 ok x ADC1_CH7, RTC_GPIO5 Input Only 21 ok ok SDA 32 ok ok ADC1_CH4, RTC_GPIO9 19 ok ok MISO (VSPI) 33 ok ok ADC1_CH5, RTC_GPIO8 18 ok ok CLK(VSPI) 25 ok ok ADC2_CH8, RTC_GPIO6 5 ok ok PWM CS (VSPI) 26 ok ok ADC2_CH9, RTC_GPIO7 17 ok ok Tx 27 ok ok ADC2_CH7, RTC_GPIO17 16 ok ok Rx


7 14 ok ok ADC2_CH0,PWM, RTC_GPIO16 CLK(HSPI) 4 ok ok ADC2_CH0, RTC_GPIO10 12 ok ok ADC2_CH5, boot fail if pulled high, RTC_GPIO15 MISO (HSPI) 2 ok ok ADC2_CH2, connected to on-board LED, RTC_GPIO12 13 ok ADC2_CH4, RTC_GPIO14 MOSI (HSPI) 15 ok ok PWM, RTC_GPIO13 CS (HSPI) GND x x power GND x x power VIN x x power 3v3 x x power Jadual Ringkasan Pin (baki pin selain dari jadual di atas) Pin I O Nota PIN I O Nota 0 pull up ok ADC2_CH1, RTC_GPIO11 outputs PWM signal at boot 20 6 x x connected to the integrated SPI flash 9 x x connected to the integrated SPI flash 7 x x connected to the integrated SPI flash 10 x x connected to the integrated SPI flash 8 x x connected to the integrated SPI flash 11 x x connected to the integrated SPI flash 28 29 30 31 37 ADC1_CH1 38 ADC1_CH2 24 Note: ● ADC2 pins cannot be used when Wi-Fi is used. ● All pins that can act as outputs can be used as PWM pins (GPIOs 34 to 39 can’t generate PWM). ● All GPIOs can be configured as interrupts. https://randomnerdtutorials.com/esp32-pinout-reference-gpios/


8 Installing the ESP32 Board in Arduino IDE in windows OS https://www.arduino.cc/en/Main/OldSoftwareReleases#previous Installing ESP 32 Board manager https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructio ns/ https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index. json


9


10 After installation


11


12 Check port yang digunakan untuk sambung dengan ESP32 Testing board


13 Arduino Cheat Sheet http://www.cheat-sheets.org/saved-copy/Arduino-cheat-sheet-v02c.pdf Task 1: LED void setup() { pinMode(23,OUTPUT); // configure GPIO 5 as OUTPUT // Run once in coding } void loop() { digitalWrite(23,HIGH); // 3.3V output to GPIO 5 delay(1000); // delay 1s digitalWrite(23,LOW); delay(1000); }


14 Task 2: Add another LED to run running light Task 3: Analog Input int varRes=33; void setup() { Serial.begin(9600); } void loop() { int bacaanVR = analogRead(varRes); Serial.println(bacaanVR); float voltan = bacaanVR * 0.0008; Serial.print(voltan); Serial.println( " volts"); delay(500); } I2C LCD https://playground.arduino.cc/Main/I2cScanner/ Find I2C Scanner


15


16 Install library of I2C < tools> < Manage libraries> Run Simple coding to test Your LCD Hands On Intall library for DS18B20


17 Combine Serial monitor and i2c LCD Task 5:


18 Task 6 ESP 32 and MQTT


19 ESP INPUT and Output Digital output with onboard LED Digital output with onboard Buzzer Digital input with onboard Switch --- https://www.ipson.nl/how-to-connect/ If you are using a normally open (N.O.) push-button switch, when the switch is pressed, it will send a low level (0V) to the terminal (thus the term “active low”). Working with LCD Analog input with onboard potentiometer Analog input with onboard LDR Read temperature using DS18B20


20


21


22 DAY 2 - IoT Project - Tajuk : Automatic Monitoring and control IoT System for chemical storage room. Jadual Sambungan Input/Output dan Perantaramuka Micro Pengawal ESS32 Input/Outp ut ESP32 Pin # PinMode Nama Pin Nama Pembolehubah Nota 1 LED1 GPIO 25 OUTPUT relay01 - Mengawal lampu 2 LED2 GPIO 26 OUTPUT relay02 - Menawal Kipas 3 DHT11 GPIO 34 INPUT pinDHT dataDHT Membaca suhu 6 buzzer GPIO 02 OUTPUT buzz - Alarm 3 DS18B20 GPIO 4 onewire senSuhu dataSuhu Membaca suhu air kolam 9 LCD GPIO22 GPIO21 Module I2C I2c LCD - Paparan skrin Sda = GPIO22 Scl = GPIO21


23 Task 1: Test LCD (i2c) Setup wi-FI Setup wi-fi


2 4 I n c l u d e W i - f i f u n c t i o n v oid s e t u p _wifi( ) { d ela y ( 1 0 ); Serial.prin tln ( ); Serial.print("Co n n e c tin g t o " ); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CO N N E C T E D ) { d ela y ( 5 0 0 ); Serial.print( "." ); }Serial.println(""); Serial.println("WiFi c o n n e c t e d " ); Serial.println("IP address: "); Serial.println(WiFi.localIP()); delay(3000); }


25 Read LDR Insert/setting PubSub library


26 #include <PubSubClient.h> //-------MQTT------------------------------------- const char* mqtt_server = "broker.hivemq.com"; WiFiClient espClient; //mqtt PubSubClient client(espClient); //MQTT client.setServer(mqtt_server, 1883); //mqtt client.setCallback(callback); //mqtt Call back and reconnect void callback(char* topic, byte* message, unsigned int length) { Serial.print("Message arrived on topic: "); Serial.print(topic); Serial.print(". Message: "); String messageTemp; for (int i = 0; i < length; i++) { Serial.print((char)message[i]); messageTemp += (char)message[i]; } Serial.println(); // add more if statements to control more GPIOs with MQTT // If a message is received on the topic esp32/output, you check if the message is either "on" or "off". if (String(topic) == "home/suis1") {


27 Serial.print("Changing output to "); if(messageTemp == "on"){ Serial.println("on"); // digitalWrite(Relay01,HIGH); client.publish("home/relay1/status", "RELAY 1 ON"); } else if(messageTemp == "off"){ Serial.println("off"); // digitalWrite(Relay01,LOW); client.publish("home/relay1/status", "RELAY 1 OFF"); } } if (String(topic) == "home/suis2") { Serial.print("Changing output to "); if(messageTemp == "on"){ Serial.println("on"); // digitalWrite(Relay02,HIGH); client.publish("home/relay2/status", "RELAY 2 ON"); } else if(messageTemp == "off"){ Serial.println("off"); // digitalWrite(Relay02,LOW); client.publish("home/relay2/status", "RELAY 2 OFF"); } } void reconnect() { //----client subscribe---------------------------------------------------------- // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("azapkm01")) { //client mesti unik dan berbeza---------- Serial.println("connected"); // Subscribe client.subscribe("home/suis1"); client.subscribe("home/suis2"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } }


28 Complete Coding #include <Wire.h> #include <LiquidCrystal_I2C.h> #include <WiFi.h> #include <PubSubClient.h> //-------MQTT------------------------------------- const char* mqtt_server = "broker.hivemq.com"; WiFiClient espClient; //mqtt PubSubClient client(espClient); //MQTT //-------WiFI------------------------------------- const char* ssid = "PMK- MYLINE"; // put wi-fi id const char* password = "polimelaka"; // wi-fi password //-------LCD------------------------------------- LiquidCrystal_I2C lcd(0x27,20,4); void setup_wifi() { delay(10); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); delay(3000); } void callback(char* topic, byte* message, unsigned int length) { Serial.print("Message arrived on topic: "); Serial.print(topic); Serial.print(". Message: "); String messageTemp; for (int i = 0; i < length; i++) { Serial.print((char)message[i]);


29 messageTemp += (char)message[i]; } Serial.println(); // add more if statements to control more GPIOs with MQTT // If a message is received on the topic esp32/output, you check if the message is either "on" or "off". if (String(topic) == "home/suis1") { Serial.print("Changing output to "); if(messageTemp == "on"){ Serial.println("on"); // digitalWrite(Relay01,HIGH); client.publish("home/relay1/status", "RELAY 1 ON"); } else if(messageTemp == "off"){ Serial.println("off"); // digitalWrite(Relay01,LOW); client.publish("home/relay1/status", "RELAY 1 OFF"); } } if (String(topic) == "home/suis2") { Serial.print("Changing output to "); if(messageTemp == "on"){ Serial.println("on"); // digitalWrite(Relay02,HIGH); client.publish("home/relay2/status", "RELAY 2 ON"); } else if(messageTemp == "off"){ Serial.println("off"); // digitalWrite(Relay02,LOW); client.publish("home/relay2/status", "RELAY 2 OFF"); } } } //----client subscribe---------------------------------------------------------- void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("azapkm01")) { //client mesti unik dan berbeza---------- Serial.println("connected"); // Subscribe client.subscribe("home/suis1"); client.subscribe("home/suis2"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds");


3 0 // W ait 5 s e c o n d s b e fo r e r e t r yin g d ela y ( 5 0 0 0 ); } } } v oid s e t u p ( ) { S e rial.b e gin ( 1 1 5 2 0 0 ); lcd.init(); lcd.backlight(); lcd.setCursor(0,0); lcd.print("IOT PMK 20 2 1 " ); delay(1000); lcd.clear(); setup_wifi(); client.setServer(mqtt_serve r, 1 8 8 3 ); // m q t t clie n t.s e t C allb a c k ( c allb a c k ); // m q t t } in t s e n s o r L D R = 3 4; char varPotString[8 ]; long lastMsg = 0; char msg[50]; int value = 0; void loop() { if (!client.connected ( ) ) { r e c o n n e c t ( ); }client.loop(); long now = millis ( ); if (now - lastMsg > 2 0 0 0 ) { lastMsg = now; //--------------sensor LDR int nilaildr=analogRead(ld r ); Serial.println(nilaildr); delay(500); varPotString[8]; dtostrf(nilaildr, 1, 1, varPotS t rin g ); client.publish("", varPotString); //------end----------------------------


31 } } Sensor Monitoring Step 1 : LCD Step 2 : LDR Actutor control Day 3 : Node Red Installing Node Red 1st step a) Install node.js https://nodejs.org/en/download/


32 b) INstalling node-red on windows https://nodered.org/docs/getting-started/windows


33 Run on ip address Run on local host Installing node red dashboard


34 MQTT setup Run dashboard


35


Click to View FlipBook Version