B4R Question Control Leds

hvvdl

Member
I have an application that controls many leds via smartphone using B4A. Each Led is controlled by an esp8266 01s via MQTT. When there is a message, an LED blinks. When the next LED is blinking, the current LED is not blinking and lights up normally. But I can't control Timer1.Enabled=False.
Hope your help.
B4R:
Sub Process_Globals
    Public Serial1 As Serial
    Private AStream As AsyncStreams
    Private WiFi As ESP8266WiFi
    Private WiFiStr As WiFiSocket
    Private MQTT As MqttClient
    Private MQTTOpt As MqttConnectOptions
    Private MQTTUser As String = "esp000005"
    Private MQTTPassword As String = "*****302"
    Private MQTTHostName As String ="ssl://test.mosquitto.org"
    Private MQTTPort As Int = 1883
    Private Pin As Pin
    Public Timer1 As Timer
    End Sub

Private Sub AppStart
    Serial1.Initialize(9600)
    Log("AppStart")
    AStream.Initialize(Serial1.Stream, "Astream_NewData", "Astream_Error")
    If WiFi.Connect2("***WIFI","****6302") Then
        Pin.Initialize(3, Pin.MODE_OUTPUT)
        Log("Connected to WiFi, Local IP ", WiFi.LocalIp) 'Else Log("Not Connected to WiFi")
        Dim ClientId As String = Rnd(0, 999999999) 'create a unique id
        MQTT.Initialize2(WiFiStr.stream, MQTTHostName, MQTTPort, ClientId, "MQTT_MessageArrived", "MQTT_Disconnected")
        MQTTOpt.Initialize(MQTTUser, MQTTPassword)
        MQTT_Connect(0)
        Log(MQTTHostName)
    Else
         Log("Not Connected to WiFi")
    End If
End Sub

Sub MQTT_MessageArrived (Topic As String, Payload() As Byte)
    
    Timer1.Initialize("Nhay_stick",500)
    'Timer1.Enabled=False
        Log("Topic = ", Topic, " and Payload = ", Payload)
    Dim BC As ByteConverter
    Log(Topic)
    If Topic = MQTTUser Then
        If BC.StringFromBytes(Payload) = True Then
            Timer1.Enabled=True
            Nhay_stick
        End If
End If
End Sub

Sub MQTT_Connect(Unused As Byte)
    If MQTT.Connect = False Then
        Log("Trying to connect to broker")
        MQTT.Connect2(MQTTOpt)
        CallSubPlus("MQTT_Connect", 1000, 0)
    Else
        Log("Connected to broker")
        MQTT.Subscribe(MQTTUser, 0)
    End If
End Sub

Sub MQTT_Disconnected
    Log("Disconnected")
    MQTT.Close
    MQTT_Connect(0)
End Sub

Sub Astream_NewData (Buffer() As Byte)
    Log("Received: ", Buffer)
    Timer1.Enabled=False
End Sub

Sub AStream_Error
        Log("error")
End Sub

Sub Nhay_stick()
    Pin.Initialize(3, Pin.MODE_INPUT)
    Delay(200)
    Pin.Initialize(3, Pin.MODE_OUTPUT)
    Delay(700)
End Sub
B4A:
ub Process_Globals
    Private MQTT As MqttClient
    Private MQTTPassword As String = "*****302"
    Private MQTTServerURI As String = "tcp://test.mosquitto.org:1883"
    Private BC As ByteConverter
    Private K1 As Boolean=False
    'Dim rst() As String
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private Button1 As Button
    Private lblStatus As Label
    Private SpnTimerMinutes As Spinner
    Private ToggleButton1 As ToggleButton
    Private txtESPName As EditText
    Private btnTat As Button
    Private txtESPs As EditText
    
End Sub


Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
 
    
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub MQTT_Connect(MQTTUser As String)
    Dim ClientId As String = Rnd(0, 999999999) 'create a unique id
    MQTT.Initialize("MQTT", MQTTServerURI,ClientId)
    Dim ConnOpt As MqttConnectOptions
    ConnOpt.Initialize(MQTTUser, MQTTPassword)
    MQTT.Connect2(ConnOpt)
End Sub

Sub MQTT_Connected (Success As Boolean)
    If Success = False Then
        Log(LastException)
        lblStatus.Text = "Error connecting"
    Else
        lblStatus.Text = " - Connected to broker"
        MQTT.Publish2(txtESPName.Text , BC.StringToBytes(K1, "utf8"),0,False)
    End If
