Error in command Import


#1

error
I am getting an error saying : cannot import name Email


#2

@mongakomal98 Can you please check if you have another file with the name as boltiot.py in your folder?


#3

Hi ,

In python, you can not name a file same as the inbuilt function/packages name. For example. email is an inbuilt package of python and I think you have created a file with name email.py also in your current directory.

To show the list of file in your current directory. Type listing command as below.

ls

Then remove the email.py file from your directory.

sudo rm email.py

Now run your program again.

Do let me know in case you need any other information.


#4

I am getting the temperature values but once I open the fridge door anomaly detection is not happening!!Capture
My code:

import conf, json, time,math,statistics
from boltiot import Bolt,Email
#minimum_limit=300
#maximum_limit=600
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]
Mn=statistics.mean(history_data)
Variance=0
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)
mailer =Email(conf.MAILGUN_API_KEY, conf.SANDBOX_URL,conf.SENDER_EMAIL,conf.RECIPIENT_EMAIL)
history_data=[]

while True:
print(“reading sensor value”)
response= mybolt.analogRead(‘A0’)

print(“sensor value is:”+str(data[‘value’]))

data=json.loads(response)
if data[‘success’]!=‘1’:
print(“error while recieving data”)
print(“this is the error”+data[‘value’])
time.sleep(10)
continue
sensor_value=int(data[‘value’])
sensor_value1=sensor_value/10.24
print(“current refrigerator temperature is:”+str(sensor_value))

print(“making request to mailgun to send email”)

response = mailer.send_email(“alert”,“current temp value is”+str(sensor_value))

sensor_value=0
try:
sensor_value=int(data[‘value’])

if sensor_value> maximum_limit or sensor_limit<minimum_limit:

print(“making request to mailgun”)

except e:
print(“There was an error while parsing the response:”,e)
continue
bound=compute_bounds(history_data,conf.FRAME_SIZE,conf.MUL_FACTOR)
if not bound:
required_data_count=conf.FRAME_SIZE-len(history_data)
print(“more data needed”)
history_data.append(int(data[‘value’]))
time.sleep(10)
continue
try:
if sensor_value>bound[0]:
sensor_value1=sensor_value/10.24
print(“The temperature value has increased”)
response=mailer.send_email(“Alert”,“Someone opened the fridge door”)
print(“email response is:”,response)
history_data.append(sensor_value);
except Exception as e:
print(“error”,e)
time.sleep(10)


#5

Refer this code:

import conf, json, time,math,statistics
from boltiot import Bolt,Email

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]
Mn=statistics.mean(history_data)
Variance=0
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)
mailer =Email(conf.MAILGUN_API_KEY, conf.SANDBOX_URL,conf.SENDER_EMAIL,conf.RECIPIENT_EMAIL)
history_data=[]
while True:
print(“reading sensor value”)
response= mybolt.analogRead(‘A0’)

print(“sensor value is:”+str(data[‘value’]))

data=json.loads(response)
if data[‘success’]!=‘1’:
print(“error while recieving data”)
print(“this is the error”+data[‘value’])
time.sleep(10)
continue
sensor_value=int(data[‘value’])
sensor_value1=sensor_value/10.24
print(“current refrigerator temperature is:”+str(sensor_value))

print(“making request to mailgun to send email”)

response = mailer.send_email(“alert”,“current temp value is”+str(sensor_value))

sensor_value=0
try:
sensor_value=int(data[‘value’])
except e:
print(“There was an error while parsing the response:”,e)
continue
bound=compute_bounds(history_data,conf.FRAME_SIZE,conf.MUL_FACTOR)
if not bound:
required_data_count=conf.FRAME_SIZE-len(history_data)
print(“more data needed”)
history_data.append(int(data[‘value’]))
time.sleep(10)
continue
try:
if sensor_value>bound[0]:
sensor_value1=sensor_value/10.24
print(“The temperature value has increased”)
response=mailer.send_email(“Alert”,“Someone opened the fridge door”)
print(“email response is:”,response)
history_data.append(sensor_value);
except Exception as e:
print(“error”,e)
time.sleep(10)


#6

@vinayak.joshi Can you please help her out?


#7

@mongakomal98,

It seems that you have set a large Frame size in the configurations of your code.

You typically have to wait, 10*FRAME_SIZE seconds before the anomaly detection algorithm has enough data points to actually work.

Either set the FRAME_SIZE in your configurations to a lower value.

Do understand, that Machine learning algorithms require you to tune them before they start working properly.
As such, you will have to experiment with different values for FRAME_SIZE and MUL_FACTOR, in your config file before the system correctly detects an anomaly.