Sub Process_Globals
Public Serial1 As Serial
Public Wifi As ESP8266WiFi
Public esp As ESP8266
Public eeprom As EEPROM
Private const MAGIC_EEPROM As Byte = 213
Private sr As B4RSerializator
Private bc As ByteConverter
Public mqtt As MqttClient
Private mqttSocket As WiFiSocket
Private MQTTOpt As MqttConnectOptions
Private d1 As D1Pins
Private PinGPIO05 As Pin
Private PinGPIO04 As Pin
Private PinGPIO00 As Pin
Private PinGPIO15 As Pin
Private PinGPIO13 As Pin
Private PinGPIO12 As Pin
Private PinGPIO14 As Pin
Private PinGPIO16 As Pin
Private bc As ByteConverter
Private State As Boolean
Public Timer1 As Timer
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
PinGPIO05.Initialize(d1.D1,PinGPIO05.MODE_OUTPUT)'Pin D2 ESP Relay 1
PinGPIO04.Initialize(d1.D2,PinGPIO04.MODE_OUTPUT)'Pin D5 ESP Relay 2
PinGPIO00.Initialize(d1.D3,PinGPIO00.MODE_OUTPUT)'Pin D6 ESP Relay 3
PinGPIO15.Initialize(d1.D8,PinGPIO15.MODE_OUTPUT)'Pin D7 ESP Relay 4
PinGPIO13.Initialize(d1.D7,PinGPIO13.MODE_OUTPUT)'Pin D2 ESP Relay 5
PinGPIO12.Initialize(d1.D6,PinGPIO12.MODE_OUTPUT)'Pin D5 ESP Relay 6
PinGPIO14.Initialize(d1.D5,PinGPIO14.MODE_OUTPUT)'Pin D6 ESP Relay 7
PinGPIO16.Initialize(d1.D0,PinGPIO16.MODE_OUTPUT)'Pin D7 ESP Relay 8
'Set Desligar Pinos ESP
PinGPIO05.DigitalWrite(False)
PinGPIO04.DigitalWrite(False)
PinGPIO00.DigitalWrite(False)
PinGPIO15.DigitalWrite(False)
PinGPIO13.DigitalWrite(False)
PinGPIO12.DigitalWrite(False)
PinGPIO14.DigitalWrite(False)
PinGPIO16.DigitalWrite(False)
WiFiServer.Start
ConnectToNetwork
Timer1.Initialize("Temp",1000)
Timer1.Enabled=True
End Sub
Public Sub ConnectToNetwork
Wifi.Disconnect
If GetStoredData = 1 Then
'read settings from EEPROM
Dim SSID As String = bc.StringFromBytes(GStore1.slot0) 'ignore
Dim Password As String = bc.StringFromBytes(GStore1.slot1) 'ignore" '
Log("Tentando se Conectar a: ", SSID, " Senha: ", Password)
If Wifi.Connect2(SSID,Password) Then
Log("Conectado com Sucesso : ", GStore1.slot0)
Log("IP Local: ", Wifi.LocalIp)
StopAP
ConnectToMqtt
Else
Log("WIFI Falha ao Se Conectar - Iniciar WifiServer ")
WiFiServer.Start
End If
Else
Log("Dados de Rede não Encontrados .")
WiFiServer.Start
End If
End Sub
Public Sub SaveNetworkDetails(Data() As Byte)
Log("Salvando dados de Rede")
Log("Comprimento de Dados : ", Data.Length)
Log("Memoria=", AvailableRAM)
Log("Tamanho :",StackBufferUsage)
eeprom.WriteBytes(Array As Byte(MAGIC_EEPROM, Data.Length), 0)
eeprom.WriteBytes(Data, 2)
ConnectToNetwork
End Sub
Private Sub ConnectToMqtt
mqtt.Initialize2(mqttSocket.Stream, bc.StringFromBytes(GStore1.slot2),bc.StringFromBytes(GStore1.slot3), bc.StringFromBytes(GStore1.slot4), "mqtt_MessageArrived", "mqtt_Disconnected") 'ignore
MQTTOpt.Initialize(bc.StringFromBytes(GStore1.Slot5),bc.StringFromBytes(GStore1.slot6))
If mqtt.Connect2(MQTTOpt) = False Then
Log("Erro de Conexão com o Broker")
Else
Log("Nova Conexão Com o broker OK")
'Subscribe to topic = to B4J/B4A/B4i/B4R/WebApp/Html......
mqtt.Subscribe("Control_Rele8/#",1) '<---Mude o nome do tópico!
mqtt.Subscribe("Rele_A",1)' <---Mude o nome do tópico!
mqtt.Subscribe("Rele_B",1)' <---Mude o nome do tópico!
mqtt.Subscribe("Rele_C",1)' <---Mude o nome do tópico!
mqtt.Subscribe("Rele_D",1)' <---Mude o nome do tópico!
mqtt.Subscribe("Rele_E",1)' <---Mude o nome do tópico!
mqtt.Subscribe("Rele_F",1)' <---Mude o nome do tópico!
mqtt.Subscribe("Rele_G",1)' <---Mude o nome do tópico!
mqtt.Subscribe("Rele_H",1)' <---Mude o nome do tópico!
End If
End Sub
Sub mqtt_MessageArrived(Topic As String, payload()As Byte)
If bc.StringFromBytes(payload) = "true" Then State = True Else State = False
Log(Topic," ",Not(State))
Select Case Topic
Case "Rele_A"
PinGPIO05.DigitalWrite(Not(State))'Relay 1
Case "Rele_B"
PinGPIO04.DigitalWrite(Not(State))'Relay 2
Case "Rele_C"
PinGPIO00.DigitalWrite(Not(State))'Relay 3
Case "Rele_D"
PinGPIO15.DigitalWrite(Not(State))'Relay 4
Case "Rele_E"
PinGPIO13.DigitalWrite(Not(State))'Relay 5
Case "Rele_F"
PinGPIO12.DigitalWrite(Not(State))'Relay 6
Case "Rele_G"
PinGPIO14.DigitalWrite(Not(State))'Relay 7
Case "Rele_H"
PinGPIO16.DigitalWrite(True) '(Not(State))'Relay 8
CallSubPlus("Rele_Portao",1000, 1)'---> Sub
End Select
End Sub
'Motor Garage impulse OFF
Sub Rele_Portao(Tag As Byte)
PinGPIO16.DigitalWrite(False)'Relay 4 OFF Motor Garage
End Sub
Sub Temp
Dim TempESP32 As String = 23 'Graus teste
mqtt.Publish("temperature",bc.StringToBytes(TempESP32))
Log("Temperature ",TempESP32)
End Sub
public Sub mqtt_Disconnected
Log("MQTT_Disconnected")
End Sub
Public Sub GetStoredDataLength As Byte
Dim header() As Byte = eeprom.ReadBytes(0, 2)
If header(0) = MAGIC_EEPROM Then
Return header(1)
End If
Return 0
End Sub
Sub GetStoredData As Byte
Dim length As Byte = GetStoredDataLength
Log("Parâmetros do Wifi Salvo na Eeprom =",length)
If length > 0 Then
Dim ObjectsBuffer(8) As Object
Dim Data() As Byte = eeprom.ReadBytes(2, length)
Dim Objects() As Object = sr.ConvertBytesToArray(Data, ObjectsBuffer)
If Objects.Length <> 0 Then
For i = 0 To 7
GStore1.Put(i,Objects(i))
Next
Return 1
Else
Return 0
End If
Else
Return 0
End If
End Sub
Public Sub ClearStoredDataLength
Dim header() As Byte = eeprom.ReadBytes(0, 2)
If header(0) = MAGIC_EEPROM Then
header(1) = 0
eeprom.WriteBytes(header,0)
Log("Eeprom Apagada")
End If
GStore1.Slot0(" ")
GStore1.Slot1(" ")
Delay(100)
esp.restart
End Sub
public Sub StopAP
Log("Stop AP")
RunNative("stopAP", Null)
End Sub
#if C
#include <ESP8266WiFi.h>
void stopAP (B4R::Object* u) {
WiFi.softAPdisconnect(1);
}
#end if