End Sub

Private Sub MQTT_Disconnected
    lblStatus.Text = " - Disconnected from broker"
End Sub

Private Sub MQTT_MessageArrived (Topic As String, Payload() As Byte)
End Sub

Private Sub Button1_Click
    K1=True
    CallSub(Me, MQTT_Connect(txtESPName.Text))
End Sub
 

klaus

Expert
Licensed User
Longtime User
What do you mean with: But how to enable Timer1.Enabled=False. ?
Timer1.Enabled = True activates the timer and
Timer1.Enabled = False stops the timer.
It is up to you to decide where you want activtate and stop it.
 
Upvote 0

hvvdl

Member
I understand that problem. I ask in my B4R code that Timer1.Enabled=False is not enabled. Therefore, at a time there can be many LEDs blinking. I want at a time only one Led blink. When one LED blinks, the other LEDs must stop flashing.
Thank you very much for taking the time to help me.
 
Upvote 0

KiloBravo

Active Member
Licensed User
Therefore, at a time there can be many LEDs blinking. I want at a time only one Led blink. When one LED blinks, the other LEDs must stop flashing.

How many LEDS and what PINS are they connected to ?
I only see one PIN in your code to control an LED ? ( Pin.Initialize(3, Pin.MODE_OUTPUT))
 
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Sub Astream_NewData (Buffer() As Byte) Log("Received: ", Buffer) Timer1.Enabled=False End Sub
Did this log "Recieved:" appears? or did you get crash!
I see you log out buffer bytes! try:
B4X:
Sub Astream_NewData (Buffer() As Byte)
    Log("Received: ", BytesToString(Buffer,0,Buffer.length,"UTF-8"))
    Timer1.Enabled=False
End Sub
' OR just comment Log line.
 
Upvote 0

hvvdl

Member
In B4R there is no "BytesToString"
Thank you very much!!
 

Attachments

  • Forum1.jpg
    Forum1.jpg
    65.3 KB · Views: 27
Upvote 0

hvvdl

Member
Therefore, at a time there can be many LEDs blinking. I want at a time only one Led blink. When one LED blinks, the other LEDs must stop flashing.

How many LEDS and what PINS are they connected to ?
I only see one PIN in your code to control an LED ? ( Pin.Initialize(3, Pin.MODE_OUTPUT))
In Project, about 50 esp 01s, same 50 led
Thanks!!
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You need to give more information.
I suppose that the B4X application is controlling all LEDs.
Then you know in the B4X application which LED you sent a message.
And when you send a new message to another LED you need to send a message to the previous one to stop flashing.
That is how I understand your problem.
Otherwise you need to give more precise information on what exactly you want to do.
 
Upvote 0

hvvdl

Member
You understand exactly what problems I am having. Project about 40-50 leds controlled by ESP8266 01s. I want when one Led to blink, I send a message via MQTT to another Led so that it blinks then the LED currently blinking will be off.
I use two MQTT Brokers, one to turn on the blinking of the Led, the other to turn off the blinking. But the ESP 8266 keeps rebooting.
I'm new to B4A, B4R, so my understanding is very little.
We hope to help.
Thank you very much!!!
 

Attachments

  • B4A.zip
    10.2 KB · Views: 26
  • B4R.zip
    1.9 KB · Views: 24
Upvote 0

Laurent95

Active Member
Licensed User
Longtime User
You understand exactly what problems I am having. Project about 40-50 leds controlled by ESP8266 01s. I want when one Led to blink, I send a message via MQTT to another Led so that it blinks then the LED currently blinking will be off.
I use two MQTT Brokers, one to turn on the blinking of the Led, the other to turn off the blinking. But the ESP 8266 keeps rebooting.
I'm new to B4A, B4R, so my understanding is very little.
We hope to help.
Thank you very much!!!
Hello,

Didn't test your code, i've not enough time to do it, and to be honest i don't use more the esp8266 "01" since a while :).
Anyway, you told that you're new and unfortunately what i'll write below is not commented, too often :rolleyes:

