Belum lengkap rasanya kalau saya belum mencoba praktek modbus yang telah lengkap piramida IOT nya kedalam platform IOT lokal yaitu ANTARES by telkomiot. Tentunya pembaca yang ingin mengikuti pembahasan kali ini diharapkan membaca penelusuran saya dengan Antares vs ESP 8266 disini dan disini. Masih dengan rangkaian yang sama kita akan mencoba mengirim data pembacaan sensor modbus dan juga menunggu perintah dikirimkan dari antares ke sensor modbus yang saya buat menggunakan arduino.
Dari standar OneM2M yang digunakan antares maka ada ketentuan data MQTT yang di publish dan subscribe hanya terbatas pada 2 topik berikut :
PUBLISH TOPIC :
/oneM2M/req/access:key/antares-cse/json
SUBSCRIBE TOPIC :
/oneM2M/resp/antares-cse/access:key/json
Data yang dikirim / PUBLISH berupa JSON dimana polanya harus sesuai dimana data apapun yang akan dikirim ke Antares merupakan isi dari dalam "con" :
{
"m2m:rqp": {
"fr": "access:key",
"to": "/antares-cse/antares-id/project-name/device-name",
"op": 1,
"rqi": 123456,
"pc": {
"m2m:cin": {
"cnf": "message",
"con": "{\"data\":\"value\"}"
}
},
"ty": 4
}
}
Jadi script publish mqtt menggunakan function atau routine sebagai berikut :
void kirim(String tombol, String suhu, String humi) // kirim 3 item sekaligus { Serial.println("publish to antares topik: " ); Serial.print(TOPIKPUB); Serial.print(" : "); Serial.println(tombol + " - " + suhu + " - " + humi ); String pubString ; //isi json pub message yg super panjang pubString += F("{"); pubString += F("\"m2m:rqp\": {"); pubString += F("\"fr\": \""); pubString += String(accessKey) ; pubString += F("\","); pubString += F("\"to\": \"/antares-cse/antares-id/"); pubString += String(projDev); pubString += F("\","); pubString += F("\"op\": 1,"); pubString += F("\"rqi\": 123456,"); pubString += F("\"pc\": {"); pubString += F("\"m2m:cin\": {"); pubString += F("\"cnf\": \"message\","); pubString += F("\"con\": \"{\\\"tombol\\\":"); pubString += tombol; pubString += F(",\\\"suhu\\\":"); pubString += suhu; pubString += F(",\\\"humidity\\\":"); pubString += humi; pubString += F("}\""); pubString += F("}"); pubString += F("},"); pubString += F("\"ty\": 4"); pubString += F("}"); pubString += F("}"); // kirim ke topik TOPIKPUB char message_buff[pubString.length() + 1]; pubString.toCharArray(message_buff, pubString.length() + 1); client.publish(TOPIKPUB,message_buff); }
Untuk menunggu request dari antares harus diperhatikan yang diterima melalui callback merupakan JSON juga lhooo...jadi kalau mumet sebaiknya dihentikan segera melanjutkan membaca blog ini!
{
"m2m:rsp" : {
"rsc" : 2001,
"rqi" : "123456",
"pc" : {
"m2m:cin" : {
"rn" : "cin_63068886",
"ty" : 4,
"ri" : "/antares-cse/cin-63068886",
"pi" : "/antares-cse/cnt-682859183",
"ct" : "20200707T152502",
"lt" : "20200707T152502",
"st" : 0,
"cnf" : "message",
"cs" : 35,
"con" : "{\"led\":\"on\"}"
}
},
"to" : "access:key",
"fr" : "/antares-cse"
}
}
Untuk mengolah data request dari antares maka ESP8266 akan melakukan parsing JSON dari payload MQTT dengan menggunakan library ARDUINO JSON
DynamicJsonDocument doc(512); deserializeJson(doc, message); String parsedString = doc["m2m:rsp"]["pc"]["m2m:cin"]["con"]; //pertama cari isi con deserializeJson(doc, parsedString); String lednya = doc["led"]; //kedua cari isi led on apa off Serial.println("lednya : " + lednya);
Jadi untuk mendapatkan isi dari message led "on" atau "off" maka diperlukan 2 kali parsing json karena isi dalam "con" juga di syaratkan sebagai JSON. Kenapa begitu? Ya karena itulah standar onem2m yang digunakan jadi harus diikuti saja.
Selengkapnya bisa dilihat pada video berikut ini
Script lengkap :
// by www.aisi55.com please attach our credential if you using our script #include <SoftwareSerial.h> #include <ESP8266WiFi.h> #include <PubSubClient.h> #include <ArduinoJson.h> SoftwareSerial mod(4, 5); // RX, TX const char *ssid = "nama wifi"; const char *pass = "password"; const char *mqtt_server = "mqtt.antares.id"; const int mqtt_port = 1883; const char *mqtt_user = ""; const char *mqtt_pass = ""; const char *mqtt_client_name = "ahocool1265352"; //sesuaikan dengan parameter akun antares kamu #define TOPIKPUB "/oneM2M/req/access:key/antares-cse/json" #define TOPIKSUB "/oneM2M/resp/antares-cse/access:key/json"
#define accessKey "access:key"
#define projDev "Project_name/Device_name" WiFiClient wclient; PubSubClient client(wclient); byte ledOn[] = {0x05, 0x05, 0x00, 0x00, 0xFF, 0x00, 0x8D, 0xBE};//ngidupin Led byte ledOff[] = {0x05, 0x05, 0x00, 0x00, 0x00, 0x00, 0xCC, 0x4E};//matikan Led byte tombol[] = {0x05, 0x02, 0x00, 0x00, 0x00, 0x01, 0xB8, 0x4E};//tombol byte tombolL[] = {0x05, 0x02, 0x01, 0x00, 0xa0, 0xB8}; // tombol Low byte tombolH[] = {0x05, 0x02, 0x01, 0x01, 0x61, 0x78}; // tombol High byte humitemp[] = {0x05, 0x04, 0x00, 0x00, 0x00, 0x03, 0xB1, 0x8F};//baca dht11 byte dhtOK[] = {0x05, 0x04, 0x06} ; byte bufferDataModbus[50]; byte *ptr; bool urut= false; bool led= false; unsigned long previousMillis = 0; String suhunya ="0"; String huminya ="0"; String tomb ="0"; void setup() { Serial.begin(9600); Serial.println(F("ESP8266 Modbus Bridge to ANTARES")); Serial.println(F("http://www.aisi555.com")); Serial.println(); if (WiFi.status() != WL_CONNECTED) { Serial.print("Connecting to Wifi: "); Serial.print(ssid); Serial.println("..."); WiFi.begin(ssid, pass); if (WiFi.waitForConnectResult() != WL_CONNECTED) return; Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } mod.begin(9600); ptr = bufferDataModbus; client.setServer(mqtt_server, mqtt_port); client.setCallback(mqtt_callback); } void reconnectmqtt() { Serial.println("Connecting to MQTT server.."); if (client.connect(mqtt_client_name,mqtt_user, mqtt_pass)) { Serial.println("Connected to MQTT server"); } else { Serial.println("Could not connect to MQTT server"); } if (client.connected()){ Serial.println("subscribe to topic: "); Serial.println(TOPIKSUB); client.subscribe(TOPIKSUB); } } void kirim(String tombol, String suhu, String humi) { Serial.println("publish to antares topik: " ); Serial.print(TOPIKPUB); Serial.print(" : "); Serial.println(tombol + " - " + suhu + " - " + humi ); String pubString ; pubString += F("{"); pubString += F("\"m2m:rqp\": {"); pubString += F("\"fr\": \""); pubString += String(accessKey) ; pubString += F("\","); pubString += F("\"to\": \"/antares-cse/antares-id/"); pubString += String(projDev); pubString += F("\","); pubString += F("\"op\": 1,"); pubString += F("\"rqi\": 123456,"); pubString += F("\"pc\": {"); pubString += F("\"m2m:cin\": {"); pubString += F("\"cnf\": \"message\","); pubString += F("\"con\": \"{\\\"tombol\\\":"); pubString += tombol; pubString += F(",\\\"suhu\\\":"); pubString += suhu; pubString += F(",\\\"humidity\\\":"); pubString += humi; pubString += F("}\""); pubString += F("}"); pubString += F("},"); pubString += F("\"ty\": 4"); pubString += F("}"); pubString += F("}"); char message_buff[pubString.length() + 1]; pubString.toCharArray(message_buff, pubString.length() + 1); client.publish(TOPIKPUB,message_buff); } void loop() { if (!client.connected()) { reconnectmqtt(); } else client.loop(); //cek terus kalau ada data masuk unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= 5000) { // save the last time you read the sensor previousMillis = currentMillis; if(!urut )mod.write(tombol, sizeof(tombol)); else mod.write(humitemp, sizeof(humitemp)); urut =!urut; } long millisResponModbus = millis() + 1000; while (!mod.available()) { if (millisResponModbus < millis()) { break;//timeout } } while (mod.available()) { byte b = mod.read(); *ptr++ = b; Serial.print("0x"); Serial.print(String(b, HEX)); Serial.print(" "); delay(2); } if (memcmp(bufferDataModbus, ledOn, sizeof(ledOn)) == 0) { ptr = bufferDataModbus; memset(bufferDataModbus, 0x00, sizeof(bufferDataModbus)); } else if (memcmp(bufferDataModbus, ledOff, sizeof(ledOff)) == 0) { ptr = bufferDataModbus; memset(bufferDataModbus, 0x00, sizeof(bufferDataModbus)); } else if (memcmp(bufferDataModbus, tombolL, sizeof(tombolL)) == 0) { ptr = bufferDataModbus; tomb="0"; kirim(tomb,suhunya,huminya); memset(bufferDataModbus, 0x00, sizeof(bufferDataModbus)); } else if (memcmp(bufferDataModbus, tombolH, sizeof(tombolH)) == 0) { ptr = bufferDataModbus; tomb="1"; kirim(tomb,suhunya,huminya); memset(bufferDataModbus, 0x00, sizeof(bufferDataModbus)); } else if (memcmp(bufferDataModbus, dhtOK, sizeof(dhtOK)) == 0) { ptr = bufferDataModbus; suhunya = String(ptr[4]) + '.' +String(ptr[6]) ; huminya = String(ptr[8]); kirim(tomb,suhunya,huminya) ; memset(bufferDataModbus, 0x00, sizeof(bufferDataModbus)); } else { ptr = bufferDataModbus; //Serial.println(""); memset(bufferDataModbus, 0x00, sizeof(bufferDataModbus)); } } void mqtt_callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived in topic: "); Serial.println(topic); //Serial.print("Message:"); String message; for (int i = 0; i < length; i++) { message = message + (char)payload[i]; //Conver *byte to String } // Serial.print(message); //kepanjangan makanya di tutup DynamicJsonDocument doc(512); deserializeJson(doc, message); String parsedString = doc["m2m:rsp"]["pc"]["m2m:cin"]["con"]; deserializeJson(doc, parsedString); String lednya = doc["led"]; Serial.println("lednya : " + lednya); if(lednya == "on") { mod.write(ledOn, sizeof(ledOn)); Serial.println("lednya hidup"); } if(lednya == "off") { mod.write(ledOff, sizeof(ledOff)); Serial.println("lednya mati "); } Serial.println(); Serial.println("-----------------------"); }
0 komentar:
Posting Komentar