Javascript functions: Error in variable defined already as pins

As you can see in the code, I have made a fuction to run only once at start. But the issue is that when I run it the variable ph_value and other variables show as undefined but it is already defined in the pins…

//defining prev variables only once
var myFunc = function func(){
    if( myFunc.fired ) return;
    myFunc.fired = true;
    var prev_ph_value = ph_value;
    var prev_turb_value = turb_value;
    var prev_tds_value = tds_value;
    var prev_temp_value = temp_value;
};

myFunc();

//setting anomalous values
if (ph_value >= prev_ph_value){
    var anomalous_ph_value = ph_value - prev_ph_value;
}
else{
    var anomalous_ph_value = prev_ph_value - ph_value;
}

if (turb_value >= prev_turb_value){
    var anomalous_turb_value = turb_value - prev_turb_value;
}
else{
    var anomalous_turb_value = prev_turb_value - turb_value;
}

if (tds_value >= prev_tds_value){
    var anomalous_tds_value = tds_value - prev_tds_value;
}
else{
    var anomalous_tds_value = prev_tds_value - tds_value;
}

if (temp_value >= prev_temp_value){
    var anomalous_temp_value = temp_value - prev_temp_value;
}
else{
    var anomalous_temp_value = prev_temp_value - temp_value;
}

//making charts
var Graph3 = new boltGraph();
Graph3.setChartType("lineGraph");
Graph3.setAxisName('Time','pH');
Graph3.plotChart('time_stamp','ph_value','anomalous_ph_value');

var Graph3 = new boltGraph();
Graph3.setChartType("lineGraph");
Graph3.setAxisName('Time','TDS(ppm)->');
Graph3.plotChart('time_stamp','tds_value','anomalous_tds_value');

var Graph3 = new boltGraph();
Graph3.setChartType("lineGraph");
Graph3.setAxisName('Time','Turbidity(NTU)');
Graph3.plotChart('time_stamp','turb_value','anomalous_turb_value');

var Graph3 = new boltGraph();
Graph3.setChartType("lineGraph");
Graph3.setAxisName('Time','Temperature(*C)');
Graph3.plotChart('time_stamp','temp_value','anomalous_temp_value');

var Graph3 = new boltGraph();
Graph3.setChartType("lineGraph");
Graph3.setAxisName('Time','Battery Voltage(Volt)');
Graph3.plotChart('time_stamp','battery_volt');

var Graph3 = new boltGraph();
Graph3.setChartType("lineGraph");
Graph3.setAxisName('Time','Solar Power(Watts)');
Graph3.plotChart('time_stamp','solar_power');

//setting prev. values
var prev_ph_value = ph_value;
var prev_turb_value = turb_value;
var prev_tds_value = tds_value;
var prev_temp_value = temp_value;


Sorry due to limit, I had to reply…

Hi @electronicspro,

Bolt Graph does not expose the data API. The parameter you are passing inside the plotChart does not contain any data.

Graph3.plotChart(‘time_stamp’,‘temp_value’,‘anomalous_temp_value’);

What exactly are you trying to achieve?

@rahul.singh1 Ok so let me try to explain what I want to do… I have 4 water sensors, battery voltage and solar power sent to bolt wifi module using uart communication, then it is sent to bolt cloud where I want to make anomalous value based on difference between previous reading sent earlier (for the first time it will be same as the first reading received) and readings received(except battery voltage and solar power) and plot the two values on a line chart. I am not much familiar with ML right now hence I chose the simple way to detect any anomalous value.

Hi @electronicspro,

I think it will be easier if you use the Python code to achieve the same.

Check the below reference -

  1. https://trainings.boltiot.com/courses/429176/lectures/6847053
  2. https://github.com/Inventrom/bolt-api-python
  3. Check the code section for this project https://www.hackster.io/bolt-iot-developers-u-p-region/trash-talker-using-bolt-iot-f2af5e
1 Like

Yeah even I was thinking of the same. I had not completed the lecture on python yet. Anyway thanks for your precious time.

1 Like