Sub Process_Globals
Public Serial1 As Serial
Private wifi As ESP8266WiFi
Private mqtt As MqttClient
Private socket As WiFiSocket
Private Timer1 As Timer
Private Led As Pin
End Sub
Private Sub AppStart
Serial1.Initialize(9600)
Log("AppStart")
Timer1.Initialize("Timer1_Tick", 500)
Led.Initialize(3,Led.MODE_OUTPUT)
If wifi.Connect2("WIFISSID", "Password") = False Then
Log("Error connecting to router!")
Return
Else
Log("Conectado a red WIFI")
End If
'IP AS: Byte(192, 168, 0, 1)
mqtt.Initialize(socket.Stream,"192.168.4.2",51042, "esp", "Mqtt_MessageArrived", "Mqtt_Disconnected")
Dim options As MqttConnectOptions
options.Initialize("WIFISSID", "password") 'If your mqtt server don't use user and password, use mqtt.Connect
mqtt.Connect2(options)
Connect(0)
End Sub
Sub Connect(unused As Byte)
If mqtt.Connect = False Then
Log("trying to connect again")
CallSubPlus("Connect", 500, 0)
Return
End If
Log("Connected to broker")
mqtt.Subscribe("esp", 0)
End Sub
Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
Log("Message arrived. Topic=", " payload: ", Payload)
If Topic = "esp" Then
If Payload = "nhay" Then
' Led.DigitalWrite(False)
Timer1.Enabled = True
' Dim b() As Byte = "nhay"
' mqtt.Publish("nhay", b)
' Log("Publicada apertura")
Else
End If
End If
End Sub
Sub Mqtt_Disconnected
Log("Disconnected mqtt")
mqtt.Close
Connect(0)
End Sub
Private Sub Timer1_Tick
Dim currentState As Boolean = Led.DigitalRead
Log("CurrentState: ", currentState)
Dim NewState As Boolean = Not(currentState)
Log("NewState: ", NewState)
Led.DigitalWrite(NewState)
End Sub