Difference between revisions of "WeMOS: MQTT Basic Example"

From OnnoWiki
Jump to navigation Jump to search
(Created page with "Sumber: https://www.instructables.com/id/NodeMCU-MQTT-Basic-Example/ About: Osoyoo brand products are mainly focused on STEM education.User experience and customer feeling ar...")
 
 
Line 99: Line 99:
  
 
[[File:FCA3KWDJ7QGLD7Z.LARGE.jpg|center|200px|thumb]]
 
[[File:FCA3KWDJ7QGLD7Z.LARGE.jpg|center|200px|thumb]]
Config MQTT Client (MQTTBOX)
+
 
Config MQTT Client (MQTTBOX)
 
Config MQTT Client (MQTTBOX)
 
Config MQTT Client (MQTTBOX)
 
3 More Images
 
  
 
In this step,we will show how to create an MQTT client on the MQTTBox.
 
In this step,we will show how to create an MQTT client on the MQTTBox.
Line 111: Line 107:
 
Config the MQTT CLIENT SETTINGS as below:
 
Config the MQTT CLIENT SETTINGS as below:
  
    MQTT Client Name —- Choose any name you like
+
MQTT Client Name —- Choose any name you like
    Protocol —- Choose “mqtt/tcp”
+
Protocol —- Choose “mqtt/tcp”
    Host — Type your “mqtt_server” in this column,make sure it is same as your sketch.(We use “broker.mqtt-dashboard.com” here)
+
Host — Type your “mqtt_server” in this column,make sure it is same as your sketch.(We use “broker.mqtt-dashboard.com” here)
    Keep other settings as default
+
Keep other settings as default
    Click to save your changes.
+
Click to save your changes.
  
 
Next, you will automatically enter the new page.If all above configration is correct,the “Not Connected” will change to “Connected” ,your MQTT client name and Host name will be displayed at the top of this page.
 
Next, you will automatically enter the new page.If all above configration is correct,the “Not Connected” will change to “Connected” ,your MQTT client name and Host name will be displayed at the top of this page.
  
 
Topic setting:
 
Topic setting:
 +
 
Make sure your MQTT client publish topic is same as your Arduino sketch subscribe topic(inTopic here). Make sure your MQTT client subscribe topic is same as your Arduino sketch publish topic(outTopic here).
 
Make sure your MQTT client publish topic is same as your Arduino sketch subscribe topic(inTopic here). Make sure your MQTT client subscribe topic is same as your Arduino sketch publish topic(outTopic here).
Step 4: Program Running Result
 
Program Running Result
 
Program Running Result
 
Program Running Result
 
Program Running Result
 
  
Once the upload done,if the wifi hotspot name and password setting is ok, and MQTT broker is connected, open the Serial Monitor,you will see the publish message “hello world” on the serial monitor.
+
==Step 4: Program Running Result==
 +
 
 +
[[File:FSR9VA3J7QGLD96.LARGE.jpg|center|200px|thumb]]
 +
 
 +
[[File:F6W6UB0J7QGLDBL.LARGE.jpg|center|200px|thumb]]
  
Then open the MQTT client and publish payload “1” to the topic, this NodeMCU will receive these messages by subscribing to the “inTopic”,and the LED will be lit.
+
[[File:FL7440OJ7QGLD97.LARGE.jpg|center|200px|thumb]]
  
Publish payload “0” to this topic,the NodeMCU LED will be turned off.
+
[[File:F0ASF0DJ7QGLDAE.LARGE.jpg|center|200px|thumb]]
  
  
 +
Once the upload done,if the wifi hotspot name and password setting is ok, and MQTT broker is connected, open the Serial Monitor,you will see the publish message “hello world” on the serial monitor.
  
 +
Then open the MQTT client and publish payload “1” to the topic, this NodeMCU will receive these messages by subscribing to the “inTopic”,and the LED will be lit.
  
 +
Publish payload “0” to this topic,the NodeMCU LED will be turned off.
  
 
==Referensi==
 
==Referensi==

Latest revision as of 13:47, 18 February 2020

Sumber: https://www.instructables.com/id/NodeMCU-MQTT-Basic-Example/

About: Osoyoo brand products are mainly focused on STEM education.User experience and customer feeling are always our first priority. Our goal is to create the best designed product to help students and hobbyist to... More About osoyooproduct »

This lesson will show the basic MQTT protocol usage on the NodeMCU board .We use the MQTTBox as the MQTT client here, and we will use the NodeMCU to complete following operations:

Publish “hello world” to the topic “outTopic” every two seconds.Subscribe to the topic “inTopic”, print out any received messages.It assumes the received payloads are strings not binaries.If the subscribed message is “1”, light the onboards LED.Turn off the onboard LED if the message to subscribe is “0”.

Preparation:

  • Osoyoo NodeMCU x1
  • USB Cable x1
  • PC x1
  • Arduino IDE(Versin 1.6.4+)

Connect the NodeMCU to PC via USB cable.

Teacher Notes

Teachers! Did you use this instructable in your classroom? Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Library Installation

Library Installation Library Installation Library Installation

Install PubSubClientlibrary

We need install MQTT endpoint library(PubSubClient) to communicate with MQTT broker, please download the library from following link:

http://osoyoo.com/wp-content/uploads/samplecode/pubsubclient.zip

Unzip above file, move the unzipped folder to Arduino IDE library folder.

Open the Arduino IED,you can find the “pubsubclient” on the “Examples” column.

Install MQTT Client:

we will use the MQTTBox as the MQTT client. please download from:

http://workswithweb.com/html/mqttbox/installing_apps.html

Step 2: Code

FL2OXWPJ7QGLD1Q.LARGE.jpg
FY9LZG4J7QGLD1T.LARGE.jpg


open Arduino IDE–>File–>Example–>pubsubclient–>mqtt esp8266,you will get sample code.

Edit the code to fit your own WiFi and MQTT settings as following operations:

1)Hotspot Configration: Find below code line,put your own ssid and password on there.

const char* ssid = “your_hotspot_ssid”;
const char* password = “your_hotspot_password”;

2)MQTT Server Address Setting, here we use free MQTT broker "broker.mqtt-dashboard.com" . You can use your own MQTT broker URL or IP address to set above mqtt_server value. You can also use some famous free MQTT server to test the project such as “broker.mqtt-dashboard.com”, “iot.eclipse.org” etc.

const char* mqtt_server = “broker.mqtt-dashboard.com”;

3)MQTT Client Settings If your MQTT broker require clientID,username and password authentication,you need to

change

if (client.connect(clientId.c_str()))

To

if (client.connect(clientId,userName,passWord)) //put your clientId/userName/passWord here

If not,just keep them as default.

After do that,choose the corresponding board type and port type as below,then upload the sketch to the NodeMCU.

Board:”NodeMCU 0.9(ESP-12 Module)”
CPU Frequency:”80MHz”Flash Size:”4M (3M SPIFFS)”
Upload Speed:”115200″
Port: Choose your own Serial Port for your NodeMCU

Step 3: Config MQTT Client (MQTTBOX)

FQEIRLBJ7QGLD30.LARGE.jpg
F4LTW0VJ7QGLD49.LARGE.jpg
FMJEZB7J7QGLD4A.LARGE.jpg
FKYIELTJ7QGLD5H.LARGE.jpg
F7C4AL1J7QGLD6O.LARGE.jpg
FUJ6XSVJ7QGLD6S.LARGE.jpg
FCA3KWDJ7QGLD7Z.LARGE.jpg


In this step,we will show how to create an MQTT client on the MQTTBox.

Open your MQTTBox and click the blue button to add a new MQTT client.

Config the MQTT CLIENT SETTINGS as below:

MQTT Client Name —- Choose any name you like
Protocol —- Choose “mqtt/tcp”
Host — Type your “mqtt_server” in this column,make sure it is same as your sketch.(We use “broker.mqtt-dashboard.com” here)
Keep other settings as default
Click to save your changes.

Next, you will automatically enter the new page.If all above configration is correct,the “Not Connected” will change to “Connected” ,your MQTT client name and Host name will be displayed at the top of this page.

Topic setting:

Make sure your MQTT client publish topic is same as your Arduino sketch subscribe topic(inTopic here). Make sure your MQTT client subscribe topic is same as your Arduino sketch publish topic(outTopic here).

Step 4: Program Running Result

FSR9VA3J7QGLD96.LARGE.jpg
F6W6UB0J7QGLDBL.LARGE.jpg
FL7440OJ7QGLD97.LARGE.jpg
F0ASF0DJ7QGLDAE.LARGE.jpg


Once the upload done,if the wifi hotspot name and password setting is ok, and MQTT broker is connected, open the Serial Monitor,you will see the publish message “hello world” on the serial monitor.

Then open the MQTT client and publish payload “1” to the topic, this NodeMCU will receive these messages by subscribing to the “inTopic”,and the LED will be lit.

Publish payload “0” to this topic,the NodeMCU LED will be turned off.

Referensi

Pranala Menarik