Trouble connecting to API

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

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

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

  <script src="" type="text/javascript" charset="utf-8"></script>
  <link href="sliderv2.css" rel="stylesheet" type="text/css" />

    <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)">
    <!-- 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">
    <button id="minusbutton" onclick="fineAdjust(-50)">&lArr; -50</button>
    <button id="plusbutton"  onclick="fineAdjust(+50)">+50 &rArr;</button>
    <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 = "" + 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;
      function fineAdjust(value) {
        var currentValue = parseInt(document.getElementById('curPos').innerHTML);
        var setValue = value + currentValue;
        document.getElementById("degBoxId").value = setValue;

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


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?

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 to run some targeted tests.

1 Like

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

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.

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

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?

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

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)

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?

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:


here are the docs for it:

This solved the problem! Thank you very much!

1 Like