Arduino connected to bolt -2 analog sensors

Dear Sir/Madam,
I wish to connect two sensors with arduino uno and boltiot. I followed the following post:
https://forum.boltiot.com/t/how-to-connect-multiple-sensors-to-bolt/1220/5?u=swethatsunil.mec
But, now both the output graphs are showing the same readings. Please help. Following are screenshots for the same.

Hi @swethatsunil.mec,

Please clarify,

Where did you get the code which tells you that pinMode function can be used in the manner
pinMode(A0, channel11)???

First of all, you need to modify these lines of code

pinMode(A0, channel11);
pinMode(A1, channel12); 

To

pinMode(A0, INPUT);
pinMode(A1, INPUT);

Also change

boltiot.processPushDataCommand(analogRead(channel11), analogRead(channel12));

to

boltiot.processPushDataCommand(analogRead(A1), analogRead(A1));

Once you upload the modified code to the Arduino, your Arduino UNO will be able to upload data to the Bolt Cloud properly.

@vinayak.joshi
Sir, How can we do the same using setCommandString,i.e., pushing 2 analog sensor data to bolt from Arduino ?

Hi @swethatsunil.mec,

This example in the BoltIoT-Arduino-Helper library can be modified to send 6 analog sensor data to the Bolt Cloud using the setCommandString function. Please refer the same and modify it for your needs.
Just change the line from

boltiot.setCommandString(“RD\r”,pushData);

to

boltiot.setCommandString(“RD\r”,getAnalogData);

@vinayak.joshi Sir,
I did the Arduino code part, but how do I access the values or control sensors from Bolt cloud ?



This is how I attempted it; but it is wrong somehow.
I want to read two analog sensors and control brightness of a LED.

Hi,

As per the code you have shared for the Arduino, you are sending back only 2 values when the Arduino receives the “RD\r” command from the Bolt Cloud.

But you have configured the Bolt Cloud product to look for 3 CSV values.
Just configure the Bolt Cloud product to look for only 2 CSV values and the system will start logging the data sent by the Arduino.

@vinayak.joshi Sir,
I did with 2 CSV value and it is working fine with push data command, but I also want to control brightness of an LED. Since that is done using command handler, I tried receiving 2 analog values and controlling an LED using the code shown in the previous post. That is why I declared 3 CSV, please guide in my doing this.

Hi,

You don’t need the 3rd CSV value for that. The CSV value is just the data that gets sent from the Arduino to the Bolt Cloud.

For controlling the LED, you have to send data from the Bolt Cloud to the Arduino. This can be done using the serialWrite function in your code javascript code.

singleButton({ name : "turn on", action :"serialWrite", arguments : ["TON"], align : "left"});
singleButton({ name : "turn off", action :"serialWrite", arguments : ["TOFF"], align : "right"});

In your Arduino code, you will have to add the following code to the setup function.

boltiot.setCommandString("TON", turnOnLED);
boltiot.setCommandString("TOFF", turnOffLED);

Also, you will have to add 2 new functions to your Arduino code as follows:

String turnOnLED(String *data){
    //Add code here to turn on the LED.
    return "";
}


String turnOffLED(String *data){
    //Add code here to turn off the LED.
    return "";
}

Once done, you will see 2 buttons in your device view. One will turn on the LED and the other will turn off the LED.

@vinayak.joshi
Dear Sir,
I am still getting the readings in graph, but LED is not switching on using the buttons.

Hi @swethatsunil.mec,

Please go through the following reference sheet for functions available on the Arduino IDE and how they are supposed to be used.

You have used analogWrite in an attempt to control pin A5.
There are 2 problems with your approach.
analogWrite only works for pin 3,5,6, 9,10 and 11 on the Arduino UNO, or the Boltduino. These pins can also be identified by the ‘~’ mark next to them on the board. The other pins will be like 7, or 8. But these pins will be printed at ~3 or ~6.

So analogWrite does not work with pin A5.

Even if it did work on pin A5, analogWrtie takes a number in the range of 0 to 255 to set the voltage on the given pin.
0 for 0 v and 255 for 5v.
In Arduino ‘LOW’ translates to 0, which is correct for you, where as ‘HIGH’ translates to a 1, which means that even if analogWrite worked for pin A5, you would be setting a voltage of about 0.02v to the pin, which would not be enough to turn on an LED.

The correct way of doing this, is to use the instruction

digitalWrite(A5, HIGH);

To turn the LED on, and

digitalWrite(A5, LOW);

to turn the LED off.

Please read up on the functions available on the Arduino documentation here before trying them out.

@vinayak.joshi
Dear Sir,
Will the buttons work only after 5 min, that is if I have selected data transfer to be done every 5 minutes in configuration of product ?

Hi @swethatsunil.mec,

Did you test the changes I mentioned in the Arduino code?

The buttons should work immediately. The 5 minutes interval that you set in the hardware configurations is only meant for collecting data from the Bolt WiFi module.

@vinayak.joshi
Dear Sir,
I made the changes that you said, but the buttons are not working always. It is working only after an interval of 5 minutes.

Hi @swethatsunil.mec,

Please do not post screenshots of your arduino code. Please post the actual code that you wrote. This way I can make the required edits to your code and reply back to you. It is not possible for me to copy the code from your screenshots.

I just realised what is causing the issue. In an earlier attempt you had used the handleCommandString to set the pushData fucntion for the command RD\r and were returning the 2 analogReadings via the pushData function. You can handle commands being sent via the Bolt Cloud, “only if” you are doing this.

YOU SHOULD NOT USE THE handleCommand AND THE processPushDataCommand IN THE SAME CODE. THEY INTERFERE WITH EACH OTHER.

Right now, you will need to remove the ‘processPushDataCommand’ function call from your code.

Add the String pushData(String *data) function which you had used earlier to send back 2 analog readings, and use the boltiot.setCommandString("RD\r",pushData); instruction in your setup function.

Your code will start working as expected.

@vinayak.joshi
Dear Sir,
Thank you for your response, the buttons are now working fine, but I want the Led to be turned on using an FSR. That is when the FSR is clicked once, the LED glows, one more click and it should go off. The following is the code that I have written for the same. But it is not working.

#include <BoltIoT-Arduino-Helper.h>
#ifndef API_KEY
#define API_KEY “–”
#endif
#ifndef DEVICE_ID
#define DEVICE_ID “–”
#endif
void setup(){
boltiot.begin(Serial);
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(3,OUTPUT);
boltiot.setCommandString(“TON”, turnOnLED);
boltiot.setCommandString(“TOFF”, turnOffLED);
boltiot.setCommandString(“RD\r”,getAnalogData);
boltiot.setCommandString(“GetAnalogData”,getAnalogData);
}
String getAnalogData(String *data){
String retval="";
retval=retval+analogRead(A0)+",";
retval=retval+analogRead(A1);
return retval;
}
String turnOnLED(String *data){
analogWrite(3,255);
return “”;
}
String turnOffLED(String *data){
analogWrite(3,0);
return “”;
}
int count=0;
void loop(){
boltiot.handleCommand();
int ldr=analogRead(A0);
int fsr=analogRead(A1);
if (fsr>=4){
count++;
if (count%2!=0){
turnOnLED;
}
else{
turnOffLED;
}
}
}