物联网设备编程语言是一门用来快速构建可接入格物云物联网云平台(或其他物联网云平台)的动态领域语言,让你可以用更少的代码写出更靠谱的物联网设备。
本编程语言包含一下几个关键字
-
app
定义设备的名字,和配置授权信息
app appName {
key: "key",
token: "token"
}
-
init
初始化代码
init do
#define GL_SERIAL Serial
#define DEBUG_SERIAL Serial
#define SEND_DELAY_MS attr_delay
done
-
setup
配置设备代码
setup do
GL_SERIAL.begin(115200);
while (!GL_SERIAL) {;}
done
-
loop
循环执行代码
loop do
// some_loop_func();
done
-
func
定义函数
func read_temp do
// you code here
done
-
attr
设备属性
attr delay {
type: "unsigned long", // 数据类型, 默认 int
default: 300, // 默认值, 默认 0
min: 60, // 最小有效值, 默认 0
max: 86400, // 最大有效值,默认 10000
scale: 1000 // 数据倍数, 默认 1
}
-
metric
设备采集数据
metric temperature {
type: "float", // 数据类型, 默认 float
max_threshold: 100, // 可更改的最大阈值, 默认最大有效值减去最小有效值的 2 分之一
min_threshold: 5, // 可更改的最小阈值, 默认最大有效值减去最小有效值的 100 分之一
max: 100, // 最大有效值, 默认 100
min: 0, // 最小有效值, 默认 0
threshold: 1, // 变化阈值,默认 可改变最小阈值
prec: 6 // 保留几位小数, 默认 2
}
-
every
重复运行
every read_temp 6000 // 每隔几毫秒执行一次
-
telemetry
设备实时数据
telemetry {
fn: read_sensor_data
}
-
command
服务器下发指令处理及错误信息
command get_sensor_data {
fn: read_sensor_data,
error: "read sensor data error"
}
完整例子如下:
app DhtApp {
key: "bdde6db9f3daf38f3a",
token: "14b61d617a9c428a95542dbd097d7a0e"
}
init do
#define GL_SERIAL Serial
#define DEBUG_SERIAL Serial
#define SEND_DELAY_MS attr_delay
done
setup do
GL_SERIAL.begin(115200);
while (!GL_SERIAL) {;}
done
init do
#define DHTPIN 9 // what digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
// #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
// #define DHTTYPE DHT21 // DHT 21 (AM2301)
// https://github.com/adafruit/DHT-sensor-library.git
#include <DHT.h>
DHT dht(DHTPIN, DHTTYPE);
done
setup do
dht.begin();
done
func read_temp do
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
metric_humidity = dht.readHumidity();
metric_temperature = dht.readTemperature();
return RET_SUCC;
done
attr delay {
type: "unsigned long",
default: 300,
min: 60,
max: 86400,
scale: 1000
}
metric temperature {
type: "float",
max_threshold: 100,
min_threshold: 5,
max: 100,
min: 0,
threshold: 1,
prec: 6 // 小数点位数
}
metric humidity {
type: "float",
max_threshold: 100,
min_threshold: 5,
max: 100,
min: 0,
threshold: 1,
prec: 6
}
every read_temp 6000
init do
int mock_sensor_data = 0;
done
func read_sensor_data do
sprintf(retval, FC(F("{\"sensor_data\": %d}")), mock_sensor_data);
return RET_SUCC;
done
telemetry {
fn: read_sensor_data
}
command get_sensor_data {
fn: read_sensor_data,
error: "read sensor data error"
}
func some_poll do
return RET_SUCC;
done
loop do
some_poll();
done
网友评论