Sub Process_Globals
Public Serial1 As Serial
Dim R,G,B,ledno,RED,BLUE,GREEN As Int
Dim indicator As Pin
Dim d1pins As Wemos_D1_R2_Pins
Private client As WiFiSocket
Private wifi As ESP8266WiFi
Private bc As ByteConverter
Private mqtt As MqttClient
Private serverIp() As Byte = Array As Byte(192, 168, 43, 1)
Private const serverPort As UInt = 51042
Dim TIMER1 As Timer
Dim TIMER2 As Timer
Dim I As Int
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
indicator.Initialize(d1pins.D4,indicator.MODE_OUTPUT)
TIMER1.Initialize("TIMER1_Tick",15000)
TIMER2.Initialize("TIMER2_Tick",30000)
TIMER1.Enabled=True
Log("AppStart")
indicator.DigitalWrite(True)
RunNative("setup",Null)
If wifi.Connect2("ATSYS","******") Then
client.ConnectIP(serverIp,serverPort)
Log("Waiting for connection.")
Log("My ip: ", wifi.LocalIp)
Else
Log("Failed to connect to Wifi.")
End If
mqtt.Initialize(client.Stream, serverIp, serverPort, "ESP", "Mqtt_MessageArrived", "Mqtt_Disconnected")
Connect(0)
End Sub
Sub Connect(unused As Byte)
If mqtt.Connect = False Then
Log("trying to connect again")
CallSubPlus("Connect", 1000, 0)
indicator.DigitalWrite(True)
TIMER1.Enabled=True
Return
End If
Log("Connected to broker")
mqtt.Subscribe("COLOR", 0)
indicator.DigitalWrite(False)
End Sub
Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
Log("Message arrived. Topic=", Topic, " payload: ", Payload)
If Topic="COLOR" Then
RED=bc.StringFromBytes(bc.SubString2(Payload,0,3))
GREEN=bc.StringFromBytes(bc.SubString2(Payload,3,6))
BLUE=bc.StringFromBytes(bc.SubString2(Payload,6,9))
Log(RED)
Log(GREEN)
Log(BLUE)
For j=0 To 63
led_data(j,RED,GREEN,BLUE)
Next
End If
TIMER1.Enabled=False
TIMER2.Enabled=True
End Sub
Sub Mqtt_Disconnected
Log("Disconnected")
mqtt.Close
Connect(0)
End Sub
Sub led_data(n As Int,r1 As Int,g1 As Int, b1 As Int)
R=r1
G=g1
B=b1
ledno=n
RunNative("show",Null)
End Sub
Sub TIMER1_Tick
Log("OK")
I=Rnd(0,9)
Select I
Case 0
RED=255
GREEN=0
BLUE=0
For j=0 To 63
led_data(j,RED,GREEN,BLUE)
Next
Case 1
RED=0
GREEN=255
BLUE=0
For j=0 To 63
led_data(j,RED,GREEN,BLUE)
Next
Case 2
RED=0
GREEN=0
BLUE=255
For j=0 To 63
led_data(j,RED,GREEN,BLUE)
Next
Case 3
RED=255
GREEN=255
BLUE=0
For j=0 To 63
led_data(j,RED,GREEN,BLUE)
Next
Case 4
RED=255
GREEN=0
BLUE=255
For j=0 To 63
led_data(j,RED,GREEN,BLUE)
Next
Case 5
RED=255
GREEN=0
BLUE=0
For j=0 To 63
led_data(j,RED,GREEN,BLUE)
Next
Case 6
RED=0
GREEN=255
BLUE=255
For j=0 To 63
led_data(j,RED,GREEN,BLUE)
Next
Case 7
RED=142
GREEN=0
BLUE=255
For j=0 To 63
led_data(j,RED,GREEN,BLUE)
Next
Case 8
RED=123
GREEN=255
BLUE=123
For j=0 To 63
led_data(j,RED,GREEN,BLUE)
Next
Case 9
RED=255
GREEN=255
BLUE=255
For j=0 To 63
led_data(j,RED,GREEN,BLUE)
Next
End Select
End Sub
Sub TIMER2_Tick
TIMER1.Enabled=True
TIMER2.Enabled=False
End Sub
#if C
#include <Adafruit_NeoPixel.h>
#define PIN D6
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(64, PIN, NEO_GRB + NEO_KHZ800);
void setup(B4R::Object* o){
wifi_station_set_auto_connect(true);
pixels.setBrightness(35);
pixels.begin();
}
void show(B4R::Object* o) {
pixels.setPixelColor(b4r_main::_ledno,pixels.Color(b4r_main::_r, b4r_main::_g,b4r_main::_b));
// pixels.setPixelColor(0, pixels.Color(255,0,0));
pixels.show();
}
#End if