nach manuellem Test auf RS-Therm; kein funktionsfähiges-Python-Script für GT863-PY

  • Hallo!


    Manuell kann ich über die Terminal-Softwar "RS-Therm" mit einem Telit GT863-PY erfolgreich Daten an den Service thingspeak.com überragen:


    Hier ist der Log des Terminals:
    _________________________


    Send: AT#SKTOP<cr>


    CONNECT


    Send: POST /update HTTP/1.1<cr><lf>


    Send: Host:api.thingspeak.com<cr><lf>


    Send: Connection:close<cr><lf>


    Send: X-THINGSPEAKAPIKEY:xyz<cr><lf>


    Send: Content-Type:application/x-www-form-urlencoded<cr><lf>


    Send: Content-Length:9<cr><lf>


    Send: <cr><lf>


    Send: field1=4<cr><lf>


    Send: <cr><lf>
    HTTP/1.1 200 OK.......
    _____________________


    Mir ist aber keine funktionsfähige Umsetzung in ein Python-Script gelungen.


    Hier ist eine von diversen Varianten (Aufruf nach manuellem Connect)


    res = MDM.send('POST /update HTTP/1.1',0)
    res = MDM.send('Host:api.thingspeak.com',0)
    res = MDM.send('Connection:close',0)
    res = MDM.send('X-THINGSPEAKAPIKEY:xyz',0)
    res = MDM.send('Content-Type:application/x-www-form-urlencoded',0)
    res = MDM.send('Content-Length:9',0)
    res = MDM.send('\\r',0)
    res = MDM.send('field1=5',0)
    res = MDM.send('\\r\\n',0)


    Wo ist der Fehler.


    Manfred

  • Hello,


    Please try the following:


    res = MDM.send('POST /update HTTP/1.1\r\n',0)
    res = MDM.send('Host:api.thingspeak.com\r\n',0)
    res = MDM.send('Connection:close'\r\n,0)
    res = MDM.send('X-THINGSPEAKAPIKEY:xyz\r\n',0)
    res = MDM.send('Content-Type:application/x-www-form-urlencoded\r\n',0)
    res = MDM.send('Content-Length:9\r\n',0)
    res = MDM.send('\r\n',0)
    res = MDM.send('field1=5',0)
    res = MDM.send('\r\n',0)


    In HTTP posting, you need to send \r\n after each http header line as it is done in your example when you did it via RSterm.
    An alternative solution is the following:


    PostStr = 'POST /update HTTP/1.1\r\n'
    PostStr = PostStr + 'Host:api.thingspeak.com\r\n'
    PostStr = PostStr + 'Connection:close'\r\n
    PostStr = PostStr + 'X-THINGSPEAKAPIKEY:xyz\r\n'
    PostStr = PostStr + 'Content-Type:application/x-www-form-urlencoded\r\n'
    PostStr = PostStr + 'Content-Length:9\r\n'
    PostStr = PostStr + '\r\n'
    PostStr = PostStr + 'field1=5'
    PostStr = PostStr + '\r\n'


    res = MDM.send(PostStr,10)


    Let me know if this solves your problem.


    Mazen

  • Let me know if this solves your problem.


    Mazen

    Hello and thank you Mazen


    It seems that it is necessary to include the 'AT#SKTOP' command in the Python script like this:


    res = MDM.send('AT#SKTOP\r\n',0)
    res = MDM.send('POST /update HTTP/1.1\r\n',0)
    res = MDM.send('Host:api.thingspeak.com\r\n',0)
    res = MDM.send('Connection:close\r\n',0)
    res = MDM.send('X-THINGSPEAKAPIKEY:YJRX6XC0D0I83B17\r\n',0)
    res = MDM.send('Content-Type:application/x-www-form-urlencoded\r\n',0)
    res = MDM.send('Content-Length:9\r\n',0)
    res = MDM.send('\r\n',0)
    res = MDM.send('field1=5\r\n',0)
    res = MDM.send('\r\n',0)
    res = MDM.send('\r\n',0)
    SER.send('End\r\n')


    The result shown on the terminal screen is...
    ___________________
    Send: AT#EXECSCR<cr>
    AT#EXECSCR


    OK
    End


    CONNECT


    NO CARRIER
    ________________


    This doesn't work.


    As you can see, the CONNECT message comes AFTER the End message.


    I experimented with the MOD.sleep(55) command to achieve a wait for the connect before continuing with the rest of the script, but without success.


    Any idea?


    Manfred

  • Hello Manfred,


    Using AT#SKTOP activates the GPRS cotext first and then connects a socket so this will take some time. Sending the http post data afterwards immediately after the command results in losing it and data will not be sent through the socket.
    After sending AT#SKTOP command, I suggest that you do call a function like this that checks if you are connected before you start sending the http post data. If the function returns 1, then it means you are connected.
    def CheckIfConnected(self):
    timeout = MOD.secCounter() + 15
    tmpdatabuff = ''
    while timeout > MOD.secCounter():
    recv = MDM.receive(15)
    tmpdatabuff =tmpdatabuff + recv
    if(tmpdatabuff.find('CONNECT')!=-1):
    #printW('Connected to Server\r\n')
    return 1
    if(tmpdatabuff.find('NO CARRIER')!=-1):
    #printW('Unable to connect to server\r\n')
    return -1
    if(tmpdatabuff.find('ERROR')!=-1):
    #printW('Connection Error\r\n')
    return -1
    return -1


    You can also use MDM.getDCD() to check if you are connected. So after sending the command AT#SKOP, wait a 5 seconds delay, then do the following:
    if(MDM.getDCD()==1):
    #printW('Connected to server\r\n')
    #Start sending data