Monitoring two cores via web

Hi there,

I am working on multiple Particle Cores via web page.
I am trying to monitoring ‘mode’ of the devices via Spark.publish and Variable. I followed the example Tutorial: Spark Variable and Function on One Web Page

  1. I used push button to switch mode and I was able to monitor the mode change via web.(It worked like a charm)
  2. I added LSM9ds0 Sensor (by adafruit and library by KenethlImcp ), and this somehow causes problem. I need a help on this.

The symptom is that it goes to infinite loop after few minutes (cyan breathing led but inaccessible) and sometimes cyan blinking rapidly blinks for few seconds and comes back to normal. Here is my code

Help me please

// This #include statement was automatically added by the Spark IDE.
#include "Adafruit_Sensor.h"

// This #include statement was automatically added by the Spark IDE.
#include "Adafruit_LSM9DS0.h"

/* Assign a unique base ID for this sensor */
Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0();  // Use I2C, ID #1000

float motionXaxis = 0.0;
float pre_motionXaxis = 0.0;

int button = D2;
int storyMode =0;

int led = D7;
int led2 = D6;
int curState = 0;
int preState = 0;

int movementDetected = 0;
String sendingMode;


void setup()
{
  Serial.begin(9600);


   if(!lsm.begin())
   {
     Serial.print(F("Ooops, no LSM9DS0 detected ... Check your wiring or I2C ADDR!"));
     while(1);
   }
   Serial.println(F("Found LSM9DS0 9DOF"));

  configureSensor();
  //Spark.subscribe("movement", motionDetection); //receiving data from T2
  Spark.variable("getStory", &storyMode, INT); //informing data
  Spark.function("setStory", setStoryWeb);
  pinMode(button, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);

}

void configureSensor()
{
  // 1.) Set the accelerometer range
  lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_2G);
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_4G);
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_6G);
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_8G);
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_16G);

  // 2.) Set the magnetometer sensitivity
  lsm.setupMag(lsm.LSM9DS0_MAGGAIN_2GAUSS);
  //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_4GAUSS);
  //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_8GAUSS);
  //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_12GAUSS);

  // 3.) Setup the gyroscope
  lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_245DPS);
  //lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_500DPS);
  //lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_2000DPS);
}
void loop()
{
  buttonInput();
  motionDetection();
}

void buttonInput(){
  curState = digitalRead(button);
  if(curState != preState){
    if(curState == HIGH){
      changeStoryMode();
    }
  }
  preState = curState;
}

int setStoryWeb(String storyValue){
  int newValue = storyValue.toInt();
  if(newValue == 1){
    storyMode = 2;
    changeStoryMode();
  }
  else if(newValue == 2){
    storyMode = 1;
    changeStoryMode();

  } else {
    storyMode = 99;
  }
  return 0;
}

void changeStoryMode(){

  if(storyMode == 0){
    storyMode = 1;
    sendingMode = "1";
  }
  else if(storyMode == 1){
    storyMode = 2;
    sendingMode = "2";
  }
  else if(storyMode == 2){
    storyMode = 1;
    sendingMode = "1";
  }
  else {
    storyMode = 99;
    sendingMode = "99";
  }

  Serial.println(storyMode);
  Spark.publish("storyMode", sendingMode);
}


void motionDetection(){
  lsm.read();
  /*
  sensors_event_t accel, mag, gyro, temp;

  lsm.getEvent(&accel, &mag, &gyro, &temp);*/

  /*// print out accelleration data
  Serial.print("Accel X: "); Serial.print(accel.acceleration.x); Serial.print(" ");
  Serial.print("  \tY: "); Serial.print(accel.acceleration.y);       Serial.print(" ");
  Serial.print("  \tZ: "); Serial.print(accel.acceleration.z);     Serial.println("  \tm/s^2");

  // print out magnetometer data
  Serial.print("Magn. X: "); Serial.print(mag.magnetic.x); Serial.print(" ");
  Serial.print("  \tY: "); Serial.print(mag.magnetic.y);       Serial.print(" ");
  Serial.print("  \tZ: "); Serial.print(mag.magnetic.z);     Serial.println("  \tgauss");

  // print out gyroscopic data
  Serial.print("Gyro  X: "); Serial.print(gyro.gyro.x); Serial.print(" ");
  Serial.print("  \tY: "); Serial.print(gyro.gyro.y);       Serial.print(" ");
  Serial.print("  \tZ: "); Serial.print(gyro.gyro.z);     Serial.println("  \tdps");

  // print out temperature data
  Serial.print("Temp: "); Serial.print(temp.temperature); Serial.println(" *C");

  Serial.println("**********************\n");*/

  motionXaxis = lsm.gyroData.x;

  if(abs(pre_motionXaxis - motionXaxis) > 1000){
    digitalWrite(led, HIGH);

  }
  else{
    digitalWrite(led, LOW);
  }
  pre_motionXaxis = motionXaxis;

}

