nbiot mqtt test
Dependencies: mbed itoa DFRobot_SIM7000-master millis
main.cpp@1:450535d62644, 2019-05-15 (annotated)
- Committer:
- s107062534
- Date:
- Wed May 15 14:28:17 2019 +0000
- Revision:
- 1:450535d62644
- Parent:
- 0:8864eb729b32
- Child:
- 2:62b58f79338f
nbiot mqtt test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nobelkuo | 0:8864eb729b32 | 1 | #include "mbed.h" |
s107062534 | 1:450535d62644 | 2 | #include <iostream> |
s107062534 | 1:450535d62644 | 3 | #include <string> |
s107062534 | 1:450535d62644 | 4 | #include "DFRobot_SIM7000.h" |
nobelkuo | 0:8864eb729b32 | 5 | |
s107062534 | 1:450535d62644 | 6 | #define server_ip "140.114.78.140" //140.114.89.66 //140.114.78.140 |
s107062534 | 1:450535d62644 | 7 | #define PORT 1883 //TCP: 3307 //MQTT: 1883 |
s107062534 | 1:450535d62644 | 8 | #define CLIENT_ID "" |
s107062534 | 1:450535d62644 | 9 | #define USERNAME "delta" |
s107062534 | 1:450535d62644 | 10 | #define PASSWORD "delta" |
s107062534 | 1:450535d62644 | 11 | #define SUBSCRIBE_TOPIC_PREFIX "nbiot/" |
s107062534 | 1:450535d62644 | 12 | #define PUBLISH_TOPIC "nbiot/gw" |
s107062534 | 1:450535d62644 | 13 | |
s107062534 | 1:450535d62644 | 14 | DFRobot_SIM7000 sim7000; |
s107062534 | 1:450535d62644 | 15 | DigitalOut myled(LED1); |
s107062534 | 1:450535d62644 | 16 | |
s107062534 | 1:450535d62644 | 17 | Serial pc(USBTX, USBRX, 9600); |
s107062534 | 1:450535d62644 | 18 | Serial nbiot_shield(PA_9, PA_10, 19200); //(tx,rx,baud) |
s107062534 | 1:450535d62644 | 19 | |
s107062534 | 1:450535d62644 | 20 | char IMSI[20]; |
s107062534 | 1:450535d62644 | 21 | string str_IMSI; |
s107062534 | 1:450535d62644 | 22 | char *cTopic; |
nobelkuo | 0:8864eb729b32 | 23 | |
s107062534 | 1:450535d62644 | 24 | void PC_callback() { //useless now |
nobelkuo | 0:8864eb729b32 | 25 | // Note: you need to actually read from the serial to clear the RX interrupt |
s107062534 | 1:450535d62644 | 26 | // nbiot_shield.putc(pc.getc()); |
s107062534 | 1:450535d62644 | 27 | char cmd[20]; |
s107062534 | 1:450535d62644 | 28 | pc.gets(cmd, 20); |
s107062534 | 1:450535d62644 | 29 | pc.printf("Command from PC: %s", cmd); |
s107062534 | 1:450535d62644 | 30 | nbiot_shield.printf(cmd); |
nobelkuo | 0:8864eb729b32 | 31 | } |
s107062534 | 1:450535d62644 | 32 | void NBIOT_callback() { //useless now |
s107062534 | 1:450535d62644 | 33 | //pc.printf("NBIOT_callback\r\n"); |
s107062534 | 1:450535d62644 | 34 | //pc.putc(nbiot_shield.getc()); |
s107062534 | 1:450535d62644 | 35 | |
s107062534 | 1:450535d62644 | 36 | char msg[20]; |
s107062534 | 1:450535d62644 | 37 | nbiot_shield.gets(msg, 20); |
s107062534 | 1:450535d62644 | 38 | pc.printf("Message from SIM: %s", msg); |
nobelkuo | 0:8864eb729b32 | 39 | } |
s107062534 | 1:450535d62644 | 40 | |
s107062534 | 1:450535d62644 | 41 | |
s107062534 | 1:450535d62644 | 42 | void connect_to_server(){ |
s107062534 | 1:450535d62644 | 43 | printf("Connecting to the server %s......\r\n", server_ip); |
s107062534 | 1:450535d62644 | 44 | while(1){ |
s107062534 | 1:450535d62644 | 45 | if(sim7000.openNetwork(TCP,server_ip,PORT)){ //Start up TCP connection //TCP: 3307 //MQTT: 1883 |
s107062534 | 1:450535d62644 | 46 | printf("Connect OK\r\n"); |
s107062534 | 1:450535d62644 | 47 | break; |
s107062534 | 1:450535d62644 | 48 | }else{ |
s107062534 | 1:450535d62644 | 49 | printf("Fail to connect\r\n"); |
s107062534 | 1:450535d62644 | 50 | wait(10); |
s107062534 | 1:450535d62644 | 51 | } |
s107062534 | 1:450535d62644 | 52 | } |
s107062534 | 1:450535d62644 | 53 | |
s107062534 | 1:450535d62644 | 54 | printf("Connecting to the MQTT......\r\n"); |
s107062534 | 1:450535d62644 | 55 | if(sim7000.mqttConnect(IMSI,USERNAME,PASSWORD)){ //MQTT connect request |
s107062534 | 1:450535d62644 | 56 | printf("OK\r\n"); |
s107062534 | 1:450535d62644 | 57 | }else{ |
s107062534 | 1:450535d62644 | 58 | printf("Failed\r\n"); |
s107062534 | 1:450535d62644 | 59 | //return; |
s107062534 | 1:450535d62644 | 60 | } |
s107062534 | 1:450535d62644 | 61 | |
s107062534 | 1:450535d62644 | 62 | string topic = SUBSCRIBE_TOPIC_PREFIX + str_IMSI.substr(5); //first 5 number are fixed |
s107062534 | 1:450535d62644 | 63 | |
s107062534 | 1:450535d62644 | 64 | cTopic = new char[topic.length() + 1]; |
s107062534 | 1:450535d62644 | 65 | strcpy(cTopic, topic.c_str()); |
s107062534 | 1:450535d62644 | 66 | printf("Subscribe to the MQTT Topic %s......\r\n", cTopic); |
s107062534 | 1:450535d62644 | 67 | if(sim7000.mqttSubscribe(cTopic)){ |
s107062534 | 1:450535d62644 | 68 | printf("OK\r\n"); |
s107062534 | 1:450535d62644 | 69 | }else{ |
s107062534 | 1:450535d62644 | 70 | printf("Failed\r\n"); |
s107062534 | 1:450535d62644 | 71 | //return; |
s107062534 | 1:450535d62644 | 72 | } |
s107062534 | 1:450535d62644 | 73 | |
s107062534 | 1:450535d62644 | 74 | } |
s107062534 | 1:450535d62644 | 75 | |
nobelkuo | 0:8864eb729b32 | 76 | |
nobelkuo | 0:8864eb729b32 | 77 | int main() { |
s107062534 | 1:450535d62644 | 78 | millisStart(); |
s107062534 | 1:450535d62644 | 79 | |
s107062534 | 1:450535d62644 | 80 | sim7000.begin(nbiot_shield); |
s107062534 | 1:450535d62644 | 81 | |
s107062534 | 1:450535d62644 | 82 | printf("Turn ON SIM7000......\r\n"); |
s107062534 | 1:450535d62644 | 83 | if(sim7000.turnON()){ //Turn ON SIM7000 |
s107062534 | 1:450535d62644 | 84 | printf("Turn ON !\r\n"); |
s107062534 | 1:450535d62644 | 85 | } |
s107062534 | 1:450535d62644 | 86 | |
s107062534 | 1:450535d62644 | 87 | printf("Set baud rate......\r\n"); |
s107062534 | 1:450535d62644 | 88 | while(1){ |
s107062534 | 1:450535d62644 | 89 | if(sim7000.setBaudRate(19200)){ //Set SIM7000 baud rate from 115200 to 19200 reduce the baud rate to avoid distortion |
s107062534 | 1:450535d62644 | 90 | printf("Set baud rate:19200\r\n"); |
s107062534 | 1:450535d62644 | 91 | break; |
s107062534 | 1:450535d62644 | 92 | }else{ |
s107062534 | 1:450535d62644 | 93 | printf("Faile to set baud rate\r\n"); |
s107062534 | 1:450535d62644 | 94 | wait(1); |
nobelkuo | 0:8864eb729b32 | 95 | } |
s107062534 | 1:450535d62644 | 96 | } |
s107062534 | 1:450535d62644 | 97 | |
s107062534 | 1:450535d62644 | 98 | printf("Set the APN......\r\n"); |
s107062534 | 1:450535d62644 | 99 | while(1){ |
s107062534 | 1:450535d62644 | 100 | if (sim7000.check_send_cmd("AT+CSTT=\"internet.iot\"\r\n","OK")){ |
s107062534 | 1:450535d62644 | 101 | printf("OK\r\n"); |
s107062534 | 1:450535d62644 | 102 | break; |
s107062534 | 1:450535d62644 | 103 | } |
s107062534 | 1:450535d62644 | 104 | else{ |
s107062534 | 1:450535d62644 | 105 | printf("Faile to set the APN\r\n"); |
s107062534 | 1:450535d62644 | 106 | wait_ms(1000); |
s107062534 | 1:450535d62644 | 107 | } |
s107062534 | 1:450535d62644 | 108 | } |
s107062534 | 1:450535d62644 | 109 | |
s107062534 | 1:450535d62644 | 110 | printf("Bring Up Wireless Connection with GPRS......\r\n"); |
s107062534 | 1:450535d62644 | 111 | while(1){ |
s107062534 | 1:450535d62644 | 112 | if(sim7000.check_send_cmd("AT+CIICR\r\n","OK")){ |
s107062534 | 1:450535d62644 | 113 | printf("Successfully\r\n"); |
s107062534 | 1:450535d62644 | 114 | break; |
s107062534 | 1:450535d62644 | 115 | }else{ |
s107062534 | 1:450535d62644 | 116 | printf("Faile to bring Up Wireless Connection with GPRS\r\n"); |
s107062534 | 1:450535d62644 | 117 | wait_ms(1000); |
s107062534 | 1:450535d62644 | 118 | } |
s107062534 | 1:450535d62644 | 119 | } |
s107062534 | 1:450535d62644 | 120 | |
s107062534 | 1:450535d62644 | 121 | printf("Get local IP address......\r\n"); |
s107062534 | 1:450535d62644 | 122 | char gprsBuffer[32]; |
s107062534 | 1:450535d62644 | 123 | while(1){ |
s107062534 | 1:450535d62644 | 124 | sim7000.cleanBuffer(gprsBuffer,32); |
s107062534 | 1:450535d62644 | 125 | sim7000.send_cmd("AT+CIFSR\r\n"); |
s107062534 | 1:450535d62644 | 126 | sim7000.readBuffer(gprsBuffer, 32, DEFAULT_TIMEOUT); |
nobelkuo | 0:8864eb729b32 | 127 | |
s107062534 | 1:450535d62644 | 128 | if(NULL != strstr(gprsBuffer, "ERROR")){ |
s107062534 | 1:450535d62644 | 129 | printf("Faile to Get local IP address\r\n"); |
s107062534 | 1:450535d62644 | 130 | wait_ms(1000); |
s107062534 | 1:450535d62644 | 131 | } |
s107062534 | 1:450535d62644 | 132 | else{ |
s107062534 | 1:450535d62644 | 133 | //strcpy(local_IP, gprsBuffer+strlen("AT+CIFSR\r\n")+1); //implement issue here |
s107062534 | 1:450535d62644 | 134 | printf("OK\r\n"); |
s107062534 | 1:450535d62644 | 135 | break; |
s107062534 | 1:450535d62644 | 136 | } |
s107062534 | 1:450535d62644 | 137 | } |
s107062534 | 1:450535d62644 | 138 | |
s107062534 | 1:450535d62644 | 139 | printf("Get IMSI......\r\n"); |
s107062534 | 1:450535d62644 | 140 | while(1){ |
s107062534 | 1:450535d62644 | 141 | sim7000.cleanBuffer(gprsBuffer,32); |
s107062534 | 1:450535d62644 | 142 | sim7000.send_cmd("AT+CIMI\r\n"); |
s107062534 | 1:450535d62644 | 143 | sim7000.readBuffer(gprsBuffer, 32, DEFAULT_TIMEOUT); |
nobelkuo | 0:8864eb729b32 | 144 | |
s107062534 | 1:450535d62644 | 145 | if(NULL != strstr(gprsBuffer, "ERROR")){ |
s107062534 | 1:450535d62644 | 146 | printf("Faile to Get IMSI\r\n"); |
s107062534 | 1:450535d62644 | 147 | wait_ms(1000); |
s107062534 | 1:450535d62644 | 148 | } |
s107062534 | 1:450535d62644 | 149 | else{ |
s107062534 | 1:450535d62644 | 150 | strncpy(IMSI, gprsBuffer+strlen("AT+CIMI\r\n")+1, 15); //implement issue here |
s107062534 | 1:450535d62644 | 151 | //Serial.println(gprsBuffer); |
s107062534 | 1:450535d62644 | 152 | printf("%s\r\n", IMSI); |
s107062534 | 1:450535d62644 | 153 | str_IMSI = IMSI; |
s107062534 | 1:450535d62644 | 154 | break; |
s107062534 | 1:450535d62644 | 155 | } |
s107062534 | 1:450535d62644 | 156 | } |
s107062534 | 1:450535d62644 | 157 | |
s107062534 | 1:450535d62644 | 158 | connect_to_server(); |
s107062534 | 1:450535d62644 | 159 | |
s107062534 | 1:450535d62644 | 160 | |
s107062534 | 1:450535d62644 | 161 | bool send_flag = true; |
s107062534 | 1:450535d62644 | 162 | int count = 0; |
s107062534 | 1:450535d62644 | 163 | char msg[100]; |
s107062534 | 1:450535d62644 | 164 | string str_msg; |
s107062534 | 1:450535d62644 | 165 | unsigned long previousTime = 0; |
s107062534 | 1:450535d62644 | 166 | unsigned long currentTime; |
s107062534 | 1:450535d62644 | 167 | int disconnection_count = 0; |
s107062534 | 1:450535d62644 | 168 | //string str_test = "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjj"; //100bytes |
s107062534 | 1:450535d62644 | 169 | |
s107062534 | 1:450535d62644 | 170 | //pc.attach(&PC_callback); |
s107062534 | 1:450535d62644 | 171 | //nbiot_shield.attach(&NBIOT_callback); |
s107062534 | 1:450535d62644 | 172 | |
s107062534 | 1:450535d62644 | 173 | while(1) { |
s107062534 | 1:450535d62644 | 174 | |
s107062534 | 1:450535d62644 | 175 | if(nbiot_shield.readable()){ //check downlink |
s107062534 | 1:450535d62644 | 176 | pc.putc(nbiot_shield.getc()); |
s107062534 | 1:450535d62644 | 177 | /*char recv_msg[50]; |
s107062534 | 1:450535d62644 | 178 | if(sim7000.mqttRecv(cTopic, recv_msg, 50)){ //read buffer |
s107062534 | 1:450535d62644 | 179 | printf("recevied: %s",recv_msg); |
s107062534 | 1:450535d62644 | 180 | }*/ |
s107062534 | 1:450535d62644 | 181 | } |
s107062534 | 1:450535d62644 | 182 | |
s107062534 | 1:450535d62644 | 183 | currentTime = millis(); |
s107062534 | 1:450535d62644 | 184 | //printf("%d\r\n", currentTime - previousTime); |
s107062534 | 1:450535d62644 | 185 | if(currentTime - previousTime > 600000 || send_flag == true){ |
s107062534 | 1:450535d62644 | 186 | send_flag = false; |
s107062534 | 1:450535d62644 | 187 | printf("---------------------------\r\n"); |
s107062534 | 1:450535d62644 | 188 | sprintf(msg, "delta%d,%s", count, IMSI); |
s107062534 | 1:450535d62644 | 189 | printf("%s\r\n", msg); |
s107062534 | 1:450535d62644 | 190 | str_msg = msg; |
s107062534 | 1:450535d62644 | 191 | if (sim7000.mqttPublish(PUBLISH_TOPIC, str_msg)){ |
s107062534 | 1:450535d62644 | 192 | printf("Send OK\r\n"); |
s107062534 | 1:450535d62644 | 193 | }else{ |
s107062534 | 1:450535d62644 | 194 | printf("Failed to send\r\n"); //Can do reconnection here |
s107062534 | 1:450535d62644 | 195 | disconnection_count++; |
s107062534 | 1:450535d62644 | 196 | sim7000.closeNetwork(); |
s107062534 | 1:450535d62644 | 197 | wait_ms(1000); |
s107062534 | 1:450535d62644 | 198 | connect_to_server(); |
s107062534 | 1:450535d62644 | 199 | send_flag = true; |
s107062534 | 1:450535d62644 | 200 | } |
s107062534 | 1:450535d62644 | 201 | printf("Disconnection count: %d\r\n", disconnection_count); |
s107062534 | 1:450535d62644 | 202 | previousTime = currentTime; |
s107062534 | 1:450535d62644 | 203 | count++; |
s107062534 | 1:450535d62644 | 204 | } |
s107062534 | 1:450535d62644 | 205 | //wait(1); |
nobelkuo | 0:8864eb729b32 | 206 | |
nobelkuo | 0:8864eb729b32 | 207 | } |
s107062534 | 1:450535d62644 | 208 | |
nobelkuo | 0:8864eb729b32 | 209 | } |