Javascript from HTML causes Photon to SOS

Hi gentlemen

I am having some issues with my sketch, probably because of my beginner entry level. I am learning html/css/javascript and C from scratch. so let me explain:

I built on @bko’s tutorial trying to output the value of a temperature sensor on an html page. The temperature value updates every 5 seconds, as per the above tutorial.
Here is my code. I am using a DS18B20 Digital temperature sensor.


Photon Code

    // This #include statement was automatically added by the Particle IDE.
    #include "OneWire/OneWire.h"
    
    // This #include statement was automatically added by the Particle IDE.
    #include "spark-dallas-temperature/spark-dallas-temperature.h"
    
    // Data wire is plugged into pin 2 on the Arduino
    #define ONE_WIRE_BUS 2
     
    // Setup a oneWire instance to communicate with any OneWire devices 
    // (not just Maxim/Dallas temperature ICs)
    // Init Dallas on pin digital pin 3
    DallasTemperature dallas(new OneWire(D3));
    void setup(){
    dallas.begin();
    }
    
    void loop(){
        dallas.requestTemperatures();
        float celsius = dallas.getTempCByIndex( 0 );
        Particle.variable ("temperature", String(celsius));
        Particle.publish ("room temperature", String(celsius));
        delay(5000); }   

HTML/Javascript

Now, on the html side, here is the crucial part:

    <!-- Javascript for first module
      –––––––––––––––––––––––––––––––––––––––––––––––––– -->
    <script type="text/javascript">
    
         window.setInterval(function() {
    
            var deviceID = "20001c001347353236343033";
            var accessToken = "accessTokenHere";
            var varName1 = "temperature";
    
            requestURL = "https://api.particle.io/v1/devices/" + deviceID + "/" + varName1 + "/?access_token=" + accessToken;
            $.getJSON(requestURL, function(json) {
                     document.getElementById("data3").innerHTML = json.result + "C";
                     });
                 }, 5000);
        </script>
    <!-- End Document

The problem

When I run the html on my page the photon blinks in SOS and restarts. I get readings in the HTML, in the form of a float. When I close the tab from my browser with the html page, the Photon returns to a steady behaviour of publishing the data every 5 secs.

I believe the problem is at the photon code. It’s actually my first one.
I am also publishing the temperature in order to check the values at the dashboard.

I would appreciate any input.

Thanks!
John

This won’t work.
You need to put this instruction into setup() and use a global variable to expose.

double celsius;
void setup()
{
  ...
  Particle.variable ("temperature", celsius);
}

void loop()
{
  dallas.requestTemperatures();
  celsius = dallas.getTempCByIndex( 0 );
  ...
}
4 Likes

@ScruffR thank you for your reply.

I have revised the code based on your suggestion. Unfortunately it still doesn’t work. As soon as I open the html in my browser the Photon blinks in SOS and restarts.

Here is the new code:

DallasTemperature dallas(new OneWire(D3));
double celsius;

void setup(){
    dallas.begin();
    Particle.variable ("temperature", celsius);
}

void loop(){
    dallas.requestTemperatures();
    celsius = dallas.getTempCByIndex( 0 );
    Particle.publish ("room temperature", String(celsius));
    delay(5000);
}

The device gets restarted every a number of seconds. Here is a screenshot from the dashboard.

I have no idea. Any input is welcome!

Cheers
John

How does your device react to a variable request via one of these pages
http://jordymoors.nl/interface/
http://suda.github.io/particle-web-interface/

@ScruffR Thanks, using the interfaces above does not make the Photon to go into SOS mode. But I do not get any values for the variable.

Clicking the update button does not give any results either. Only removes the questionmark from the field :confused:

Thanks

And the other page?

Mark the variable type in your screenshot.
This would suggest your device is not running the code you posted above.
You should get a double not a string due to double celcius; and Particle.variable ("temperature", celsius);.

@ScruffR ok, I get a value but the variable remains a string.


Regarding the other site, it shows a value as well:

The events published by the Photon look ok as well. Here they are if they make sense to you.

Many thanks
John

@ScruffR I am looking at the photon code again. I commented out this line

Particle.publish ("room temperature", String(celsius));

And it seems that my html/javascript in not breaking the photon.

So, what might be wrong with this line? Is it the variable?

Update:

Ok i think i am getting somewhere. I included the publish command but used different name. Here is what i did.


DallasTemperature dallas(new OneWire(D3));
double celsius;
double celsiusValue;

void setup(){
    dallas.begin();
    Particle.variable ("temperature", celsius);
}

void loop(){
    dallas.requestTemperatures();
    celsius = dallas.getTempCByIndex( 0 );
    celsiusValue = celsius;                               // I am not sure if this is a good practice
    Particle.publish ("room temperature", String(celsiusValue));    // Instead of celsius, now i am calling celciusValue
    delay(5000);
}

And now, after flashing the sketch I am actually getting DOUBLE values like 26.75 instead of long floats as before. However -weirdly- after a while I start to get values like 26.6525 again, including my html interface as well.

Good thing is that Photon does not go in SOS mode anymore.

I am not sure though if this is a proper solution indeed. Is this valid to behave this way?

Many thanks!
John

I’d say this is not expected and the code before should have worked as well.
How is it when you revert back to celsius?