If i take a look at your code i saw a timer who each 500ms declare pins to work in input for a short delay, then after, the same pin in output for a short delay too (delays are less than 1 sec), why ?
If it's because the esp8266 "01" have only 1 pin easy accessible ?
-> Then, why not trying to use the serial pins, or the I2C bus pins, to do normal and reverse thing in normal use ? But i think you use I2C bus for your Leds.
With this approach you haven't possibility to debug comfortablment your program. But to be honest have you tried already to do it with an ESP08266 "01" ???
Me never :) Only 1 time with a Led to try to catch informations, never won, even with a led...

At my idea your approach put mess in your code, one of things you must never forget with an esp8266 is that they manage also the Wifi and the 8266 have only one core !
It's why all programs for an esp8266, any model you use, must don't have never any blocking task in the loop.
That to contribute to a normal management of the Wifi (and other things that the MCU must do also at the mean time of course), otherwise you'll have permanent reset by the watchdog of the MCU. Because his incapacity to do these important tasks, the firmwares are like that.

So, to help you, i think you must change for another esp like a WeMos D1 where you have more pins to work or try to change your approach to don't block the MCU, eventually taking the ESP 01 only for Wifi and work on another µC who have no hidden subtask.
If it's for another reason, enough place by example, that will be more difficult to resolve that. But like i wrote above, it's possible to use serial or I2C bus pins on an ESP, even if most of programmers will claim that it's a bad idea ;)

But in any case, i repeat it's important, you must not put any timer with too short delay without have a reset by the watchdog of the MCU.
The best is to don't put a timer anywhere ;)

Hope that could help you, have fun.
Laurent

EDIT: I haven't read all, so sorry to don't payed attention.
But using "50" ESP 01 means you have enough place to use another ESP with more pins, or whatever you want to use

So, maybe the solution is to change for an ESP8266 12E, or better, don't try to drive Led one by one with one ESP by Led, by example take addressable band of Leds . With this type of Leds band, you can addressing each of your leds one by one. Even it's long time i don't do animations on a band of Leds, when i did it all wasn't in same color all time, and on some animations only 1 lighted at a time.

Anyway what i wrote above stay true for any ESP8266.

EDIT2:
Ok, i've a canceled meeting, so, i've reading all after lunch. Then best questions, and some suggesting are :
- Why one µC by Led, it's expensive for nothing, unless you have diabolic prices, or you did your Leds, panel, band, yourself and, important, your leds are not addressables ? ;
-> suggesting : To be honest the best way is to change for an adressable, panel or band, of leds. Some cost less than 5$, not the cost of 50. esp "01" :)
A link for an example of the prices : Price of Leds band
You have already a B4R library to wok with these Leds, called Neopixel or WS2812 : B4R library
- Why using MQTT, in local, there are other solutions on your local Lan with a fixed IP address ?
-> suggesting : Unless you want eat too much size of the memory of the "01", MQTT library isn't included and contribute to make your program too fat.
You have ultra light webserver library to help you for ESP. Even to do a web page on your Lan. But i don't know if it's enable on B4R.
Anyway you can addressing an Arduino library inside B4R with the RunNative command. But you must study how to use it.
 
Last edited:
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Welcome to the forum new member hvvdl. You are in the right place to get help. I would suggest you read the booklets from Klaus, in his signature in post 13 above or here. Everyone needs help sometime, even the experts.

Don't forget, to get the best help: ask a question as clearly as possible. Give any error information. Show as much code as possible.

Firstly, the code below is wrong. No wonder the ESP is resetting! You are initialising the same pin twice in less than 1 second and that via a timer. To turn on an LED use:

LEDPinNumber.DigitalWrite(True) and of course, false to turn off.

Wrong!:
Sub Timer1_Tick()
    Pin.Initialize(3, Pin.MODE_INPUT)
    Delay(200)
    Pin.Initialize(3, Pin.MODE_OUTPUT)
    Delay(700)
End Sub


So you have 50 ESP's, each with one LED. Each is connected to the MQTT broker.
I think you are making things very difficult for yourself. I would suggest:

1. Each ESP becomes an ID, an integer value between 1 and 50
2. You use one broker.
3. The message you send contains ONLY the ID of the LED to turn on (blinking).
4. All ESP's get the message and check if they are the ID being called. If not, then they turn off blinking (even if its not turned on).

5. Remove the timer from the B4R code, you don't need it and it is blocking.
6. Here are some changes, have a look and see if they can be of help (NOT tested).

B4X:
Dim myID as Integer=1
Dim MessageWasForMe as boolean=false
  
