
Transmit pressure/temperature reading once per hour from xDot to iupivot server
Dependencies: ATParser MPL3115A2 mbed
main.cpp@4:9235d082b6a3, 2018-04-06 (annotated)
- Committer:
- csinders
- Date:
- Fri Apr 06 16:45:35 2018 +0000
- Revision:
- 4:9235d082b6a3
- Parent:
- 3:59fbf4f41f69
Code working for Lab 9, just have to set up in Luddy.
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 | 4:9235d082b6a3 | 34 | //pc.printf("The message from the device is %s\r\n",buf); |
csinders | 3:59fbf4f41f69 | 35 | if (!strncmp("OK",buf,2)) { |
csinders | 3:59fbf4f41f69 | 36 | //pc.printf("I compared against %s and got OK\r\n",buf); |
csinders | 3:59fbf4f41f69 | 37 | return 1; |
csinders | 3:59fbf4f41f69 | 38 | } else if (!strncmp("ERROR",buf,5)) { |
csinders | 3:59fbf4f41f69 | 39 | //pc.printf("I compared against %s and got ERROR\r\n",buf); |
csinders | 3:59fbf4f41f69 | 40 | return 0; |
csinders | 3:59fbf4f41f69 | 41 | } |
csinders | 3:59fbf4f41f69 | 42 | return confirmOK(); |
csinders | 3:59fbf4f41f69 | 43 | } |
csinders | 2:9af94ceb9d37 | 44 | if (i >= bufferSize) { |
csinders | 1:b672c24629d5 | 45 | return 0; |
csinders | 1:b672c24629d5 | 46 | } |
csinders | 1:b672c24629d5 | 47 | } |
csinders | 4:9235d082b6a3 | 48 | return confirmOK(); |
csinders | 1:b672c24629d5 | 49 | } |
csinders | 1:b672c24629d5 | 50 | /* Sends a message to the xDot |
csinders | 1:b672c24629d5 | 51 | Messages are expected to end in \n so that an OK confirmation will be |
csinders | 1:b672c24629d5 | 52 | recieved |
csinders | 1:b672c24629d5 | 53 | */ |
csinders | 2:9af94ceb9d37 | 54 | int sendAtMessage(char * message) { |
csinders | 1:b672c24629d5 | 55 | while (*message) { |
csinders | 1:b672c24629d5 | 56 | device.putc(*message); |
csinders | 1:b672c24629d5 | 57 | message++; |
csinders | 1:b672c24629d5 | 58 | } |
csinders | 1:b672c24629d5 | 59 | return confirmOK(); |
csinders | 1:b672c24629d5 | 60 | } |
csinders | 1:b672c24629d5 | 61 | |
csinders | 1:b672c24629d5 | 62 | /* |
csinders | 1:b672c24629d5 | 63 | Send a message to the xDot without checking for acknowledge |
csinders | 1:b672c24629d5 | 64 | */ |
csinders | 2:9af94ceb9d37 | 65 | void sendAtMessageNACK(char * message, int size) { |
csinders | 2:9af94ceb9d37 | 66 | int i = 0; |
csinders | 2:9af94ceb9d37 | 67 | while (i < size) { |
csinders | 1:b672c24629d5 | 68 | device.putc(*message); |
csinders | 4:9235d082b6a3 | 69 | //wait_ms(500); // Not succesfully sending the message unless it waits. |
csinders | 4:9235d082b6a3 | 70 | //pc.printf("putting %c on device\n\r", *message); |
csinders | 4:9235d082b6a3 | 71 | message++; |
csinders | 2:9af94ceb9d37 | 72 | i++; |
csinders | 1:b672c24629d5 | 73 | } |
csinders | 1:b672c24629d5 | 74 | } |
csinders | 1:b672c24629d5 | 75 | int main() { |
csinders | 2:9af94ceb9d37 | 76 | pc.baud(115200); |
csinders | 2:9af94ceb9d37 | 77 | device.baud(115200); |
csinders | 4:9235d082b6a3 | 78 | |
csinders | 4:9235d082b6a3 | 79 | pc.printf("Starting program\n\r"); |
csinders | 1:b672c24629d5 | 80 | |
csinders | 4:9235d082b6a3 | 81 | myled = 0; |
csinders | 2:9af94ceb9d37 | 82 | |
csinders | 2:9af94ceb9d37 | 83 | if (!sendAtMessage("AT\n")) { |
csinders | 2:9af94ceb9d37 | 84 | pc.printf("xDot not responding to message\n\r"); |
csinders | 2:9af94ceb9d37 | 85 | } |
csinders | 1:b672c24629d5 | 86 | //Try to connect xDot to network |
csinders | 1:b672c24629d5 | 87 | // Provice network name |
csinders | 2:9af94ceb9d37 | 88 | char networkName[] = "AT+NI=1,MTCDT-19400691\n"; |
csinders | 2:9af94ceb9d37 | 89 | if (!sendAtMessage(networkName)) { |
csinders | 2:9af94ceb9d37 | 90 | pc.printf("Unable to set network name\n\r"); |
csinders | 2:9af94ceb9d37 | 91 | } |
csinders | 1:b672c24629d5 | 92 | //Provide network passphrase |
csinders | 1:b672c24629d5 | 93 | char networkPassphrase[] = "AT+NK=1,MTCDT-19400691\n"; |
csinders | 2:9af94ceb9d37 | 94 | if (!sendAtMessage(networkPassphrase)) { |
csinders | 2:9af94ceb9d37 | 95 | pc.printf("Network passphrase not successfully set\n\r"); |
csinders | 2:9af94ceb9d37 | 96 | } |
csinders | 1:b672c24629d5 | 97 | //Set the frequency sub band |
csinders | 1:b672c24629d5 | 98 | char frequencySubBand[] = "AT+FSB=1\n"; |
csinders | 4:9235d082b6a3 | 99 | if (!sendAtMessage(frequencySubBand)) { |
csinders | 4:9235d082b6a3 | 100 | pc.printf("Network frequency SubBand not successfully set\n\r"); |
csinders | 2:9af94ceb9d37 | 101 | } |
csinders | 1:b672c24629d5 | 102 | //Join the network |
csinders | 1:b672c24629d5 | 103 | char joinNetwork[] = "AT+JOIN\n"; // Unable to join network atm. |
csinders | 1:b672c24629d5 | 104 | if (!sendAtMessage(joinNetwork)) { |
csinders | 2:9af94ceb9d37 | 105 | pc.printf("Failed to joined the network\n\r"); |
csinders | 1:b672c24629d5 | 106 | } |
csinders | 4:9235d082b6a3 | 107 | pc.printf("I got here\r\n"); |
csinders | 4:9235d082b6a3 | 108 | int i = 0; |
csinders | 4:9235d082b6a3 | 109 | for (i = 0; i < 48; i++) { |
csinders | 4:9235d082b6a3 | 110 | int tempBufSize = 4; |
csinders | 4:9235d082b6a3 | 111 | int presBufSize = 6; |
csinders | 4:9235d082b6a3 | 112 | double temp = pressure_sensor.getTemperature(); |
csinders | 4:9235d082b6a3 | 113 | char tempBuf[tempBufSize]; |
csinders | 4:9235d082b6a3 | 114 | double pres = pressure_sensor.getPressure(); |
csinders | 4:9235d082b6a3 | 115 | char presBuf[presBufSize]; |
csinders | 4:9235d082b6a3 | 116 | //pc.printf("Current temp is %f\n\r",temp); |
csinders | 4:9235d082b6a3 | 117 | //pc.printf("Current pres is %f\n\r",pres); |
csinders | 4:9235d082b6a3 | 118 | sprintf(tempBuf, "%2.1f",temp); |
csinders | 4:9235d082b6a3 | 119 | sprintf(presBuf, "%4.1f",pres); |
csinders | 4:9235d082b6a3 | 120 | //pc.printf("After convert with sprintf temp = %s\n\r",tempBuf); |
csinders | 4:9235d082b6a3 | 121 | //pc.printf("After convert with sprintf pres = %s\n\r",presBuf); |
csinders | 4:9235d082b6a3 | 122 | sendAtMessageNACK("AT+SEND=",8); |
csinders | 4:9235d082b6a3 | 123 | sendAtMessageNACK(tempBuf,tempBufSize); |
csinders | 4:9235d082b6a3 | 124 | sendAtMessageNACK(",",1); |
csinders | 4:9235d082b6a3 | 125 | sendAtMessageNACK(presBuf,presBufSize); |
csinders | 4:9235d082b6a3 | 126 | //wait_ms(500); |
csinders | 4:9235d082b6a3 | 127 | if (sendAtMessage("\n")) { |
csinders | 4:9235d082b6a3 | 128 | pc.printf("message succesfully sent\n\r"); |
csinders | 4:9235d082b6a3 | 129 | myled = 1; |
csinders | 4:9235d082b6a3 | 130 | } else { |
csinders | 4:9235d082b6a3 | 131 | pc.printf("Failed to send the message for some reason\r\n"); |
csinders | 4:9235d082b6a3 | 132 | } |
csinders | 4:9235d082b6a3 | 133 | |
csinders | 4:9235d082b6a3 | 134 | wait(1000); |
csinders | 4:9235d082b6a3 | 135 | |
csinders | 4:9235d082b6a3 | 136 | } |
csinders | 1:b672c24629d5 | 137 | |
csinders | 1:b672c24629d5 | 138 | //When an s is read from the serial pc, |
csinders | 1:b672c24629d5 | 139 | // read the current pressure/temperature from the I2C pressure sensor |
csinders | 1:b672c24629d5 | 140 | // and send it to the MQTT server pivot.iuiot |
csinders | 1:b672c24629d5 | 141 | //Unsure of what size to set Buffer so that it doesn't go over |
csinders | 1:b672c24629d5 | 142 | //when trying to send a message |
csinders | 4:9235d082b6a3 | 143 | |
csinders | 4:9235d082b6a3 | 144 | /* |
csinders | 1:b672c24629d5 | 145 | while(1) { |
csinders | 2:9af94ceb9d37 | 146 | if (pc.readable()) { |
csinders | 4:9235d082b6a3 | 147 | //pc.printf("pc.readable\r\n"); |
csinders | 1:b672c24629d5 | 148 | if (pc.getc() == 's') { |
csinders | 2:9af94ceb9d37 | 149 | pc.printf("I recieved an s\r\n"); |
csinders | 2:9af94ceb9d37 | 150 | int tempBufSize = 4; |
csinders | 2:9af94ceb9d37 | 151 | int presBufSize = 6; |
csinders | 1:b672c24629d5 | 152 | double temp = pressure_sensor.getTemperature(); |
csinders | 2:9af94ceb9d37 | 153 | char tempBuf[tempBufSize]; |
csinders | 1:b672c24629d5 | 154 | double pres = pressure_sensor.getPressure(); |
csinders | 2:9af94ceb9d37 | 155 | char presBuf[presBufSize]; |
csinders | 1:b672c24629d5 | 156 | pc.printf("Current temp is %f\n\r",temp); |
csinders | 1:b672c24629d5 | 157 | pc.printf("Current pres is %f\n\r",pres); |
csinders | 2:9af94ceb9d37 | 158 | sprintf(tempBuf, "%2.1f",temp); |
csinders | 1:b672c24629d5 | 159 | sprintf(presBuf, "%4.1f",pres); |
csinders | 1:b672c24629d5 | 160 | pc.printf("After convert with sprintf temp = %s\n\r",tempBuf); |
csinders | 1:b672c24629d5 | 161 | pc.printf("After convert with sprintf pres = %s\n\r",presBuf); |
csinders | 2:9af94ceb9d37 | 162 | sendAtMessageNACK("AT+SEND=",8); |
csinders | 2:9af94ceb9d37 | 163 | sendAtMessageNACK(tempBuf,tempBufSize); |
csinders | 2:9af94ceb9d37 | 164 | sendAtMessageNACK(",",1); |
csinders | 2:9af94ceb9d37 | 165 | sendAtMessageNACK(presBuf,presBufSize); |
csinders | 2:9af94ceb9d37 | 166 | wait_ms(500); |
csinders | 2:9af94ceb9d37 | 167 | if (sendAtMessage("\n")) { |
csinders | 2:9af94ceb9d37 | 168 | pc.printf("message succesfully sent\n\r"); |
csinders | 4:9235d082b6a3 | 169 | } else { |
csinders | 4:9235d082b6a3 | 170 | pc.printf("Failed to send the message for some reason\r\n"); |
csinders | 4:9235d082b6a3 | 171 | } |
csinders | 1:b672c24629d5 | 172 | } |
csinders | 1:b672c24629d5 | 173 | } |
csinders | 1:b672c24629d5 | 174 | } |
csinders | 4:9235d082b6a3 | 175 | */ |
csinders | 1:b672c24629d5 | 176 | |
csinders | 1:b672c24629d5 | 177 | /* |
csinders | 0:50820e1648aa | 178 | while(1) { |
csinders | 0:50820e1648aa | 179 | if(pc.readable()) { |
csinders | 0:50820e1648aa | 180 | device.putc(pc.getc()); |
csinders | 0:50820e1648aa | 181 | myled = !myled; |
csinders | 0:50820e1648aa | 182 | } |
csinders | 0:50820e1648aa | 183 | if(device.readable()) { |
csinders | 0:50820e1648aa | 184 | pc.putc(device.getc()); |
csinders | 0:50820e1648aa | 185 | myled = !myled; |
csinders | 0:50820e1648aa | 186 | } |
csinders | 0:50820e1648aa | 187 | } |
csinders | 1:b672c24629d5 | 188 | */ |
csinders | 0:50820e1648aa | 189 | } |