Connecting NodeMcu with MQTT IOT broker using Micro python
NodeMcu with MQTT IOT broker and publishing msg on MQTT dashboard
In this blog m dedicated to explain about the MQTT broker service and how it will help us in IOT development. I will also explain how we can publish our data using MQTT broker on MQTT dashboard.
MQTT Broker service :
MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium.
How we can publish our messages on MQTT dashboard with NodeMcu and MQTT broker using micro python as programming language.
Step 1: Open a website http://www.hivemq.com/demos/websocket-client/ in this there will be Client id that id is generated after every refreshing of page. There is also Host name and Port no. is given it will be required in our program.
Fig: MQTT Broker webpage
Step 2: Now click on Subscription bar a drop down menu will be there, now click on new subscription and add the subscription. Then click on Connect bar. Your MQTT will start working, Now we have to connect the NodeMcu to MQTT dashboard.
Step 3: Now open another website on next Tab of your browser i.e. www.mqtt-dashboard.com.
In this webpage in Left side the broker name will be given and also TCP port is also given, it will be required while programming your NodeMcu.
Step 4 : Now its time to program our board to get connected with MQTT dashboard. For this first of all we should know how we have connect NodeMcu to a router or a network then only it will be able to communicate with broker. In my previous blog I have explained How to connect NodeMcu to a Router or a Network .
After this we can start our programming section. Here I have made program in which a button is there in hardware and when it will be pressed the msg will be published on HIVEMQ Code on Esplorer :
________________________________________________________________________________
import network
sta_if=network.WLAN(network.STA_IF)
sta_if.connect("Tenda_77BA70","blueheart@lab") # Tenda 77BA70 is SSID and other is password
import time
import ubinascii
import machine
from umqtt.simple import MQTTClient
# Many ESP8266 boards have active-low "flash" button on GPIO0.
button = machine.Pin(5, machine.Pin.IN,machine.Pin.PULL_UP)
# Default MQTT server to connect to
SERVER = "broker.hivemq.com"
CLIENT_ID = ubinascii.hexlify(machine.unique_id())
TOPIC = b"led"
def main(server=SERVER):
c = MQTTClient(CLIENT_ID, server)
c.connect()
print("Connected to %s, waiting for button presses" % server)
while True:
#print (button.value())
if button.value() == 0:
print("no connect")
if button.value() == 1:
#break
time.sleep_ms(20)
print("Button pressed")
c.publish(TOPIC, b"hi how r u")
time.sleep_ms(200)
# c.disconnect()
main()
..............................................................................................................................................................
Program Ends
Step 4: After the program is done upload it and then go to MQTT dashboard and in right side your HIVEMQ subscription name will be showing if not showing waiytt for a while. When the subscription name will be there means yours MQTT broker starts working.
The messages will be start publishing on HiveMQ. You can use this message to take any decision for this you to publish your message on FRED-NODERED. In my next blog I will tell you about how to publish message on FRED-NODERED using MQTT broker.


Comments
Post a Comment