nbiot mqtt test

Dependencies:   mbed itoa DFRobot_SIM7000-master millis

Revision:
1:450535d62644
Parent:
0:8864eb729b32
Child:
2:62b58f79338f
--- a/main.cpp	Fri May 10 07:10:39 2019 +0000
+++ b/main.cpp	Wed May 15 14:28:17 2019 +0000
@@ -1,60 +1,209 @@
 #include "mbed.h"
- 
-Serial pc(USBTX, USBRX);
-Serial modbus(PB_9, PB_8,9600);//(tx,rx,baud)
- 
-DigitalOut DE_RE(PB_0);
+#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() {
+void PC_callback() { //useless now
     // Note: you need to actually read from the serial to clear the RX interrupt
-    //modbus.printf("PC_callback\r\n");
-    modbus.putc(pc.getc());
+    // nbiot_shield.putc(pc.getc());
+    char cmd[20];
+    pc.gets(cmd, 20);
+    pc.printf("Command from PC: %s", cmd);
+    nbiot_shield.printf(cmd);
 } 
-void MODBUS_callback() {
-    //pc.printf("MODBUS_callback\r\n");
-    pc.putc(modbus.getc());
+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() {
-    pc.attach(&PC_callback);
-    modbus.attach(&MODBUS_callback);
-    while(1) {
-        
-        /*
-        if(pc.readable()) {
-            modbus.putc(pc.getc());
-            pc_activity = !pc_activity;
+    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);
         }
-        */
-        DE_RE=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);
         
-        modbus.putc(0x01);
-        modbus.putc(0x03);
-        modbus.putc(0x00);
-        modbus.putc(0x00);
-        modbus.putc(0x00);
-        modbus.putc(0x01);
-        modbus.putc(0x84);
-        modbus.putc(0x0A);
-        //wait(3);
-        wait_ms(2);
-        DE_RE =0 ;
-        /*    
-        modbus.putc(0x31);
-        //}
-        //DE_RE =0 ;
-        //wait(0.1);
+        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);
         
-        if(modbus.readable()>0) {
-            pc.printf("modbus read something\r\n");
-            pc.putc(modbus.getc());
-        } else{
-            pc.printf("modbus read nothing\r\n");
-        }
-        
-        */
-        wait(10);
     }
+    
 }
\ No newline at end of file