B4R Question How to Send Data to webpage using NodeMCU

Status
Not open for further replies.

AndroidMadhu

Active Member
Licensed User
Hello,

I am able to connect with wifi router successfully using nodeMCU.
But now I want to send data [received from Arduino] to Webpage [www.mypage.com].
At mypage.com domain, mysql and php script is running, upon receive the data from nodeMCu, php script will insert the data to mysql.

I am not able to found any suitable link for the same. Please advice on this please....
 

AndroidMadhu

Active Member
Licensed User
@Erel,
Thank you for this hint.. It is very helpful. However I have one confusion....
In main module there is one sub JobDone, where I found one line
B4X:
HttpJob.Post("http://www.b4x.com/print.php?key1=value1", "PostKey1=PostValue2&abc=def")
May be I am raising my question like novice, but I am really not able to understand the last portion
B4X:
"PostKey1=PostValue2&abc=def"
Could you please help me to understand this portion.
 

AndroidMadhu

Active Member
Licensed User
I have incorporated as per the above post. But no Log is coming...

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Public wifi As ESP8266WiFi
    Public bc As ByteConverter
    Private timer1 As Timer
    
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    timer1.Initialize("timer1_Tick",1000)
    timer1.Enabled=True
    Log("AppStart")
    scanNetworks
    If wifi.Connect2("MADHU","XXXXXX") Then
        Log("Connected Successfully!!")
        Log("My IP : ",wifi.LocalIp)
    Else
        Log("Connection Failed!!!")
    End If
    Return
    HttpJob.Initialize("Example")
End Sub

Private Sub timer1_Tick
End Sub
Private Sub scanNetworks
    Dim numberofnetwork As Byte = wifi.Scan
    Log("Found: ", numberofnetwork, " networks.")
    For i = 0 To numberofnetwork - 1
        Log(wifi.ScannedSSID(i))
        Log(wifi.LocalIp)
    Next
End Sub

Sub JobDone(Job As JobResult)
    'initialize the job
    If Job.JobName="Example" Then
        HttpJob.AddHeader("Content-Type", "application/x-www-form-urlencoded")
        HttpJob.Post("http://iot.mypage.com/addTemperature?temperature=80","PostKey1=PostValue2&abc=def")
    End If
    Log("Status :",Job.Status)
    Log("Response :",Job.Response)
    
End Sub
Please advice ....
 

AndroidMadhu

Active Member
Licensed User
I commented the sub scannetworks.

But still I am not able to find any logs relate to response or Status etc
B4X:
Log("Status :",job.Status)
Log("Response :",job.Response)
? Is the sub "JobDone" not running? I am not able to find any clue ...

Please advice....
 

AndroidMadhu

Active Member
Licensed User
Run the example as-is (only change the wifi credentials). Does it work?
I have performed two steps:
1. Configured Thingspeak. When I am running the below link at the browser, I am able to see the value.
GET https://api.thingspeak.com/update?api_key=XXXXXXXXX&field1=20

2. Now when I am trying to run it from B4R ... Below error I am getting...

B4X:
JobName: Example
ErrorMessage: Failed to connect
Status: 0
Below is the code I ran As is and only changed the below line..

B4X:
Sub JobDone (Job As JobResult)
    Log("*******************************")
    Log("JobName: ", Job.JobName)
    If Job.Success Then
        Dim bc As ByteConverter
        Log("Response: ", bc.SubString2(Job.Response, 0, Min(200, Job.Response.Length))) 'truncate to 200 characters
        If Job.JobName = "Example" Then
            'send another request
            'This time it is a POST request and we set the Content-Type header
            HttpJob.Initialize("Example2")
            'add headers before calling Post or Download (this is different than the standard HttpUtils2).
            HttpJob.AddHeader("Content-Type", "application/x-www-form-urlencoded")
            'HttpJob.Post("http://www.b4x.com/print.php?key1=value1", "PostKey1=PostValue2&abc=def")
            HttpJob.Post("https://api.thingspeak.com/update?api_key=xxxxxxxxxxxxxx&field1=100","PostKey1=PostValue2&abc=def")  <===== Changed the Line
        End If
    Else
        Log("ErrorMessage: ", Job.ErrorMessage)
        Log("Status: ", Job.Status)
        Log(Job.Response)
    End If
End Sub
 

thetahsk

Active Member
Licensed User

Erel

Administrator
Staff member
Licensed User

AndroidMadhu

Active Member
Licensed User
The below is the AS IS code that I copied from your previous example:

B4X:
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    If wifi.Connect2("MADHU","xxxxxxxx") Then
        Log("Connected to router.")
    Else
        Log("Failed to connect to router.")
        Return
    End If
    HttpJob.Initialize("Example")
    HttpJob.Download("https://www.example.com")
End Sub


