
Transmit pressure/temperature reading once per hour from xDot to iupivot server
Dependencies: ATParser MPL3115A2 mbed
main.cpp@3:59fbf4f41f69, 2018-04-06 (annotated)
- Committer:
- csinders
- Date:
- Fri Apr 06 15:24:35 2018 +0000
- Revision:
- 3:59fbf4f41f69
- Parent:
- 2:9af94ceb9d37
- Child:
- 4:9235d082b6a3
Fixed the confirm ok, we are able to send input now
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:59fbf4f41f69 | 16 | // Wait until device has a response |
csinders | 2:9af94ceb9d37 | 17 | while (!device.readable()) { |
csinders | 2:9af94ceb9d37 | 18 | wait_ms(100); |
csinders | 2:9af94ceb9d37 | 19 | } |
csinders | 2:9af94ceb9d37 | 20 | int bufferSize = 100; |
csinders | 2:9af94ceb9d37 | 21 | char buf[bufferSize]; |
csinders | 3:59fbf4f41f69 | 22 | int i = 0; |
csinders | 3:59fbf4f41f69 | 23 | for (i = 0; i < bufferSize; i++) { |
csinders | 2:9af94ceb9d37 | 24 | buf[i] = 0; |
csinders | 2:9af94ceb9d37 | 25 | } |
csinders | 2:9af94ceb9d37 | 26 | i = 0; |
csinders | 3:59fbf4f41f69 | 27 | |
csinders | 3:59fbf4f41f69 | 28 | //Read message from device into buffer |
csinders | 2:9af94ceb9d37 | 29 | for (i = 0; device.readable(); i++) { |
csinders | 1:b672c24629d5 | 30 | buf[i] = device.getc(); |
csinders | 3:59fbf4f41f69 | 31 | if (buf[i] == '\n') { |
csinders | 3:59fbf4f41f69 | 32 | // Read new line so check to see if we got an OK or Error |
csinders | 3:59fbf4f41f69 | 33 | // else keep reading til we get one |
csinders | 3:59fbf4f41f69 | 34 | if (!strncmp("OK",buf,2)) { |
csinders | 3:59fbf4f41f69 | 35 | //pc.printf("I compared against %s and got OK\r\n",buf); |
csinders | 3:59fbf4f41f69 | 36 | return 1; |
csinders | 3:59fbf4f41f69 | 37 | } else if (!strncmp("ERROR",buf,5)) { |
csinders | 3:59fbf4f41f69 | 38 | //pc.printf("I compared against %s and got ERROR\r\n",buf); |
csinders | 3:59fbf4f41f69 | 39 | return 0; |
csinders | 3:59fbf4f41f69 | 40 | } |
csinders | 3:59fbf4f41f69 | 41 | return confirmOK(); |
csinders | 3:59fbf4f41f69 | 42 | } |
csinders | 2:9af94ceb9d37 | 43 | if (i >= bufferSize) { |
csinders | 1:b672c24629d5 | 44 | return 0; |
csinders | 1:b672c24629d5 | 45 | } |
csinders | 1:b672c24629d5 | 46 | } |
csinders | 3:59fbf4f41f69 | 47 | return 0; |
csinders | 1:b672c24629d5 | 48 | } |
csinders | 1:b672c24629d5 | 49 | /* Sends a message to the xDot |
csinders | 1:b672c24629d5 | 50 | Messages are expected to end in \n so that an OK confirmation will be |
csinders | 1:b672c24629d5 | 51 | recieved |
csinders | 1:b672c24629d5 | 52 | */ |
csinders | 2:9af94ceb9d37 | 53 | int sendAtMessage(char * message) { |
csinders | 1:b672c24629d5 | 54 | while (*message) { |
csinders | 1:b672c24629d5 | 55 | device.putc(*message); |
csinders | 1:b672c24629d5 | 56 | message++; |
csinders | 1:b672c24629d5 | 57 | } |
csinders | 1:b672c24629d5 | 58 | return confirmOK(); |
csinders | 1:b672c24629d5 | 59 | } |
csinders | 1:b672c24629d5 | 60 | |
csinders | 1:b672c24629d5 | 61 | /* |
csinders | 1:b672c24629d5 | 62 | Send a message to the xDot without checking for acknowledge |
csinders | 1:b672c24629d5 | 63 | */ |
csinders | 2:9af94ceb9d37 | 64 | void sendAtMessageNACK(char * message, int size) { |
csinders | 2:9af94ceb9d37 | 65 | int i = 0; |
csinders | 2:9af94ceb9d37 | 66 | while (i < size) { |
csinders | 1:b672c24629d5 | 67 | device.putc(*message); |
csinders | 2:9af94ceb9d37 | 68 | pc.printf("putting %c on device\n\r", *message++); |
csinders | 2:9af94ceb9d37 | 69 | i++; |
csinders | 1:b672c24629d5 | 70 | } |
csinders | 1:b672c24629d5 | 71 | } |
csinders | 1:b672c24629d5 | 72 | int main() { |
csinders | 2:9af94ceb9d37 | 73 | pc.baud(115200); |
csinders | 2:9af94ceb9d37 | 74 | device.baud(115200); |
csinders | 2:9af94ceb9d37 | 75 | ATParser atp = ATParser(device,"\r\n",256,2000,false); |
csinders | 1:b672c24629d5 | 76 | |
csinders | 1:b672c24629d5 | 77 | pc.printf("Starting program\n\r"); |
csinders | 2:9af94ceb9d37 | 78 | |
csinders | 2:9af94ceb9d37 | 79 | if (!sendAtMessage("AT\n")) { |
csinders | 2:9af94ceb9d37 | 80 | pc.printf("xDot not responding to message\n\r"); |
csinders | 2:9af94ceb9d37 | 81 | } |
csinders | 1:b672c24629d5 | 82 | //Try to connect xDot to network |
csinders | 1:b672c24629d5 | 83 | // Provice network name |
csinders | 2:9af94ceb9d37 | 84 | char networkName[] = "AT+NI=1,MTCDT-19400691\n"; |
csinders | 2:9af94ceb9d37 | 85 | if (!sendAtMessage(networkName)) { |
csinders | 2:9af94ceb9d37 | 86 | pc.printf("Unable to set network name\n\r"); |
csinders | 2:9af94ceb9d37 | 87 | } |
csinders | 1:b672c24629d5 | 88 | //Provide network passphrase |
csinders | 1:b672c24629d5 | 89 | char networkPassphrase[] = "AT+NK=1,MTCDT-19400691\n"; |
csinders | 2:9af94ceb9d37 | 90 | if (!sendAtMessage(networkPassphrase)) { |
csinders | 2:9af94ceb9d37 | 91 | pc.printf("Network passphrase not successfully set\n\r"); |
csinders | 2:9af94ceb9d37 | 92 | } |
csinders | 1:b672c24629d5 | 93 | //Set the frequency sub band |
csinders | 1:b672c24629d5 | 94 | char frequencySubBand[] = "AT+FSB=1\n"; |
csinders | 2:9af94ceb9d37 | 95 | if (sendAtMessage(frequencySubBand)) { |
csinders | 2:9af94ceb9d37 | 96 | //pc.printf("Network frequency SubBand successfully set\n\r"); |
csinders | 2:9af94ceb9d37 | 97 | } |
csinders | 1:b672c24629d5 | 98 | //Join the network |
csinders | 1:b672c24629d5 | 99 | char joinNetwork[] = "AT+JOIN\n"; // Unable to join network atm. |
csinders | 1:b672c24629d5 | 100 | if (!sendAtMessage(joinNetwork)) { |
csinders | 2:9af94ceb9d37 | 101 | pc.printf("Failed to joined the network\n\r"); |
csinders | 1:b672c24629d5 | 102 | } |
csinders | 2:9af94ceb9d37 | 103 | |
csinders | 1:b672c24629d5 | 104 | |
csinders | 1:b672c24629d5 | 105 | //When an s is read from the serial pc, |
csinders | 1:b672c24629d5 | 106 | // read the current pressure/temperature from the I2C pressure sensor |
csinders | 1:b672c24629d5 | 107 | // and send it to the MQTT server pivot.iuiot |
csinders | 1:b672c24629d5 | 108 | |
csinders | 1:b672c24629d5 | 109 | //Unsure of what size to set Buffer so that it doesn't go over |
csinders | 1:b672c24629d5 | 110 | //when trying to send a message |
csinders | 1:b672c24629d5 | 111 | while(1) { |
csinders | 2:9af94ceb9d37 | 112 | if (pc.readable()) { |
csinders | 2:9af94ceb9d37 | 113 | pc.printf("pc.readable\r\n"); |
csinders | 1:b672c24629d5 | 114 | if (pc.getc() == 's') { |
csinders | 2:9af94ceb9d37 | 115 | pc.printf("I recieved an s\r\n"); |
csinders | 2:9af94ceb9d37 | 116 | int tempBufSize = 4; |
csinders | 2:9af94ceb9d37 | 117 | int presBufSize = 6; |
csinders | 1:b672c24629d5 | 118 | double temp = pressure_sensor.getTemperature(); |
csinders | 2:9af94ceb9d37 | 119 | char tempBuf[tempBufSize]; |
csinders | 1:b672c24629d5 | 120 | double pres = pressure_sensor.getPressure(); |
csinders | 2:9af94ceb9d37 | 121 | char presBuf[presBufSize]; |
csinders | 1:b672c24629d5 | 122 | pc.printf("Current temp is %f\n\r",temp); |
csinders | 1:b672c24629d5 | 123 | pc.printf("Current pres is %f\n\r",pres); |
csinders | 2:9af94ceb9d37 | 124 | sprintf(tempBuf, "%2.1f",temp); |
csinders | 1:b672c24629d5 | 125 | sprintf(presBuf, "%4.1f",pres); |
csinders | 1:b672c24629d5 | 126 | pc.printf("After convert with sprintf temp = %s\n\r",tempBuf); |
csinders | 1:b672c24629d5 | 127 | pc.printf("After convert with sprintf pres = %s\n\r",presBuf); |
csinders | 2:9af94ceb9d37 | 128 | sendAtMessageNACK("AT+SEND=",8); |
csinders | 2:9af94ceb9d37 | 129 | sendAtMessageNACK(tempBuf,tempBufSize); |
csinders | 2:9af94ceb9d37 | 130 | sendAtMessageNACK(",",1); |
csinders | 2:9af94ceb9d37 | 131 | sendAtMessageNACK(presBuf,presBufSize); |
csinders | 2:9af94ceb9d37 | 132 | wait_ms(500); |
csinders | 2:9af94ceb9d37 | 133 | if (sendAtMessage("\n")) { |
csinders | 2:9af94ceb9d37 | 134 | pc.printf("message succesfully sent\n\r"); |
csinders | 2:9af94ceb9d37 | 135 | } |
csinders | 2:9af94ceb9d37 | 136 | //void memset(* void tmp,0,100); |
csinders | 1:b672c24629d5 | 137 | } |
csinders | 1:b672c24629d5 | 138 | } |
csinders | 1:b672c24629d5 | 139 | } |
csinders | 1:b672c24629d5 | 140 | |
csinders | 1:b672c24629d5 | 141 | /* |
csinders | 0:50820e1648aa | 142 | while(1) { |
csinders | 0:50820e1648aa | 143 | if(pc.readable()) { |
csinders | 0:50820e1648aa | 144 | device.putc(pc.getc()); |
csinders | 0:50820e1648aa | 145 | myled = !myled; |
csinders | 0:50820e1648aa | 146 | } |
csinders | 0:50820e1648aa | 147 | if(device.readable()) { |
csinders | 0:50820e1648aa | 148 | pc.putc(device.getc()); |
csinders | 0:50820e1648aa | 149 | myled = !myled; |
csinders | 0:50820e1648aa | 150 | } |
csinders | 0:50820e1648aa | 151 | } |
csinders | 1:b672c24629d5 | 152 | */ |
csinders | 0:50820e1648aa | 153 | } |