@cherltte are you using the library from the Web IDE. Your include statements doesn’t seem to be correct somehow

Thanks for reply KEnnethlimcp! I’m using Particle Dev. The code is partially working. I get the sensor data and led turning on. But i am having trouble communicating with web. Also it goes to infinite loop after few minutes(cyan breathing but inaccessible) and when i reset (not factory reset) it occasionally comes back to normal state. Sometimes i had to factory reset. Any suggestions?

// This #include statement was automatically added by the Spark IDE.
#include "Adafruit_Sensor.h"

// This #include statement was automatically added by the Spark IDE.
#include "Adafruit_LSM9DS0.h"

/* Assign a unique base ID for this sensor */
Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0();  // Use I2C, ID #1000

float motionXaxis = 0.0;
float pre_motionXaxis = 0.0;

int button = D2;
int storyMode =0;

int led = D7;
int led2 = D6;
int curState = 0;
int preState = 0;

int movementDetected = 0;
String sendingMode;


void setup()
{
  Serial.begin(9600);


   if(!lsm.begin())
   {
     Serial.print(F("Ooops, no LSM9DS0 detected ... Check your wiring or I2C ADDR!"));
     while(1);
   }
   Serial.println(F("Found LSM9DS0 9DOF"));

  configureSensor();
  //Spark.subscribe("movement", motionDetection); //receiving data from T2
  Spark.variable("getStory", &storyMode, INT); //informing data
  Spark.function("setStory", setStoryWeb);
  pinMode(button, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);

}

void configureSensor()
{
  // 1.) Set the accelerometer range
  lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_2G);
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_4G);
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_6G);
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_8G);
  //lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_16G);

  // 2.) Set the magnetometer sensitivity
  lsm.setupMag(lsm.LSM9DS0_MAGGAIN_2GAUSS);
  //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_4GAUSS);
  //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_8GAUSS);
  //lsm.setupMag(lsm.LSM9DS0_MAGGAIN_12GAUSS);

  // 3.) Setup the gyroscope
  lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_245DPS);
  //lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_500DPS);
  //lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_2000DPS);
}
void loop()
{
  buttonInput();
//  motionDetection();
}

void buttonInput(){
  curState = digitalRead(button);
  if(curState != preState){
    if(curState == HIGH){
      changeStoryMode();
    }
  }
  preState = curState;
}

int setStoryWeb(String storyValue){
  int newValue = storyValue.toInt();
  if(newValue == 1){
    storyMode = 2;
    changeStoryMode();
  }
  else if(newValue == 2){
    storyMode = 1;
    changeStoryMode();

  } else {
    storyMode = 99;
  }
  return 0;
}

void changeStoryMode(){

  if(storyMode == 0){
    storyMode = 1;
    sendingMode = "1";
  }
  else if(storyMode == 1){
    storyMode = 2;
    sendingMode = "2";
  }
  else if(storyMode == 2){
    storyMode = 1;
    sendingMode = "1";
  }
  else {
    storyMode = 99;
    sendingMode = "99";
  }

  Serial.println(storyMode);
  Spark.publish("storyMode", sendingMode);
}


