def readAccel(isG): accel = Adafruit_ADXL345.ADXL345(address=0x53, busnum=1) accel.set_range(Adafruit_ADXL345.ADXL345_RANGE_2_G) x, y, z = [r * SCALE_MULTIPLIER for r in accel.read()] if isG is False: (x, y, z) = tuple(r * EARTH_GRAVITY_MS2 for r in (x, y, z)) return x, y, z
def motion(): accel = Adafruit_ADXL345.ADXL345() x, y, z = accel.read() if x > 180 or y > 180 or z > 180: print "motion detected" lcd.clear() lcd.message("Motion Detected") time.sleep(1)
def setup(): global accel, logger logger = logging.getLogger(__name__) if logLevel == "debug": logger.setLevel(logging.DEBUG) elif logLevel == "info": logger.setLevel(logging.INFO) elif logLevel == "error": logger.setLevel(logging.ERROR) else: logger.setLevel(logging.INFO) streamHandler = logging.StreamHandler() rollingFileHandler = RotatingFileHandler(logFilePath, maxBytes=10000, backupCount=4) formatter = logging.Formatter(loggingFormat) streamHandler.setFormatter(formatter) rollingFileHandler.setFormatter(formatter) logger.addHandler(streamHandler) logger.addHandler(rollingFileHandler) # Create an ADXL345 instance. accel = Adafruit_ADXL345.ADXL345() # Alternatively you can specify the device address and I2C bus with parameters: #accel = Adafruit_ADXL345.ADXL345(address=0x54, busnum=2) # You can optionally change the range to one of: # - ADXL345_RANGE_2_G = +/-2G (default) # - ADXL345_RANGE_4_G = +/-4G # - ADXL345_RANGE_8_G = +/-8G # - ADXL345_RANGE_16_G = +/-16G # For example to set to +/- 16G: accel.set_range(Adafruit_ADXL345.ADXL345_RANGE_16_G) # Or change the data rate to one of: # - ADXL345_DATARATE_0_10_HZ = 0.1 hz # - ADXL345_DATARATE_0_20_HZ = 0.2 hz # - ADXL345_DATARATE_0_39_HZ = 0.39 hz # - ADXL345_DATARATE_0_78_HZ = 0.78 hz # - ADXL345_DATARATE_1_56_HZ = 1.56 hz # - ADXL345_DATARATE_3_13_HZ = 3.13 hz # - ADXL345_DATARATE_6_25HZ = 6.25 hz # - ADXL345_DATARATE_12_5_HZ = 12.5 hz # - ADXL345_DATARATE_25_HZ = 25 hz # - ADXL345_DATARATE_50_HZ = 50 hz # - ADXL345_DATARATE_100_HZ = 100 hz (default) # - ADXL345_DATARATE_200_HZ = 200 hz # - ADXL345_DATARATE_400_HZ = 400 hz # - ADXL345_DATARATE_800_HZ = 800 hz # - ADXL345_DATARATE_1600_HZ = 1600 hz # - ADXL345_DATARATE_3200_HZ = 3200 hz # For example to set to 6.25 hz: accel.set_data_rate(Adafruit_ADXL345.ADXL345_DATARATE_3200_HZ) getRawData() logger.info("Vibration sensor set up done")
def gatherData(): accel = Adafruit_ADXL345.ADXL345() while (1): # Read the X, Y, Z axis acceleration values and print them. x, y, z = accel.read() print('X={0}, Y={1}, Z={2}'.format(x, y, z))
def ace (): # Create an ADXL345 instance. accel = Adafruit_ADXL345.ADXL345() while True: # Read the X, Y, Z axis acceleration values and print them. x, y, z = accel.read() print('X={0}, Y={1}, Z={2}'.format(x, y, z)) # Wait half a second and repeat. time.sleep(1.5) #print('Printing X, Y, Z axis values, press Ctrl-C to quit...') return(x)
def ace(): accel = Adafruit_ADXL345.ADXL345() while True: # Read the X, Y, Z axis acceleration values and print them. x, y, z = accel.read() print('X={0}, Y={1}, Z={2}'.format(x, y, z)) # Wait half a second and repeat. time.sleep(1.5) if abs(y) <= 5: print("Values too small /n") else: AFS(y, 1711, 6, 10) #print('Printing X, Y, Z axis values, press Ctrl-C to quit...') return (x)
def gatherData(): accel = Adafruit_ADXL345.ADXL345() data = bytearray() while (len(data)<123): # Read the X, Y, Z axis acceleration values and print them. x, y, z = accel.read() newData = "X=" + str(x) + ",Y=" + str(y) + ",Z=" + str(z) + "; " new_data_bytes = bytearray(newData) index = 0 while((len(data)<123)and(index < len(new_data_bytes))): #To collect exactly 123 bytes of data. data.append(new_data_bytes[index]) index = index + 1 return bytes(data)
def __init__(self): print("Initializing") self._serial = self.getserial() self._accel = Adafruit_ADXL345.ADXL345() self._defined_server_destinations = [ 'http://10.10.10.14:8080/test', 'http://jpf-flask-pi-iot.cfapps.io/test', 'http://katie-flask-pi-iot.cfapps.io/test', 'http://megan-flask-pi-iot.cfapps.io/test', 'http://david-flask-pi-iot.cfapps.io/test', 'http://shane-flask-pi-iot.cfapps.io/test' ] self.print_server_destinations() self._invalid_server_destinations = list() self._valid_server_destinations = self.get_valid_server_destinations() self.print_server_destinations() self.print_invalid_server_destinations() self.print_valid_server_destinations()
class Accelerometer: accel = Adafruit_ADXL345.ADXL345() def get(self): while True: x, y, z = Accelerometer.accel.read() f = Accelerometer.e try: print('X={0}, Y={1}, Z={2}'.format(x, y, z)) return 'X={0}, Y={1}, Z={2}'.format(x, y, z) except IOError as e: print("Error: Accelerometer: %s." % (e.message, )) def acceleration(self): while True: x, y, z = Accelerometer.accel.read() try: acceleration = math.sqrt(x**2 + y**2 + z**2) print(acceleration) return acceleration except IOError as e: print("Error with calculating Magnitude of Acceleration: %s." % (e.message, ))
def butter_lowpass(cutoff, fs, order=5): nyq = 0.5 * fs normal_cutoff = cutoff / nyq b, a = butter(order, normal_cutoff, btype='low', analog=False) return b, a def butter_lowpass_filter(data, cutoff, fs, order=5): b, a = butter_lowpass(cutoff, fs, order=order) y = lfilter(b, a, data) return y # Create an ADXL345 instance. accel = Adafruit_ADXL345.ADXL345() # Get I2C bus bus = smbus.SMBus(1) # Alternatively you can specify the device address and I2C bus with parameters: #accel = Adafruit_ADXL345.ADXL345(address=0x54, busnum=2) # You can optionally change the range to one of: # - ADXL345_RANGE_2_G = +/-2G (default) # - ADXL345_RANGE_4_G = +/-4G # - ADXL345_RANGE_8_G = +/-8G # - ADXL345_RANGE_16_G = +/-16G # For example to set to +/- 16G: #accel.set_range(Adafruit_ADXL345.ADXL345_RANGE_16_G)
# draw.py # # apt-get install python-pygame or pip install pygame import socket # Import socket module import time import pygame import Adafruit_ADXL345 import sys pygame.init() accel = Adafruit_ADXL345.ADXL345(address=0x53, busnum=0) #Variables #Colors WHITE = (255, 255, 255) RED = (255, 0, 0) BLACK = (0, 0, 0) GREEN = (30, 105, 53) GOLD = (255, 215, 0) #Sizes size = width, height = 800, 480 screen = pygame.display.set_mode(size) #Directions DOWN = 0 LEFT = 0 RIGHT = 0 UP = 0 #Drawings theDrawing = [] Drawings = [] #Levels Level = 1 #start at level 1 #coordinates
def __init__(self): rospy.init_node('acceleometer_ros', anonymous=True) self.acc_pub = rospy.Publisher("/accel", AccelStamped, queue_size=1) self.accel = Adafruit_ADXL345.ADXL345() self.seq = 0 print("Publisher Node Initialized")
def __init__(self): ### ADXL345 coding # Create an ADXL345 instance with I2C bus parameters self.accel = Adafruit_ADXL345.ADXL345(address=0x53, busnum=1)
def main(): # Create an ADXL345 instance. accel = Adafruit_ADXL345.ADXL345() #Set up SMTP email smtpUser = '******' smtpPass = '******' toAddr = '[insert email address to send to]' subject = 'Someone is moving your motorbike!' header = 'To: ' + toAddr + '\n' + 'From: ' + smtpUser + '\n' + 'Subject: ' +subject s = smtplib.SMTP('smtp.gmail.com', 587) s.ehlo() s.starttls() s.ehlo s.login(smtpUser, smtpPass) #Set up UDP socket for sending messages to Node-red for google home integration UDP_IP = "0.0.0.0" UDP_PORT = 5000 Message = "Motorbike movement detected. Alarm active" sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) global kill, alarm_switch kill = True #To terminate program alarm_switch = True #To arm and disarm device #Start up threads t1 = threading.Thread(target = ga_commands) t1.start() t2 = threading.Thread(target = kill_thread) t2.start() #Initialise variables x, y, z = accel.read() count = 0 while(kill): x_, y_, z_ = accel.read() #Update parameters if(alarm_switch): #If a movement of more than 30 degrees is detected for 5 seconds #Perform immobilsing routine if (abs(x-x_) > 30 or abs(y-y_) > 30 or abs(z-z_) > 30): count = count +1 if (count > 5): body = 'Motorbike movement detected' s.sendmail(smtpUser, toAddr, header +'\n' + body) #Send emali sock.sendto(Message, (UDP_IP, UDP_PORT)) #Broadcast over google home print('Sending Message') x, y, z = accel.read() count = 0 else: count = 0 # Wait half a second and repeat. time.sleep(0.5) s.quit()
def __init__(self): self.imu = adxl.ADXL345() super().__init__()
#sensor types @unique class SensorType(Enum): Temperature = 1 Acceleration = 2 Humidity = 3 GPSPosition = 4 Speed = 5 customSensorIdProvided = False #ADXL345 accelerometer instance accelerometer = Accel.ADXL345() ##SSD1306 OLED display stuff disp = OLED.SSD1306_128_64(rst=RSTPin) # Initialize library. disp.begin() # Clear display. disp.clear() disp.display() # Create blank image for drawing. # Make sure to create image with mode '1' for 1-bit color. width = disp.width height = disp.height
def __init__(self): self.accelerometer = Adafruit_ADXL345.ADXL345()