#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 1000
#End Region
Sub Process_Globals
Public Serial1 As Serial
Private wifi As ESP8266WiFi
Private bc As ByteConverter
Private raf As RandomAccessFile
Private API_KEY() As Byte = "AIzaSy..........."
Private const SSID As String = "NETWORK-SSID"
Private const PASS As String = "NETWORK-PASS"
Type KeyValuePair(key() As Byte, val() As Byte)
Public field(30) As KeyValuePair ' <<<<<<<<---------- INCREASE THIS NUMBER TO USE MORE CUSTOM FIELDS
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log(" ") : Log(" ") : Log("AppStart") : Log(" ")
Log("Connecting to ", SSID)
' If wifi.Connect(SSID) Then 'change as needed
If Not(wifi.Connect2(SSID, PASS)) Then
Log("Failed to connect")
Return
End If
Log("Connected to Network")
'send the android message
SendMessage("general", "ESP8266", "This is the title", "This is the message") 'send Android message
End Sub
Sub JobDone (jr As JobResult)
Log("JobDone: ", jr.JobName)
Log("Success: ", jr.Success)
If jr.JobName = "ios_general" Then
SendMessage("ios_general", "ESP8266", "This is the title", "This is the message") 'send the ios message (cambiare il nome del topic in ios_general )
End If
End Sub
Private Sub SendMessage(Topic() As Byte, From() As Byte, Title() As Byte, Message() As Byte)
HttpJob.Initialize(bc.StringFromBytes(Topic))
Dim buffer(800) As Byte 'must be large enough to hold the message payload
field(0).key = "from" : field(0).val = From
' field(1).Key = "to" : field(1).val = tokens(0) 'RegID
field(2).key = "title" : field(2).val = Title
field(3).key = "message" : field(3).val = Message
raf.Initialize(buffer, True)
WriteBytes(raf, JoinBytes(Array("{""data"":{""", field(0).key, """:""")))
WriteBytes(raf, field(0).val)
' WriteBytes(raf, "{""data"":{""title"":""")
WriteBytes(raf, JoinBytes(Array(""":{""", field(2).key, """:""")))
WriteBytes(raf, field(2).val)
' WriteBytes(raf, """,""body"":""")
WriteBytes(raf, JoinBytes(Array(""",""", field(3).key, """:""")))
WriteBytes(raf, field(3).val)
WriteBytes(raf, """}")
'end of data
WriteBytes(raf, ",""to"":""\/topics\/")
WriteBytes(raf, Topic)
WriteBytes(raf, """")
WriteBytes(raf, ",""priority"": 10")
If bc.StartsWith(Topic, "ios_") Then
WriteBytes(raf, ",""notification"": {""title"": """)
WriteBytes(raf, field(2).val)
WriteBytes(raf, """,""body"":""")
WriteBytes(raf, field(3).val)
WriteBytes(raf, """, ""sound"": ""default""}")
End If
WriteBytes(raf, "}")
Log("REQUEST BODY:")
Log(bc.StringFromBytes(raf.ReadBytes2(raf.CurrentPosition, 0)))
HttpJob.AddHeader("Authorization", JoinBytes(Array("key=".GetBytes, API_KEY)))
HttpJob.AddHeader("Content-Type", "application/json")
Log("stack: ", StackBufferUsage, ", buffer size:", raf.CurrentPosition)
HttpJob.Post("https://fcm.googleapis.com/fcm/send", bc.SubString2(buffer, 0, raf.CurrentPosition))
End Sub
Private Sub WriteBytes(r As RandomAccessFile, Data() As Byte)
r.WriteBytes(Data, 0, Data.Length, raf.CurrentPosition)
End Sub