Transmit pressure/temperature reading once per hour from xDot to iupivot server

Dependencies:   ATParser MPL3115A2 mbed

Revision:
2:9af94ceb9d37
Parent:
1:b672c24629d5
Child:
3:59fbf4f41f69
--- a/main.cpp	Wed Apr 04 14:55:38 2018 +0000
+++ b/main.cpp	Thu Apr 05 14:56:40 2018 +0000
@@ -1,31 +1,50 @@
 #include "mbed.h"
 #include "MPL3115A2.h"
 #include "string.h"
+#include "ATParser.h"
+
 MPL3115A2 pressure_sensor(PB_7,PB_6,0x60);
-Serial pc(SERIAL_TX, SERIAL_RX, 115200);
-Serial device(PA_9, PA_10, 115200);  // tx, rx
+BufferedSerial pc(SERIAL_TX, SERIAL_RX);
+BufferedSerial device(PA_9, PA_10);  // tx, rx
 DigitalOut myled(LED1);
 
+
 /*  Checks to see if the xDot has a buffered message OK to 
     confirm that operation has succeeded
 */
 int confirmOK() { 
-    char buf[20];
+    while (!device.readable()) {
+        wait_ms(100);
+    }
+    int bufferSize = 100;
+    char buf[bufferSize];
     int i = 0; 
-    while (device.readable()) { 
+    for (i = 0; i < bufferSize; i++) { 
+        buf[i] = 0;
+    }
+    i = 0;
+    // Wait until device has a response 
+    for (i = 0; device.readable(); i++) { 
         buf[i] = device.getc();
-        if (i > 20) { 
+        if (i >= bufferSize) { 
             return 0;
         }
-        i++;
     }
-    return strncmp("OK",buf,2);
+    pc.printf("I got %s from the device\r\n", buf);
+    wait_ms(100);
+    if (strncmp("OK",buf,2)) { 
+        return 1;
+    } else if (strncmp("ERROR",buf,5)) { 
+        return 0;
+    } else {
+        confirmOK();
+    }
 }
 /*  Sends a message to the xDot
     Messages are expected to end in \n so that an OK confirmation will be 
     recieved
 */
-int sendAtMessage(char * message) { 
+int sendAtMessage(char * message) {
     while (*message) { 
         device.putc(*message);
         message++;
@@ -36,32 +55,46 @@
 /*
 Send a message to the xDot without checking for acknowledge
 */
-void sendAtMessageNACK(char * message) { 
-    while (*message) { 
+void sendAtMessageNACK(char * message, int size) {
+    int i = 0;
+    while (i < size) { 
         device.putc(*message);
-        message++;
+        pc.printf("putting %c on device\n\r", *message++);
+        i++;
     }
 }
 int main() {
+    pc.baud(115200);
+    device.baud(115200);
+    ATParser atp = ATParser(device,"\r\n",256,2000,false);
     
     pc.printf("Starting program\n\r");
-
+    
+    if (!sendAtMessage("AT\n")) { 
+        pc.printf("xDot not responding to message\n\r");
+    }
     //Try to connect xDot to network
     // Provice network name
-    char networkName[] = "AT+NI=1, MTCDT-19400691\n";
-    sendAtMessage(networkName);
+    char networkName[] = "AT+NI=1,MTCDT-19400691\n";
+    if (!sendAtMessage(networkName)) {
+        pc.printf("Unable to set network name\n\r");
+    }
     //Provide network passphrase
     char networkPassphrase[] = "AT+NK=1,MTCDT-19400691\n";
-    sendAtMessage(networkPassphrase);
+    if (!sendAtMessage(networkPassphrase)) { 
+        pc.printf("Network passphrase not successfully set\n\r");
+    }
     //Set the frequency sub band
     char frequencySubBand[] = "AT+FSB=1\n";
-    sendAtMessage(frequencySubBand);
+    if (sendAtMessage(frequencySubBand)) { 
+        //pc.printf("Network frequency SubBand successfully set\n\r");
+    }
     //Join the network
     char joinNetwork[] = "AT+JOIN\n"; // Unable to join network atm.
     if (!sendAtMessage(joinNetwork)) { 
-        pc.printf("Error, unable to connect to network");
+        pc.printf("Failed to joined the network\n\r");
     }
-    wait_ms(500);
+    
     
     //When an s is read from the serial pc,
     // read the current pressure/temperature from the I2C pressure sensor
@@ -70,19 +103,31 @@
     //Unsure of what size to set Buffer so that it doesn't go over
     //when trying to send a message
     while(1) {
-        if (pc.readable()) { 
+        if (pc.readable()) {
+            pc.printf("pc.readable\r\n");
             if (pc.getc() == 's') { 
+                pc.printf("I recieved an s\r\n");
+                int tempBufSize = 4;
+                int presBufSize = 6;
                 double temp = pressure_sensor.getTemperature();
-                char tempBuf[6];
+                char tempBuf[tempBufSize];
                 double pres = pressure_sensor.getPressure();
-                char presBuf[7];
+                char presBuf[presBufSize];
                 pc.printf("Current temp is %f\n\r",temp);
                 pc.printf("Current pres is %f\n\r",pres);
-                sprintf(tempBuf, "%2.2f",temp);
+                sprintf(tempBuf, "%2.1f",temp);
                 sprintf(presBuf, "%4.1f",pres);
                 pc.printf("After convert with sprintf temp = %s\n\r",tempBuf);
                 pc.printf("After convert with sprintf pres = %s\n\r",presBuf);
-                
+                sendAtMessageNACK("AT+SEND=",8);
+                sendAtMessageNACK(tempBuf,tempBufSize);
+                sendAtMessageNACK(",",1);
+                sendAtMessageNACK(presBuf,presBufSize);
+                wait_ms(500);
+                if (sendAtMessage("\n")) { 
+                    pc.printf("message succesfully sent\n\r");
+                }  
+                //void memset(* void tmp,0,100);
             }
         }
     }