Sub MQTT_MessageArrived (Topic As String, Payload() As Byte)
  
    Log("Topic = ", Topic, " and Payload = ", Payload)
    Dim BC As ByteConverter
    Log(Topic)
  
   If BC.StringFromBytes(Payload) = myID Then
       MessageWasForMe=True
   Else
     MessageWasForMe=False
   End If

    Nhay_stick

End Sub


Sub Nhay_stick()
if MessageWasForMe then
    Pin.DigitalWrite(True)    'turn on LED
    Delay(200)
    Pin.DigitalWrite(False)   'turn off LED
    Delay(700)
   Nhay_stick                     'repeat this loop until a new message is received!
Else
  Pin.DigitalWrite(True)    'turn on LED
End If

End Sub
 
Last edited:
Upvote 0

hvvdl

Member
Welcome to the forum new member hvvdl. You are in the right place to get help. I would suggest you read the booklets from Klaus, in his signature in post 13 above or here. Everyone needs help sometime, even the experts.

Don't forget, to get the best help: ask a question as clearly as possible. Give any error information. Show as much code as possible.

Firstly, the code below is wrong. No wonder the ESP is resetting! You are initialising the same pin twice in less than 1 second and that via a timer. To turn on an LED use:

LEDPinNumber.DigitalWrite(True) and of course, false to turn off.

Wrong!:
Sub Timer1_Tick()
    Pin.Initialize(3, Pin.MODE_INPUT)
    Delay(200)
    Pin.Initialize(3, Pin.MODE_OUTPUT)
    Delay(700)
End Sub


So you have 50 ESP's, each with one LED. Each is connected to the MQTT broker.
I think you are making things very difficult for yourself. I would suggest:

1. Each ESP becomes an ID, an integer value between 1 and 50
2. You use one broker.
3. The message you send contains ONLY the ID of the LED to turn on (blinking).
4. All ESP's get the message and check if they are the ID being called. If not, then they turn off blinking (even if its not turned on).

5. Remove the timer from the B4R code, you don't need it and it is blocking.
6. Here are some changes, have a look and see if they can be of help (NOT tested).

B4X:
Dim myID as Integer=1
Dim MessageWasForMe as boolean=false
 
Sub MQTT_MessageArrived (Topic As String, Payload() As Byte)
 
    Log("Topic = ", Topic, " and Payload = ", Payload)
    Dim BC As ByteConverter
    Log(Topic)
 
   If BC.StringFromBytes(Payload) = myID Then
       MessageWasForMe=True
   Else
     MessageWasForMe=False
   End If

    Nhay_stick

End Sub


Sub Nhay_stick()
if MessageWasForMe then
    Pin.DigitalWrite(True)    'turn on LED
    Delay(200)
    Pin.DigitalWrite(False)   'turn off LED
    Delay(700)
   Nhay_stick                     'repeat this loop until a new message is received!
Else
  Pin.DigitalWrite(True)    'turn on LED
End If

End Sub
THanks Mark Read!!!
I I will follow your instructions.
 
Upvote 0

hvvdl

Member
Welcome to the forum new member hvvdl. You are in the right place to get help. I would suggest you read the booklets from Klaus, in his signature in post 13 above or here. Everyone needs help sometime, even the experts.

Don't forget, to get the best help: ask a question as clearly as possible. Give any error information. Show as much code as possible.

Firstly, the code below is wrong. No wonder the ESP is resetting! You are initialising the same pin twice in less than 1 second and that via a timer. To turn on an LED use:

LEDPinNumber.DigitalWrite(True) and of course, false to turn off.

Wrong!:
Sub Timer1_Tick()
    Pin.Initialize(3, Pin.MODE_INPUT)
    Delay(200)
    Pin.Initialize(3, Pin.MODE_OUTPUT)
    Delay(700)
End Sub


So you have 50 ESP's, each with one LED. Each is connected to the MQTT broker.
I think you are making things very difficult for yourself. I would suggest:

1. Each ESP becomes an ID, an integer value between 1 and 50
2. You use one broker.
3. The message you send contains ONLY the ID of the LED to turn on (blinking).
4. All ESP's get the message and check if they are the ID being called. If not, then they turn off blinking (even if its not turned on).

5. Remove the timer from the B4R code, you don't need it and it is blocking.
6. Here are some changes, have a look and see if they can be of help (NOT tested).

B4X:
Dim myID as Integer=1
Dim MessageWasForMe as boolean=false
 