void motionDetection(){
  lsm.read();
  /*
  sensors_event_t accel, mag, gyro, temp;

  lsm.getEvent(&accel, &mag, &gyro, &temp);*/

  /*// print out accelleration data
  Serial.print("Accel X: "); Serial.print(accel.acceleration.x); Serial.print(" ");
  Serial.print("  \tY: "); Serial.print(accel.acceleration.y);       Serial.print(" ");
  Serial.print("  \tZ: "); Serial.print(accel.acceleration.z);     Serial.println("  \tm/s^2");

  // print out magnetometer data
  Serial.print("Magn. X: "); Serial.print(mag.magnetic.x); Serial.print(" ");
  Serial.print("  \tY: "); Serial.print(mag.magnetic.y);       Serial.print(" ");
  Serial.print("  \tZ: "); Serial.print(mag.magnetic.z);     Serial.println("  \tgauss");

  // print out gyroscopic data
  Serial.print("Gyro  X: "); Serial.print(gyro.gyro.x); Serial.print(" ");
  Serial.print("  \tY: "); Serial.print(gyro.gyro.y);       Serial.print(" ");
  Serial.print("  \tZ: "); Serial.print(gyro.gyro.z);     Serial.println("  \tdps");

  // print out temperature data
  Serial.print("Temp: "); Serial.print(temp.temperature); Serial.println(" *C");

  Serial.println("**********************\n");*/

  motionXaxis = lsm.gyroData.x;

  if(abs(pre_motionXaxis - motionXaxis) > 1000){
    digitalWrite(led, HIGH);

  }
  else{
    digitalWrite(led, LOW);
  }
  pre_motionXaxis = motionXaxis;

}

So commenting out motionDetection(); works fine?

yes, it works fine without it.
One silly question, which probably doesn’t relevant: is it possible that computer performance affecting this symptom? Apparently, both (one with motionDetection and without) works just fine. This is very peculiar because I did not change anything from last night setting (except one of core I modified as your comments). All of sudden they are working fine. Do you think that there is some kind of cache? or something that has been built up on website? computer?
I am not sure if this making any sense at all. I am very beginner. and I thought this is something to do with this problem.
Any idea?

An update.

So I have two cores working on. Lets call them A and B.
Both devices didn’t work last night (same symptom as I described before). This morning both A and B was working fine somehow (I didn’t make any change). And I uploaded test code (one without motionDetection) to B, and now B is having previous symptom. For A, it worked fine for an hour, but now I am having trouble communicating through web (A however, is in workable cyan breathing state). When I terminate browser and reopen and run A it worked for few minutes and having delay.
Any suggestion?

this is my javascript and html source

/*jslint browser: true*/
/*jslint node: true*/
/*global $, jQuery, alert*/

var story = ["Peterpan", "Hulk", "Pooh", "Madagasca"],
    listStory = "",
    x,
    text,
    IDt2 = "",
    IDt3 = "",
    accessToken = "",
    setFunc = "setStory",
    getFunc = "getStory";


    function setStory() {
        "use strict";
        x = document.getElementById("numb").value;
        if (isNaN(x) || x < 1 || x > 5) {
            text = "Input not valid";
        } else {
            text = story[x - 1];
        }
        document.getElementById("resultInput").innerHTML = text;
    
        //set story to Sparks
        var requestURLt2 = "https://api.spark.io/v1/devices/" + IDt2 + "/" + setFunc + "/",
            requestURLt3 = "https://api.spark.io/v1/devices/" + IDt3 + "/" + setFunc + "/";
        $.post(requestURLt2, {
            params: x,
            access_token: accessToken
        });
        $.post(requestURLt3, {
            params: x,
            access_token: accessToken
        });
    
    }
    
    
