I have tryed this code but I don't understand why I can't send more than one SendMessage?
When input #14 is low TDelay is started and after 40 sec. the TDelay_Tick Sub is executed and my message is sent to fcm.googleapis.com/fcm/send and I get this Log.
Tidsfördröjningen är aktivrad! = "delay activated"
stack: 316, buffer size:145
trying to connect to: fcm.googleapis.com port: 443 ssl: 1
connected: fcm.googleapis.com
HTTP/1.0 200 OK
Content-Type: application/json; charset=UTF-8
Date: Sat, 21 Dec 2019 21:13:45 GMT
Expires: Sat, 21 Dec 2019 21:13:45 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options:
nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alt-Svc: quic=":
443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma
=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000
Accept-Ranges: none
Vary: Accept-En
coding
{"message_id":1217586609119534674}
JobDone: general
Success: 1
Everything is ok but now is my Timer1 enabled and when Timer1_Tick Sub is executed my SendMessage is not sent to firebase? I got this Log.
stack: 316, buffer size:148
trying to connect to: fcm.googleapis.com port: 443 ssl: 1
JobDone: general
Success: 0
Why can't this message be sent to firebase? If I let the program run an hour or so I get the message 3-4 times but I expected this message every 5 minutes. And stop when input is going to high "1" when everything is stopped.
Is it not ok to have SendMessage in timer event?
What am I doing wrong?
Thanks for considering my requst.
When input #14 is low TDelay is started and after 40 sec. the TDelay_Tick Sub is executed and my message is sent to fcm.googleapis.com/fcm/send and I get this Log.
Tidsfördröjningen är aktivrad! = "delay activated"
stack: 316, buffer size:145
trying to connect to: fcm.googleapis.com port: 443 ssl: 1
connected: fcm.googleapis.com
HTTP/1.0 200 OK
Content-Type: application/json; charset=UTF-8
Date: Sat, 21 Dec 2019 21:13:45 GMT
Expires: Sat, 21 Dec 2019 21:13:45 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options:
nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alt-Svc: quic=":
443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma
=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000
Accept-Ranges: none
Vary: Accept-En
coding
{"message_id":1217586609119534674}
JobDone: general
Success: 1
Everything is ok but now is my Timer1 enabled and when Timer1_Tick Sub is executed my SendMessage is not sent to firebase? I got this Log.
stack: 316, buffer size:148
trying to connect to: fcm.googleapis.com port: 443 ssl: 1
JobDone: general
Success: 0
Why can't this message be sent to firebase? If I let the program run an hour or so I get the message 3-4 times but I expected this message every 5 minutes. And stop when input is going to high "1" when everything is stopped.
Is it not ok to have SendMessage in timer event?
What am I doing wrong?
Thanks for considering my requst.
B4X:
'Åke Artklo Willo kyltork. 2019-12-17
'Modifierad med ingångsfördröjning 2019-12-19.
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 2500
#End Region
' static const Byte D0 = 16;
' static const Byte D1 = 5;
' static const Byte D2 = 4;
' static const Byte D3 = 0;
' static const Byte D4 = 2;
' static const Byte D5 = 14;
' static const Byte D6 = 12;
' static const Byte D7 = 13;
' static const Byte D8 = 15;
' static const Byte RX = 3;
' static const Byte TX = 1;
Sub Process_Globals
Public Serial1 As Serial
Private inP As D1Pins
Private D5 As Pin
Public inPState As Boolean = True
Private TDummy As Timer
Public Timer1 As Timer
Public TDelay As Timer
Public TOffDelay As Timer
Public TDelay_Elapsed As Boolean = False
Public Timer1_State As Boolean = False
Public TDelay_State As Boolean = False
Public TOffDelay_State As Boolean = False
Private API_KEY() As Byte = "AAAA2S"
Private wifi As ESP8266WiFi
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
If wifi.Connect2("router","password") Then 'change as needed
Log("Connected to router.")
Else
Log("Failed to connect to router!!!")
Return
End If
Log("AppStart")
Timer1.Initialize("Timer1_Tick", 300000) ' Skickas var 5:e minut!!
Timer1.Enabled = False
TDelay.Initialize("TDelay_Tick", 40000) ' Fördröjning innan larm skickas.
TDelay.Enabled = False
TOffDelay.Initialize("TOffDelay_Tick", 40000) 'Fördröjning avstängning larm.
TOffDelay.Enabled = False
TDummy.Initialize("TDummy_Tick", 30000) 'Timer för att komma förbi timer stopp workaround.
TDummy.Enabled = True
D5.Initialize(inP.D5, D5.MODE_INPUT_PULLUP)
D5.AddListener("inP_StateChanged")
Log("send the android message when input #14 is aktivated.")
'SendMessage("general", "This is the title", "This is the body")
'SendMessage("general", "AppStart!!", "Sent the android message!!!")
End Sub
Sub JobDone (jr As JobResult)
Log("JobDone: ", jr.JobName)
Log("Success: ", jr.Success)
If jr.JobName = "ios_general" Then
'send the ios message
SendMessage("ios_general", "This is the title", "This is the body")
End If
End Sub
Private Sub SendMessage(Topic() As Byte, Title() As Byte, Body() As Byte)
Dim bc As ByteConverter
HttpJob.Initialize(bc.StringFromBytes(Topic))
Dim buffer(200) As Byte 'must be large enough to hold the message payload
Dim raf As RandomAccessFile
raf.Initialize(buffer, True)
WriteBytes(raf, "{""data"":{""title"":""")
WriteBytes(raf, Title)
WriteBytes(raf, """,""body"":""")
WriteBytes(raf, Body)
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, Title)
WriteBytes(raf, """,""body"":""")
WriteBytes(raf, Body)
WriteBytes(raf, """, ""sound"": ""default""}")
End If
WriteBytes(raf, "}")
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(raf As RandomAccessFile, Data() As Byte)
raf.WriteBytes(Data, 0, Data.Length, raf.CurrentPosition)
End Sub
Sub inP_StateChanged (State As Boolean)
Log("inP_StateChanged = ", State)
If State = False Then 'Ingången aktiverad "LÅG"
Log("Tidsfördröjningen är aktivrad!")
TDelay.Enabled = True
TOffDelay.Enabled = False
Else
'Ingången opåverkad "HÖG"
If TDelay_Elapsed = True Then ' Tidsfördröjning har gått ut. Vi har en signal som är längre än TDelay är satt till.
Log("Larmet är avstängt avstängning fördröjning är aktiverad!!")
'SendMessage("general", "Willo Maskin SUMMALARM kyltork !", "Summalarm AVSTÄNGT kyltorken är NORMAL")
TDelay_Elapsed = False
Timer1.Enabled = False
TOffDelay.Enabled = True
Else 'Tidsfördröjningen har inte gått full tid så det är troligen felsignal stäng av signalfördröjningen.
Log("Tidsfördröjningen avstängd och inget larm skickas. 'Störning!'")
TDelay.Enabled = False
End If
End If
End Sub
Sub TDelay_Tick
TOffDelay_State = False
TDelay.Enabled = False
TDelay_Elapsed = True
Timer1.Enabled = True
Log("Tidsfördröjningen har gått larm skickas!")
TDelay_State=True
SendMessage("general", "Willo Maskin summalarm 0:AKTIVT kyltork !!", "FEL! Kyltorken L A R M A R !!!")
End Sub
Sub Timer1_Tick
Log("Timer1 fördröjningen mellan aktiva larm aktiverad!")
Timer1_State=True
SendMessage("general", "Willo Maskin summalarm 1:AKTIVT kyltork loop!", "FEL! Kyltorken L A R M A R !!!")
End Sub
Sub TOffDelay_Tick
Log("TOffDelay fördröjningen aktivtlarm avstängt!")
TOffDelay_State = True
SendMessage("general", "Willo Maskin SUMMALARM kyltork off!", "Summalarm AVSTÄNGT kyltorken är NORMAL")
Timer1_State = False
TDelay_State = False
TOffDelay.Enabled = False
End Sub
Sub TDummy_Tick
Log(" ")
End Sub
code