irhamnur00
New Member
I was just trying to make my first project with B4R. Also, I was a newbie in microcontroller programming.
In this my first project, I make a dashboard in Node-RED to display a value from the ESP32 and to control the blue LED on ESP32. The value had been successfully sent, but until now the topic message for turning on the LED from the server can't be received. These several days, I was looking for a way to receive a topic message from Node-RED server. I had to edit Mqtt_MessageArrived which I think that a function duty to receive a topic message from the server, but I'm not found any solution on the internet.
Below is my program code:
Below is my Node-Red Flow:
In this my first project, I make a dashboard in Node-RED to display a value from the ESP32 and to control the blue LED on ESP32. The value had been successfully sent, but until now the topic message for turning on the LED from the server can't be received. These several days, I was looking for a way to receive a topic message from Node-RED server. I had to edit Mqtt_MessageArrived which I think that a function duty to receive a topic message from the server, but I'm not found any solution on the internet.
Below is my program code:
The Code:
Sub Process_Globals
Public Serial1 As Serial
Private wifi As ESP8266WiFi
Private mqtt As MqttClient
Private wifiClient As WiFiSocket
Private ServerIP() As Byte = Array As Byte(000, 000, 000, 000)
Private const ServerPort As UInt = 1883
Private const pinBiru As Byte = 0x02
Private D2 As Pin
Private LEDbiru As String = "esp-irham/LEDbiru"
End Sub
Sub Connect(unused As Byte)
If mqtt.Connect = False Then
Log("trying to connect again")
CallSubPlus("Connect", 1000, 0)
Return
End If
Log("Connected to broker")
mqtt.Subscribe("esp32", 0)
mqtt.Publish2("pc", Array As Byte(1, 2, 3), True)
If mqtt.Subscribe(LEDbiru, 0) Then
Log("subscribed")
End If
End Sub
Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
Dim bc As ByteConverter
Dim PayLoadStr As String = bc.StringFromBytes(Payload)
Log(PayLoadStr)
Log("Message arrived. Topic=", Topic, " payload: ", Payload)
'Take action. Mengeset dan Memilih topic yang masuk
Select Topic
Case LEDbiru
If PayLoadStr = "on" Then
Log("On")
D2.DigitalWrite(True)
Else
Log("Off")
D2.DigitalWrite(False)
End If
End Select
End Sub
Sub Mqtt_Disconnected
Log("Disconnected")
mqtt.Close
Connect(0)
End Sub
Sub WifiConnect
If wifi.Connect2("SSID", "wifipass") Then
Log("Connected to network")
Else
Log("Failed to connect to network. Trying to reconnect")
'CallSubPlus("WifiConnect", 1000, 0)
'Return
End If
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
D2.Initialize(pinBiru, D2.MODE_OUTPUT)
Log("AppStart")
'Wifi connection function
WifiConnect
mqtt.Initialize(wifiClient.Stream, ServerIP, ServerPort, "esp-irham", "Mqtt_MessageArrived", "Mqtt_Disconnected")
'MQTT connection functions
Connect(0)
Dim Nilai() As Byte = "1 "
If mqtt.Publish2("tes-tulisan", Nilai, True) Then
Log("Sent Successfully")
End If
Dim Angka() As Byte = "3" 'Array As Byte(7)
If mqtt.Publish2("tes-angka", Angka, True) Then
Log("2nd Sent Successfully")
End If
'Sleep(5000)
'D2.DigitalWrite(True)
End Sub
Below is my Node-Red Flow: