Next is to add code to upload the sensor data to an Adafruit IO feed. DeviceScript supports the HTTPS and MQTT endpoints.
Post data to Adafruit IO
Adafruit IO supports a POST HTTPS endpoint to add a data entry to a feed.
First the program collects the Adafruit IO feed information user
and feed
, the Adafruit.io key key
and reads the temperature from the sensor value
; then to use the fetch
function to issue an HTTP POST request to the secure endpoint of Adafruit.io.
import { fetch } from "@devicescript/net" // collect connection info and data const user = ... const feed = ... const key = ... const value = await temperature.reading.read() // craft Adafruit.io payload and send POST request await fetch(`https://io.adafruit.com/api/v2/${user}/feeds/${feed}/data`, { method: "POST", headers: { "X-AIO-Key": key, "Content-Type": "application/json" }, body: JSON.stringify({ value }), })
devicescript-adafruit-io package
The devicescript-adafruit-io package on GitHub may be used to simplify using Adafruit IO.
DeviceScript supports sharing code and libraries through npm or GitHub. It also supports MQTT.
Use the command below to install the package in your project.
npm install --save pelikhan/devicescript-adafruit-io#v0.0.4
The package provides createData
which wraps reading the settings and secrets, crafting a POST request and analyzing the results.
import { createData } from "devicescript-adafruit-io" const value = await temperature.reading.read() const status = await createData(value) console.log({ status })
import { pins, board } from "@dsboard/adafruit_qt_py_c3" import { startSHTC3 } from "@devicescript/drivers" import { schedule } from "@devicescript/runtime" import { createData } from "devicescript-adafruit-io" const { temperature, humidity } = await startSHTC3() schedule(async () => { const value = await temperature.reading.read() await createData(value) }, { timeout: 1000, interval: 60000 })
Text editor powered by tinymce.