Arduino interfacing


#1

I have interfaced a distance sensor with arduino and then I have connected arduino and bolt (tx of arduino to rx of bolt) but when i do serial read i get around 8-10 values and i need to refresh everytime. Tell me a way that values will be displayed automatically and continuously when i do serial read


#2

You can read your sensor in every 5 minutes or whatever time delay you want to give while reading values. then send last read value to the bolt so it will show continuous value. Give time delay while reading sensor values.
You can check the given link below for reference


#3

I have referenced your code for data visulation but it is not working.
I am trying data visulation of moisture sensor which is connected to arduino but it is not working.
I am writing my arduino +html code below .Can you find out where is the fault int that

ARDUINO CODE-

#include <SoftwareSerial.h>
SoftwareSerial mySerial(8,9);
String data;
//uint8_t dataRead = 0

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

void loop()
{
if (mySerial.available() > 0) {
delay(50);
String readData = “”;
Serial.print(“cmd from bolt:”);
while (mySerial.available() > 0 ) {
readData = readData + (char)mySerial.read();
}
Serial.println(readData);
if (readData.indexOf(“RD”) != -1) {
String data = sensor_data_to_push();
mySerial.print(data);
Serial.print(“data sent to bolt:”);
Serial.print(data);
}
}
}

String sensor_data_to_push() {
String data;
float hum = analogRead(A0);
data = String(hum);
return data;
}


HTLM CODE -

  <!doctype html>
 <html>
 <head>
 <title>BOLT</title>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/canvasjs/1.7.0/canvasjs.min.js"></script>
 </head>
 <body bgcolor="grey" >
 <center> 
     <font color="skyblue" size="5px"><h3> <b>Data Visulation</b></h3> <br></font>
 <div id="chartContainer1" style="height: 30rem; padding-top:1rem;width:70%;box-shadow: 0px 1px 5px 0px #000;background: -webkit-linear-gradient(#0073A2, #2196f3);margin-top:0.05rem;"> </div> 
 <script type="text/javascript">
 var dps1;
 var chart1;
 window.onload = function ()
 {
   dps1 = []; //dataPoints. 
   chart1 = new CanvasJS.Chart("chartContainer1",{
    title :
     {
      text: "Soil Moisture Data "
     },
     backgroundColor: "transparent",
     height: 500,
       axisX: 
       { title: "Time",
       labelFontFamily: "Segoe UI Light",
       lineThickness: 0,
       tickThickness: 0,
       labelFontColor:"white",
       titleFontColor: "white",
       titleFontFamily:"Segoe UI Light",
       },
       axisY:
        { stripLines:[
          { value:0,
          thickness:1,
          color: "white",
          }],
        tickThickness: 0.5,
        minimum: 0,
        maximum: 1450,
        title: "Soil moisture", 
        labelFontFamily: "Segoe UI Light",
        gridThickness: 0,
        labelFontColor:"white",
        titleFontColor: "white",
        titleFontFamily:"Segoe UI Light",
        },
       data: [{
       type: "line",
       dataPoints : dps1,
       markerColor:"white",
       color:"white",
       }]
    });
     chart1.render();
     console.log("in on laod");
     var yVal = 15; 
     var updateInterval = 5000;
     setInterval(5000);
     setBaud(9600);
     WRSerialData();
     setInterval(function(){sendSerialData()}, updateInterval);    
}
     
     
function setBaud(baud) {
    var xmlhttp = new XMLHttpRequest();
    switch(baud) {
      case 2400:
        baud = 0;
        break;
      case 4800:
        baud = 1;
        break;
      case 9600:
        baud = 2;
        break;
      case 19255555200:
        baud = 3;
        break;
      default:
        baud = 4;
    }
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        {
        }
      }
    };
    xmlhttp.open("GET","/serialBegin?baud=2&deviceName={{Name}}",true);
    xmlhttp.send();
  }
     
     
     
     
function WRSerialData() {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var obj = JSON.parse(xmlhttp.responseText);
        if(obj.success=="1")
        {
          x = xmlhttp.responseText
          obj = JSON.parse(x);
          hAndT = obj.value 
          if( hAndT.indexOf("empty") == -1 )
          {
            var commaIndex = hAndT.indexOf(",") ;
            var soil = "";
            for (var i = 0; i < commaIndex; i++) {
              soil = soil + hAndT[i];
            }
            soil = parseFloat(soil);
          }
        }
        else{
        }
      }
    };
    xmlhttp.open("GET","/serialWR?data=hello&deviceName={{Name}}",true);
    xmlhttp.send();
  } 
     
     
 function updateChart1(soil) 
     {
          yVal = parseInt(soil);
          var xVal = new Date(); 
          dps1.push({x: xVal,y: yVal});
           if (dps1.length > 5 )
           {
             dps1.shift();
           }
          chart1.render();
     } 
     
     
     
     
function sendSerialData(serialdata) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        {
        var obj = JSON.parse(xmlhttp.responseText);
        if(obj.success=="1")
        {
          x = xmlhttp.responseText
          obj = JSON.parse(x);
          hAndT = obj.value 
          if( hAndT.indexOf("empty") == -1 )
          {
            var commaIndex = hAndT.indexOf(",") ;
            var soil = "";
            for (var i = 0; i < commaIndex; i++) {
              soil = soil + hAndT[i];
            }
            soil = parseFloat(soil);
            updateChart1(soil);
          }
        }
        else{
        }
        }
      }
    };
    xmlhttp.open("GET","/serialWR?data=hello&deviceName={{Name}}",true);
    xmlhttp.send();
}
 </script>
 </center> 
 </body> 
 </html>

#4

@akkaman007
The arduino code that you are using checks if the Bolt has sent an “RD” command to request for data. Pointed by the below line.
if (readData.indexOf("RD") != -1)
This command is sent by the bolt when you create a hardware configuration in the developer console. You will be prompted to create the hardware configuration while creating a new product, or you can create it for an existing product by clicking on the Hardware configuration icon in the products tab.

Since you are using an arduino to connect the sensor, you need to select UART option in the hardware configuration. Select only 1 data field position from the dropdown since you are just collecting moisture sensor data on A0. and you can the variable name as moisture or anything else but you need to note this as this variable will be used subsequently to plot the graphs. Let the data rate be 5 mins & the UART baud rate will be 9600.

Once the hardware configuration has been created, you can go to the devices tab & click on Push Data icon for the respective Bolt device for which you want to collect data. The bolt will start collecting data from the arduino every 5 mins.

To view the data create a javascript file with a .js extension e.g. moisture.js and add the following code in your text editor.
setChartType('lineGraph')
plotChart('time_stamp', 'moisture') //here you should write the variable name given by you in the hardware configuration

The last step is to save this file and upload it to your Bolt Cloud account using the Upload tab. After uploading the file, you will be redirected to the products tab. You should select the uploaded file as the home page for the product containing your hardware configuration.

You can then go to your home page & click on the device. If all steps are performed correctly then you should be able to view a line graph of the data collected.


Data visulisation of LDR sensor with Arduino
#5

Where should i insert the following code-

setChartType('lineGraph')
plotChart('time_stamp', 'moisture') //here you should write the variable name given by you in the hardware configuration

in my above HTML code.


#6

Hi @akkaman007
Save that file with js extension. For example, moisture.js and upload it to the bolt cloud.
After uploading link mosture.js file with a product. Linking the js file to a product is same as linking the HTML file.

Let me know in case you need further assistance.


#7

@akkaman007 you don’t need your previous HTML code. you need to create a completely new file with just those 2 lines and follow the process I gave above. Let me know if you have any more doubts.