//retreaving story mode from devices
function myTimer() {
    "use strict";
    var requestURLGetT2 = "https://api.spark.io/v1/devices/" + IDt2 + "/" + getFunc + "/?access_token=" + accessToken,
        requestURLGetT3 = "https://api.spark.io/v1/devices/" + IDt3 + "/" + getFunc + "/?access_token=" + accessToken,
        requestURLSetT2 = "https://api.spark.io/v1/devices/" + IDt2 + "/" + getFunc + "/?access_token=" + accessToken,
        requestURLSetT3 = "https://api.spark.io/v1/devices/" + IDt3 + "/" + getFunc + "/?access_token=" + accessToken,
        storyName,
        tempT2,
        tempT3;
    tempT2 = document.getElementById("T2Mode").value;
    tempT3 = document.getElementById("T3Mode").value;
    //console.log(tempT2);

    $.getJSON(requestURLGetT2, function (json) {

        if (json.result === 1) {
            storyName = 'Peterpan';
        } else if (json.result === 2) {
            storyName = 'Hulk';
        } else {
            storyName = 'no data';
        }

        document.getElementById("T2Mode").innerHTML = storyName + "	mode";

        if (json.result === false) {
            document.getElementById("T2Mode").innerHTML = "device not found";
        }
    });

    //console.log(tempT3);
    //    if (typeof (tempT3) === "undefined") {
    //        document.getElementById("T3Mode").innerHTML = "not connected";
    //    } else {
    $.getJSON(requestURLGetT3, function (json) {

        if (json.result === 1) {
            storyName = 'Peterpan';
        } else if (json.result === 2) {
            storyName = 'Hulk';
        } else {
            storyName = 'no data';
        }
        document.getElementById("T3Mode").innerHTML = storyName + "	mode";
        //document.getElementById("T2Mode").style.fontSize = "28px";
    });
    //    }
    $.getJSON(requestURLSetT2, function (json) {

        if (json.result === 1) {
            storyName = 'Peterpan';
        } else if (json.result === 2) {
            storyName = 'Hulk';
        } else {
            storyName = 'no data';
        }
        document.getElementById("T2Gst").innerHTML = storyName + "	moving";
        //document.getElementById("T2Mode").style.fontSize = "28px";
    });
    $.getJSON(requestURLSetT3, function (json) {

        if (json.result === 1) {
            storyName = 'Peterpan';
        } else if (json.result === 2) {
            storyName = 'Hulk';
        } else {
            storyName = 'no data';
        }
        document.getElementById("T3Gst").innerHTML = storyName + "	moving";
        //document.getElementById("T2Mode").style.fontSize = "28px";
    });
}

//var myVar = setInterval(function () {
//    "use strict";
//    myTimer();
//}, 1000);

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
    <head>
        <script type=text/javascript src="input.js"></script>
        <link rel="stylesheet" type="text/css" href="style.css">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
<body>
    

    <script>
        var myVar = setInterval(function () {
            myTimer();
        }, 1000);
    </script>

    <h1>TOIOT</h1>
    <h4> WEB sys </h4>
    <table id="input">
        <tr>
            <td id="list"> 1: Peterpan 2: Hulk 3: Pooh 4: Madagasca</td>
        </tr>
        <tr>
            <td>
                <input id="numb" type="number">
                <button type="button" onclick="setStory()">Submit</button>
                <span id="description">*select a story between 1 and 4:</span>
            </td>
        </tr>
        
    </table>
    
    <p id="description" style="font-size:12px"> you have selected: <span style="color:dodgerblue" id="resultInput"></span></p>
    <br>
    <br>  
    <br>


 

       <h4> TOY sys </h4>
        <table id="status">
            <tr>
                <th>  </th>
                <th> T2 <span id="description">51ff71065082554925270487</span> </th>
                <th> T3 <span id="description">51ff6a065082554924230887 </span></th>
            </tr>
            <tr>
                <th id="mod"> Gesture </th>
                <td><span id="T2Gst"></span></td>
                <td><span id="T3Gst"></span></td>
            </tr>
            <tr>
                <th id="mod"> Current Mode </th>
                <td><span id="T2Mode"></span></td>
                <td><span id="T3Mode"></span></td>
            </tr>
        </table>
    </body>
    </html>

Hi @cherltte. I don’t know the answer to your questions, but as an observation of your code, you should edit your post to remove your Core ID’s and (most importantly) your access token. With this information publicly posted, anyone could access your devices :fearful: For future posts in place of those two values, put something like "CORE ID HERE" and "ACCESSTOKEN HERE", simply as a placeholder.

Also, welcome to the :spark: community!

4 Likes

@gaudsend Thank you! I totally forgot about it.

Is there anyone who can help me?
Currently i am testing to set one second interval to publish for preventing overloaded communication to see if this helps. I will keep post the process.
Meanwhile if there are anyone who can’ help me, please leave a comment. Any help would be appreciated.