Sub JobDone (Job As JobResult)
    Log("*******************************")
    Log("JobName: ", Job.JobName)
    If Job.Success Then
        Dim bc As ByteConverter
        Log("Response: ", bc.SubString2(Job.Response, 0, Min(200, Job.Response.Length))) 'truncate to 200 characters
        If Job.JobName = "Example" Then
            'send another request
            'This time it is a POST request and we set the Content-Type header
            HttpJob.Initialize("Example2")
            'add headers before calling Post or Download (this is different than the standard HttpUtils2).
            HttpJob.AddHeader("Content-Type", "application/x-www-form-urlencoded")
            HttpJob.Post("http://www.b4x.com/print.php?key1=value1", "PostKey1=PostValue2&abc=def")
            'HttpJob.Download("https://api.thingspeak.com/update?api_key=L74M50W92G0TPRDZ&field1=39")
            
        End If
    Else
        Log("ErrorMessage: ", Job.ErrorMessage)
        Log("Status: ", Job.Status)
        Log(Job.Response)
    End If
End Sub
The below code I have made using wificlient and thingspeak. I am able to connect successfully at thingspeak server, but not able to send data to thingspeak

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 600
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private wificli As WiFiSocket
    Private astream As AsyncStreams
    Private EOL() As Byte = Array As Byte(13, 10)
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    If wifi.Connect2("MADHU","xxxxxxxx") Then
        Log("Connected to router.")
    Else
        Log("Failed to connect to router.")
        Return
    End If
    'HttpJob.Initialize("Example")
    'HttpJob.Download("https://www.example.com")
    wificli.ConnectHost("api.thingspeak.com",80)
    
    If wificli.Connected Then
    Log("Connected to Thingspeak Successfully!!!")  <=== Here the message is coming as success
    Else
        Log("Failed To connect Thingspeak!!")
    End If
    Send_data
End Sub


'Sub JobDone (Job As JobResult)
'    Log("*******************************")
'    Log("JobName: ", Job.JobName)
'    If Job.Success Then
'        Dim bc As ByteConverter
'        Log("Response: ", bc.SubString2(Job.Response, 0, Min(200, Job.Response.Length))) 'truncate to 200 characters
'        If Job.JobName = "Example" Then
'            'send another request
'            'This time it is a POST request and we set the Content-Type header
'            HttpJob.Initialize("Example2")
'            'add headers before calling Post or Download (this is different than the standard HttpUtils2).
'            HttpJob.AddHeader("Content-Type", "application/x-www-form-urlencoded")
'            HttpJob.Post("http://www.b4x.com/print.php?key1=value1", "PostKey1=PostValue2&abc=def")
'            'HttpJob.Download("https://api.thingspeak.com/update?api_key=L74M50W92G0TPRDZ&field1=39")
'           
'        End If
'    Else
'        Log("ErrorMessage: ", Job.ErrorMessage)
'        Log("Status: ", Job.Status)
'        Log(Job.Response)
'    End If
'End Sub

Private Sub Send_data
    astream.Write("https://api.thingspeak.com/update?api_key=L74M50W92G0TPRDZ&field1=39").Write(EOL)
End Sub
 

MathiasM

Member
Licensed User
I'm not at my computer, but couldn't it be because you're passing GET parameters in a POST request?
B4X:
HttpJob.Post("http://iot.mypage.com/addTemperature?temperature=80","PostKey1=PostValue2&abc=def")
You're setting the content-type to form POST data, yet you're posting the temperature via a GET parameter (in the URL) (which won't work) and you're posting meaningless data in as POST data.

Does anything interesting happens if you change your code to:

B4X:
HttpJob.Post("http://iot.mypage.com/addTemperature","temperature=80")
Also, how are you processing the data in PHP?
 

AndroidMadhu

Active Member
Licensed User
@MathiasM
Also, how are you processing the data in PHP?
Sorry it is a typo.... It should be addTemperature.php.

Also what about the connection about wificlient with thingspeak. I have updated the code [The last one].
I am able to connect with thingspeak, but not able to send any data to thingspeak.
 

AndroidMadhu

Active Member
Licensed User
Neither httpjob nor wificlient is working...
Not able to send any data using nodemcu to thingspeak or my website

Please advice....
 

MathiasM

Member
Licensed User
Hello

I'm now at my computer, so I have a clearer view of what's going on. And to be frank, your code doesn't make much sense to me at all.
I'm not an expert in any means. I have also never used Thingspeak in my life.
First of all, I'm going to focus on your Thingspeak example, as I don't know how your PHP backend is written.

Then, I think you lack knowledge about the basic stuff that is happening here.

You should do something like this (not tested, my daughter is on my lap and the ESP module is too far away :))

B4X:
HttpJob.Initialize("POSTtempThingSpeak")
 HttpJob.AddHeader("Content-Type", "application/x-www-form-urlencoded")
 HttpJob.Post("https://api.thingspeak.com/update", "api_key=L74M50W92G0TPRDZ&field1=39)
And then create a Jobdone event Sub where you handle the response (if any, I don't know Thingspeak)

But I think it would really help you to understand more what you're doing, instead of copy pasting.
Also read somewhat about HTTP requests.

I hope this somewhat helped you.
 
Status
Not open for further replies.
Top