OneNet气象站应用
目的
- 本教程将实现一个气象站的应用,设备把温湿度上传到OneNet,同时OneNet可控制设备上的LED灯亮灭
所用模块
主代码
- 主程序为建立类似应用的模板,大部分情况不用改动
/***
* 资源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();
}
}
用户自定义代码
- lwm2mObjs.h
#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();
}
- userDef.h
#define UARTSPEED 9600
#define INTERVAL_timer 5000
unsigned long timerlastTime = millis();
uint8_t refId = 1;
#include <SoftwareSerial.h>
SoftwareSerial softSerial(4, 5); /* RX:D4, TX:D5 */
#define nbiotSerial softSerial
// #define nbiotSerial Serial1
NBIOTAT nbiot(&nbiotSerial);
char str_imei[16] = "XXXXXXXXX";
char str_imsi[16] = "XXXXXXXXX";
int rssi=99;
#define OLEDDisplay
- oled.h
#include <U8g2lib.h>
//Hardware I2C
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0);
void showOLED(char* str_imei, char* str_imsi, char* str_stat, int rssi) {
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_6x10_mr);
u8g2.setCursor(0, 20);
u8g2.print("imei:");
u8g2.setCursor(30, 20);
u8g2.print(str_imei);
u8g2.setCursor(0, 30);
u8g2.print("imsi:");
u8g2.setCursor(30, 30);
u8g2.print(str_imsi);
u8g2.setCursor(0, 40);
u8g2.print("stat:");
u8g2.setCursor(40, 40);
u8g2.print(str_stat);
u8g2.setCursor(0, 50);
u8g2.print("rssi:");
u8g2.setCursor(40, 50);
u8g2.print(rssi);
} while ( u8g2.nextPage() );
}
注意问题
- 确保附近有NB基站
- 确保使用的SIM为NB的SIM卡
- 确保IDE为最新版本
- 在loop里不要定义String类型的变量