#Region Project Notes
'
#End Region
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 400
#End Region
'
Sub Process_Globals
'    Serial   
      Public Serial1 As Serial                'Serial connection for Debug..
    Public Serial2 As SoftwareSerial        'RS232 connection to SM70 Ozone board
'   
    Private bc As ByteConverter
    Private wifi As ESP8266WiFi
    Private WiFiStr As WiFiSocket
      Private server As WiFiServerSocket
      Private astream1 As AsyncStreams
    Private astream2 As AsyncStreams
    Public connected(2) As Boolean
     Private ser As B4RSerializator
'   
    Public id As String    = "1000" ' ID string for this controller to use with MQTT ******* Change this for each controller
'                                    may use MAC address ??
'    My test network
    Private ssid As String = "OBS_Ozone"
    Private pwd As String = "oxyzone1"
'   
    Private D1 As D1Pins
    Private led As Pin
    Private t1 As Timer
'
'MQTT
      Private mqtt As MqttClient
    Private MqttTopicOzone As String = "ozone"   
    Private MqttTopicAlarm As String = "alarm"
    Private mqttTopicRaw As String = "raw"
    Public ppm As Double    'transmitted ppm of ozone.
'
'Sensor
    Private alarm As Pin    'alarm pin from Aeroqual SM70
'Server   
    Private serverIP() As Byte = Array As Byte(192, 168, 10, 128 )
    Private const ServerPort As UInt = 1883    'MQTT Port
'
End Sub
'
Private Sub AppStart
      Serial1.Initialize(115200)
     Log("AppStart")
'   
    t1.Initialize("t1_Tick",1000)
    led.Initialize(D1.D4,led.MODE_OUTPUT)   
    t1.Enabled = True
    '
    If wifi.Connect2(ssid,pwd) Then 'change to your network SSID (use Connect2 if a password is required).
        Log("Connected to wireless network.")
        Log("My ip: ", wifi.LocalIp)
        t1.Enabled = False
        led.DigitalWrite(False)
    Else
        Log("Failed to connect.")
        t1.Enabled = True    ' blink Blue LED if we have a valid connection
        Return
    End If
'   
    server.Initialize(51042, "server_NewConnection")
    server.Listen
'   
    Serial2.Initialize(9600,12,13)    ' Wemos D6(GPIO12) for RX , D7(GPIO13) for TX
    astream2.Initialize(Serial2.Stream, "astream2_NewData", "astream2_Error")
'
    mqtt.Initialize(WiFiStr.Stream, serverIP, ServerPort, id, "Mqtt_MessageArrived", "Mqtt_Disconnected")
'   
    alarm.Initialize(D1.D3,alarm.MODE_INPUT_PULLUP)
'
    MQTTConnect(0)
' 
    alarm.AddListener("alarm_StateChanged")   
 '
End Sub
'Alarm input - alarm is held high by WEMOS internal Pull-Up resistor. Alarm contacts connected to ground when alarm energises
'send alarm signal via MQTT
Sub alarm_StateChanged (state As Boolean)
    If state = False Then    'alarm contacts pull input low (alarm on)
        Dim o As String = "True"
        Dim s As String = JoinStrings(Array As String(id,",", o))
        mqtt.Publish(MqttTopicAlarm,s.GetBytes)
    Else
        Dim o As String = "False"        'alarm contacts open and input goes high via pull-up
        Dim s As String = JoinStrings(Array As String(id,",", o))
        mqtt.Publish(MqttTopicAlarm,s.GetBytes)
    End If
'   
End Sub
'
Sub Server_NewConnection (NewSocket As WiFiSocket)
    Log("Client connected")
    astream1.Initialize(NewSocket.Stream, "astream1_NewData", "astream1_Error")
End Sub
'
Sub AStream1_NewData (Buffer() As Byte)
    Dim be(10) As Object
    Dim data() As Object = ser.ConvertBytesToArray(Buffer, be)
    Log("Received:")
    For Each o As Object In data
        Log(o)
    Next
End Sub
'
Sub AStream1_Error
    Log("Error")
    server.Listen
