查看“OneNet气象站应用”的源代码
←
OneNet气象站应用
跳转至:
导航
、
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
==目的== *本教程将实现一个气象站的应用,设备把温湿度上传到OneNet,同时OneNet可控制设备上的LED灯亮灭 ==所用模块== *'''[[mCenter+]]''' *'''[[mCookie-Module NBIOT/zh]]''' *'''[[Sensor-Color LED/zh]]''' *'''[[Sensor-Tem&Hum-S2/zh]]''' *'''[[Microduino-OLED/zh]]''' ==代码== <source lang="cpp"> /*** * 资源ID表: * http://www.openmobilealliance.org/wp/OMNA/LwM2M/LwM2MRegistry.html ***/ #include <NBIOTAT.h> #include "userDef.h" #include "lwm2mObjs.h" #ifdef OLEDDisplay #include "oled.h" #endif void setup() { Serial.begin(UARTSPEED); nbiotSerial.begin(UARTSPEED); pinMode(LED_Pin, OUTPUT); #ifdef OLEDDisplay u8g2.begin(); #endif #ifdef OLEDDisplay showOLED(str_imei, str_imsi, "SIM Ready", rssi); #endif //检查SIM卡是否识别 while (!nbiot.isSIMReady()) { Serial.println("SIM ERROR"); delay(2000); } Serial.println("SIM OK"); #ifdef OLEDDisplay showOLED(str_imei, str_imsi, "SIM OK", rssi); #endif //获取IMEI strcpy(str_imei, nbiot.queryIMEI()); Serial.print("IMEI:"); Serial.println(str_imei); #ifdef OLEDDisplay showOLED(str_imei, str_imsi, "IMEI", rssi); #endif //获取IMSI strcpy(str_imsi, nbiot.queryCIMI()); Serial.print("IMSI:"); Serial.println(str_imsi); #ifdef OLEDDisplay showOLED(str_imei, str_imsi, "IMSI", rssi); #endif //获取频段 Serial.print("band:"); Serial.println(nbiot.queryNband()); #ifdef OLEDDisplay showOLED(str_imei, str_imsi, "Attach...", rssi); #endif //等待附着基站... Serial.println("Waitting for Attaching"); while (!nbiot.queryCGATT()) { Serial.print("."); delay(3000); } Serial.println(); Serial.println("Attach OK"); #ifdef OLEDDisplay showOLED(str_imei, str_imsi, "Attach OK", rssi); #endif //获取信号强度 rssi = nbiot.queryCSQ(); Serial.print("rssi:"); Serial.println(rssi); //用户自定义OneNet资源列表 prv_create_lwm2m_objs(); #ifdef OLEDDisplay showOLED(str_imei, str_imsi, "custom Objs", rssi); #endif //关闭Socket Serial.print("close:"); Serial.println(nbiot.MIPLCLOSE(refId)); //删除资源列表 Serial.print("delete:"); Serial.println(nbiot.MIPLDELETE(refId)); //创建socket refId = nbiot.MIPLCREATE(); Serial.print("refId:"); Serial.println(refId); #ifdef OLEDDisplay showOLED(str_imei, str_imsi, "Create", rssi); #endif //上传资源列表到OneNet nbiot.uploadResourceList(); #ifdef OLEDDisplay showOLED(str_imei, str_imsi, "upload", rssi); #endif //初始化上报信息 initNotifyToOneNet(); #ifdef OLEDDisplay showOLED(str_imei, str_imsi, "init", rssi); #endif } void loop() { //设置生命周期为永远 nbiot.lifeForever(); //接收消息启动 nbiot.reciverEnable(); // 如果有写操作下发 if (nbiot.writeClicked()) { //执行写操作相应功能 writeAction(&nbiot.oneNetMessage); #ifdef OLEDDisplay showOLED(str_imei, str_imsi, "write", rssi); #endif } // 如果有读操作下发 if (nbiot.readClicked()) { //执行读操作相应功能 readAction(&nbiot.oneNetMessage); #ifdef OLEDDisplay showOLED(str_imei, str_imsi, "read", rssi); #endif } //间隔执行上报信息操作 if (timerlastTime > millis()) timerlastTime = millis(); if (millis() - timerlastTime > INTERVAL_timer) { //更新温湿度并上报 tempHumiUpdate(); #ifdef OLEDDisplay showOLED(str_imei, str_imsi, "notify", rssi); #endif timerlastTime = millis(); } } </source> ==用户自定义代码== <source lang="cpp"> #include <Microduino_Tem_Hum.h> const static uint16_t TemperatureSensorID = 3303; const static uint16_t HumiditySensorID = 3304; const static uint16_t LightControlID = 3311; const static uint16_t OnOff = 5850; const static uint16_t Dimmer = 5851; const static uint16_t SensorValue = 5700; const static uint16_t MinRangeValue = 5603; const static uint16_t SensorUnits = 5701; const static uint16_t ResetMinMaxMeasuredValues = 5605; const static uint8_t LED_Pin = D6; int dimmerValue = 0; static const uint16_t const_AttrIds_tempHumi[] = { SensorValue, MinRangeValue, SensorUnits}; static const cis_datatype_t const_AttrType_tempHumi[] = { cis_data_type_float, cis_data_type_float, cis_data_type_string, }; static const uint16_t const_ActIds_tempHumi[] = { ResetMinMaxMeasuredValues}; static const uint16_t const_AttrIds_LED[] = { OnOff, Dimmer}; static const cis_datatype_t const_AttrType_LED[] = { cis_data_type_bool, cis_data_type_integer}; Tem_Hum_S2 termo; float temptureValue = 0.00; float humidityValue = 0.00; float tempHumiMinValue= 6.00; LWM2MObject tempture; LWM2MObject humidity; LWM2MObject LEDContorl; LWM2MObject *tempP; LWM2MObject *humiP; LWM2MObject *LEDContorlP; void prv_create_lwm2m_objs() { termo.begin(); st_resource res_tempHumi = { .attrListPtr = const_AttrIds_tempHumi, .attrListTyp = const_AttrType_tempHumi, .attrCount = sizeof(const_AttrIds_tempHumi) / sizeof(uint16_t), .actListPtr = const_ActIds_tempHumi, .actCount = sizeof(const_ActIds_tempHumi) / sizeof(uint16_t), }; st_resource res_LED = { .attrListPtr = const_AttrIds_LED, .attrListTyp = const_AttrType_LED, .attrCount = sizeof(const_AttrIds_LED) / sizeof(uint16_t), .actListPtr = NULL, .actCount = 0, }; tempture.objId = TemperatureSensorID; tempture.insCount = 1; tempture.insBitmap = 1; tempture.resArray = &res_tempHumi; humidity.objId = HumiditySensorID; humidity.insCount = 1; humidity.insBitmap = 1; humidity.resArray = &res_tempHumi; LEDContorl.objId = LightControlID; LEDContorl.insCount = 1; LEDContorl.insBitmap = 1; LEDContorl.resArray = &res_LED; tempP = nbiot.createLWM2MObj(&tempture); humiP = nbiot.createLWM2MObj(&humidity); LEDContorlP = nbiot.createLWM2MObj(&LEDContorl); } void initNotifyToOneNet() { nbiot.MIPLNOTIFY(tempture.msgId, tempture.objId, 0, SensorUnits, "`C"); nbiot.MIPLNOTIFY(humidity.msgId, humidity.objId, 0, SensorUnits, "precent"); } void tempHumiUpdate() { Serial.println("start notify..."); temptureValue = termo.getTemperature(); humidityValue = termo.getHumidity(); Serial.println(temptureValue); Serial.println(humidityValue); Serial.println(nbiot.MIPLNOTIFY(tempture.msgId, tempture.objId, 0, SensorValue, temptureValue)); Serial.println(nbiot.MIPLNOTIFY(humidity.msgId, humidity.objId, 0, SensorValue, humidityValue)); } void writeAction(messageLWM2M* message) { switch (message->objId) { case LightControlID: //led control switch (message->resId) { case OnOff: if (nbiot.valueToBool(message->value)) { digitalWrite(LED_Pin, HIGH); nbiot.UPDATEOneNet(LEDContorl.msgId, true); } else { digitalWrite(LED_Pin, LOW); nbiot.UPDATEOneNet(LEDContorl.msgId, false); } break; case Dimmer: dimmerValue = nbiot.valueToInt(message->value); Serial.print("dimmer:"); Serial.println(dimmerValue); nbiot.UPDATEOneNet(LEDContorl.msgId, dimmerValue); break; default: Serial.println("no such res"); } case TemperatureSensorID: //tempture //TODO it break; default: Serial.println("no such obj ID"); } //write action finish nbiot.writeDone(); } void readAction(messageLWM2M* message) { switch (message->objId) { case TemperatureSensorID: //tempture if(MinRangeValue==message->resId) { nbiot.MIPLREADRSP(tempHumiMinValue); nbiot.UPDATEOneNet(tempture.msgId, tempHumiMinValue); } break; case HumiditySensorID: //humidity if(MinRangeValue==message->resId) { nbiot.MIPLREADRSP(tempHumiMinValue); nbiot.UPDATEOneNet(humidity.msgId, tempHumiMinValue); } break; default: Serial.println("no such obj ID"); } //write action finish nbiot.readDone(); } </source> ==注意问题== *出厂默认的波特率是9600,如果无法通讯,请更换波特率试试; *数据通讯格式一定还要设置成“\r\n”,回车换行模式。
返回至
OneNet气象站应用
。
导航菜单
个人工具
创建账户
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
Welcome
首页
创客大赛
大赛详情
3D打印
安装月球车
图形化编程
操控月球车
升级月球车
编程工具下载
软件下载
Arduino
Processing
Mixly
Scratch
模块套件
Microduino 102
mCookie 102
mCookie 202
mCookie 302
IBC
其他
应用套件
四轴飞行器
平衡车
小车CUBE
音乐播放器
刷卡音乐播放器
wifi气象站
彩虹音乐触摸灯
分贝检测仪
迎门汇报
LED点阵时钟
LED点阵屏幕
硬件
mCookie
Sensor
Microduino
MicroWrt
MicroNux
MicroRobot-Core
MicroRobot-CoreESP
ideaBoard
ideaBox
MicroMV
MicroAI
帮助
常见问题
帮助
工具
链入页面
相关更改
特殊页面
页面信息