Sub MQTT_MessageArrived (Topic As String, Payload() As Byte)
 
    Log("Topic = ", Topic, " and Payload = ", Payload)
    Dim BC As ByteConverter
    Log(Topic)
 
   If BC.StringFromBytes(Payload) = myID Then
       MessageWasForMe=True
   Else
     MessageWasForMe=False
   End If

    Nhay_stick

End Sub


Sub Nhay_stick()
if MessageWasForMe then
    Pin.DigitalWrite(True)    'turn on LED
    Delay(200)
    Pin.DigitalWrite(False)   'turn off LED
    Delay(700)
   Nhay_stick                     'repeat this loop until a new message is received!
Else
  Pin.DigitalWrite(True)    'turn on LED
End If

End Sub
I will review my problem. Thank you very much.
Welcome to the forum new member hvvdl. You are in the right place to get help. I would suggest you read the booklets from Klaus, in his signature in post 13 above or here. Everyone needs help sometime, even the experts.

Don't forget, to get the best help: ask a question as clearly as possible. Give any error information. Show as much code as possible.

Firstly, the code below is wrong. No wonder the ESP is resetting! You are initialising the same pin twice in less than 1 second and that via a timer. To turn on an LED use:

LEDPinNumber.DigitalWrite(True) and of course, false to turn off.

Wrong!:
Sub Timer1_Tick()
    Pin.Initialize(3, Pin.MODE_INPUT)
    Delay(200)
    Pin.Initialize(3, Pin.MODE_OUTPUT)
    Delay(700)
End Sub


So you have 50 ESP's, each with one LED. Each is connected to the MQTT broker.
I think you are making things very difficult for yourself. I would suggest:

1. Each ESP becomes an ID, an integer value between 1 and 50
2. You use one broker.
3. The message you send contains ONLY the ID of the LED to turn on (blinking).
4. All ESP's get the message and check if they are the ID being called. If not, then they turn off blinking (even if its not turned on).

5. Remove the timer from the B4R code, you don't need it and it is blocking.
6. Here are some changes, have a look and see if they can be of help (NOT tested).

B4X:
Dim myID as Integer=1
Dim MessageWasForMe as boolean=false
 
Sub MQTT_MessageArrived (Topic As String, Payload() As Byte)
 
    Log("Topic = ", Topic, " and Payload = ", Payload)
    Dim BC As ByteConverter
    Log(Topic)
 
   If BC.StringFromBytes(Payload) = myID Then
       MessageWasForMe=True
   Else
     MessageWasForMe=False
   End If

    Nhay_stick

End Sub


Sub Nhay_stick()
if MessageWasForMe then
    Pin.DigitalWrite(True)    'turn on LED
    Delay(200)
    Pin.DigitalWrite(False)   'turn off LED
    Delay(700)
   Nhay_stick                     'repeat this loop until a new message is received!
Else
  Pin.DigitalWrite(True)    'turn on LED
End If

End Sub

Hello,

Didn't test your code, i've not enough time to do it, and to be honest i don't use more the esp8266 "01" since a while :).
Anyway, you told that you're new and unfortunately what i'll write below is not commented, too often :rolleyes:

If i take a look at your code i saw a timer who each 500ms declare pins to work in input for a short delay, then after, the same pin in output for a short delay too (delays are less than 1 sec), why ?
If it's because the esp8266 "01" have only 1 pin easy accessible ?
-> Then, why not trying to use the serial pins, or the I2C bus pins, to do normal and reverse thing in normal use ? But i think you use I2C bus for your Leds.
With this approach you haven't possibility to debug comfortablment your program. But to be honest have you tried already to do it with an ESP08266 "01" ???
Me never :) Only 1 time with a Led to try to catch informations, never won, even with a led...

At my idea your approach put mess in your code, one of things you must never forget with an esp8266 is that they manage also the Wifi and the 8266 have only one core !
It's why all programs for an esp8266, any model you use, must don't have never any blocking task in the loop.
That to contribute to a normal management of the Wifi (and other things that the MCU must do also at the mean time of course), otherwise you'll have permanent reset by the watchdog of the MCU. Because his incapacity to do these important tasks, the firmwares are like that.

