Monica Houston
Published © Apache-2.0

Analyze Accelerometer Data on MaaXBoard with Edge Impulse

Use CircuitPython (Adafruit Blinka) and Edge Impulse to collect and train a model using accelerometer data from Avnet's MaaXBoard.

BeginnerProtip4 hours832
Analyze Accelerometer Data on MaaXBoard with Edge Impulse

Things used in this project

Hardware components

MaaxBoard
Avnet MaaxBoard
×1
6 DOF Sensor - MPU6050
DFRobot 6 DOF Sensor - MPU6050
×1

Software apps and online services

Edge Impulse Studio
Edge Impulse Studio

Story

Read more

Schematics

accelerometer with MaaXBoard

Hook up pin 2 (D3) to SDA, pin 3 (D5) to SCL

Code

collect.py

Python
Change the default collection time
Change 'x-file-name' variable to be the class of motion you are currently detecting.
run python3 collect.py
# First, install the dependencies via:
#    $ pip3 install requests

import json
import time, hmac, hashlib
import requests
import re, uuid
import math
import time
import board
import adafruit_mpu6050

i2c = board.I2C()  # uses board.SCL and board.SDA
mpu = adafruit_mpu6050.MPU6050(i2c)

# Your API & HMAC keys can be found here (go to your project > Dashboard > Keys to find this)
HMAC_KEY = ""
API_KEY = "ei_"

# empty signature (all zeros). HS256 gives 32 byte signature, and we encode in hex, so we need 64 characters here
emptySignature = ''.join(['0'] * 64)

# use MAC address of network interface as deviceId
device_name =":".join(re.findall('..', '%012x' % uuid.getnode()))

# here we have new data every 16 ms
INTERVAL_MS = 16

if INTERVAL_MS <= 0:
    raise Exception("Interval in miliseconds cannot be equal or lower than 0.")

# here we'll collect 2 seconds of data at a frequency defined by interval_ms
freq =1000/INTERVAL_MS
values_list=[]

for i in range (2*int(round(freq,0))):
    values_list.append(mpu.acceleration)

data = {
    "protected": {
        "ver": "v1",
        "alg": "HS256",
        "iat": time.time() # epoch time, seconds since 1970
    },
    "signature": emptySignature,
    "payload": {
        "device_name":  device_name,
        "device_type": "LINUX_TEST",
        "interval_ms": INTERVAL_MS,
        "sensors": [
            { "name": "accX", "units": "m/s2" },
            { "name": "accY", "units": "m/s2" },
            { "name": "accZ", "units": "m/s2" }
        ],
        "values": values_list
    }
}



# encode in JSON
encoded = json.dumps(data)

# sign message
signature = hmac.new(bytes(HMAC_KEY, 'utf-8'), msg = encoded.encode('utf-8'), digestmod = hashlib.sha256).hexdigest()

# set the signature again in the message, and encode again
data['signature'] = signature
encoded = json.dumps(data)

# and upload the file
res = requests.post(url='https://ingestion.edgeimpulse.com/api/training/data',
                    data=encoded,
                    headers={
                        'Content-Type': 'application/json',
                        'x-file-name': 'idle01',
                        'x-api-key': API_KEY
                    })
if (res.status_code == 200):
    print('Uploaded file to Edge Impulse', res.status_code, res.content)
else:
    print('Failed to upload file to Edge Impulse', res.status_code, res.content)

collect_features.py

Python
Run this to collect features to classify on your board
import time
import board
import adafruit_mpu6050
import os

i2c = board.I2C()  # uses board.SCL and board.SDA
mpu = adafruit_mpu6050.MPU6050(i2c)

# values = mpu.acceleration
# print(values)
# # prints values like this: (-2.080561242675781, 3.536235852050781, 9.60793126220703)

# here we have new data every 16 ms
INTERVAL_MS = 16

if INTERVAL_MS <= 0:
    raise Exception("Interval in miliseconds cannot be equal or lower than 0.")

# here we'll collect 2 seconds of data at a frequency defined by interval_ms
freq = 1000/INTERVAL_MS
values_list = []
all_features = []

for i in range(2*int(round(freq, 0))):
    values_list = [round(elem, 6) for elem in mpu.acceleration]
    all_features.extend(values_list)

with open('features.txt', 'a+') as f:
    f.write(','.join([str(i) for i in all_features]))
    f.close()

'''there will be 372 total features (124 sets of X,Y,Z data) in this format:
'-0.658 3.792 9.78 -0.658 3.773...etc'''

Credits

Monica Houston

Monica Houston

76 projects • 451 followers
I don't live on a boat anymore.

Comments