
Transmit pressure/temperature reading once per hour from xDot to iupivot server
Dependencies: ATParser MPL3115A2 mbed
main.cpp@1:b672c24629d5, 2018-04-04 (annotated)
- Committer:
- csinders
- Date:
- Wed Apr 04 14:55:38 2018 +0000
- Revision:
- 1:b672c24629d5
- Parent:
- 0:50820e1648aa
- Child:
- 2:9af94ceb9d37
Changed name to correct lab number.; Was unable to connect to network to test if input is correct. Unsure of what size to make temperature and pressure buffer.
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 | 0:50820e1648aa | 4 | MPL3115A2 pressure_sensor(PB_7,PB_6,0x60); |
csinders | 0:50820e1648aa | 5 | Serial pc(SERIAL_TX, SERIAL_RX, 115200); |
csinders | 0:50820e1648aa | 6 | Serial device(PA_9, PA_10, 115200); // tx, rx |
csinders | 1:b672c24629d5 | 7 | DigitalOut myled(LED1); |
csinders | 0:50820e1648aa | 8 | |
csinders | 1:b672c24629d5 | 9 | /* Checks to see if the xDot has a buffered message OK to |
csinders | 1:b672c24629d5 | 10 | confirm that operation has succeeded |
csinders | 1:b672c24629d5 | 11 | */ |
csinders | 1:b672c24629d5 | 12 | int confirmOK() { |
csinders | 1:b672c24629d5 | 13 | char buf[20]; |
csinders | 1:b672c24629d5 | 14 | int i = 0; |
csinders | 1:b672c24629d5 | 15 | while (device.readable()) { |
csinders | 1:b672c24629d5 | 16 | buf[i] = device.getc(); |
csinders | 1:b672c24629d5 | 17 | if (i > 20) { |
csinders | 1:b672c24629d5 | 18 | return 0; |
csinders | 1:b672c24629d5 | 19 | } |
csinders | 1:b672c24629d5 | 20 | i++; |
csinders | 1:b672c24629d5 | 21 | } |
csinders | 1:b672c24629d5 | 22 | return strncmp("OK",buf,2); |
csinders | 1:b672c24629d5 | 23 | } |
csinders | 1:b672c24629d5 | 24 | /* Sends a message to the xDot |
csinders | 1:b672c24629d5 | 25 | Messages are expected to end in \n so that an OK confirmation will be |
csinders | 1:b672c24629d5 | 26 | recieved |
csinders | 1:b672c24629d5 | 27 | */ |
csinders | 1:b672c24629d5 | 28 | int sendAtMessage(char * message) { |
csinders | 1:b672c24629d5 | 29 | while (*message) { |
csinders | 1:b672c24629d5 | 30 | device.putc(*message); |
csinders | 1:b672c24629d5 | 31 | message++; |
csinders | 1:b672c24629d5 | 32 | } |
csinders | 1:b672c24629d5 | 33 | return confirmOK(); |
csinders | 1:b672c24629d5 | 34 | } |
csinders | 1:b672c24629d5 | 35 | |
csinders | 1:b672c24629d5 | 36 | /* |
csinders | 1:b672c24629d5 | 37 | Send a message to the xDot without checking for acknowledge |
csinders | 1:b672c24629d5 | 38 | */ |
csinders | 1:b672c24629d5 | 39 | void sendAtMessageNACK(char * message) { |
csinders | 1:b672c24629d5 | 40 | while (*message) { |
csinders | 1:b672c24629d5 | 41 | device.putc(*message); |
csinders | 1:b672c24629d5 | 42 | message++; |
csinders | 1:b672c24629d5 | 43 | } |
csinders | 1:b672c24629d5 | 44 | } |
csinders | 1:b672c24629d5 | 45 | int main() { |
csinders | 1:b672c24629d5 | 46 | |
csinders | 1:b672c24629d5 | 47 | pc.printf("Starting program\n\r"); |
csinders | 1:b672c24629d5 | 48 | |
csinders | 1:b672c24629d5 | 49 | //Try to connect xDot to network |
csinders | 1:b672c24629d5 | 50 | // Provice network name |
csinders | 1:b672c24629d5 | 51 | char networkName[] = "AT+NI=1, MTCDT-19400691\n"; |
csinders | 1:b672c24629d5 | 52 | sendAtMessage(networkName); |
csinders | 1:b672c24629d5 | 53 | //Provide network passphrase |
csinders | 1:b672c24629d5 | 54 | char networkPassphrase[] = "AT+NK=1,MTCDT-19400691\n"; |
csinders | 1:b672c24629d5 | 55 | sendAtMessage(networkPassphrase); |
csinders | 1:b672c24629d5 | 56 | //Set the frequency sub band |
csinders | 1:b672c24629d5 | 57 | char frequencySubBand[] = "AT+FSB=1\n"; |
csinders | 1:b672c24629d5 | 58 | sendAtMessage(frequencySubBand); |
csinders | 1:b672c24629d5 | 59 | //Join the network |
csinders | 1:b672c24629d5 | 60 | char joinNetwork[] = "AT+JOIN\n"; // Unable to join network atm. |
csinders | 1:b672c24629d5 | 61 | if (!sendAtMessage(joinNetwork)) { |
csinders | 1:b672c24629d5 | 62 | pc.printf("Error, unable to connect to network"); |
csinders | 1:b672c24629d5 | 63 | } |
csinders | 1:b672c24629d5 | 64 | wait_ms(500); |
csinders | 1:b672c24629d5 | 65 | |
csinders | 1:b672c24629d5 | 66 | //When an s is read from the serial pc, |
csinders | 1:b672c24629d5 | 67 | // read the current pressure/temperature from the I2C pressure sensor |
csinders | 1:b672c24629d5 | 68 | // and send it to the MQTT server pivot.iuiot |
csinders | 1:b672c24629d5 | 69 | |
csinders | 1:b672c24629d5 | 70 | //Unsure of what size to set Buffer so that it doesn't go over |
csinders | 1:b672c24629d5 | 71 | //when trying to send a message |
csinders | 1:b672c24629d5 | 72 | while(1) { |
csinders | 1:b672c24629d5 | 73 | if (pc.readable()) { |
csinders | 1:b672c24629d5 | 74 | if (pc.getc() == 's') { |
csinders | 1:b672c24629d5 | 75 | double temp = pressure_sensor.getTemperature(); |
csinders | 1:b672c24629d5 | 76 | char tempBuf[6]; |
csinders | 1:b672c24629d5 | 77 | double pres = pressure_sensor.getPressure(); |
csinders | 1:b672c24629d5 | 78 | char presBuf[7]; |
csinders | 1:b672c24629d5 | 79 | pc.printf("Current temp is %f\n\r",temp); |
csinders | 1:b672c24629d5 | 80 | pc.printf("Current pres is %f\n\r",pres); |
csinders | 1:b672c24629d5 | 81 | sprintf(tempBuf, "%2.2f",temp); |
csinders | 1:b672c24629d5 | 82 | sprintf(presBuf, "%4.1f",pres); |
csinders | 1:b672c24629d5 | 83 | pc.printf("After convert with sprintf temp = %s\n\r",tempBuf); |
csinders | 1:b672c24629d5 | 84 | pc.printf("After convert with sprintf pres = %s\n\r",presBuf); |
csinders | 1:b672c24629d5 | 85 | |
csinders | 1:b672c24629d5 | 86 | } |
csinders | 1:b672c24629d5 | 87 | } |
csinders | 1:b672c24629d5 | 88 | } |
csinders | 1:b672c24629d5 | 89 | |
csinders | 1:b672c24629d5 | 90 | /* |
csinders | 0:50820e1648aa | 91 | while(1) { |
csinders | 0:50820e1648aa | 92 | if(pc.readable()) { |
csinders | 0:50820e1648aa | 93 | device.putc(pc.getc()); |
csinders | 0:50820e1648aa | 94 | myled = !myled; |
csinders | 0:50820e1648aa | 95 | } |
csinders | 0:50820e1648aa | 96 | if(device.readable()) { |
csinders | 0:50820e1648aa | 97 | pc.putc(device.getc()); |
csinders | 0:50820e1648aa | 98 | myled = !myled; |
csinders | 0:50820e1648aa | 99 | } |
csinders | 0:50820e1648aa | 100 | } |
csinders | 1:b672c24629d5 | 101 | */ |
csinders | 0:50820e1648aa | 102 | } |