Trouble connecting to API


#1

This issue happened to both my photon and argon. I really don’t think it’s my code’s problem since I was testing with the code that I have tested multiple times. Basically I will be able to connect to API for the first few minutes and then it will get disconnected. When I run diagnostic test on them, it said the device cloud is healthy but the device vital is having problems “can’t fetch device metadata”

I have also tried to enter safe mode then flash tinker code to the device but even that failed and returned a hard fault.

Here’s the simple code I flashed that changes the delay time

int t = 0; 

void setup()
{
  Particle.function("setT", setTime);
  Particle.variable("getT", &t, INT);
}

void loop()
{
    Particle.publish("time delay", String(t));
    delay(1000);
}

int setTime(String posValue) {
    t = posValue.toInt();
    return 0;
}

And here’s the html code I used to change delay time.

<!DOCTYPE HTML>
<html>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
  <title>Slider</title>
  <link href="sliderv2.css" rel="stylesheet" type="text/css" />

  <body>
    <P>Set Delay Time:<br><br>
    <div class="slider-wrapper">
      <input type="range" name="degBox" id="degBoxId" min="0" max="8000" step="1" value="4000" list="myData" onchange="setValue(this)">
    </div>
    <!-- This adds the tick marks to the range but does not in Safari -->
    <datalist id="myData">
      <option value="0" label="0">
      <option value="500">
      <option value="1000">
      <option value="1500">
      <option value="2000" label="2000">
      <option value="2500">
      <option value="3000">
      <option value="3500">
      <option value="4000" label="4000">
      <option value="4500">
      <option value="5000">
      <option value="5500">
      <option value="6000" label="6000">
      <option value="6500">
      <option value="7000">
      <option value="7500">
      <option value="8000" label="8000">
    </datalist>
    <br><br>
    <button id="minusbutton" onclick="fineAdjust(-50)">&lArr; -50</button>
    <button id="plusbutton"  onclick="fineAdjust(+50)">+50 &rArr;</button>
    <br><br>
    <P>Current Delay Time: <span id="curPos"></span><br>

    <script type="text/javascript">
      var deviceID    = "xxxxx";
      var accessToken = "xxxxxx";
      var setFunc = "setT";
      var getFunc = "getT";

      window.setInterval(function() {
        requestURL = "https://api.particle.io/v1/devices/" + deviceID + "/" + getFunc + "/?access_token=" + accessToken;
        $.getJSON(requestURL, function(json) {
                 document.getElementById("curPos").innerHTML = json.result + " ms";
                 document.getElementById("curPos").style.fontSize = "28px";
                 document.getElementById("degBoxId").value = parseInt(json.result);
                 });
      }, 1000);

      function setValue(obj) {
        var newValue = document.getElementById('degBoxId').value;
        sparkSetPos(newValue);
      }
      
      function fineAdjust(value) {
        var currentValue = parseInt(document.getElementById('curPos').innerHTML);
        var setValue = value + currentValue;
        sparkSetPos(setValue);
        document.getElementById("degBoxId").value = setValue;
      }

      function sparkSetPos(newValue) {
  var requestURL = "https://api.particle.io/v1/devices/" +deviceID + "/" + setFunc + "/";
        $.post( requestURL, { params: newValue, access_token: accessToken });
      }

    </script>
  </body>
</html>

This code will work for the first few minutes and then failed. I’m really confused about what’s going on and was wondering if anyone is experiencing the same issue. I have already reset the access token today should I try it again?


#2

Apart from the fact that this syntax is outdated I can’t see anything wrong with your firmware code either (I haven’t checked the HTML tho’).

However, I’m notoriously known by the community that I keep advising against the use of String and so I’d rewrite your code this way

int t = 0; 

void setup()
{
  Particle.function("setT", setTime);
  Particle.variable("getT", t);
}

void loop()
{
    char txt[16];
    static uint32_t ms = 0;
    if (millis() - ms < 1000) return;
    ms = millis();

    snprintf(txt, sizeof(txt), "%d", t);
    Particle.publish("time delay", txt, PRIVATE);
}

int setTime(const char* posValue) {
    t = atoi(posValue);
    return t;  // always try to return a MEANINGFUL value
}

Try that and instead of your HTML page use console.particle.io/devices to run some targeted tests.


#3

Thanks for the reply! I modified my code and it seems like it’s really not the code’s fault since it’s giving the same problem. I think there’s something wrong with my access token that it can only last for minutes??

Because the code at first works fine and then started giving

POST https://api.particle.io/v1/devices/device_id/setT/ 400
GET https://api.particle.io/v1/devices/device_id/getT/?access_token=xxx 404

The device then went offline and the failed the diagnostic test (it passed earlier)
I looked up the error code meaning

400 Bad Request - Your request is not understood by the device, or the requested subresource (variable/function) has not been exposed.
404 Not Found - The device you requested is not currently connected to the cloud.


#4

How did you create that access token?
If you go to Web IDE image the access token provided there won’t expire.

BTW, I have slightly altered the code above to make the device more responsive by removing delay() and replacing it with a non-blocking alternative.


#5

That is exactly where i got my access token from, I also tried reset it a couple of times.


#6

Running that code on my Photon doesn’t do anything unexpected.
What device OS version have you got?
Are you doing anything particular on the HTML page when this happens?
Does this also happen when you just let the device run without interaction, only viewing the events being published?
Does the RGB LED show anything out the ordinary?


#7

OS 1.1.1
If it’s just viewing the events without interaction everything is fine. But once I tried to interact using setT it started having issues.
The RGB LED has always been breathing white light. Even when it failed the diagnostic test. but sometimes it will quick flash white for 1 sec and that’s when the device went offline and back online

I’ve run this same code a thousand times before and this never happened:(


#8

I guess you mean cyan - white would mean no connection at all and WiFi module turned off.

Are you running my adapted code (without delay())?
What browser are you using? (I test with Chrome on Windows 10 without issue)


#9

Hi So the code without delay works perfectly fine. But once I started use delay it’s not working again. I wonder why? My initial purpose of this code is to use the setT function to control the delay time delay(t) with a website. Would you mind explaining to me why the delay function is causing problems?


#10

I wonder if this setting would help in your case:

//enable the user code (our program below) to run in parallel with cloud connectivity code
// source: https://docs.particle.io/reference/device-os/firmware/photon/#system-thread

SYSTEM_THREAD(ENABLED);

here are the docs for it:
https://docs.particle.io/reference/device-os/firmware/photon/#system-thread


#11

This solved the problem! Thank you very much!