So, to help you, i think you must change for another esp like a WeMos D1 where you have more pins to work or try to change your approach to don't block the MCU, eventually taking the ESP 01 only for Wifi and work on another µC who have no hidden subtask.
If it's for another reason, enough place by example, that will be more difficult to resolve that. But like i wrote above, it's possible to use serial or I2C bus pins on an ESP, even if most of programmers will claim that it's a bad idea ;)

But in any case, i repeat it's important, you must not put any timer with too short delay without have a reset by the watchdog of the MCU.
The best is to don't put a timer anywhere ;)

Hope that could help you, have fun.
Laurent

EDIT: I haven't read all, so sorry to don't payed attention.
But using "50" ESP 01 means you have enough place to use another ESP with more pins, or whatever you want to use

So, maybe the solution is to change for an ESP8266 12E, or better, don't try to drive Led one by one with one ESP by Led, by example take addressable band of Leds . With this type of Leds band, you can addressing each of your leds one by one. Even it's long time i don't do animations on a band of Leds, when i did it all wasn't in same color all time, and on some animations only 1 lighted at a time.

Anyway what i wrote above stay true for any ESP8266.

EDIT2:
Ok, i've a canceled meeting, so, i've reading all after lunch. Then best questions, and some suggesting are :
- Why one µC by Led, it's expensive for nothing, unless you have diabolic prices, or you did your Leds, panel, band, yourself and, important, your leds are not addressables ? ;
-> suggesting : To be honest the best way is to change for an adressable, panel or band, of leds. Some cost less than 5$, not the cost of 50. esp "01" :)
A link for an example of the prices : Price of Leds band
You have already a B4R library to wok with these Leds, called Neopixel or WS2812 : B4R library
- Why using MQTT, in local, there are other solutions on your local Lan with a fixed IP address ?
-> suggesting : Unless you want eat too much size of the memory of the "01", MQTT library isn't included and contribute to make your program too fat.
You have ultra light webserver library to help you for ESP. Even to do a web page on your Lan. But i don't know if it's enable on B4R.
Anyway you can addressing an Arduino library inside B4R with the RunNative command. But you must study how to use it.
I will review my problem. Thank you very much.
 
Upvote 0

hvvdl

Member
Welcome to the forum new member hvvdl. You are in the right place to get help. I would suggest you read the booklets from Klaus, in his signature in post 13 above or here. Everyone needs help sometime, even the experts.

Don't forget, to get the best help: ask a question as clearly as possible. Give any error information. Show as much code as possible.

Firstly, the code below is wrong. No wonder the ESP is resetting! You are initialising the same pin twice in less than 1 second and that via a timer. To turn on an LED use:

LEDPinNumber.DigitalWrite(True) and of course, false to turn off.

Wrong!:
Sub Timer1_Tick()
    Pin.Initialize(3, Pin.MODE_INPUT)
    Delay(200)
    Pin.Initialize(3, Pin.MODE_OUTPUT)
    Delay(700)
End Sub


So you have 50 ESP's, each with one LED. Each is connected to the MQTT broker.
I think you are making things very difficult for yourself. I would suggest:

1. Each ESP becomes an ID, an integer value between 1 and 50
2. You use one broker.
3. The message you send contains ONLY the ID of the LED to turn on (blinking).
4. All ESP's get the message and check if they are the ID being called. If not, then they turn off blinking (even if its not turned on).

5. Remove the timer from the B4R code, you don't need it and it is blocking.
6. Here are some changes, have a look and see if they can be of help (NOT tested).

B4X:
Dim myID as Integer=1
Dim MessageWasForMe as boolean=false
 
Sub MQTT_MessageArrived (Topic As String, Payload() As Byte)
 
    Log("Topic = ", Topic, " and Payload = ", Payload)
    Dim BC As ByteConverter
    Log(Topic)
 
   If BC.StringFromBytes(Payload) = myID Then
       MessageWasForMe=True
   Else
     MessageWasForMe=False
   End If

    Nhay_stick

End Sub


Sub Nhay_stick()
if MessageWasForMe then
    Pin.DigitalWrite(True)    'turn on LED
    Delay(200)
    Pin.DigitalWrite(False)   'turn off LED
    Delay(700)
   Nhay_stick                     'repeat this loop until a new message is received!
Else
  Pin.DigitalWrite(True)    'turn on LED
End If

End Sub
Thank you. I have solved my problem.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Good for you! Then I would suggest you tell us the solution and edit the title of this thread and add [solved].
 
Upvote 0

Similar Threads

Top