cameronbunce
Published © Apache-2.0

Feed The Machine

Data Acquisition tools for Edge Impulse on ESP32 in MicroPython, to make better ML Models

IntermediateFull instructions provided2 hours228
Feed The Machine

Things used in this project

Hardware components

FireBeetle ESP32 IOT Microcontroller (Supports Wi-Fi & Bluetooth)
DFRobot FireBeetle ESP32 IOT Microcontroller (Supports Wi-Fi & Bluetooth)
×1
Adafruit Waterproof DS18B20 Digital temperature sensor
Adafruit Waterproof DS18B20 Digital temperature sensor
×1
Resistor 4.75k ohm
Resistor 4.75k ohm
×1

Software apps and online services

Edge Impulse Studio
Edge Impulse Studio
MicroPython
MicroPython

Hand tools and fabrication machines

Breadboard, 400 Pin
Breadboard, 400 Pin

Story

Read more

Schematics

Wiring Diagram

DS18B20 data on GPIO2 with a 4.7k pull-up to 3.3v

Code

LongRunning.py

MicroPython
LongRunning example of the data ingest API tools presented here, using a list of sensors with one axis of data
import secret, DS18B20Reader, EdgeImpulse
from machine import deepsleep

interval = 600000 # 10 minutes
SensorPin = 2
ReadingBatch = 145 # 24 hours of readings based on an interval of 10 minutes being 144

mysensors = DS18B20Reader.DS18B20Reader(SensorPin)
mysensors.initializeValues()
if mysensors.readCount() < ReadingBatch:
    mysensors.updateValues()
else:
    myAPI = EdgeImpulse.EIAPI(secret.api_key, secret.hmac_key, secret.ssid, secret.password)

    for sensor in mysensors.getSensors():
        if myAPI.sendValues(mysensors.getValues(sensor), interval):
            mysensors.clearValues(sensor)
        else:
            print(myAPI.getMessage())
            mysensors.updateValues()
deepsleep(interval)

pack and ship function

MicroPython
This is the meat and potatoes that EdgeImpulse.py does for you, so that you don't have to bother with crypto and signing and API stuff.
        import time, json, hmac, urequests, hashlib

        # empty signature (all zeros). HS256 gives 32 byte signature, and we encode in hex, so we need 64 characters here
        emptySignature = ''.join(['0'] * 64)
        data = {
            "protected": {
                "ver": "v1",
                "alg": "HS256",
                "iat": time.time() # epoch time, seconds since poweron
            },
            "signature": emptySignature,
            "payload": {
                "device_name": self.mac,
                "device_type": "ESP32-DS18B20",
                "interval_ms": interval_ms,
                "sensors": [
                    { "name": "Temperature", "units": "Celsius" }
                ],
                "values": values
            }
        }
        encoded = json.dumps(data)
        # create signature based on blank signature field
        signature = hmac.new(bytes(self.hmac, 'utf-8'), msg = encoded.encode('utf-8'), digestmod = hashlib.sha256).hexdigest()

        # update signature field to sign the message
        data['signature'] = signature
        res = urequests.post(url='https://ingestion.edgeimpulse.com/api/training/data',
                            data=encoded,
                            headers={
                                'Content-Type': 'application/json',
                                'x-file-name': 'DS18B20Long',
                                'x-api-key': self.api
                            })
        if (res.status_code == 200):
            return True
        else:
            self.__addMessage(res.content)
            return False

ESP Edge Impulse

Credits

cameronbunce

cameronbunce

2 projects • 3 followers

Comments