Python API for Bolt IOT

I have a Bolt IOT module up and running, standalone with sensors connected as well as interfaced to an Arduino board with UART interface.
I have problems with making the Python API interface. If the Bolt IOT already has a product associated and uploaded to it, will the Python API functions work?
I am calling the Python APIs from a Windows laptop with Ananconda loaded. The Bolt IOT Python package has been successfully loaded since the import of the package does not throw up any errors

@radhanand.anantha Can you please describe what problem you are facing? Any screenshots of the error and code would be great!

from boltiot import Bolt
api_key=“XXXXXXXXXXXXXXXXXXXXXX”
device_id=“BOLTXXXX”
mybolt=Bolt(api_key,device_id)
mybolt.serialBegin(‘9600’)
mybolt.serialWrite(‘RD/r’)
response=mybolt.serialRead()

Error:
TypeError: serialRead() missing 1 required positional argument: ‘char_till’

The api_key and device_id are ok since I am getting responses for version() and isOnline() functions

I have the Arduino code working with the Bolt library and I am sending three parameter values using the processPushDataCommand. I am able to draw line graphs of the three values in the Bolt IDE, so the values are being transmitted from the Arduino board. My requirement is to get the parameter values in Python so that I can run some ML algorithms

This code does not throw up an error but no data is returned. It shows success and blank data

Problem sorted out. Used the DHT22 example and did a SetCommandString and set my own command string. Able to get data in Python. Same thing should have worked with ProcessPushCommand in Arduino and calling with RD\r command in Python, but I guess some issues with CR+LF

3 Likes

I am facing the same issue and also tried using
setCommandString
but no luck.
As in my case the sensor values from arduino are perfectly being logged onto bolt cloud and I can visualize using graphs but when I try to use those analog values in my PYTHON script it gives me values that are not even close to the bolt csv logged values.
Here is my code for arduino:

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

#include <Servo.h> 
#ifndef API_KEY
#define API_KEY   "xxxxxx"
#endif
#ifndef DEVICE_ID
#define DEVICE_ID "xxxxx"
#endif

int servoPin = 3; 
Servo Servo1;
const int sensorMin = 0; // sensor minimum
const int sensorMax = 1024; // sensor maximum
boolean check;
String getAnalogData(String *data){
  String retval="";
  retval=retval+analogRead(A1)+",";
  retval=retval+analogRead(A2)+",";
  retval=retval+analogRead(A3);
  return retval;
}

void setup () {
 
  Serial.begin (9600);
  Servo1.attach(servoPin);
  pinMode(A1,INPUT);
  pinMode(A2,INPUT);
  pinMode(A3,INPUT);
  Serial.setTimeout(50);
  boltiot.begin(Serial);
  boltiot.setCommandString("RD\r",getAnalogData);
  boltiot.setCommandString("GetAnalogData",getAnalogData);
  }
 
void loop() {
  // read the input on analog pin 0:
  int sensor0 = analogRead(A1);
  //level sensor

  int sensor1 = analogRead(A2);
  //LDR
  int range = map(sensor1, sensorMin, sensorMax, 0, 3);
  
  int sensor2 = analogRead(A3);
  //rain
  boltiot.handleCommand();
}

The bolt cloud values are as follows:
image

Here is my python script:

mybolt.serialBegin('9600')
mybolt.serialWrite('RD/r')
response=mybolt.serialRead('10')

def get_levelsensor_value_from_pin(pin):
    """Returns the sensor value. Returns -999 if request fails"""
    try:
        response = mybolt.analogRead(pin)
        data = json.loads(response)
        if data["success"] != 1:
            print("Request not successfull")
            print("This is the response->", data)
            return -999
        levelsensor_value = int(data["value"])
        return levelsensor_value
    except Exception as e:
        print("Something went wrong when returning the sensor value")
        print(e)
        return -999

while True:
    # Step 1
    levelsensor_value = get_levelsensor_value_from_pin('A1')    
    print("The current sensor value is:", levelsensor_value)
    time.sleep(10)

    if levelsensor_value == -999:
        print("Request was unsuccessfull. levelsensor Skipping.")
        time.sleep(10)

I did the same with all three analog sensors and got this output.

image

PL help!! @shoeb.ahmed @pranav.kundaikar.inv @vinayak.joshi

Hi @narang.rahul.rahul8,

Since you are getting the data from the UART, for the python code, you have to use SerialWrite to first send the command “getAnalogData” to the Arduino and then use the command SerialRead to read the data from the Arduino, instead of using the analogRead command, which gets the data from the A0 pin of the Bolt WiFi module.

Sir can you please elaborate as I am new to this and finding it hard to understand. Maybe you can specify the part of code that needs to be changed.

Hi @narang.rahul.rahul8,

Please go through the following code shared by one of our other users to understnad what has to be done. Check line number 22, 23 and 24

1 Like