In this lesson, we will show how to use a MQTT client(MQTTBox here) to send a digit MQTT message to NodeMCU. NodeMCU will subscribe to corrsponding publish topic and display the subscription messages on a 4-digit 7 segment LED device.
Hardware:
Software:
NodeMCU | 4 digital display |
---|---|
3.3V | VCC |
GND | GND |
D2 | CLK |
D3 | DIO |
Connect the NodeMCU to computer via USB cable,open this sketch by using Arduino IDE(Version1.6.4+):
Edit the code to fit your own WiFi and MQTT settings as following operations:
1)Hotspot Configration:
const char* ssid = “your_hotspot_ssid”;
const char* password = “your_hotspot_password”;
Find above code line,put your own ssid and password on there.
2)MQTT Server Address Setting
const char* mqtt_server = “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.
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 coresponding board type and port type as below,then upload the sketch to the NodeMCU.
About how to config the MQTT clientr,check this link.
Topics Settings:
Onece the upload done,if wifi hotspot name and password setting is ok and MQTT broker is connected, open the Serial Monitor,you will see following result:
The NodeMCU serial port will show the IP address and the connection satatus,then print the command from MQTT client.NOTE: When the number is between 0 and 191, it can be displayed normally.The initial state output value is 83.
Open the MQTTBox and publish different number(0~191) to the broker,
The 4 digital LED display will show as below :
The Serial Monitor output as below:
DownLoad Url osoyoo.com
You must be logged in to post a comment.
I would like to display an MQTT message that has a decimal point in it, for example 3.3 or 19.1
I note that the TM1637Display library has showNumberDecEx() as another display setting which should support this, but I cannot make this work – I can make it show the colon, but the value of the MQTT reading is always truncated at the decimal, eg. 3.3 comes out as 3, 19.1 comes out as 19
I also cannot position the start segment of the number – the value “The position least significant digit” means nothing to me!
Can anyone suggest a code modification that uses that display setting and will show an MQTT value of this nature, with the value either side of the colon representing the decimal point? Thanks!