【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百零九:Gravity: I2C & UART BC20 NB-IoT & GNSS通信模块
NB-IoT广域低功耗无线通信 GPS/北斗精准定位 户外物联网必备
项目二十:通过Easy-IOT平台发布消息
实验开源代码
- /*
- 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
- 实验二百零九:Gravity: I2C & UART BC20 NB-IoT & GNSS通信模块
- NB-IoT广域低功耗无线通信 GPS/北斗精准定位 户外物联网必备
- 项目二十:通过Easy-IOT平台发布消息
- 实验接线:
- BC20 UNO
- VIN 5V
- GND GND
- SCL A5
- SDA A4
- */
-
- #include "DFRobot_BC20_Gravity.h"
-
- //RGB有7种颜色可供选择
- #define RED 0
- #define BLUE 1
- #define GREEN 2
- #define YELLOW 3
- #define PURPLE 4
- #define CYAN 5
- #define WHITE 6
-
- //配置设备证书信息
- char* Iot_id = "wGkCJkIng";
- char* Client_ID = "BC20 NB-IoT";
- char* Iot_pwd = "QMkC1kInRz";
-
- //配置域名和端口号
- char* EasyIot_SERVER = "182.254.130.180";
- char* PORT = "1883";
-
- //设置需要发布的设备编号
- char* pubTopic = "hJmOvRIng";
-
- //IIC通讯
- #define USE_IIC
-
- //硬件串口通讯
- //#define USE_HSERIAL
-
- //软件串口通讯
- //#define USE_SSERIAL
-
- DFRobot_BC20_IIC myBC20(0x33);
-
- /*
- 每次模块收到消息时,都会调用此函数。
- 它将打印消息和相应的主题。
- */
- void callback(char * topic, uint8_t * payload, unsigned int len) {
- Serial.print("接收[主题:");
- Serial.print(topic);
- Serial.print("] ");
- for (int i = 0; i < len; i++) {
- Serial.print((char)payload[i]);
- }
- Serial.println();
-
- //根据接收到的消息控制板载 RGB LED。
- char* receivedData = (char*)payload;
- if (strcmp(receivedData, "OFF") == 0) {
- myBC20.LED_OFF();
- Serial.println("LED 灯熄灭");
- }
- else if (strcmp(receivedData, "RED") == 0) {
- myBC20.changeColor(RED);
- myBC20.LED_ON();
- Serial.println("LED 是红色的");
- }
- else if (strcmp(receivedData, "GREEN") == 0) {
- myBC20.changeColor(GREEN);
- myBC20.LED_ON();
- Serial.println("LED 为绿色");
- }
- else if (strcmp(receivedData, "BLUE") == 0) {
- myBC20.changeColor(BLUE);
- myBC20.LED_ON();
- Serial.println("LED 为蓝色");
- }
- else if (strcmp(receivedData, "YELLOW") == 0) {
- myBC20.changeColor(YELLOW);
- myBC20.LED_ON();
- Serial.println("LED 是黄色的");
- }
- else if (strcmp(receivedData, "PURPLE") == 0) {
- myBC20.changeColor(PURPLE);
- myBC20.LED_ON();
- Serial.println("LED 是紫色的");
- }
- else if (strcmp(receivedData, "CYAN") == 0) {
- myBC20.changeColor(CYAN);
- myBC20.LED_ON();
- Serial.println("LED 为青色");
- }
- else if (strcmp(receivedData, "WHITE") == 0) {
- myBC20.changeColor(WHITE);
- myBC20.LED_ON();
- Serial.println("LED 是白色的");
- }
- else {
- ;
- }
- }
-
- void ConnectCloud() {
- Serial.print("尝试MQTT连接......");
- while (!myBC20.connected()) {
- Serial.print(".");
- if (myBC20.connect(Client_ID, Iot_id, Iot_pwd)) {
- Serial.println("连接服务器正常");
- } else {
- //用于检测设备与服务器的连接
- if (myBC20.getQMTCONN())
- break;
- }
- }
-
- while (!myBC20.subTopic('0', '1',pubTopic , '0')) {
- Serial.print(".");
- }
- Serial.print("主题:");
- Serial.print(pubTopic);
- Serial.println(" 订阅了!");
- }
-
- void setup() {
- Serial.begin(115200);
- myBC20.LED_OFF();
-
- /*Initialize BC20*/
- Serial.print("正在启动BC20,请稍等...... ");
- myBC20.changeColor(RED);
- while (!myBC20.powerOn()) {
- Serial.print(".");
- myBC20.LED_ON();
- delay(500);
- myBC20.LED_OFF();
- delay(500);
- }
- Serial.println("BC20 启动成功!");
-
- //检查 SIM 卡是否插入
- Serial.println("正在检查SIM卡……");
- myBC20.changeColor(GREEN);
- while (!myBC20.checkNBCard()) {
- Serial.print(".");
- myBC20.LED_ON();
- delay(500);
- myBC20.LED_OFF();
- delay(500);
- }
- Serial.println("OK!");
-
- //禁用睡眠模式
- myBC20.configSleepMode(eSleepMode_Disable);
- //禁用 PSM
- myBC20.setPSMMode(ePSM_OFF);
-
- /*
- 模块将自动尝试连接到网络(移动站)。
- 检查它是否已连接到网络。
- */
- Serial.print("正在连接网络......");
- myBC20.changeColor(BLUE);
- while (myBC20.getGATT() == 0) {
- Serial.print(".");
- myBC20.LED_ON();
- delay(500);
- myBC20.LED_OFF();
- delay(500);
- }
- Serial.println("已连接!");
-
- //设置回调函数
- myBC20.setCallback(callback);
-
- Serial.println("连接到DFRobot Easy-IoT");
-
- //配置物联网服务器
- myBC20.setServer(EasyIot_SERVER, PORT);
- Serial.println("服务器可用!");
-
- //连接DFRobot Easy-IoT
- ConnectCloud();
- }
-
- void loop() {
- myBC20.loop();
- }
复制代码
|