Arduino uses BC26/BC35 to connect to Alibaba Cloud platform

I. Introduction

The project I participated in recently needs to use the NB-IoT module. After searching on the Internet, I found that the NB-IoT modules commonly used by Arduino are as follows:
Please add a picture description

Please add a picture description

Both of these modules are from DFRobot, and there are some independent modules. There are information about these communication modules on the Internet, most of them are debugging with Arduino UNO or STM32, and I have never seen anyone use Arduino Mega 2560 to connect with these modules.

Because this project needs to use a lot of pins, the number of pins of UNO cannot meet the demand, and Mac computers cannot program 32 chips, so we can only choose Mega2560.

This raises a new question, can the compatibility of the modules meet the requirements?

2. Preparations before commissioning

1. Communication module selection and software version

The modules selected this time are BC260YandBC35
Please add a picture description

Please add a picture description

The software version uses the

Please add a picture description

2. Burning of firmware

Because the original firmware does not support the MQTT protocol, this piece of AT firmware that supports MQTT needs to be burned after you get it. The specific burning method needs to beask the merchant, since the firmware and references belong to the officialrestrictivedocument, so it will not be published here.

only when burningA few points to note:
1) The burning software should be placed in the root directory of the system as much as possible, and there should be no Chinese or spaces in the path, because it will increase the possibility of burning failure; 2) The file
level of the firmware to be burned should not be too deep, try to put it in Under the root directory of the system, and there should be no Chinese or spaces in the path, it will also increase the risk of burning failure;,
3) The USB to TTL downloader used must be installed on the computer. For example, my USB TTL downloader is CH340C chip, then I need to install the driver of the corresponding chip on the computer;

PS: The first few times I burned the firmware failed because of this reason, and manual reset was required in the middle, that is to say, the RESETpins need to be grounded manually for a short time.

3. Need a clean desktop before debugging

This is very important, very important, very important! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !

I just suffered a loss in this aspect when I was debugging, so I must have a clean desktop.

Because of a very outrageous error, the whole afternoon was spent on debugging.

The reason is this. When debugging the program, it suddenly ran through once, and then it didn’t run through again. No matter how to change the program or change the wiring, it is useless. It is no problem to test the 2560 and the communication module alone. It will not work together, and the problem of library file incompatibility is also ruled out in the process. Then the problem lies in the wiring between the module and the 2560, because when I tested, because the Dupont wire was too long, it became a ball, and there was a speaker, a group of high-frequency signal wires and a group of wires around the wire. Group AC wires. After I cleaned up the desktop and straightened the DuPont line, the program suddenly stopped reporting errors. It shows that these environments have affected the transmission of the signal. It is estimated that it is signal crosstalk.

Please add a picture description

It can be clearly seen in the picture that there is an HDMI cable, a Thunderbolt 3 cable, two AC power supply cables, several USB data cables, and speakers on the left and right. I put the grown Dupont wires in a ball on top of this pile of wires (I didn’t take a picture of a similar scene, so I took this picture to reproduce the scene) and then debugged and found a bug.

So you must have a clean and tidy desktop before debugging! ! ! ! !

3. Start debugging hardware

Go directly to the program, this program is provided by the store, let's do a step-by-step analysis

#include <Arduino.h>
#include <SoftwareSerial.h>
#include <ArduinoJson.h>

DynamicJsonDocument json(200);

SoftwareSerial mySerial(50,52) ;//RX  TX

void CommandtoNBIOT (String cmd, char *res)
{
    
    
  while (1)
  {
    
    
    mySerial.println(cmd);
    delay(300);
    while (mySerial.available() > 0)
    {
    
    
      if (mySerial.find(res))
      {
    
    
        digitalWrite(7, HIGH);   // turn the LED on (HIGH is the voltage level)
        delay(100);              // wait for a second
        digitalWrite(7, LOW);    // turn the LED off by making the voltage LOW
        delay(100);
        Serial.println(res);
        return;
      }
      else
      {
    
    
        Serial.print(cmd);
        Serial.println("  Return ERROR!");
      }
    }
    delay(200);
  }
}

void MQTT_PubData(void)//发布数据到平台
{
    
     //"/sys/a1qmGxDM8cd/mzhtest001/thing/event/property/post","{"id":"26","version":"1.0","params":{"CurrentTemperature":{"value":23},"RelativeHumidity":{"value":58}},"method":"thing.event.property.post"}"
  char sendjson[] = "{\"id\":\"26\",\"version\":\"1.0\",\"sys\":{\"ack\":0},\"params\":{\"CurrentTemperature\":{\"value\":50},\"CurrentHumidity\":{\"value\":10}},\"method\":\"thing.event.property.post\"}";
  char T_json[200];
  sprintf(T_json, sendjson); //,humidata
  mySerial.println("AT+QMTPUB=0,0,0,0,\"/sys/gbruRYswLWR/WEATHER/thing/event/property/post\""); //send message,主题更改
  delay(100);
  mySerial.println(T_json);
  mySerial.write(0x1A);
  delay(300);
  // wait for a second
  while (mySerial.available() > 0)
  {
    
    
  //  char inByte = mySerial.read();
  //  Serial.print(inByte);
  if (mySerial.find("+QMTPUB: 0,0,0"))
   {
    
    
   Serial.println("+QMTPUB: 0,0,0");
   break;
  }
  }
}



void BC260Y_init(void)//初始化MQTT连接
{
    
    
  Serial.println("1");
  // prints title with ending line break
  CommandtoNBIOT("AT", "OK");
  mySerial.println("ATE0&W");//关闭回显
  delay(300);
  CommandtoNBIOT("AT+CPIN?", "+CPIN: READY"); //返+CPIN:READY,表明识别到卡了
  Serial.println("2");
  CommandtoNBIOT("AT+CGATT?", "+CGATT: 1"); //返+CGACT: 1,就能正常工作了
  Serial.println("3");
  mySerial.println("AT+QMTCLOSE=0");//关闭上一次socekt连接
  delay(300);
  mySerial.println("at+qmtcfg=\"aliauth\",0, \"gbruRYswLWR\",\"WEATHER\",\"d9661e128b5590cf5b7f880d991c7f7e\"");//配置三元素
  delay(300);
  CommandtoNBIOT("AT+QMTOPEN=0,\"iot-as-mqtt.cn-shanghai.aliyuncs.com\",1883", "+QMTOPEN: 0,0"); //建立服务器的IP和端口连接
  delay(300);
  CommandtoNBIOT("AT+QMTCONN=0,\"WEATHER\"", "+QMTCONN: 0,0,0"); //建立服务器的IP和端口连接
  delay(300);
}


void setup() {
    
    
  // put your setup code here, to run once:
  // initialize digital pin 13 as an output.
  pinMode(7, OUTPUT);
  Serial.begin(9600); //
  mySerial.begin(9600);
  delay(2000);
  
  BC260Y_init();
}

void loop() {
    
    
  
  // put your main code here, to run repeatedly:
  MQTT_PubData();//发布数据
  Serial.println("Data_Pub_Success");
  delay(4000);
}

First of all, let’s talk about the first part, set a soft serial port

SoftwareSerial mySerial(50,52) ;//RX  TX

I personally recommend using the following method

SoftwareSerial mySerial = SoftwareSerial(50,52) ;//RX  TX

Here it is defined that digital port No. 50 is RX, and digital port No. 52 is TX

The wiring method is as follows:

BC260/BC35                              Arduino

   TX0         ------------------>        D50 
   
   RX0         ------------------>        D52
   
   GND         ------------------>        GND 
   
   VIN         ------------------>        5 V 
   

Then let the soft serial port 9600run at the baud rate

void setup() {
    
    
	pinMode(7, OUTPUT);		//我在7针脚上接了一个灯
	Serial.begin(9600); 	//让串口在9600波特率上运行
	mySerial.begin(9600);	//让软串口在9600波特率上运行
	delay(2000);
	BC260Y_init();			//初始化BC260/BC35
}

This program shows how to send AT commands to the communication module

void CommandtoNBIOT (String cmd, char *res)
{
    
    
	while (1)
	{
    
    
		mySerial.println(cmd);
		delay(300);
		while (mySerial.available() > 0)
		{
    
    
			if (mySerial.find(res))
			{
    
    
				digitalWrite(7, HIGH);   
			    delay(100);              
			    digitalWrite(7, LOW);    
			    delay(100);
			    Serial.println(res);
			    return;
			}
			else
			{
    
    
			    Serial.print(cmd);
			    Serial.println("  Return ERROR!");
			}
		}
		delay(200);
	}
}

Use to mySerial.println(cmd)send AT commands. Remember, must be used printlnto send, if there is no line break, the communication module will not execute this command.
After waiting for a period of time, use findthe value returned by the serial port of the communication module to find if there is the return value I want.

ATFor example, it should return after I input OK, then the cmd inside corresponds to AT, and res corresponds to OK.

If the desired return value is not obtained, the module will send commands cyclically.

The initial part of the code is as follows:

void BC260Y_init(void)//初始化MQTT连接
{
    
    
	Serial.println("1");
	// prints title with ending line break
	CommandtoNBIOT("AT", "OK");
	mySerial.println("ATE0&W");//关闭回显
	delay(300);
	CommandtoNBIOT("AT+CPIN?", "+CPIN: READY"); //返+CPIN:READY,表明识别到卡了
	Serial.println("2");
	CommandtoNBIOT("AT+CGATT?", "+CGATT: 1"); //返+CGACT: 1,就能正常工作了
	Serial.println("3");
	mySerial.println("AT+QMTCLOSE=0");//关闭上一次socekt连接
	delay(300);
	mySerial.println("at+qmtcfg=\"aliauth\",0, \"${ProductKey}\",\"${deviceName}\",\"${DeviceSecret}\"");//配置三元素
	delay(300);
	CommandtoNBIOT("AT+QMTOPEN=0,\"iot-as-mqtt.cn-shanghai.aliyuncs.com\",1883", "+QMTOPEN: 0,0"); //建立服务器的IP和端口连接
	delay(300);
	CommandtoNBIOT("AT+QMTCONN=0,\"${deviceName}\"", "+QMTCONN: 0,0,0"); //建立服务器的IP和端口连接
	delay(300);
}

Code reference for data sending part

void MQTT_PubData(void)//发布数据到平台
{
    
     
	char sendjson[] = "{\"id\":\"26\",\"version\":\"1.0\",\"sys\":{\"ack\":0},\"params\":{\"CurrentTemperature\":{\"value\":50},\"CurrentHumidity\":{\"value\":10}},\"method\":\"thing.event.property.post\"}";
    char T_json[200];
    sprintf(T_json, sendjson); //,humidata
    mySerial.println("AT+QMTPUB=0,0,0,0,\"/sys/${ProductKey}/${deviceName}/thing/event/property/post\""); //send message,主题更改
    //具体的请参考阿里云的topic相关文档
    delay(100);
    mySerial.println(T_json);
    mySerial.write(0x1A);
    delay(300);
    // wait for a second
    while (mySerial.available() > 0)
    {
    
    
    //  char inByte = mySerial.read();
    //  Serial.print(inByte);
        if (mySerial.find("+QMTPUB: 0,0,0"))
         {
    
    
             Serial.println("+QMTPUB: 0,0,0");
             break;
         }
    }
}

So how to quickly judge whether the module can be used?

#include <SoftwareSerial.h>

SoftwareSerial bcserial = SoftwareSerial(50,52); //rx tx

void setup() {
    
    
  // put your setup code here, to run once:
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  bcserial.begin(9600);
  delay(100);
  while (1)
  {
    
    
      Serial.print("AT\r\n");
      bcserial.println("AT");
      delay(300);
      Serial.println(bcserial.read());
      while (bcserial.available() > 0)
      {
    
    
          if (bcserial.find("OK"))
          {
    
    
              digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
              delay(100);              // wait for a second
              digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
              delay(100);
              Serial.println('OK');
              return;
           }
           else
           {
    
    
              Serial.print("AT");
              Serial.println("  Return ERROR!");
           }
        }
        delay(200);
    }
  
}

void loop() {
    
    
    // put your main code here, to run repeatedly:
    Serial.println("OKKKKKK");
}

If it returns information like this, it means that the connection between the module and Arduino is good
Please add a picture description

4. Settings of Alibaba Cloud Platform

First of all, you need to activate Alibaba Cloud's IoT platform. For testing, just use a public instance.

1. Add products and add features

In the product management on the left, create a product:

Please add a picture description
Product Name Custom

Please add a picture description
After clicking View, select the function definition and then click Add Custom Function (I have already added and published this Please add a picture description
)
Please add a picture description
Please add a picture description
and then click Publish to go online.

2. Add device

Please add a picture description
Click Device Management, then click Add Device, and the following screen will appear. You can define the device name yourself.
Please add a picture description
Then fill in the three elements of the device in the corresponding positions in the Arduino program, and then burn them into the Arduino, waiting for the device to be activated and connected.
Please add a picture description
Please add a picture description

After the activation is successful, you can see the data of the function just added in the physical data.

V. Summary

The first time I was tossing NB-IoT, I stepped on a lot of pits, and more pits were stepped on by my partner (both of us were exposed to this for the first time).

Posting this is just to record the whole process that can run successfully.

The Arduino program is still too troublesome to use. There are plans to package a library compatible with BC260Y and BC35 NB-IoT modules in the future, which will be relatively convenient to use. But I also have to learn a lot.

Guess you like

Origin blog.csdn.net/qq_17790209/article/details/119769792