Lab 12 MQTT

จุดประสงค์การเรียนรู้

- สามารถรับส่งข้อมูลจากESP32โดยใช้โปรโตคอลMQTTจากโปรแกรมmqtt boxได้


วัสดุอุปกรณ์

1. หลอด LED 3 จำนวน 3 หลอด

2. ตัวต้านทาน 220 โอห์ม จำนวน 3 ตัว

3. บอร์ด ESP32 Devkit V.1

4. สายMicro USB

5. Breadboard

6. สายแพร Male-Male


วิธีทำการทดลอง

1. ต่อวงจรหลอดLEDกับบอร์ดESP32 ดังนี้

LED หลอดที่ 1 ต่อกับพอร์ต D5 (GPIO5)

LED หลอดที่ 2 ต่อกับพอร์ต D19 (GPIO19)

LED หลอดที่ 3 ต่อกับพอร์ต D23 (GPIO23)

2. ทำการหาIP Address ของเครื่องเรา ถ้าในUbuntu สามารถพิมพ์คำสั่งในTerminalด้วยคำว่า : ifconfig

หลังจากนั้นในTerminalก็จะบอกIP Address ของเครื่องเราดังรูปข้างล่าง

3. ทำการติดตั้งโปรแกรมmqtt box หากใช้งานบนUbuntuสามารถเปิดUbuntu softwareขึ้นมา

แล้วค้นหาโปรแกรมที่มีชื่อว่าMQTTBox ดังรูปข้างล่าง

4.ทำการเปิดโปรแกรมMOTTBox และArduino IDE ทำการcopy codeในช่องข้างล่าง และทำการแก้ไขค่าconfigทั้งในMQTTBox และArduino IDE ให้เรียบร้อยตามIPที่ได้รับมาจากระบบเครือข่าย ก่อนการUploadโปรแกรม

Arduino IDE Code

#include <Arduino.h>

#include<ArduinoJson.h>

#include <WiFi.h>

#include <ESP32Servo.h>

#include <PubSubClient.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>


Adafruit_SSD1306 OLED(-1);


// Update these with values suitable for your network.

const char* ssid = "BJHome2G";

const char* password = "123456789";


// Config MQTT Server

#define mqtt_server "192.168.1.15"

#define mqtt_port 1883

#define mqtt_user "user"

#define mqtt_password "123456"


#define snd 18 // Buzzer

#define LED_PIN_A 5

#define LED_PIN_B 19

#define LED_PIN_C 23


WiFiClient espClient;

PubSubClient client(espClient);


void setup() {

tone(snd, 800, 100); tone(snd, 900, 100); tone(snd, 800, 100);tone(snd, 900, 100);delay(50);tone(snd, 1000, 1000);

OLED.begin(SSD1306_SWITCHCAPVCC, 0x3C); // กำหนดแอดเดรสของพอร์ตจอเป็น 0x3C (for the 128x64)

pinMode(LED_PIN_A, OUTPUT);

pinMode(LED_PIN_B, OUTPUT);

pinMode(LED_PIN_C, OUTPUT);

Serial.begin(115200);

delay(10);

Serial.println();

Serial.print("Connecting to ");

Serial.println(ssid);

WiFi.begin(ssid, password);

digitalWrite(LED_PIN_A, HIGH);

digitalWrite(LED_PIN_B, HIGH);

digitalWrite(LED_PIN_C, HIGH);

delay(500);

digitalWrite(LED_PIN_A, LOW);

digitalWrite(LED_PIN_B, LOW);

digitalWrite(LED_PIN_C, LOW);

OLED.clearDisplay();

OLED.setCursor(0, 0);

OLED.setTextColor(WHITE, BLACK);

OLED.setTextSize(2);

OLED.println(" WiFi");

OLED.println("Conecting");

OLED.display();


while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

}

Serial.println("");

Serial.println("WiFi connected");

Serial.println("IP address: ");

Serial.println(WiFi.localIP());

client.setServer(mqtt_server, mqtt_port);

client.setCallback(callback);

tone(snd, 1000, 100); delay(150); tone(snd, 1000, 100); delay(150); tone(snd, 1000, 100);

OLED.clearDisplay();

