SIM900 throws error after resetting and re-connecting.

Posted on
Page
of 2
/ 2
Next
  • Excuse me this issue may not be SIM900 related, which performs quite good with the module... see my fix below:

    Whenever I reset my SIM900 (switch it totally off) and would run a JSON-Request after reset it throws this kind of error, even I am online:

    req start
    Closed: undefined
    Uncaught SyntaxError: Got ID:total_documents expected ':'
    at line 3 col 3
    {"total_documents":0,"result"l_documents­":0,"result":""}:""}
    ^
    in function called from system

    However when I first doing a request with a non JSON result say the hello-request with a text result. The consecutive calls with JSON will NOT fail.

    If someone has a hint, please tell me. kind regards, Alex

  • Sounds strange - The {"total_documents":0, ... stuff sounds like that's the JSON response to your request? If so, could it be that you're calling setTimeout, setInterval, or some function that expects a callback with that string as an argument?

    Basically Espruino can often take a string or a function as an argument to callbacks - usually you'd just have something executable in that string so it's fine, but if you put something like JSON in it, it will fail. If you put "Hello World" in, that will work, because it's valid JavaScript, but {key:value} will fail as it's not valid (afaik surrounding it with brackets is valid: ({key:value}))

    It'd be an idea to figure out what variable contains that data, and then go through your code and figure out where it is used.

  • Of course I do deserializing pass an object to the callback : - >

  • I try one guess... Could it be related to the fact, I applied a success callback on my own func within getIP (SIM900). This callback does not get executed in the SIM900 before the final statement?

  • @Gordon I reset the SIM900 with a relais. I really switch it off and back on. After that the uplink (refresh IP) works... but not the requests. they will fail with above errors. Should I initialize something I forgot about?
    When is the gprs.reset() to be used? I could not bring my SIM900 online if I only use reset with callback. I really had to restart it by withdrawing power and put it back on.

  • Is there a reset pin on your SIM900? Without it, gprs.reset() may not be as good at resetting (if SIM900 was in the middle of receiving block of data when Espruino reset).

    But I don't think that is your problem... I don't think it's really SIM900 related at all - it looks to me like your code is passing non-executable code into a function that expects a callback. If you could post your code up here, we could take a look for you?

  • I have an SIM900 wich is Arduino compatible. As I know the Arduino has a RST-Pin I connected the appropriate SIM900 one to B4 on espruino.

  • All I do is run the below calls .... one after another it works as expected over 1.5 hour ... even chaining them via callback.

    Then once I start resetting the SIM900 (my own reset) wich completely switches the SIM900 off and back on.

    The first request (getJobs) will fail repeatedly.

    // template for this device
    var device1 = {
        protocol: "v1",
        checksum: "",
        device: "defaultDevice@xyz",
        at: "now",
        data: {}
    };
    
    function post(template, payload, fn, fne) {
      template.data = payload;
      content = JSON.stringify(template);
      var options = {
        host: 'api.carriots.com',
        path: '/status',
        port: '80',
        method:'POST',
        headers: {
          "carriots.apiKey": "aaaaaaaaaaaaaaaaa",
          "Content-Type": "application/json",
          "Content-Length": content.length
        }
      };
      var req = require("http").request(options, function(res)  {
        var d = "";
        res.on('data', function(data) { d+= data; });
        res.on('error', function(data) {
          console.log('error', data);
          if (fne){
            return fne();
          }
        });
        res.on('close', function(data) {
          var obj = JSON.parse(d);
            if (fn){
              return fn(obj);
            }
        });
      }).end(content);
    }
    
    

    the calls are done like:

    function getJobs(fn, fne) {
      var options = {
        host: 'api.carriots.com',
        path: '/streams/?device=Commander@mydevice',
        port: '80',
        method:'GET',
        headers: {
          "carriots.apiKey": "key",
          "Content-Type": "application/json"
        }
      };
      var req = require("http").request(options, function(res)  {
        var d = "";
        res.on('data', function(data) { d+= data; });
        res.on('error', function(data) {
          console.log('error', data);
          if (fne){
            return fne();
          }
        });
        res.on('close', function() {
          var obj = JSON.parse(d);
          if (fn){
            return fn(obj);
          }
          console.log('req closed');
        });
      }).end();
    }
    
    
  • ups... I forgot to post the requests...

    1.post(device, {xyz: 1})
    2.getJobs()

    I chain it:
    getJobs(function(){ post(.....) }, function(){ // error })

  • And what exact messages do you get on the console? The messages you posted previously started like this:

    req start
    Closed: undefined
    

    But you don't have those print statements in the code you posted up.

    As it happens after Closed: undefined, it might be worth adding more print statements so you can get a better idea when the error is actually happening

  • Hi Gordon, as you know I have made a bunch of changes to my own SIM900 module. Most of these changes you probably wont want in the standard library but there are a couple of important bug fixes here and there for the original code. There is one bug in the receiveHandler which can cause corruption depending on the data framing.

    Shall I send you my current file so you can see my fixes ?

  • Sorry for the delay - yes, that'd be great! If you're ok with sharing it I'd love to be able to pull your changes back.

    Potentially you could create a second module that you used with require(mymodule)(gsm) that actually added your extra functions onto the existing module - so you kept up to date if there were more changes in the future.

  • I rewritten things entirely from scratch.... also with use of promises.
    However, the error still occurs...
    My question is whether the SIM900 receives the response string as a whole?
    In the JSON response I see a AT-Command-Response (+RECEIVE,0,940:) in middle of the string.
    Can this be a problem? I do not see the end of the JSON-String, so my assumption is the missing characters were subtracted by the length of the AT-Response in middle.
    Can I fix this myself?

    $http(sampleGetReq).then(function(data){­
    : // success
    : console.log('success', data);
    :});
    ={ }

    OK

    0, CONN
    ECT OK

    0, S
    END OK

    +RECEIVE,0,
    1380:
    HTTP/1.1 200 OK
    Date: Thu, 28 Jul 2016 22:25:00 GMT
    Content-Type: application/vnd.carriots.v2+json; charset=utf-8
    Connectio
    n: close
    Server: Carriots REST API
    Cache-Control: no-cache
    Allow: GET, POST
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Credentials: true
    Access-Control-Expose-Headers: Date,Server,Allow,Connection,Content-Len­gth,Content-Type,Cache-Control
    {"total_documents"
    :6,"result":[{"_id":"579842a25c5d7550425­376e3","protocol":"v2","checksum":"","_t­":"str","at":1469596322,"device":"Comman­der@afitterling.afitterling","data":{"ti­me":1,"cycle":1},"id_developer":"185c0dd­e8b7c9df29d122b25d85fce1c26ad7645ed021f5­c611fe4115132f4f8@afitterling.afitterlin­g","created_at":1469596322,"owner":"afit­terling"},{"_id":"579842ae5c5d752b455376­79","protocol":"v2","checksum":"","_t":"­str","at":1469596334,"device":"Commander­@afitterling.afitterling","data":{"time"­:1,"cycle":1},"id_developer":"6c49c7f8ca­e5845cacd060ff93edb189517f691a47dbe4817a­831869d3f
    e8ca7@afitterling.afitterling","created_­at":1469596334,"owner":"afitterling"},{"­_id":"579842b85c5d75e1425376cc","protoco­l":"v2","checksum":"","_t":"str","at":14­69596344,"device":"Commander@afitterling­.afitterling","data":{"time":1,"cycle":1­},"id_developer":"91df2481ae379b2c25fc48­5c6de961efea091aab3a4823853a17a22cd1feac­52@afitterling.afitterling","created_at"­:1469596344,"owner":"afitterling"},{"_id­":"579842d
    +RECEIVE,0,940:
    95c5d7599295379f1","protocol":"v2","chec­ksum":"","_t":"str","at":1469596377,9638­7,"d
    evice":"Commander@afitterling.afitterlin­g","data":{"timex":1,"cylce":1},"id_deve­loper":"20ed81b80c70b058cff3d1f339d8558c­c00fc1dbf32fd193bde61b83c0185f72@afitter­ling.afitterling","created_at":146959638­7,"owner":"afitterling"},{"_id":"579842f­05c5d7550425376ea","protocol":"v2","chec­ksum":"","_t":"str","at":1469596400,"dev­ice":"Commander@afitterling.afitterling"­,"data":{"timex":1,"cylce":1},"iddeveloper":"a281efa90907e5e8ff7f6a4fe1af­adcd23e746a717a8b7610ab459f8ab538258@afi­tterling.afitterling","created

    0, CLOS
    ED

    the next call then will get stuck then.

    kind regards,
    Alex

  • Strange - I haven't seen that before. After RECEIVE,0,940 there should be 940 characters - but I don't see that at all.

    Can you check E.getErrorFlags() when this happens? It's possible it is running slowly enough that characters can't be processed and are getting lost. In fact if you have other code that takes a long time to execute then this could be the problem with the original code as well.

    Can you try using gsm.at.debug() and dumping the output from that? I'm not quite sure how you're generating what I see in the dump above?

  • yes, I will try that.

  • Hi Gordon, thanks again for you support. I have only limited time today. Only, if you have time.
    I am looking forward for your expert statement.

    >requestsNew()
    =undefined
    >
     
    ] "\r\n" <--- "\r\n"
    10.228.192.245
     
    ] "10.228.192.245\r\n" <--- "10.228.192.245\r\n"
    {
      "host": "api.carriots.com",
      "path": "/streams/?_t=str&device=Commander@afitt­erling.afitterling",
      "port": "80",
      "method": "GET",
      "headers": {
        "Content-Type": "application/json",
        "carriots.apiKey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx­",
        "Content-Length": 0 }
     } null
    ["AT+CIPSTART=0,\"TCP\",\"api.carriots.c­om\",80\r\n"
    >
    OK
    ] "\r\nOK\r\n" <--- "\r\nOK\r\n"
    >
    0, CONNEC
    ] "\r\n0, CONNEC" <--- "\r\n0, CONNEC"
    T OK
    ] "0, CONNECT OK\r\n" <--- "T OK\r\n"
    >
    >
    ] "\r\n> " <--- "\r\n> "
    >
    0, SEND O
    ] "\r\n0, SEND O" <--- "\r\n0, SEND O"
    K
    ] "0, SEND OK\r\n" <--- "K\r\n"
    >
    +RECEIVE,
    ] "\r\n+RECEIVE," <--- "\r\n+RECEIVE,"
    DEBUG: +RECEIVE,
    0,1380:
    HTTP/1.1 200 OK
    Date: Fri, 29 Jul 2016 07:46:50 GMT
    Content-Type: application/vnd.carriots.v2+json; charset=utf-8
    Connection: cl
    ] "+RECEIVE,0,1380:\r\nHTTP/1.1 200 OK\r\nDate: Fri, 29 Jul 2016 07:46:50 GMT\r\nContent-Type: application/vnd.carriots.v2+json; charset=utf-8\r\nConnection: cl" <--- "0,1380:\r\nHTTP/1.1 200 OK\r\nDate: Fri, 29 Jul 2016 07:46:50 GMT\r\nContent-Type: application/vnd.carriots.v2+json; charset=utf-8\r\nConnection: cl"
    DEBUG: +RECEIVE,0,1380:
    HTTP/1.1 200 OK
    Date: Fri, 29 Jul 2016 07:46:50 GMT
    Content-Type: application/vnd.carriots.v2+json; charset=utf-8
    Connection: cl
    ose
    Server: Carriots REST API
    Cache-Control: no-cache
    Allow: GET, POST
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Credentials: true
    Access-Control-Expose-Headers: Date,Server,Allow,Connection,Content-Len­gth,Content-Type,Cache-Control
    {"total_documents":3,"result":[{"_id":"5­79b07af5c5d750a2e53769a","protocol":"v2"­,"checksum":"","_
    ] "+D,0,1249:ose\r\nServer: Carriots REST API\r\nCache-Control: no-cache\r\nAllow: GET, POST\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Credentials: true\r\nAccess-Control-Expose-Headers: Date,Server,Allow,Connection,Content-Len­gth,Content-Type,Cache-Control\r\n\r\n{\­"total_documents\":3,\"result\":[{\"_id\­":\"579b07af5c5d750a2e53769a\",\"protoco­l\":\"v2\",\"checksum\":\"\",\"_" <--- "ose\r\nServer: Carriots REST API\r\nCache-Control: no-cache\r\nAllow: GET, POST\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Credentials: true\r\nAccess-Control-Expose-Headers: Date,Server,Allow,Connection,Content-Len­gth,Content-Type,Cache-Control\r\n\r\n{\­"total_documents\":3,\"result\":[{\"_id\­":\"579b07af5c5d750a2e53769a\",\"protoco­l\":\"v2\",\"checksum\":\"\",\"_"
    t":"str","at":1469777839,"device":"Comma­nder@afitterling.afitterling","data":{"T­his":"Is a JSON example","Carriots":{"text":"Hello World","number":123,"cool":true}},"id_de­veloper":"20bab0871682e6f1f2724a2753a83c­3b959af6088165e2abf88dfa045ebac8a0@afitt­erling.afitterling","created_at":1469777­839,"owner":"afitterling"},{"_id":"579b0­7b85c5d75792d5376c2","protocol":"v2","ch­ecksum":"","_t":"str","at":1469777848,"d­evice":"Commander@afitterling.afitterlin­g","data":{"This":"Is a JSON example","Carriots":{"texfitterli
    ] "+D,0,900:t\":\"str\",\"at\":1469777839,­\"device\":\"Commander@afitterling.afitt­erling\",\"data\":{\"This\":\"Is a JSON example\",\"Carriots\":{\"text\":\"Hello­ World\",\"number\":123,\"cool\":true}},\­"id_developer\":\"20bab0871682e6f1f2724a­2753a83c3b959af6088165e2abf88dfa045ebac8­a0@afitterling.afitterling\",\"created_a­t\":1469777839,\"owner\":\"afitterling\"­},{\"_id\":\"579b07b85c5d75792d5376c2\",­\"protocol\":\"v2\",\"checksum\":\"\",\"­_t\":\"str\",\"at\":1469777848,\"device\­":\"Commander@afitterling.afitterling\",­\"data\":{\"This\":\"Is a JSON example\",\"Carriots\":{\"texfitterli" <--- "t\":\"str\",\"at\":1469777839,\"device\­":\"Commander@afitterling.afitterling\",­\"data\":{\"This\":\"Is a JSON example\",\"Carriots\":{\"text\":\"Hello­ World\",\"number\":123,\"cool\":true}},\­"id_developer\":\"20bab0871682e6f1f2724a­2753a83c3b959af6088165e2abf88dfa045ebac8­a0@afitterling.afitterling\",\"created_a­t\":1469777839,\"owner\":\"afitterling\"­},{\"_id\":\"579b07b85c5d75792d5376c2\",­\"protocol\":\"v2\",\"checksum\":\"\",\"­_t\":\"str\",\"at\":1469777848,\"device\­":\"Commander@afitterling.afitterling\",­\"data\":{\"This\":\"Is a JSON example\",\"Carriots\":{\"texfitterli"
    ng"},{"_id":"579b093a5c5d750a2e5376bc","­protocol":"v2","checksum":"","_t":"str",­"at":1469778234,"device":"Commander@afit­terling.afitterling","data":{"This":"Is a JSON example","Carriots":{"text":"Hello W
    +RECEIVE,0,187:
    orld","number":123,"cool":true}},"id_dev­eloper":"4aaed914017f667ecf933be52e14711­1d3b0b12460c7eff8d41e35b6164b31c6@afitte­rling.afitterling","created_at":14697782­34,"owner":"afitterling"}]}
    ] "+D,0,387:ng\"},{\"_id\":\"579b093a5c5d7­50a2e5376bc\",\"protocol\":\"v2\",\"chec­ksum\":\"\",\"_t\":\"str\",\"at\":146977­8234,\"device\":\"Commander@afitterling.­afitterling\",\"data\":{\"This\":\"Is a JSON example\",\"Carriots\":{\"text\":\"Hello­ W\r\n+RECEIVE,0,187:\r\norld\",\"number\­":123,\"cool\":true}},\"id_developer\":\­"4aaed914017f667ecf933be52e147111d3b0b12­460c7eff8d41e35b6164b31c6@afitterling.af­itterling\",\"created_at\":1469778234,\"­owner\":\"afitterling\"}]}" <--- "ng\"},{\"_id\":\"579b093a5c5d750a2e5376­bc\",\"protocol\":\"v2\",\"checksum\":\"­\",\"_t\":\"str\",\"at\":1469778234,\"de­vice\":\"Commander@afitterling.afitterli­ng\",\"data\":{\"This\":\"Is a JSON example\",\"Carriots\":{\"text\":\"Hello­ W\r\n+RECEIVE,0,187:\r\norld\",\"number\­":123,\"cool\":true}},\"id_developer\":\­"4aaed914017f667ecf933be52e147111d3b0b12­460c7eff8d41e35b6164b31c6@afitterling.af­itterling\",\"created_at\":1469778234,\"­owner\":\"afitterling\"}]}"
    >
    0, CL
    ] "wner\":\"afitterling\"}]}\r\n0, CL" <--- "\r\n0, CL"
    OSED
    ] "0, CLOSED\r\n" <--- "OSED\r\n"
    Uncaught Error: Field or method "total_documents" does not already exist, and can't create it on undefined
     at line 1 col 614
    ..."Carriots":{"texfitterling"},{"_id":"­579b093a5c5d750a2e5376b...
                                  ^
    in function called from system
     at line 208 col 33
                    total_docs: data.total_documents
                                    ^
    in function called from system
    >E.getErrorFlags()
    =[
      "FIFO_FULL"
     ]
    > 
    
  • I accidentally also put my debug output from the request in their above? Should I take it out.
    only the lines:

    {
      "host": "api.carriots.com",
      "path": "/streams/?_t=str&device=Commander@afitt­erling.afitterling",
      "port": "80",
      "method": "GET",
      "headers": {
        "Content-Type": "application/json",
        "carriots.apiKey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx­",
        "Content-Length": 0 }
     } null
    
  • It is wired once more carriots wants me to send content-length on get....

  • I believe I shouldn't bother about this behavior. I just seen, the carriots io supports the max parameter. So I will get only two at a time. And my code will be fixed. :-)

  • I'm still confused about Uncaught Error: Field or method "total_documents" does not already exist, and can't create it on undefined

    But the "FIFO_FULL" flag really is your problem - some part of your program is taking as long time to execute, and during that time the FIFO gets full and characters get dropped.

    Which board are you using this on? And you're connecting over USB?

  • I am using the Pico board.
    I run it currently over usb but additionally with an external power supply, as the usb connection is only for debugging purpoises. I sometimes pull off usb.

    If the document gets parsed by one the total_document is there every time. If however the document getting JSON parsed is only truncated this is missing.
    I could successfully reproduce that the error occurs only if a certain treshold of response length is tresspassed. And the response string from the hardware seems truncated.
    I looks as if it receives only a limited amount of bytes? But there is only one callback? So my assumption was the callback gets called with an incomplete set of data, that itself will be passed in to the callbacks/promises.

    A good thing. As I understood now at least the source of error, I could so far fix it with the max param that carriots provides. I pick only one job at a time. If this works for 7d/24h I'll be glad. My garden is in a remote area very far away. :-)

  • I use the maximum baud rate on the SIM900, can this be a cause?

  • in the above log from the example it looks like that the response string itself handed into JSON.parse is not valid json anymore. It has passages like:

    e":"Commander@afitterling.afitterling","­data":{"This":"Is a JSON example","Carriots":{"texfitterli
    ] "+D,0,900:t\":\"str\",\"at\"
    

    So I came to the conclusion the SIM900 mixed it up(?)

  • I use the maximum baud rate on the SIM900, can this be a cause?

    What is the baud rate? It definitely won't help... The Pico can buffer around 500 bytes, so it won't take long to fill it at higher rates. I'd definitely try and lower the rate significantly - it should stop the FIFO_FULL messages anyway.

    So I came to the conclusion the SIM900 mixed it up(?)

    I think the SIM900 is probably fine - it's just that Espruino's buffers are getting full so it's losing the odd character.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

SIM900 throws error after resetting and re-connecting.

Posted by Avatar for sp33c @sp33c

Actions