nbiot mqtt test
Dependencies: mbed itoa DFRobot_SIM7000-master millis
main.cpp
- Committer:
- s107062534
- Date:
- 2019-05-15
- Revision:
- 1:450535d62644
- Parent:
- 0:8864eb729b32
- Child:
- 2:62b58f79338f
File content as of revision 1:450535d62644:
#include "mbed.h" #include <iostream> #include <string> #include "DFRobot_SIM7000.h" #define server_ip "140.114.78.140" //140.114.89.66 //140.114.78.140 #define PORT 1883 //TCP: 3307 //MQTT: 1883 #define CLIENT_ID "" #define USERNAME "delta" #define PASSWORD "delta" #define SUBSCRIBE_TOPIC_PREFIX "nbiot/" #define PUBLISH_TOPIC "nbiot/gw" DFRobot_SIM7000 sim7000; DigitalOut myled(LED1); Serial pc(USBTX, USBRX, 9600); Serial nbiot_shield(PA_9, PA_10, 19200); //(tx,rx,baud) char IMSI[20]; string str_IMSI; char *cTopic; void PC_callback() { //useless now // Note: you need to actually read from the serial to clear the RX interrupt // nbiot_shield.putc(pc.getc()); char cmd[20]; pc.gets(cmd, 20); pc.printf("Command from PC: %s", cmd); nbiot_shield.printf(cmd); } void NBIOT_callback() { //useless now //pc.printf("NBIOT_callback\r\n"); //pc.putc(nbiot_shield.getc()); char msg[20]; nbiot_shield.gets(msg, 20); pc.printf("Message from SIM: %s", msg); } void connect_to_server(){ printf("Connecting to the server %s......\r\n", server_ip); while(1){ if(sim7000.openNetwork(TCP,server_ip,PORT)){ //Start up TCP connection //TCP: 3307 //MQTT: 1883 printf("Connect OK\r\n"); break; }else{ printf("Fail to connect\r\n"); wait(10); } } printf("Connecting to the MQTT......\r\n"); if(sim7000.mqttConnect(IMSI,USERNAME,PASSWORD)){ //MQTT connect request printf("OK\r\n"); }else{ printf("Failed\r\n"); //return; } string topic = SUBSCRIBE_TOPIC_PREFIX + str_IMSI.substr(5); //first 5 number are fixed cTopic = new char[topic.length() + 1]; strcpy(cTopic, topic.c_str()); printf("Subscribe to the MQTT Topic %s......\r\n", cTopic); if(sim7000.mqttSubscribe(cTopic)){ printf("OK\r\n"); }else{ printf("Failed\r\n"); //return; } } int main() { millisStart(); sim7000.begin(nbiot_shield); printf("Turn ON SIM7000......\r\n"); if(sim7000.turnON()){ //Turn ON SIM7000 printf("Turn ON !\r\n"); } printf("Set baud rate......\r\n"); while(1){ if(sim7000.setBaudRate(19200)){ //Set SIM7000 baud rate from 115200 to 19200 reduce the baud rate to avoid distortion printf("Set baud rate:19200\r\n"); break; }else{ printf("Faile to set baud rate\r\n"); wait(1); } } printf("Set the APN......\r\n"); while(1){ if (sim7000.check_send_cmd("AT+CSTT=\"internet.iot\"\r\n","OK")){ printf("OK\r\n"); break; } else{ printf("Faile to set the APN\r\n"); wait_ms(1000); } } printf("Bring Up Wireless Connection with GPRS......\r\n"); while(1){ if(sim7000.check_send_cmd("AT+CIICR\r\n","OK")){ printf("Successfully\r\n"); break; }else{ printf("Faile to bring Up Wireless Connection with GPRS\r\n"); wait_ms(1000); } } printf("Get local IP address......\r\n"); char gprsBuffer[32]; while(1){ sim7000.cleanBuffer(gprsBuffer,32); sim7000.send_cmd("AT+CIFSR\r\n"); sim7000.readBuffer(gprsBuffer, 32, DEFAULT_TIMEOUT); if(NULL != strstr(gprsBuffer, "ERROR")){ printf("Faile to Get local IP address\r\n"); wait_ms(1000); } else{ //strcpy(local_IP, gprsBuffer+strlen("AT+CIFSR\r\n")+1); //implement issue here printf("OK\r\n"); break; } } printf("Get IMSI......\r\n"); while(1){ sim7000.cleanBuffer(gprsBuffer,32); sim7000.send_cmd("AT+CIMI\r\n"); sim7000.readBuffer(gprsBuffer, 32, DEFAULT_TIMEOUT); if(NULL != strstr(gprsBuffer, "ERROR")){ printf("Faile to Get IMSI\r\n"); wait_ms(1000); } else{ strncpy(IMSI, gprsBuffer+strlen("AT+CIMI\r\n")+1, 15); //implement issue here //Serial.println(gprsBuffer); printf("%s\r\n", IMSI); str_IMSI = IMSI; break; } } connect_to_server(); bool send_flag = true; int count = 0; char msg[100]; string str_msg; unsigned long previousTime = 0; unsigned long currentTime; int disconnection_count = 0; //string str_test = "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjj"; //100bytes //pc.attach(&PC_callback); //nbiot_shield.attach(&NBIOT_callback); while(1) { if(nbiot_shield.readable()){ //check downlink pc.putc(nbiot_shield.getc()); /*char recv_msg[50]; if(sim7000.mqttRecv(cTopic, recv_msg, 50)){ //read buffer printf("recevied: %s",recv_msg); }*/ } currentTime = millis(); //printf("%d\r\n", currentTime - previousTime); if(currentTime - previousTime > 600000 || send_flag == true){ send_flag = false; printf("---------------------------\r\n"); sprintf(msg, "delta%d,%s", count, IMSI); printf("%s\r\n", msg); str_msg = msg; if (sim7000.mqttPublish(PUBLISH_TOPIC, str_msg)){ printf("Send OK\r\n"); }else{ printf("Failed to send\r\n"); //Can do reconnection here disconnection_count++; sim7000.closeNetwork(); wait_ms(1000); connect_to_server(); send_flag = true; } printf("Disconnection count: %d\r\n", disconnection_count); previousTime = currentTime; count++; } //wait(1); } }