OLED.setTextColor(WHITE, BLACK);

OLED.setCursor(0, 0);

OLED.setTextSize(1);

OLED.println("WiFi connected");

OLED.print("IP : "); OLED.println( WiFi.localIP());

OLED.print("*******************");

OLED.display();

}


void loop() {

if (!client.connected()) {

Serial.print("Attempting MQTT connection...");

if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {

Serial.println("connected");

client.subscribe("ESP32"); // ESP32 Topic

} else {

Serial.print("failed, rc=");

Serial.print(client.state());

Serial.println(" try again in 5 seconds");

delay(5000);

return;

}

}

client.loop();

}


void callback(char* topic, byte* payload, unsigned int length) {

Serial.print("Message arrived [");

Serial.print(topic);

Serial.print("] ");

String msg = "";

int i = 0;

while (i < length) msg += (char)payload[i++];

if (msg == "GET") {

client.publish("ESP32", (digitalRead(LED_PIN_A) ? "LEDON" : "LEDOFF"));

Serial.println("Send !");

return;

}

// CiRA CORE Mqtt //


const size_t capacity = JSON_OBJECT_SIZE(1); //? Object Size

DynamicJsonDocument doc(capacity); //? Declare variable for store json

deserializeJson(doc, payload, length); //? deserialize JSON

String msg_cira = doc["a"]; //? Store key title to tittle

////////////////////// command mqtt control LED A -C /////////////////////////////////

if(msg == "AON"){digitalWrite(LED_PIN_A,HIGH);}

else if(msg == "AOFF"){digitalWrite(LED_PIN_A,LOW);}

else if(msg == "BON"){digitalWrite(LED_PIN_B,HIGH);}

else if(msg == "BOFF"){digitalWrite(LED_PIN_B,LOW);}

else if(msg == "CON"){digitalWrite(LED_PIN_C,HIGH);}

else if(msg == "COFF"){digitalWrite(LED_PIN_C,LOW);}

/*

digitalWrite(LED_PIN_A, (msg == "AON" ? HIGH : LOW));

digitalWrite(LED_PIN_B, (msg == "BON" ? HIGH : LOW));

digitalWrite(LED_PIN_C, (msg == "CON" ? HIGH : LOW));

*/

// digitalWrite(LED_PIN_A, (msg_cira == "LEDON" ? HIGH : LOW));



////////// Display OLED and Serial ////////////////

Serial.print("msg="); Serial.println(msg);

Serial.print("msg_cira="); Serial.println( msg_cira);



OLED.clearDisplay();

OLED.setTextColor(WHITE, BLACK);

OLED.setCursor(0, 0);

OLED.setTextSize(1);

OLED.println("WiFi connected");

OLED.print("IP : "); OLED.println( WiFi.localIP());

OLED.print("Topic : "); OLED.println( topic);

OLED.println("-------------------");

OLED.print("msg : "); OLED.println(msg);

OLED.println("-------------------");

OLED.print("msg_cira: "); OLED.println(msg_cira);

OLED.println("-------------------");

OLED.display();

}

** กรณีใช้งานใน Ubuntu ก่อนกดUpload ให้ทำการเปิดTerminal โดยเข้าไปที่desktop คลิกขวาตรงพื้นที่ว่าง แล้วเลือก Open Terminal หลังจากนั้นให้พิมพ์ข้อความในTerminalดังนี้เพื่อทำการเปิดพอร์ตUSB : sudo chmod 666 /dev/ttyUSB0 หลังจากนั้นให้กดEnter และทำการกรอกรหัสผ่านของเครื่องพร้อมกับกดEnerอีกครั้ง หลังจากนั้นก็Upload codeในArduino IDE

5. การการส่งข้อมูลด้วยคำสั่งเปิดไฟล์LED หลอดA-C โดยใช้คำสั่งดังนี้ (ดังรูปข้างล่าง) แล้วกดปุ่มPublish

AON = LED หลอดA จะติด

BON = LED หลอดB จะติด

CON = LED หลอดC จะติด

AOFF = LED หลอดA จะดับ

BOFF = LED หลอดB จะดับ

COFF = LED หลอดC จะดับ