B4R Question How to Send Data to webpage using NodeMCU

Discussion in 'B4R Questions' started by AndroidMadhu, Jun 4, 2019.

Thread Status:
Not open for further replies.
  1. AndroidMadhu

    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....
     
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. AndroidMadhu

    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
    Code:
    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
    Code:
    "PostKey1=PostValue2&abc=def"
    Could you please help me to understand this portion.
     
  4. AndroidMadhu

    AndroidMadhu Active Member Licensed User

    I have incorporated as per the above post. But no Log is coming...

    Code:
    #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 ....
     
  5. tigrot

    tigrot Well-Known Member Licensed User

    The portion you write about is the data sent by post to your web page. If you don't need wifi scan comment scannetworks call since it can last minutes.
     
    f0raster0 likes this.
  6. AndroidMadhu

    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
    Code:
    Log("Status :",job.Status)
    Log("Response :",job.Response)
    ? Is the sub "JobDone" not running? I am not able to find any clue ...

    Please advice....
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Run the example as-is (only change the wifi credentials). Does it work?
     
  8. AndroidMadhu

    AndroidMadhu Active Member Licensed User

    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...

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

    Code:
    Sub JobDone (Job As JobResult)
        
    Log("*******************************")
        
    Log("JobName: ", Job.JobName)
        
    If Job.Success Then
            
    Dim bc As ByteConverter
            
    Log("Response: ", bc.SubString2(Job.Response, 0Min(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
     
  9. thetahsk

    thetahsk Active Member Licensed User

    f0raster0 likes this.
  10. Erel

    Erel Administrator Staff Member Licensed User

    You haven't answered my question.
     
  11. AndroidMadhu

    AndroidMadhu Active Member Licensed User

    No the as is code not working. It is connecting suvcessfully..... then error msg shows Failed to connect
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Please upload the project.
     
  13. AndroidMadhu

    AndroidMadhu Active Member Licensed User

    The below is the AS IS code that I copied from your previous example:

    Code:
    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, 0Min(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

    Code:
    #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(1310)
    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
     
  14. MathiasM

    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?
    Code:
    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:

    Code:
    HttpJob.Post("http://iot.mypage.com/addTemperature","temperature=80")
    Also, how are you processing the data in PHP?
     
    f0raster0 likes this.
  15. AndroidMadhu

    AndroidMadhu Active Member Licensed User

    @MathiasM
    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.
     
  16. AndroidMadhu

    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....
     
  17. MathiasM

    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 :))

    Code:
    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.
     
    f0raster0 likes this.
  18. AndroidMadhu

    AndroidMadhu Active Member Licensed User

    yes.. this long session helped me :)

    But if want GET instead of POST what should I do?
    For example If I want to submit the request through GET method at my webpage www.mypage.com, then how Do I proceed using httpob?
     
  19. tigrot

    tigrot Well-Known Member Licensed User

    Use sintax "www.mypage.com?par1=txtx&par2=txt" the list after ? is the list of command parameters passed to web page in the get, every parameters is divided by &. You must normalize the list because of get syntax.
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    This is a duplicate post. I'm closing this thread.
     
    tigrot likes this.
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice