The steps for this recipe are as follows:
- Import the libraries:
from scipy import stats
import numpy as np
from sense_hat import SenseHat
import json
from kafka import KafkaProducer
import time
- Wait for Sense HAT to register with the OS:
time.sleep(60)
- Initialize the variables:
device= "Pi1"
server = "[the address of the kafka server]:9092"
producer = KafkaProducer(bootstrap_servers=server)
sense = SenseHat()
sense.set_imu_config(False, True, True)
gyro = []
accel = []
- Create a Z-score helper function:
def zscore(data):
return np.abs(stats.zscore(np.array(data)))[0]
- Create a sendAlert helper function:
def sendAlert(lastestGyro,latestAccel):
alert = {'Gyro':lastestGyro, 'Accel':latestAccel}
message = json.dumps(alert)
producer.send(device+'alerts', key=bytes("alert",
encoding='utf-8'),
value=bytes(message, encoding='utf-8...