Z- Score analysis for sensors connected using arduino by uart

Hi Guys!

Currently i am working upon a project that contains the sensors connected bolt iot via uart as mentioned in the Docs site! by hardware interfacing. I had uploaded this code in my Arduino Uno.

Now, My requirement is to calculate the Z-score analysis for the sensors connected via the Arduino. I had connected the following sensors in the respective pins at Arduino.

1.LM35 - A0
2.LDR - A1
3.Moisture sensor. - A2.

So, Generally procedure taught in the lecture was reading the sensor value which is connected in the bolt iot module => A0 pin by using the Bolt Python library and code running in a ubuntu server. The code in the lecture to read the value from bolt A0 pin was below.


    import conf, json, time, math, statistics
    from boltiot import Sms, Bolt
    def compute_bounds(history_data,frame_size,factor):
        if len(history_data)<frame_size :
            return None

        if len(history_data)>frame_size :
            del history_data[0:len(history_data)-frame_size]
        for data in history_data :
            Variance += math.pow((data-Mn),2)
        Zn = factor * math.sqrt(Variance / frame_size)
        High_bound = history_data[frame_size-1]+Zn
        Low_bound = history_data[frame_size-1]-Zn
        return [High_bound,Low_bound]

    mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
    sms = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)

    while True:
        response = mybolt.analogRead('A0') //==> What code to replace to fetch the values from the Arduino for manipulation?
data = json.loads(response)
        if data['success'] != 1:
            print("There was an error while retriving the data.")
            print("This is the error:"+data['value'])

        print ("This is the value "+data['value'])
            sensor_value = int(data['value'])
        except e:
            print("There was an error while parsing the response: ",e)

        bound = compute_bounds(history_data,conf.FRAME_SIZE,conf.MUL_FACTOR)
        if not bound:
            print("Not enough data to compute Z-score. Need ",required_data_count," more data points")

            if sensor_value > bound[0] :
                print ("The light level increased suddenly. Sending an SMS.")
                response = sms.send_sms("Someone turned on the lights")
                print("This is the response ",response)
            elif sensor_value < bound[1]:
                print ("The light level decreased suddenly. Sending an SMS.")
                response = sms.send_sms("Someone turned off the lights")
                print("This is the response ",response)
        except Exception as e:
            print ("Error",e)

In the above code need to replace the line highlighted that is [mybolt.analogread(‘A0’)].So, what to do now to get the values from the Arduino Analog pins ?and do the z-score analysis.

Help me to sort it out by giving a resolution! @vinayak.joshi@shoeb.ahmed
Thanking you for spending the time here!

Hi @arjunraj,

First of all you will have to upload the following code to the Arduino to get the analog data.

Then in your python code, you will have to replace “response = mybolt.analogRead(‘A0’)” with 2 statements.

    response = mybolt.serialRead(10)

Also, the data[“value”] that you get will be a string containing 6 CSV values for all the analog data from the analog pins of the Arduino.

So you will have to replace the link “sensor_value = int(data[‘value’])” with

sensor_value = int(data['value'].split(",")[0])

This will give you sensor value for A0 pin of the Arduino. Replace 0 with 1, and you will get the data from pin 1, and so on.

Hi @vinayak.joshi,

I had tried to do the stuff as mentioned above. Sometimes it works fine for the first anomaly detection, But later for the second time, it shows up with some error, Describing the exception. I will also link the code here uploaded in the ubuntu server, and I do post the screenshot of the error, what I am getting right now. I used a light sensor to test this. For the first time, I increased the intensity by turning on the flashlight. later it throws an exception. Then I tried for decreasing the intensity by covering up the sensor. Even though it works once only by sending sms. But the image described below is the error i got after the first time.

So, considering this helps me towards fixing the issue. It will help me in completing my monitoring system well.

Exception Error

Hi @arjunraj,

Please modify the code at line 36 to the following

except Exception as e:

The reason for the issue is because the Arduino connected to the Bolt WiFi module did not respond to your query with appropriate data.

For now, this will help your code recover from the issue, but you need to look at your Arduino code to know what happened over there which caused this issue.