As a followup to this thread and Erel's answer, I have stripped out all the code and think I have found the problem:
I am trying to send data to thingspeak but I cannot seem to get the code right. After two days, I think I need some help. The code is included, as is the API Key to try. The data is posted to a test channel so you can try. Only the WiFi credentials will need to be changed.
There is no real data at present, just 6 random numbers.
Thanks for any help.
Here is the log:
I am trying to send data to thingspeak but I cannot seem to get the code right. After two days, I think I need some help. The code is included, as is the API Key to try. The data is posted to a test channel so you can try. Only the WiFi credentials will need to be changed.
There is no real data at present, just 6 random numbers.
Thanks for any help.
Here is the log:
disconnecting Wifi
Connected successfully to: READ-Wlan
192.168.2.8
Connecting ...
Posting ...
api_key=28PCNUKAEZD2XD2J&field1=10.00&field2=14.00&field3=13.00&field4=12.00&field5=11.00&field6=13.00
trying to connect to: api.thingspeak.com port: 80 ssl: 0
connected: api.thingspeak.com
Connected successfully to: READ-Wlan
192.168.2.8
Connecting ...
Posting ...
api_key=28PCNUKAEZD2XD2J&field1=10.00&field2=14.00&field3=13.00&field4=12.00&field5=11.00&field6=13.00
trying to connect to: api.thingspeak.com port: 80 ssl: 0
connected: api.thingspeak.com
B4X:
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src
'Hardware: Wemos D1 Mini
'Libraries: rCore, rESP8266WiFi, rHTTPUtils2, rRandomAccessFile
Sub Process_Globals
Public Serial1 As Serial
Public WiFi As ESP8266WiFi
Public ApiKey As String="28PCNUKAEZD2XD2J"
Public WifiSSID As String="xxxxxxxxxxxx"
Public WifiPass As String="xxxxxxxxxxxx"
Public ThinkspeakServer As String="api.thingspeak.com"
Public TEMPERATURE, PRESSURE, HUMIDITY As Double
Public HeatIndex, DewPoint, Altitude As Double
Public BC As ByteConverter
Public DelayTimer As Timer
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
DelayTimer.Initialize("DelayTimer_Tick", 15000) '10 mins = 600000
DelayTimer.Enabled=True
End Sub
Sub DelayTimer_Tick
If WiFi.IsConnected=False Then
ConnectToNetwork
End If
TEMPERATURE=Rnd(10,15)
PRESSURE=Rnd(10,15)
HUMIDITY=Rnd(10,15)
HeatIndex=Rnd(10,15)
DewPoint=Rnd(10,15)
Altitude=Rnd(10,15)
UploadThinkgspeak
' disconnect wifi
WiFi.Disconnect
Log("Going to sleep ...")
End Sub
Sub UploadThinkgspeak
' https://api.thingspeak.com/update?api_key=28PCNUKAEZD2XD2J&field1=14.00field2=12.00&field3=13.00&field4=12.00&field5=14.00&field6=13.00
Log("Connecting ...")
HttpJob.Initialize("Thingspeak")
'add headers before calling Post or Download (this is different than the standard HttpUtils2 library).
HttpJob.AddHeader("Content-Type", "application/x-www-form-urlencoded")
Dim postStr As String
'postStr =JoinStrings(Array As String("/update?api_key=",ApiKey,"&field1=",TEMPERATURE,"field2=",PRESSURE,"&field3=",HUMIDITY,"&field4=",HeatIndex,"&field5=",DewPoint,"&field6=",Altitude))
postStr =JoinStrings(Array As String("api_key=",ApiKey,"&field1=",TEMPERATURE,"&field2=",PRESSURE,"&field3=",HUMIDITY,"&field4=",HeatIndex,"&field5=",DewPoint,"&field6=",Altitude))
Log("Posting ...")
Log(postStr)
HttpJob.Post("http://api.thingspeak.com/update", postStr)
End Sub
Sub JobDone (Job As JobResult)
If Job.Success Then
Log("Response: ", BC.SubString2(Job.Response, 0, Min(100, Job.Response.Length))) 'truncate to 100 characters
Else
Log("*******************************")
Log("JobName: ", Job.JobName)
Log("ErrorMessage: ", Job.ErrorMessage)
Log("Status: ", Job.Status)
Log(Job.Response)
End If
End Sub
Sub ConnectToNetwork
Log("disconnecting Wifi")
WiFi.Disconnect
If WiFi.Connect2(WifiSSID, WifiPass) Then
Log("Connected successfully to: ", WifiSSID)
Log(WiFi.LocalIp)
Else
Log("Failed to connect.")
End If
End Sub