5.3 AI MQTT CiRA

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

- สามารถรับส่งข้อมูลจากESP32โดยใช้โปรโตคอลMQTT จากPlatform CiRA COREสำหรับการควบการบอร์ดสมองกลฝังตัวโดยใช้

AI Deep learning ได้


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

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)

ตารางDatasheetการต่อวงจรทั้งหมดสำหรับทุกLab

7 Word Document.pdf

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

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

3. ทำการเปิดPlatform CiRA COREโดยลากกล่องและเขียนCodeตามรูปและตารางด้านล่าง พร้อมทั้งทำการตั้งค่ากล่องMqtt

การทำงานในแต่ละกล่อง

หน้าที่

โปรแกรมเริ่มทำงานเมื่อกดปุ่มRun

หน้าที่

รับภาพจากกล้อง

หน้าที่

นำเอาWeightหรือModelของAIมาใช้ในการประมวลผลข้อมูล

หน้าที่

แสดงผลข้อมูลที่รับเข้ามา

javascript

var objs = payload.DeepDetectCPU.objects

var len = objs.length

var detect = false

for(var i = 0; i<len; i++) {

if(objs[i].name == 'person'){detect = true

break}

}

ifelse = detect

หน้าที่ กำหนดเงื่อนไข/ทางเลือก if Else

javascript

var objs = payload.DeepDetectCPU.objects

var len = objs.length

var a = 0

for(var i = 0; i < len; i++) {

if(objs[i].name == "person") {

a = "LEDON"

break

}

}

payload = {}

payload.a = a

หน้าที่ ใช้เขียนโปรแกรมJava Script

javascript

var objs = payload.DeepDetectCPU.objects

var len = objs.length

var a = 0

for(var i = 0; i < len; i++) {

if(objs[i].name == "person") {

a = "LEDOFF"

break

}

}

payload = {}

payload.a = a

หน้าที่ กำหนดเงื่อนไข/ทางเลือก if Else

javascript

หน้าที่ ส่งข้อมูลจากCiRA CORE ผ่านโปรโตคอล Mqtt แบบPublish

4. ทำการcopy codeใน Arduino IDE ตัวอย่าง แล้วทำการแก้ไขค่าconfig wifi และ mqtt ดังรูปข้างล่าง

V3 pob

#include <Arduino.h>

#include<ArduinoJson.h>

#include <WiFi.h>

#include <ESP32Servo.h>

#include <PubSubClient.h>



// 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.11"

#define mqtt_port 1883

#define mqtt_user ""

#define mqtt_password ""


#define LED_PIN 26


WiFiClient espClient;

PubSubClient client(espClient);


void setup() {

pinMode(LED_PIN, OUTPUT);


Serial.begin(115200);

delay(10);


Serial.println();

Serial.print("Connecting to ");

Serial.println(ssid);


WiFi.begin(ssid, password);

digitalWrite(LED_PIN, HIGH);


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);


}


void loop() {

if (!client.connected()) {

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

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

Serial.println("connected");

client.subscribe("/ESP/LED"); // ESP 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("/ESP/LED", (digitalRead(LED_PIN) ? "LEDON" : "LEDOFF"));

Serial.println("Send !");

return;

}

// Cira


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

Serial.println( msg_cira);

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

//

//digitalWrite(LED_PIN, (msg == "LEDON" ? HIGH : LOW));

/*

/////////////////////////////

//if (topic == "/ESP/LED")

// {

Serial.println("--- JSON Process ---");

const size_t capacity = JSON_OBJECT_SIZE(1);

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

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

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

Serial.println("cira = " + check);

Serial.println("--- END JSON Process ---");

}*/

Serial.println(msg);

// Serial.println(topic);

/////////////////////////////////////////////////

}

5. ทำการRun Code platform CiRA CORE แล้วให้AI พบมนุษย์ และไม่พบมนุษย์ จากนั้นให้สังเกตการเปลี่ยนแปลงของหลอดLED ที่ส่งผ่าน

โปรโตคอล mqttของcira core