{"id":24100117,"url":"https://github.com/alvarowolfx/wasm-ble-edge-filter","last_synced_at":"2025-05-07T22:26:22.087Z","repository":{"id":42751261,"uuid":"280064579","full_name":"alvarowolfx/wasm-ble-edge-filter","owner":"alvarowolfx","description":"Using WASM as a Rules Engine for IoT","archived":false,"fork":false,"pushed_at":"2022-12-15T22:10:34.000Z","size":164,"stargazers_count":12,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-31T14:57:31.966Z","etag":null,"topics":["arduino","esp32","internet-of-things","iot","webassembly","webassembly-demo"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alvarowolfx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-16T05:42:09.000Z","updated_at":"2024-08-15T13:33:31.000Z","dependencies_parsed_at":"2023-01-29T04:30:31.459Z","dependency_job_id":null,"html_url":"https://github.com/alvarowolfx/wasm-ble-edge-filter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvarowolfx%2Fwasm-ble-edge-filter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvarowolfx%2Fwasm-ble-edge-filter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvarowolfx%2Fwasm-ble-edge-filter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvarowolfx%2Fwasm-ble-edge-filter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alvarowolfx","download_url":"https://codeload.github.com/alvarowolfx/wasm-ble-edge-filter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252963629,"owners_count":21832540,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arduino","esp32","internet-of-things","iot","webassembly","webassembly-demo"],"created_at":"2025-01-10T15:57:48.913Z","updated_at":"2025-05-07T22:26:22.064Z","avatar_url":"https://github.com/alvarowolfx.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Using WASM as a Rules Engine for IoT\n\nThe idea here is to have an IoT device collecting data and the data filtering happens at the edge and is defined by a Wasm code running on the device.\n\nFor this demo, I used an ESP32 scanning for BLE devices and the firmware passes the BLE device info like device name, serviceUUID, RSSID and other things, so the Wasm code can return true/false if that device should be filtered or not.\n\n_This is a work in progress_\n\n## Project Components\n\n### Device Firmware\n\n`firmware` folder\n\nCode wrote using Arduino Framework, Nimble BLE Stack and Wasm3. All wrote using PlatformIO.\n\nMain tasks\n\n- Load Wasm code from SPIFFS (under `/app.wasm` ) and pre cache the filter function.\n  - Code can the uploaded via USB using\n  - The device also exposes some APIs to be called from the Wasm land to get the BLE device info\n    - See [WasmRulesEngine.cpp](firmware/src/WasmRulesEngine.cpp#74)\n    - See [rules.ts](wasm_apps/assemblyscript/rules.ts)\n- Checks after connecting to WiFi which version of the rule if should be running and download it if needed\n  - Uses the Rules Engine API for that.\n- Scan BLE devices and filter using the Wasm code, if is available.\n- [Work In Progress] - Post data to Rules Engine API\n\n### Rules Engine API\n\n`api` folder\n\nThe backend was built using Node.js and `fastify`. It's just a proof of concepts, but is basically have device management endpoints and more important for this demo, some endpoints to send Assemblyscript code, compile it and save as a rule to be used/downloaded later by the device.\n\nThe backend exposes those endpoints bellow.\n\nDevice Management\n\n- GET /devices - List devices\n- POST /:deviceId - Create device with given ID,\n- GET /:deviceId/history - Fetch device data history,\n- POST /:deviceId/history - Save device data\n- GET /:deviceId/version - Used by the device to determine which version of the rule it should be runnig\n- PUT /:deviceId/rules/:versionName - Update which version the device should be running\n\nRules Management\n\n- GET /rules - List created rules\n- POST /:versionName - Create rule with the given versionName. The body must contain Assemblyscript code. See an example on the next section.\n- GET /:versionName/app.wasm - Return the raw Wasm file to be downloaded by the device\n- GET /:versionName - Show info of the rule\n\n### Wasm Example Script\n\n`wasm_apps` folder\n\nExample of a business logic that will be compiled to run on the device. For now the folder contains an example using Assemblyscript.\n\nA more advance user will write this kind of code to run on the edge device.\n\n```\nfunction filter(device: Device): bool {\n  if (device.name == \"OBDII\") {\n    // I want to track my car\n    return false;\n  }\n\n  if (device.serviceUUID == \"0xfeaa\") {\n    // Always send Eddystone tags\n    return false;\n  }\n\n  return device.rssi \u003c= -75; // Too Far\n}\n```\n\n### Update WASM file via USB\n\nThe file is being saved on SPIFFS and the Arduino is loading from it to run on WASM3 VM. So we can update the WASM code without changing the host code.\n\nSteps to update WASM code:\n\n- `cd` into `wasm_apps/assemblyscript` folder and run `npm run build`.\n  - This will compile the AS code to Wasm and also copy the file to the `data` folder.\n  - Run the command `pio run --target uploadfs` on the `firmware` folder.\n\n#### References\n\n- https://github.com/AssemblyScript/assemblyscript\n- https://github.com/wasm3/wasm3\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falvarowolfx%2Fwasm-ble-edge-filter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falvarowolfx%2Fwasm-ble-edge-filter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falvarowolfx%2Fwasm-ble-edge-filter/lists"}