End Sub
'MQTT
#Region MQTT
Sub MQTTConnect(unused As Byte)
    If mqtt.Connect = False Then
        Log("MQTT: trying to connect again")
         CallSubPlus("MQTTConnect", 1000, 0)
         Return
       End If
       Log("MQTT: Connected to the broker")
'    Subscribe to the 'ozone' topic which is published by the server to confirm temperature received
       mqtt.Subscribe(MqttTopicOzone, 0)
'   
End Sub
'Handle Message arrived
Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
    Log("MQTT: Message arrived. Topic=", Topic, " Payload: ", Payload)
End Sub
'Publish MQTT Message
Sub Mqtt_Publish(aa As String)
    Dim s As String = NumberFormat(ppm,0,2)
    Dim k As String = JoinStrings(Array As String(id,",",s,",",aa))
    mqtt.Publish(MqttTopicOzone,k.GetBytes)
End Sub
'Mqtt Disconnect
Sub Mqtt_Disconnected
    Log("MQTT: Disconnected")
    mqtt.Close
    MQTTConnect(0)            'should reconnect after disconnection        03/02/2019
End Sub
#End Region
'RS232 Serial connection
Sub astream2_newData (Buffer() As Byte)    '
'    *******   
'    This bit is for testing
    Dim a As String = bc.HexFromBytes(Buffer)        'contents of buffer in Hex        =    AA101DFCA43C0000000048006000A5
    mqtt.Publish(mqttTopicRaw,a)
    Log("a = ",a)
    Log("buffer len = ",Buffer.Length)                'len of buffer
    Log (bc.SubString2(a,4,12))                        'get bytes 3 to 6 from string    =     1DFCA43C
'    ********
'    If connected(0) Then            ' if there is a WIFI connection then write the buffer out to connected B4J or B4A apps
'        astream(0).write(Buffer)
'    End If
'
'    Position
'        0            AA = Sensor type    (1 byte)
'        1            10 =                 (1 byte)
'        2            1DFCA43C = Reading    (4 bytes) - little Endian - Hex bit backwards
'        6            Reserved            (6 Bytes)
'        12            Status                (1 Byte)    -b1b0 = 00 Sensor OK, 01 = sensor failing, 11 = sensor ageing - Bit 7 - 2 not used..
'        13            Reserved
'        14            Check sum
'   
'    Get the status of the sensor
    Dim s(1) As Byte
    bc.ArrayCopy2(Buffer,12,s,0,1)
'    Log("s = ",s(0))
    Dim res(8) As Byte
    ByteToBinary(s(0), res)
'    Log("sensor = ",res)
    Dim sen As String = bc.StringFromBytes(res)
'    Log("sen = ",sen)
    Dim z() As Byte = bc.SubString(sen,6)
    Log("sensor status = ",z)                'z = the sensor status "00" = OK, "01" = sensor failing, "11" = sensor aging
    Dim t As String = bc.StringFromBytes(z)
'
'    Dim Buffer() As Byte = bc.HexToBytes("AA101DFCA43C0000000048006000A5") 'you don't need this line
    Dim b(4) As Byte
    bc.ArrayCopy2(Buffer, 2, b, 0, 4)
    Dim d() As Double = bc.DoublesFromBytes(b)
'    Dim ppmOZ As String = NumberFormat(d(0),1,2)
    Log("ppm = ",d(0))                        'd(0) is the current ppm reading from the sensor
    ppm = d(0)
'
    Mqtt_Publish(t)
'
    
End Sub
'
Sub astream2_Error
    Log("Serial 2 - No Connection")
    mqtt.Publish(mqttTopicRaw,"Astream2 error")
End Sub
'
Sub ByteToBinary (Number As Byte, Result() As Byte)
    For i = 0 To 7
        If Bit.Get(Number, 7 - i) = 1 Then
            Result(i) = Asc("1")
        Else
            Result(i) = Asc("0")
        End If
    Next
End Sub
Sub t1_tick
    'turn led on and off every second to show its waiting for a connection
    Dim currentState As Boolean = led.DigitalRead
    '   Log("CurrentState: ", currentState)
    Dim NewState As Boolean = Not(currentState)
    '   Log("NewState: ", NewState)
    led.DigitalWrite(NewState)
End Sub