Blog ini sudah banyak membahas MQTT terutama sejak 2 tahun lalu saat saya mulai melirik barang yang bernama IOT. Bagi kalian yang baru beralih dari microcontroller 8 bit ke dunia IOT atau kebalikannya seorang IT programmer yang ingin berkecimpung di dunia 8 bit, mungkin ini suatu keputusan yg akan membingungkan. Jangan salah langkah deh pokoknya, lalu mulai dari mana sih ? Jadi saran saya adalah :
- Jika anda pecinta elektronika, skema belajar yg tepat adalah Serial Com=>MQTT => Python => Node-RED
- Jika anda seorang praktisi IT maka kebalikannya Node-RED => MQTT => Arduino => Serial com
#include <DHT.h> #include <ESP8266WiFi.h> //library esp 8266 #include <PubSubClient.h> //library MQTT pubsub client const char *ssid = "wifiku"; // nama AP wifi dirumah const char *pass = "passwordku"; //server atau broker MQTT gratis const char *mqtt_server = "broker.hivemq.com"; const int mqtt_port = 1883; const char *mqtt_user = ""; const char *mqtt_pass = ""; const char *mqtt_client_name = "ahocool123"; //topik yang akan dituju #define SUHUTOPIC "/test/suhu" #define HUMITOPIC "/test/kelembaban" #define SEGTOPIC "/test/seg" //Constants #define DHTPIN 13 // what pin we're connected to #define DHTTYPE DHT22 // DHT 22 (AM2302) DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino //7seg #define SEGA 16 #define SEGB 5 #define SEGC 4 #define SEGD 14 #define SEGE 12 #define SEGF 0 #define SEGG 2 //timer dht22 unsigned long previousMillis = 0; const long interval = 5000; //Variables float hum; //Stores humidity value float temp; //Stores temperature value int incomingByte = 0; // for incoming serial data //inisialisasi klien wifi vs Mqtt WiFiClient wclient; PubSubClient client(wclient); void setup() { pinMode(SEGA, OUTPUT); pinMode(SEGB, OUTPUT); pinMode(SEGC, OUTPUT); pinMode(SEGD, OUTPUT); pinMode(SEGE, OUTPUT); pinMode(SEGF, OUTPUT); pinMode(SEGG, OUTPUT); client.setServer(mqtt_server, mqtt_port); client.setCallback(mqtt_callback); Serial.begin(9600); setupwifi(); dht.begin(); } void setupwifi() { 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()); } } void reconnectmqtt() //ngecek broker KQTT { 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(SEGTOPIC); client.subscribe(SEGTOPIC); //subscribe ke topic 7 segmen } } void segmen(int angka) { switch(angka) { case 0 : { digitalWrite(SEGA, HIGH); digitalWrite(SEGB, HIGH); digitalWrite(SEGC, HIGH); digitalWrite(SEGD, HIGH); digitalWrite(SEGE, HIGH); digitalWrite(SEGF, HIGH); digitalWrite(SEGG, LOW); break; } case 1 : { digitalWrite(SEGA, LOW); digitalWrite(SEGB, HIGH); digitalWrite(SEGC, HIGH); digitalWrite(SEGD, LOW); digitalWrite(SEGE, LOW); digitalWrite(SEGF, LOW); digitalWrite(SEGG, LOW); break; } case 2 : { digitalWrite(SEGA, HIGH); digitalWrite(SEGB, HIGH); digitalWrite(SEGC, LOW); digitalWrite(SEGD, HIGH); digitalWrite(SEGE, HIGH); digitalWrite(SEGF, LOW); digitalWrite(SEGG, HIGH); break; } case 3 : { digitalWrite(SEGA, HIGH); digitalWrite(SEGB, HIGH); digitalWrite(SEGC, HIGH); digitalWrite(SEGD, HIGH); digitalWrite(SEGE, LOW); digitalWrite(SEGF, LOW); digitalWrite(SEGG, HIGH); break; } case 4 : { digitalWrite(SEGA, LOW); digitalWrite(SEGB, HIGH); digitalWrite(SEGC, HIGH); digitalWrite(SEGD, LOW); digitalWrite(SEGE, LOW); digitalWrite(SEGF, HIGH); digitalWrite(SEGG, HIGH); break; } case 5 : { digitalWrite(SEGA, HIGH); digitalWrite(SEGB, LOW); digitalWrite(SEGC, HIGH); digitalWrite(SEGD, HIGH); digitalWrite(SEGE, LOW); digitalWrite(SEGF, HIGH); digitalWrite(SEGG, HIGH); break; } case 6 : { digitalWrite(SEGA, HIGH); digitalWrite(SEGB, LOW); digitalWrite(SEGC, HIGH); digitalWrite(SEGD, HIGH); digitalWrite(SEGE, HIGH); digitalWrite(SEGF, HIGH); digitalWrite(SEGG, HIGH); break; } case 7 : { digitalWrite(SEGA, HIGH); digitalWrite(SEGB, HIGH); digitalWrite(SEGC, HIGH); digitalWrite(SEGD, LOW); digitalWrite(SEGE, LOW); digitalWrite(SEGF, LOW); digitalWrite(SEGG, LOW); break; } case 8 : { digitalWrite(SEGA, HIGH); digitalWrite(SEGB, HIGH); digitalWrite(SEGC, HIGH); digitalWrite(SEGD, HIGH); digitalWrite(SEGE, HIGH); digitalWrite(SEGF, HIGH); digitalWrite(SEGG, HIGH); break; } case 9 : { digitalWrite(SEGA, HIGH); digitalWrite(SEGB, HIGH); digitalWrite(SEGC, HIGH); digitalWrite(SEGD, HIGH); digitalWrite(SEGE, LOW); digitalWrite(SEGF, HIGH); digitalWrite(SEGG, HIGH); break; } } } void loop() { if (!client.connected()) //ngecek koneksi ke broker { reconnectmqtt(); } else client.loop(); //cek terus kalau ada data masuk unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= interval) { // save the last time you read the sensor previousMillis = currentMillis; //Read data and store it to variables hum and temp hum = dht.readHumidity(); temp= dht.readTemperature(); //Print temp and humidity values to serial monitor Serial.print("Humidity: "); Serial.print(hum); Serial.print(" %, Temp: "); Serial.print(temp); Serial.println(" Celsius"); String pubStringt = String(temp); String pubStringh = String(hum); Serial.println("publish to topic: " ); Serial.print(SUHUTOPIC); Serial.print(" : "); Serial.println(pubStringt); char message_bufft[pubStringt.length() + 1]; pubStringt.toCharArray(message_bufft, pubStringt.length() + 1); client.publish(SUHUTOPIC,message_bufft ); Serial.println("publish to topic: " ); Serial.print(HUMITOPIC); Serial.print(" : "); Serial.println(pubStringh); char message_buffh[pubStringh.length() + 1]; pubStringh.toCharArray(message_buffh, pubStringh.length() + 1); client.publish(HUMITOPIC,message_buffh ); } } // call back untuk SUBSCRIBE topik 7 segmen void mqtt_callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived in topic: "); Serial.println(topic); Serial.print("Message:"); //ada SUB masuk String message; for (int i = 0; i < length; i++) { message = message + (char)payload[i]; //Conver *byte to String } Serial.print(message); //ubah tampilan 7 segmen if(message == "SEG1") {segmen(1); } if(message == "SEG2") {segmen(2); } if(message == "SEG3") {segmen(3); } if(message == "SEG4") {segmen(4); } if(message == "SEG5") {segmen(5); } if(message == "SEG6") {segmen(6); } if(message == "SEG7") {segmen(7); } if(message == "SEG8") {segmen(8); } if(message == "SEG9") {segmen(9); } if(message == "SEG0") {segmen(0);} }
npm install node-red-contrib-mqtt-broker