“OneNet气象站应用”的版本间的差异

来自Microduino Wikipedia
跳转至: 导航搜索
所用模块
代码
第14行: 第14行:
  
 
<source lang="cpp">
 
<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>

2018年6月25日 (一) 03:38的版本

目的

  • 本教程将实现一个气象站的应用,设备把温湿度上传到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();
	}
}

注意问题

  • 出厂默认的波特率是9600,如果无法通讯,请更换波特率试试;
  • 数据通讯格式一定还要设置成“\r\n”,回车换行模式。