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

Dependencies:   ATParser MPL3115A2 mbed

Revision:
1:b672c24629d5
Parent:
0:50820e1648aa
Child:
2:9af94ceb9d37
--- a/main.cpp	Tue Apr 03 14:44:59 2018 +0000
+++ b/main.cpp	Wed Apr 04 14:55:38 2018 +0000
@@ -1,14 +1,93 @@
 #include "mbed.h"
 #include "MPL3115A2.h"
-
+#include "string.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
+DigitalOut myled(LED1);
 
-DigitalOut myled(LED1);
-int main() {    
-//Try to connect xDot to network
+/*  Checks to see if the xDot has a buffered message OK to 
+    confirm that operation has succeeded
+*/
+int confirmOK() { 
+    char buf[20];
+    int i = 0; 
+    while (device.readable()) { 
+        buf[i] = device.getc();
+        if (i > 20) { 
+            return 0;
+        }
+        i++;
+    }
+    return strncmp("OK",buf,2);
+}
+/*  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) { 
+    while (*message) { 
+        device.putc(*message);
+        message++;
+    }
+    return confirmOK();
+}
+
+/*
+Send a message to the xDot without checking for acknowledge
+*/
+void sendAtMessageNACK(char * message) { 
+    while (*message) { 
+        device.putc(*message);
+        message++;
+    }
+}
+int main() {
+    
+    pc.printf("Starting program\n\r");
+
+    //Try to connect xDot to network
+    // Provice network name
+    char networkName[] = "AT+NI=1, MTCDT-19400691\n";
+    sendAtMessage(networkName);
+    //Provide network passphrase
+    char networkPassphrase[] = "AT+NK=1,MTCDT-19400691\n";
+    sendAtMessage(networkPassphrase);
+    //Set the frequency sub band
+    char frequencySubBand[] = "AT+FSB=1\n";
+    sendAtMessage(frequencySubBand);
+    //Join the network
+    char joinNetwork[] = "AT+JOIN\n"; // Unable to join network atm.
+    if (!sendAtMessage(joinNetwork)) { 
+        pc.printf("Error, unable to connect to network");
+    }
+    wait_ms(500);
+    
+    //When an s is read from the serial pc,
+    // read the current pressure/temperature from the I2C pressure sensor
+    // and send it to the MQTT server pivot.iuiot
+    
+    //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.getc() == 's') { 
+                double temp = pressure_sensor.getTemperature();
+                char tempBuf[6];
+                double pres = pressure_sensor.getPressure();
+                char presBuf[7];
+                pc.printf("Current temp is %f\n\r",temp);
+                pc.printf("Current pres is %f\n\r",pres);
+                sprintf(tempBuf, "%2.2f",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);
+                
+            }
+        }
+    }
+    
+    /*
     while(1) {
         if(pc.readable()) {
             device.putc(pc.getc());
@@ -19,4 +98,5 @@
             myled = !myled;
         }
     }
+    */
 }
\ No newline at end of file