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

Dependencies:   ATParser MPL3115A2 mbed

Committer:
csinders
Date:
Thu Apr 05 14:56:40 2018 +0000
Revision:
2:9af94ceb9d37
Parent:
1:b672c24629d5
Child:
3:59fbf4f41f69
Almost done with Lab 9, can send messages now.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
csinders 0:50820e1648aa 1 #include "mbed.h"
csinders 0:50820e1648aa 2 #include "MPL3115A2.h"
csinders 1:b672c24629d5 3 #include "string.h"
csinders 2:9af94ceb9d37 4 #include "ATParser.h"
csinders 2:9af94ceb9d37 5
csinders 0:50820e1648aa 6 MPL3115A2 pressure_sensor(PB_7,PB_6,0x60);
csinders 2:9af94ceb9d37 7 BufferedSerial pc(SERIAL_TX, SERIAL_RX);
csinders 2:9af94ceb9d37 8 BufferedSerial device(PA_9, PA_10); // tx, rx
csinders 1:b672c24629d5 9 DigitalOut myled(LED1);
csinders 0:50820e1648aa 10
csinders 2:9af94ceb9d37 11
csinders 1:b672c24629d5 12 /* Checks to see if the xDot has a buffered message OK to
csinders 1:b672c24629d5 13 confirm that operation has succeeded
csinders 1:b672c24629d5 14 */
csinders 1:b672c24629d5 15 int confirmOK() {
csinders 2:9af94ceb9d37 16 while (!device.readable()) {
csinders 2:9af94ceb9d37 17 wait_ms(100);
csinders 2:9af94ceb9d37 18 }
csinders 2:9af94ceb9d37 19 int bufferSize = 100;
csinders 2:9af94ceb9d37 20 char buf[bufferSize];
csinders 1:b672c24629d5 21 int i = 0;
csinders 2:9af94ceb9d37 22 for (i = 0; i < bufferSize; i++) {
csinders 2:9af94ceb9d37 23 buf[i] = 0;
csinders 2:9af94ceb9d37 24 }
csinders 2:9af94ceb9d37 25 i = 0;
csinders 2:9af94ceb9d37 26 // Wait until device has a response
csinders 2:9af94ceb9d37 27 for (i = 0; device.readable(); i++) {
csinders 1:b672c24629d5 28 buf[i] = device.getc();
csinders 2:9af94ceb9d37 29 if (i >= bufferSize) {
csinders 1:b672c24629d5 30 return 0;
csinders 1:b672c24629d5 31 }
csinders 1:b672c24629d5 32 }
csinders 2:9af94ceb9d37 33 pc.printf("I got %s from the device\r\n", buf);
csinders 2:9af94ceb9d37 34 wait_ms(100);
csinders 2:9af94ceb9d37 35 if (strncmp("OK",buf,2)) {
csinders 2:9af94ceb9d37 36 return 1;
csinders 2:9af94ceb9d37 37 } else if (strncmp("ERROR",buf,5)) {
csinders 2:9af94ceb9d37 38 return 0;
csinders 2:9af94ceb9d37 39 } else {
csinders 2:9af94ceb9d37 40 confirmOK();
csinders 2:9af94ceb9d37 41 }
csinders 1:b672c24629d5 42 }
csinders 1:b672c24629d5 43 /* Sends a message to the xDot
csinders 1:b672c24629d5 44 Messages are expected to end in \n so that an OK confirmation will be
csinders 1:b672c24629d5 45 recieved
csinders 1:b672c24629d5 46 */
csinders 2:9af94ceb9d37 47 int sendAtMessage(char * message) {
csinders 1:b672c24629d5 48 while (*message) {
csinders 1:b672c24629d5 49 device.putc(*message);
csinders 1:b672c24629d5 50 message++;
csinders 1:b672c24629d5 51 }
csinders 1:b672c24629d5 52 return confirmOK();
csinders 1:b672c24629d5 53 }
csinders 1:b672c24629d5 54
csinders 1:b672c24629d5 55 /*
csinders 1:b672c24629d5 56 Send a message to the xDot without checking for acknowledge
csinders 1:b672c24629d5 57 */
csinders 2:9af94ceb9d37 58 void sendAtMessageNACK(char * message, int size) {
csinders 2:9af94ceb9d37 59 int i = 0;
csinders 2:9af94ceb9d37 60 while (i < size) {
csinders 1:b672c24629d5 61 device.putc(*message);
csinders 2:9af94ceb9d37 62 pc.printf("putting %c on device\n\r", *message++);
csinders 2:9af94ceb9d37 63 i++;
csinders 1:b672c24629d5 64 }
csinders 1:b672c24629d5 65 }
csinders 1:b672c24629d5 66 int main() {
csinders 2:9af94ceb9d37 67 pc.baud(115200);
csinders 2:9af94ceb9d37 68 device.baud(115200);
csinders 2:9af94ceb9d37 69 ATParser atp = ATParser(device,"\r\n",256,2000,false);
csinders 1:b672c24629d5 70
csinders 1:b672c24629d5 71 pc.printf("Starting program\n\r");
csinders 2:9af94ceb9d37 72
csinders 2:9af94ceb9d37 73 if (!sendAtMessage("AT\n")) {
csinders 2:9af94ceb9d37 74 pc.printf("xDot not responding to message\n\r");
csinders 2:9af94ceb9d37 75 }
csinders 1:b672c24629d5 76 //Try to connect xDot to network
csinders 1:b672c24629d5 77 // Provice network name
csinders 2:9af94ceb9d37 78 char networkName[] = "AT+NI=1,MTCDT-19400691\n";
csinders 2:9af94ceb9d37 79 if (!sendAtMessage(networkName)) {
csinders 2:9af94ceb9d37 80 pc.printf("Unable to set network name\n\r");
csinders 2:9af94ceb9d37 81 }
csinders 1:b672c24629d5 82 //Provide network passphrase
csinders 1:b672c24629d5 83 char networkPassphrase[] = "AT+NK=1,MTCDT-19400691\n";
csinders 2:9af94ceb9d37 84 if (!sendAtMessage(networkPassphrase)) {
csinders 2:9af94ceb9d37 85 pc.printf("Network passphrase not successfully set\n\r");
csinders 2:9af94ceb9d37 86 }
csinders 1:b672c24629d5 87 //Set the frequency sub band
csinders 1:b672c24629d5 88 char frequencySubBand[] = "AT+FSB=1\n";
csinders 2:9af94ceb9d37 89 if (sendAtMessage(frequencySubBand)) {
csinders 2:9af94ceb9d37 90 //pc.printf("Network frequency SubBand successfully set\n\r");
csinders 2:9af94ceb9d37 91 }
csinders 1:b672c24629d5 92 //Join the network
csinders 1:b672c24629d5 93 char joinNetwork[] = "AT+JOIN\n"; // Unable to join network atm.
csinders 1:b672c24629d5 94 if (!sendAtMessage(joinNetwork)) {
csinders 2:9af94ceb9d37 95 pc.printf("Failed to joined the network\n\r");
csinders 1:b672c24629d5 96 }
csinders 2:9af94ceb9d37 97
csinders 1:b672c24629d5 98
csinders 1:b672c24629d5 99 //When an s is read from the serial pc,
csinders 1:b672c24629d5 100 // read the current pressure/temperature from the I2C pressure sensor
csinders 1:b672c24629d5 101 // and send it to the MQTT server pivot.iuiot
csinders 1:b672c24629d5 102
csinders 1:b672c24629d5 103 //Unsure of what size to set Buffer so that it doesn't go over
csinders 1:b672c24629d5 104 //when trying to send a message
csinders 1:b672c24629d5 105 while(1) {
csinders 2:9af94ceb9d37 106 if (pc.readable()) {
csinders 2:9af94ceb9d37 107 pc.printf("pc.readable\r\n");
csinders 1:b672c24629d5 108 if (pc.getc() == 's') {
csinders 2:9af94ceb9d37 109 pc.printf("I recieved an s\r\n");
csinders 2:9af94ceb9d37 110 int tempBufSize = 4;
csinders 2:9af94ceb9d37 111 int presBufSize = 6;
csinders 1:b672c24629d5 112 double temp = pressure_sensor.getTemperature();
csinders 2:9af94ceb9d37 113 char tempBuf[tempBufSize];
csinders 1:b672c24629d5 114 double pres = pressure_sensor.getPressure();
csinders 2:9af94ceb9d37 115 char presBuf[presBufSize];
csinders 1:b672c24629d5 116 pc.printf("Current temp is %f\n\r",temp);
csinders 1:b672c24629d5 117 pc.printf("Current pres is %f\n\r",pres);
csinders 2:9af94ceb9d37 118 sprintf(tempBuf, "%2.1f",temp);
csinders 1:b672c24629d5 119 sprintf(presBuf, "%4.1f",pres);
csinders 1:b672c24629d5 120 pc.printf("After convert with sprintf temp = %s\n\r",tempBuf);
csinders 1:b672c24629d5 121 pc.printf("After convert with sprintf pres = %s\n\r",presBuf);
csinders 2:9af94ceb9d37 122 sendAtMessageNACK("AT+SEND=",8);
csinders 2:9af94ceb9d37 123 sendAtMessageNACK(tempBuf,tempBufSize);
csinders 2:9af94ceb9d37 124 sendAtMessageNACK(",",1);
csinders 2:9af94ceb9d37 125 sendAtMessageNACK(presBuf,presBufSize);
csinders 2:9af94ceb9d37 126 wait_ms(500);
csinders 2:9af94ceb9d37 127 if (sendAtMessage("\n")) {
csinders 2:9af94ceb9d37 128 pc.printf("message succesfully sent\n\r");
csinders 2:9af94ceb9d37 129 }
csinders 2:9af94ceb9d37 130 //void memset(* void tmp,0,100);
csinders 1:b672c24629d5 131 }
csinders 1:b672c24629d5 132 }
csinders 1:b672c24629d5 133 }
csinders 1:b672c24629d5 134
csinders 1:b672c24629d5 135 /*
csinders 0:50820e1648aa 136 while(1) {
csinders 0:50820e1648aa 137 if(pc.readable()) {
csinders 0:50820e1648aa 138 device.putc(pc.getc());
csinders 0:50820e1648aa 139 myled = !myled;
csinders 0:50820e1648aa 140 }
csinders 0:50820e1648aa 141 if(device.readable()) {
csinders 0:50820e1648aa 142 pc.putc(device.getc());
csinders 0:50820e1648aa 143 myled = !myled;
csinders 0:50820e1648aa 144 }
csinders 0:50820e1648aa 145 }
csinders 1:b672c24629d5 146 */
csinders 0:50820e1648aa 147 }