Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sense_hat import SenseHat
- import datetime
- import thread
- import time
- import requests
- def getserial():
- # Extract serial from cpuinfo file
- cpuserial = "0000000000000000"
- try:
- f = open('/proc/cpuinfo','r')
- for line in f:
- if line[0:6]=='Serial':
- cpuserial = line[10:26]
- f.close()
- except:
- cpuserial = "ERROR000000000"
- return cpuserial
- def sendCrashRequest (url, user, pwd, headers, u_source, u_type, u_pressure, u_humidity, u_temperature_from_pressure, u_temperature_from_humidity, u_orientation_degree_pitch, u_orientation_degree_roll, u_orientation_degree_yaw, u_gyroscope_pitch, u_gyroscope_roll, u_gyroscope_yaw, u_orientation_radians_pitch, u_orientation_radians_roll, u_orientation_radians_yaw, u_accelerometer_yaw, u_accelerometer_raw_x, u_accelerometer_raw_y, u_accelerometer_raw_z, u_compass, u_compass_raw_x, u_compass_raw_y, u_compass_raw_z):
- print("crash request: " +str(datetime.datetime.now()))
- response = requests.post(url, auth=(user, pwd), headers=headers ,data="{\
- 'u_source': '"+str(u_source)+"',\
- 'u_type': '"+str(u_type)+"',\
- 'u_pressure': '"+str(u_pressure)+"',\
- 'u_humidity': '"+str(u_humidity)+"',\
- 'u_temperature_from_pressure': '"+str(u_temperature_from_pressure)+"',\
- 'u_temperature_from_humidity': '"+str(u_temperature_from_humidity)+"',\
- 'u_orientation_degree_pitch': '"+str(u_orientation_degree_pitch)+"',\
- 'u_orientation_degree_roll': '"+str(u_orientation_degree_roll)+"',\
- 'u_orientation_degree_yaw': '"+str(u_orientation_degree_yaw)+"',\
- 'u_gyroscope_pitch': '"+str(u_gyroscope_pitch)+"',\
- 'u_gyroscope_roll': '"+str(u_gyroscope_roll)+"',\
- 'u_gyroscope_yaw': '"+str(u_gyroscope_yaw)+"',\
- 'u_orientation_radians_pitch': '"+str(u_orientation_radians_pitch)+"',\
- 'u_orientation_radians_roll': '"+str(u_orientation_radians_roll)+"',\
- 'u_orientation_radians_yaw': '"+str(u_orientation_radians_yaw)+"',\
- 'u_accelerometer_roll': '"+str(u_accelerometer_roll)+"',\
- 'u_accelerometer_pitch': '"+str(u_accelerometer_pitch)+"',\
- 'u_accelerometer_yaw': '"+str(u_accelerometer_yaw)+"',\
- 'u_accelerometer_raw_x': '"+str(u_accelerometer_raw_x)+"',\
- 'u_accelerometer_raw_y': '"+str(u_accelerometer_raw_y)+"',\
- 'u_accelerometer_raw_z': '"+str(u_accelerometer_raw_z)+"',\
- 'u_compass_raw_x': '"+str(u_compass_raw_x)+"',\
- 'u_compass_raw_y': '"+str(u_compass_raw_y)+"',\
- 'u_compass_raw_z': '"+str(u_compass_raw_z)+"',\
- 'u_compass': '"+str(u_compass)+"'}")
- thread.exit()
- def sendPeriodicRequest (url, user, pwd, headers, u_source, u_type, u_pressure, u_humidity, u_temperature_from_pressure, u_temperature_from_humidity, u_orientation_degree_pitch, u_orientation_degree_roll, u_orientation_degree_yaw, u_gyroscope_pitch, u_gyroscope_roll, u_gyroscope_yaw, u_orientation_radians_pitch, u_orientation_radians_roll, u_orientation_radians_yaw, u_accelerometer_yaw, u_accelerometer_raw_x, u_accelerometer_raw_y, u_accelerometer_raw_z, u_compass, u_compass_raw_x, u_compass_raw_y, u_compass_raw_z):
- while True:
- print("periodic request: " +str(datetime.datetime.now()))
- response = requests.post(url, auth=(user, pwd), headers=headers ,data="{\
- 'u_source': '"+str(u_source)+"',\
- 'u_type': '"+str(u_type)+"',\
- 'u_pressure': '"+str(u_pressure)+"',\
- 'u_humidity': '"+str(u_humidity)+"',\
- 'u_temperature_from_pressure': '"+str(u_temperature_from_pressure)+"',\
- 'u_temperature_from_humidity': '"+str(u_temperature_from_humidity)+"',\
- 'u_orientation_degree_pitch': '"+str(u_orientation_degree_pitch)+"',\
- 'u_orientation_degree_roll': '"+str(u_orientation_degree_roll)+"',\
- 'u_orientation_degree_yaw': '"+str(u_orientation_degree_yaw)+"',\
- 'u_gyroscope_pitch': '"+str(u_gyroscope_pitch)+"',\
- 'u_gyroscope_roll': '"+str(u_gyroscope_roll)+"',\
- 'u_gyroscope_yaw': '"+str(u_gyroscope_yaw)+"',\
- 'u_orientation_radians_pitch': '"+str(u_orientation_radians_pitch)+"',\
- 'u_orientation_radians_roll': '"+str(u_orientation_radians_roll)+"',\
- 'u_orientation_radians_yaw': '"+str(u_orientation_radians_yaw)+"',\
- 'u_accelerometer_roll': '"+str(u_accelerometer_roll)+"',\
- 'u_accelerometer_pitch': '"+str(u_accelerometer_pitch)+"',\
- 'u_accelerometer_yaw': '"+str(u_accelerometer_yaw)+"',\
- 'u_accelerometer_raw_x': '"+str(u_accelerometer_raw_x)+"',\
- 'u_accelerometer_raw_y': '"+str(u_accelerometer_raw_y)+"',\
- 'u_accelerometer_raw_z': '"+str(u_accelerometer_raw_z)+"',\
- 'u_compass_raw_x': '"+str(u_compass_raw_x)+"',\
- 'u_compass_raw_y': '"+str(u_compass_raw_y)+"',\
- 'u_compass_raw_z': '"+str(u_compass_raw_z)+"',\
- 'u_compass': '"+str(u_compass)+"'}")
- # Set the request parameters
- url = 'https://dev20467.service-now.com/api/now/table/u_cardata'
- # Eg. User name="admin", Password="admin" for this code sample.
- user = 'admin'
- pwd = '1234'
- # Set proper headers
- headers = {"Content-Type":"application/json","Accept":"application/json"}
- sense = SenseHat()
- accident = False
- # Initialize Variables
- u_source = getserial();
- u_type = "";
- u_pressure = 0
- u_humidity = 0
- u_temperature_from_pressure = 0
- u_temperature_from_humidity = 0
- u_orientation_degree_pitch = 0
- u_orientation_degree_roll = 0
- u_orientation_degree_yaw = 0
- u_gyroscope_pitch = 0
- u_gyroscope_roll = 0
- u_gyroscope_yaw = 0
- u_orientation_radians_pitch = 0
- u_orientation_radians_roll = 0
- u_orientation_radians_yaw = 0
- u_accelerometer_pitch = 0
- u_accelerometer_roll = 0
- u_accelerometer_yaw = 0
- u_compass = 0
- u_compass_raw_x = 0
- u_compass_raw_y = 0
- u_compass_raw_z = 0
- #Send every 5 seconds a Request
- thread.start_new_thread(sendPeriodicRequest, (url, user, pwd, headers, u_source, 'periodic', u_pressure, u_humidity, u_temperature_from_pressure, u_temperature_from_humidity, u_orientation_degree_pitch, u_orientation_degree_roll, u_orientation_degree_yaw, u_gyroscope_pitch, u_gyroscope_roll, u_gyroscope_yaw, u_orientation_radians_pitch, u_orientation_radians_roll, u_orientation_radians_yaw, u_accelerometer_yaw, u_accelerometer_raw_x, u_accelerometer_raw_y, u_accelerometer_raw_z, u_compass, u_compass_raw_x, u_compass_raw_y, u_compass_raw_z));
- while True:
- u_pressure = round(sense.get_pressure(),3)
- u_humidity = round(sense.get_humidity(),3)
- u_temperature_from_pressure = round(sense.get_temperature_from_pressure(),3)
- u_temperature_from_humidity = round(sense.get_temperature_from_humidity(),3)
- u_orientation_degree = sense.get_orientation_degrees(),3)
- u_orientation_degree_pitch = round(u_orientation_degree.get('pitch'),3)
- u_orientation_degree_roll = round(u_orientation_degree.get('roll'),3)
- u_orientation_degree_yaw = round(u_orientation_degree.get('yaw'),3)
- u_gyroscope = sense.get_gyroscope()
- u_gyroscope_pitch = round(u_gyroscope.get('pitch'),3)
- u_gyroscope_roll = round(u_gyroscope.get('roll'),3)
- u_gyroscope_yaw = round(u_gyroscope.get('yaw'),3)
- u_orientation_radians = sense.get_orientation_radians()
- u_orientation_radians_pitch = round(u_orientation_radians.get('pitch'),3)
- u_orientation_radians_roll = round(u_orientation_radians.get('roll'),3)
- u_orientation_radians_yaw = round(u_orientation_radians.get('yaw'),3)
- u_accelerometer = sense.get_accelerometer()
- u_accelerometer_pitch = round(u_accelerometer.get('pitch'),3)
- u_accelerometer_roll = round(u_accelerometer.get('roll'),3)
- u_accelerometer_yaw = round(u_accelerometer.get('yaw'),3)
- u_accelerometer_raw = sense.get_accelerometer_raw()
- u_accelerometer_raw_x = round(u_accelerometer_raw.get('x'),3)
- u_accelerometer_raw_y = round(u_accelerometer_raw.get('y'),3)
- u_accelerometer_raw_z = round(u_accelerometer_raw.get('z'),3)
- u_compass = round(sense.get_compass(),3)
- u_compass_raw = sense.get_compass_raw()
- u_compass_raw_x = round(u_compass.get('x'),3)
- u_compass_raw_y = round(u_compass.get('y'),3)
- u_compass_raw_z = round(u_compass.get('z'),3)
- # Accident - On Left Side
- if u_orientation_degree_roll > 70 and u_orientation_degree_roll < 110:
- accident = True
- u_type = "Crash - On Left Side";
- # Accident - On Right Side
- if u_orientation_degree_roll > 250 and u_orientation_degree_roll < 290:
- accident = True
- u_type = "Crash - On Right Side";
- # Accident - On Roof
- if u_orientation_degree_roll >= 110 and u_orientation_degree_roll <= 250:
- accident = True
- u_type = "Crash - On Roof";
- if accident:
- #Need to install requests package for python
- #easy_install requests
- # Do the HTTP request
- thread.start_new_thread(sendCrashRequest, (url, user, pwd, headers, u_source, 'crash', u_pressure, u_humidity, u_temperature_from_pressure, u_temperature_from_humidity, u_orientation_degree_pitch, u_orientation_degree_roll, u_orientation_degree_yaw, u_gyroscope_pitch, u_gyroscope_roll, u_gyroscope_yaw, u_orientation_radians_pitch, u_orientation_radians_roll, u_orientation_radians_yaw, u_accelerometer_yaw, u_accelerometer_raw_x, u_accelerometer_raw_y, u_accelerometer_raw_z, u_compass, u_compass_raw_x, u_compass_raw_y, u_compass_raw_z));
- u_orientation_degree_roll = 0
- accident = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement