Delta_NBIoT_GW / Mbed 2 deprecated nbiot_mqtt_v2

Dependencies:   mbed itoa DFRobot_SIM7000-master millis

Revision:
2:62b58f79338f
Parent:
1:450535d62644
--- a/main.cpp	Wed May 15 14:28:17 2019 +0000
+++ b/main.cpp	Tue May 21 10:52:54 2019 +0000
@@ -14,7 +14,7 @@
 DFRobot_SIM7000    sim7000;
 DigitalOut myled(LED1);
 
-Serial pc(USBTX, USBRX, 9600);
+Serial pc(USBTX, USBRX, 57600);
 Serial nbiot_shield(PA_9, PA_10, 19200); //(tx,rx,baud) 
 
 char IMSI[20];
@@ -29,33 +29,34 @@
     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());
+    pc.putc(nbiot_shield.getc());
     
-    char msg[20];
-    nbiot_shield.gets(msg, 20);
-    pc.printf("Message from SIM: %s", msg);
+    //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);
+    pc.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");
+          pc.printf("Connect OK\r\n");
           break;
       }else{
-          printf("Fail to connect\r\n");
+          pc.printf("Fail to connect\r\n");
           wait(10);
       }
     }
   
-    printf("Connecting to the MQTT......\r\n");
+    pc.printf("Connecting to the MQTT......\r\n");
     if(sim7000.mqttConnect(IMSI,USERNAME,PASSWORD)){    //MQTT connect request
-        printf("OK\r\n");
+        pc.printf("OK\r\n");
     }else{
-        printf("Failed\r\n");
+        pc.printf("Failed\r\n");
         //return;
     }
     
@@ -63,11 +64,11 @@
     
     cTopic = new char[topic.length() + 1];
     strcpy(cTopic, topic.c_str());
-    printf("Subscribe to the MQTT Topic %s......\r\n", cTopic);
+    pc.printf("Subscribe to the MQTT Topic %s......\r\n", cTopic);
     if(sim7000.mqttSubscribe(cTopic)){  
-        printf("OK\r\n");
+        pc.printf("OK\r\n");
     }else{
-        printf("Failed\r\n");
+        pc.printf("Failed\r\n");
         //return;
     }
     
@@ -79,46 +80,46 @@
     
     sim7000.begin(nbiot_shield);
     
-    printf("Turn ON SIM7000......\r\n");
+    pc.printf("Turn ON SIM7000......\r\n");
     if(sim7000.turnON()){                             //Turn ON SIM7000
-        printf("Turn ON !\r\n");
+        pc.printf("Turn ON !\r\n");
     }
     
-    printf("Set baud rate......\r\n");
+    pc.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");
+            pc.printf("Set baud rate:19200\r\n");
             break;
         }else{
-            printf("Faile to set baud rate\r\n");
+            pc.printf("Faile to set baud rate\r\n");
             wait(1);
         }
     }
     
-    printf("Set the APN......\r\n");
+    pc.printf("Set the APN......\r\n");
     while(1){
         if (sim7000.check_send_cmd("AT+CSTT=\"internet.iot\"\r\n","OK")){
-          printf("OK\r\n");
+          pc.printf("OK\r\n");
           break;
         }
         else{
-          printf("Faile to set the APN\r\n");
+          pc.printf("Faile to set the APN\r\n");
           wait_ms(1000);
         }
     }
 
-    printf("Bring Up Wireless Connection with GPRS......\r\n");
+    pc.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");
+            pc.printf("Successfully\r\n");
             break;
         }else{
-            printf("Faile to bring Up Wireless Connection with GPRS\r\n");
+            pc.printf("Faile to bring Up Wireless Connection with GPRS\r\n");
             wait_ms(1000);
         }
     }
 
-    printf("Get local IP address......\r\n");
+    pc.printf("Get local IP address......\r\n");
     char gprsBuffer[32];
     while(1){
         sim7000.cleanBuffer(gprsBuffer,32);
@@ -126,30 +127,31 @@
         sim7000.readBuffer(gprsBuffer, 32, DEFAULT_TIMEOUT);
         
         if(NULL != strstr(gprsBuffer, "ERROR")){
-            printf("Faile to Get local IP address\r\n");
+            pc.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");
+          pc.printf("OK\r\n");
           break;
         }
     }
-
-    printf("Get IMSI......\r\n");
+    
+    
+    pc.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");
+            pc.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);
+          pc.printf("%s\r\n", IMSI);
           str_IMSI = IMSI;
           break;
         }
@@ -170,39 +172,50 @@
     //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);
-            }*/
+            char recv_msg[64];
+            string str_recv_msg;
+            pc.printf("received: ");
+            sim7000.cleanBuffer(recv_msg,64);
+            //wait_ms(100);
+            sim7000.readBuffer(recv_msg, 64, DEFAULT_TIMEOUT);
+            
+            pc.printf("%s\r\n", recv_msg);
+            
+            str_recv_msg = recv_msg;
+            int i = str_recv_msg.find(cTopic);
+            if (i>=0){
+                str_recv_msg = str_recv_msg.substr(i+strlen(cTopic));
+            }
+            pc.printf("payload: %s\r\n", str_recv_msg);
+            
         }
             
         currentTime = millis();
-        //printf("%d\r\n", currentTime - previousTime);
         if(currentTime - previousTime >  600000 || send_flag == true){
             send_flag = false;
-            printf("---------------------------\r\n");
+            pc.printf("---------------------------\r\n");
             sprintf(msg, "delta%d,%s", count, IMSI);
-            printf("%s\r\n", msg);
+            pc.printf("%s\r\n", msg);
             str_msg = msg;
             if (sim7000.mqttPublish(PUBLISH_TOPIC, str_msg)){
-                printf("Send OK\r\n");
+                pc.printf("Send OK\r\n");
             }else{
-                printf("Failed to send\r\n"); //Can do reconnection here
+                pc.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);
+            pc.printf("Disconnection count: %d\r\n", disconnection_count);
             previousTime = currentTime;
             count++;
         }
-        //wait(1);
+        //wait_ms(10);
         
     }