// 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("-----------------------");
}