Interfacing Bolt with Arduino Using Hardware Serial UART communication

Im trying to get values from my Arduino to get sent to the Bolt cloud and my python server .

I succesfully managed to read the commands sent by bolt on the serial monitor in Arduino
The monitor correctly shows the moisture value as follows and detects the Bolt RD command

But the Bolt cloud does not recieve this Value and the Graph remains flat (0)
Neither does my python code in the Virtual server receive it .

OUTPUTS :


Python%20output

My Code for Arduino and Python are as follows :

ARDUINO :

#include <BoltIoT-Arduino-Helper.h>
#include <BoltDeviceCredentials.h>


const int AirValue = 1024;
const int WaterValue = 290;
int soilMoistureValue = 0;
int soilmoisturepercent = 0;
void setup() {
  // put your setup code here, to run once:
boltiot.begin(Serial);
Serial.setTimeout(100);
}
String data;
uint8_t dataRead = 0;

void loop()
{
  if (Serial.available() > 0) {

delay(50);
String readData = "";
Serial.print("cmd from bolt:");
while (Serial.available() > 0 ) {
  readData = Serial.readString();
}
Serial.println(readData);
if (readData.indexOf("RD") != -1) {
  boltiot.processPushDataCommand(analogRead(A0));
  
  Serial.print("data sent to bolt:");
  Serial.print(analogRead(A0));
}
  }
} 

PYTHON :

pythoncode

I get an error saying invalid ‘till’ value , im not quite sure what that means .

Developers , @vinayak.joshi
Can you please explain to me how im suppose to implement this thread https://docs.boltiot.com/docs/read-incoming-serial-data
with an example in my python program ?

2 Likes

Hi @kaushik.s98,

In the serialRead function, you don’t need to pass the pin number. But you have to pass the number of characters that you want to read from the incoming UART data.

response = mybolt.serialRead('10')

Bolt Python library is just a wrapper around Bolt Cloud API. Check here https://github.com/Inventrom/bolt-api-python/blob/master/boltiot/bolt.py

Do let me know in case you need further assistance.

1 Like

hey i tried doing what you told me

Python%20output

yet i get an error , please walk me through the process of atleast getting one pin to read through hardware serial
@rahul.singh1

i tried removing the sensor_value = int(data[‘value’]) and replaced it with str

the error seemed to be solved but there was no value displayed
the output was

"Sensor value is : "
@rahul.singh1 how do i solve this , iv been constantly posting on this forum for more than a month to get this to work , please try n support me through this , would really appreciate it ! :frowning:

Hi @kaushik.s98,

Before you convert it into str, first print the data after doing json.loads()

data = json.loads(response);
print (data)

and before doing serialRead, first call the serialBegin function.

Do let me know in case you need further assistance.

Hi @kaushik.s98,

I am also inviting @vinayak.joshi to help you out.

Hey I made the changes you suggested , and tried to debug .
yet i have no success ,
this is my current code and output
CODE
pythoncode

OUTPUT
Python%20output

When i click Deploy configuration on bolt cloud the serial monitor gives the following Readings

serialmonitor

The data count increases but no graph is plotted on the cloud

The python program is not even triggering the RD value anymore , only the bold cloud does ,
How do i continue with this ?

@vinayak.joshi @rahul.singh1 @ayanghosh974 Anyone who can help ?

Arduino Code

#include <BoltIoT-Arduino-Helper.h>
#include <BoltDeviceCredentials.h>


const int AirValue = 1024;
const int WaterValue = 290;
int soilMoistureValue = 0;
int soilmoisturepercent = 0;
void setup() {
  // put your setup code here, to run once:
boltiot.begin(Serial);
Serial.setTimeout(100);
}
String data;
uint8_t dataRead = 0;

void loop()
{
  if (Serial.available() > 0) {
    
    delay(50);
    String readData = "";
    Serial.print("cmd from bolt:");
    while (Serial.available() > 0 ) {
      readData = Serial.readString();
    }
    Serial.println(readData);
    if (readData.indexOf("RD") != -1) {
      boltiot.processPushDataCommand(analogRead(A0));
      
      Serial.print("data sent to bolt:");
      Serial.print(analogRead(A0));
    }
  }
}

@rahul.singh1 @vinayak.joshi , im aware that im depending alot on your replies but iv really tried alot and i still cant get this to work , i can just make any simple project to get certified , but i really want to make something worth sharing with a slight level of complexity , not just for the sake of a certificate .

Would really appreciate it if i could get some kind of active support , just till i can get atleast 1 sensor value to read

Hi @kaushik.s98,

It seems you have included the library for the BoltIoT-Arduino-Helper, but you seem to have used the commands wrongly. Since you have accessed the Serial object, which you also passed to boltiot.begin(Serial), the print statements in your code are interfering with the communication with the Bolt WiFi module.
Also, by using the readData variable to read data from the serial object into this variable, you are making sure that the Boltiot object does not get the RD\r command. So calling the processPushDataCommand is completely useless.

Please go through the following codes to understand how you have to use the library commands.

You just have to change what is returned by the pushData function in the above code, and it should work for you.

Also, remember that you SHOULD NOT use the Serial variable if you are using the library, as this may interfere with the communictaion.

@vinayak.joshi , thank you for your reply .
I have made the suggested changes and used the format above to refactor the code ,
This is how it looks now

#include <BoltIoT-Arduino-Helper.h>
#include <BoltDeviceCredentials.h>

const int AirValue = 1024;
const int WaterValue = 290;
int soilMoistureValue = 0;
int soilmoisturepercent = 0;

String pushData(String *data){
return “”+analogRead(A0);
}

void setup() {
boltiot.begin(Serial);
boltiot.setCommandString(“RD\r”,pushData);
}

void loop()
{
boltiot.handleCommand();
}

But now the serial monitor shows garbage values when i Push data to cloud …
I would really like it if you could make the changes to the code yourself and put it here to avoid further delay of results .
Thank you again for your reply

garbage valies

@rahul.singh1 @vinayak.joshi any idea ?

Hi @kaushik.s98,

What baud rate have you set for your Serial terminal?

The issue you are mentioning comes in if you have set the baud rate on the terminal to something other than 9600. Based on the amount of data you are getting, I would say, you have set it to higher than 9600.

That was for multiple transmissions , it is still 9600 . There’s something in the Arduino code that’s causing an error .

Hi @kaushik.s98,

I did some experimenting and realised that depending on which version of Arduino IDE you might face this issue.

To fix the issue for all Arduino IDEs, you need to change the pushData function form:

String pushData(String *data){
return “”+analogRead(A0);
} 

To

String pushData(String *data){
return String(analogRead(A0));
}

I have not been able to test this myself, as I am not certain which version of the Arduino IDE you are using. But another person was facing a similar issue and this helped.