In this lab, you will be collecting light data with your sensor and sending it wirelessly over the lora network to the server. In this lab you will: Utilize the system that you created for lab 9 to collect light data. Collect a light sample each hour for 24 hours. Send the 24 samples 1 time per day. Run your system over a period of 2 days in Luddy 4150 to collect light data on this room.
Dependencies: ATParser TSL2561 mbed
main.cpp@0:8d98d7f7d672, 2018-04-13 (annotated)
- Committer:
- csinders
- Date:
- Fri Apr 13 15:41:10 2018 +0000
- Revision:
- 0:8d98d7f7d672
- Child:
- 1:fb25b13e6ab3
initial commit for Lab10, No testing has been done.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
csinders | 0:8d98d7f7d672 | 1 | #include "mbed.h" |
csinders | 0:8d98d7f7d672 | 2 | #include "string.h" |
csinders | 0:8d98d7f7d672 | 3 | #include "TSL2561.h" |
csinders | 0:8d98d7f7d672 | 4 | #include "ATParser.h" |
csinders | 0:8d98d7f7d672 | 5 | |
csinders | 0:8d98d7f7d672 | 6 | BufferedSerial pc(SERIAL_TX, SERIAL_RX); |
csinders | 0:8d98d7f7d672 | 7 | BufferedSerial device(PA_9, PA_10); // tx, rx |
csinders | 0:8d98d7f7d672 | 8 | DigitalOut myled(LED1); |
csinders | 0:8d98d7f7d672 | 9 | TSL2561 lightsensor(PB_7,PB_6); |
csinders | 0:8d98d7f7d672 | 10 | |
csinders | 0:8d98d7f7d672 | 11 | /* Checks to see if the xDot has a buffered message OK to |
csinders | 0:8d98d7f7d672 | 12 | confirm that operation has succeeded |
csinders | 0:8d98d7f7d672 | 13 | */ |
csinders | 0:8d98d7f7d672 | 14 | int confirmOK() { |
csinders | 0:8d98d7f7d672 | 15 | // Wait until device has a response |
csinders | 0:8d98d7f7d672 | 16 | while (!device.readable()) { |
csinders | 0:8d98d7f7d672 | 17 | wait_ms(100); |
csinders | 0:8d98d7f7d672 | 18 | } |
csinders | 0:8d98d7f7d672 | 19 | int bufferSize = 100; |
csinders | 0:8d98d7f7d672 | 20 | char buf[bufferSize]; |
csinders | 0:8d98d7f7d672 | 21 | int i = 0; |
csinders | 0:8d98d7f7d672 | 22 | for (i = 0; i < bufferSize; i++) { |
csinders | 0:8d98d7f7d672 | 23 | buf[i] = 0; |
csinders | 0:8d98d7f7d672 | 24 | } |
csinders | 0:8d98d7f7d672 | 25 | i = 0; |
csinders | 0:8d98d7f7d672 | 26 | |
csinders | 0:8d98d7f7d672 | 27 | //Read message from device into buffer |
csinders | 0:8d98d7f7d672 | 28 | for (i = 0; device.readable(); i++) { |
csinders | 0:8d98d7f7d672 | 29 | buf[i] = device.getc(); |
csinders | 0:8d98d7f7d672 | 30 | if (buf[i] == '\n') { |
csinders | 0:8d98d7f7d672 | 31 | // Read new line so check to see if we got an OK or Error |
csinders | 0:8d98d7f7d672 | 32 | // else keep reading til we get one |
csinders | 0:8d98d7f7d672 | 33 | //pc.printf("The message from the device is %s\r\n",buf); |
csinders | 0:8d98d7f7d672 | 34 | if (!strncmp("OK",buf,2)) { |
csinders | 0:8d98d7f7d672 | 35 | //pc.printf("I compared against %s and got OK\r\n",buf); |
csinders | 0:8d98d7f7d672 | 36 | return 1; |
csinders | 0:8d98d7f7d672 | 37 | } else if (!strncmp("ERROR",buf,5)) { |
csinders | 0:8d98d7f7d672 | 38 | //pc.printf("I compared against %s and got ERROR\r\n",buf); |
csinders | 0:8d98d7f7d672 | 39 | return 0; |
csinders | 0:8d98d7f7d672 | 40 | } |
csinders | 0:8d98d7f7d672 | 41 | return confirmOK(); |
csinders | 0:8d98d7f7d672 | 42 | } |
csinders | 0:8d98d7f7d672 | 43 | if (i >= bufferSize) { |
csinders | 0:8d98d7f7d672 | 44 | return 0; |
csinders | 0:8d98d7f7d672 | 45 | } |
csinders | 0:8d98d7f7d672 | 46 | } |
csinders | 0:8d98d7f7d672 | 47 | return confirmOK(); |
csinders | 0:8d98d7f7d672 | 48 | } |
csinders | 0:8d98d7f7d672 | 49 | /* Sends a message to the xDot |
csinders | 0:8d98d7f7d672 | 50 | Messages are expected to end in \n so that an OK confirmation will be |
csinders | 0:8d98d7f7d672 | 51 | recieved |
csinders | 0:8d98d7f7d672 | 52 | */ |
csinders | 0:8d98d7f7d672 | 53 | int sendAtMessage(char * message) { |
csinders | 0:8d98d7f7d672 | 54 | while (*message) { |
csinders | 0:8d98d7f7d672 | 55 | device.putc(*message); |
csinders | 0:8d98d7f7d672 | 56 | message++; |
csinders | 0:8d98d7f7d672 | 57 | } |
csinders | 0:8d98d7f7d672 | 58 | return confirmOK(); |
csinders | 0:8d98d7f7d672 | 59 | } |
csinders | 0:8d98d7f7d672 | 60 | |
csinders | 0:8d98d7f7d672 | 61 | /* |
csinders | 0:8d98d7f7d672 | 62 | Send a message to the xDot without checking for acknowledge |
csinders | 0:8d98d7f7d672 | 63 | */ |
csinders | 0:8d98d7f7d672 | 64 | void sendAtMessageNACK(char * message, int size) { |
csinders | 0:8d98d7f7d672 | 65 | int i = 0; |
csinders | 0:8d98d7f7d672 | 66 | while (i < size) { |
csinders | 0:8d98d7f7d672 | 67 | device.putc(*message); |
csinders | 0:8d98d7f7d672 | 68 | //wait_ms(500); // Not succesfully sending the message unless it waits. |
csinders | 0:8d98d7f7d672 | 69 | //pc.printf("putting %c on device\n\r", *message); |
csinders | 0:8d98d7f7d672 | 70 | message++; |
csinders | 0:8d98d7f7d672 | 71 | i++; |
csinders | 0:8d98d7f7d672 | 72 | } |
csinders | 0:8d98d7f7d672 | 73 | } |
csinders | 0:8d98d7f7d672 | 74 | int main() { |
csinders | 0:8d98d7f7d672 | 75 | pc.baud(115200); |
csinders | 0:8d98d7f7d672 | 76 | device.baud(115200); |
csinders | 0:8d98d7f7d672 | 77 | myled = 1; |
csinders | 0:8d98d7f7d672 | 78 | wait(1); |
csinders | 0:8d98d7f7d672 | 79 | myled = 0; |
csinders | 0:8d98d7f7d672 | 80 | |
csinders | 0:8d98d7f7d672 | 81 | pc.printf("Starting program\n\r"); |
csinders | 0:8d98d7f7d672 | 82 | if (!sendAtMessage("AT\n")) { |
csinders | 0:8d98d7f7d672 | 83 | pc.printf("xDot not responding to message\n\r"); |
csinders | 0:8d98d7f7d672 | 84 | } |
csinders | 0:8d98d7f7d672 | 85 | //Try to connect xDot to network |
csinders | 0:8d98d7f7d672 | 86 | // Provice network name |
csinders | 0:8d98d7f7d672 | 87 | char networkName[] = "AT+NI=1,MTCDT-19400691\n"; |
csinders | 0:8d98d7f7d672 | 88 | if (!sendAtMessage(networkName)) { |
csinders | 0:8d98d7f7d672 | 89 | pc.printf("Unable to set network name\n\r"); |
csinders | 0:8d98d7f7d672 | 90 | } |
csinders | 0:8d98d7f7d672 | 91 | //Provide network passphrase |
csinders | 0:8d98d7f7d672 | 92 | char networkPassphrase[] = "AT+NK=1,MTCDT-19400691\n"; |
csinders | 0:8d98d7f7d672 | 93 | if (!sendAtMessage(networkPassphrase)) { |
csinders | 0:8d98d7f7d672 | 94 | pc.printf("Network passphrase not successfully set\n\r"); |
csinders | 0:8d98d7f7d672 | 95 | } |
csinders | 0:8d98d7f7d672 | 96 | //Set the frequency sub band |
csinders | 0:8d98d7f7d672 | 97 | char frequencySubBand[] = "AT+FSB=1\n"; |
csinders | 0:8d98d7f7d672 | 98 | if (!sendAtMessage(frequencySubBand)) { |
csinders | 0:8d98d7f7d672 | 99 | pc.printf("Network frequency SubBand not successfully set\n\r"); |
csinders | 0:8d98d7f7d672 | 100 | } |
csinders | 0:8d98d7f7d672 | 101 | if (!sendAtMessage("AT+TXDR3")) { |
csinders | 0:8d98d7f7d672 | 102 | pc.printf("Unable to set dataRate\n\r"); |
csinders | 0:8d98d7f7d672 | 103 | } |
csinders | 0:8d98d7f7d672 | 104 | //pc.printf("I got here\r\n"); |
csinders | 0:8d98d7f7d672 | 105 | |
csinders | 0:8d98d7f7d672 | 106 | //Join the network |
csinders | 0:8d98d7f7d672 | 107 | char joinNetwork[] = "AT+JOIN\n"; // Unable to join network atm. |
csinders | 0:8d98d7f7d672 | 108 | if (!sendAtMessage(joinNetwork)) { |
csinders | 0:8d98d7f7d672 | 109 | pc.printf("Failed to joined the network\n\r"); |
csinders | 0:8d98d7f7d672 | 110 | } |
csinders | 0:8d98d7f7d672 | 111 | |
csinders | 0:8d98d7f7d672 | 112 | int i = 0; |
csinders | 0:8d98d7f7d672 | 113 | while(1) { |
csinders | 0:8d98d7f7d672 | 114 | for (i = 0; i < 24; i++) { |
csinders | 0:8d98d7f7d672 | 115 | sendAtMessageNACK("AT+SEND=",8); |
csinders | 0:8d98d7f7d672 | 116 | int lightBufSize = 10; |
csinders | 0:8d98d7f7d672 | 117 | double light = lightsensor.lux(); |
csinders | 0:8d98d7f7d672 | 118 | char lightBuf[lightBufSize]; |
csinders | 0:8d98d7f7d672 | 119 | //pc.printf("Current light is %f\n\r",light); |
csinders | 0:8d98d7f7d672 | 120 | sprintf(lightBuf, "%6.2f",light); |
csinders | 0:8d98d7f7d672 | 121 | //pc.printf("After convert with sprintf light = %s\n\r",lightBuf); |
csinders | 0:8d98d7f7d672 | 122 | sendAtMessageNACK(lightBuf,lightBufSize); |
csinders | 0:8d98d7f7d672 | 123 | sendAtMessageNACK(",",1); |
csinders | 0:8d98d7f7d672 | 124 | wait(3600); |
csinders | 0:8d98d7f7d672 | 125 | } |
csinders | 0:8d98d7f7d672 | 126 | if (sendAtMessage("\n")) { |
csinders | 0:8d98d7f7d672 | 127 | pc.printf("message succesfully sent\n\r"); |
csinders | 0:8d98d7f7d672 | 128 | myled = 1; |
csinders | 0:8d98d7f7d672 | 129 | } else { |
csinders | 0:8d98d7f7d672 | 130 | pc.printf("Failed to send the message for some reason\r\n"); |
csinders | 0:8d98d7f7d672 | 131 | } |
csinders | 0:8d98d7f7d672 | 132 | } |
csinders | 0:8d98d7f7d672 | 133 | |
csinders | 0:8d98d7f7d672 | 134 | |
csinders | 0:8d98d7f7d672 | 135 | //When an s is read from the serial pc, |
csinders | 0:8d98d7f7d672 | 136 | // read the current pressure/temperature from the I2C pressure sensor |
csinders | 0:8d98d7f7d672 | 137 | // and send it to the MQTT server pivot.iuiot |
csinders | 0:8d98d7f7d672 | 138 | //Unsure of what size to set Buffer so that it doesn't go over |
csinders | 0:8d98d7f7d672 | 139 | //when trying to send a message |
csinders | 0:8d98d7f7d672 | 140 | |
csinders | 0:8d98d7f7d672 | 141 | /* |
csinders | 0:8d98d7f7d672 | 142 | while(1) { |
csinders | 0:8d98d7f7d672 | 143 | if (pc.readable()) { |
csinders | 0:8d98d7f7d672 | 144 | //pc.printf("pc.readable\r\n"); |
csinders | 0:8d98d7f7d672 | 145 | if (pc.getc() == 's') { |
csinders | 0:8d98d7f7d672 | 146 | pc.printf("I recieved an s\r\n"); |
csinders | 0:8d98d7f7d672 | 147 | int tempBufSize = 4; |
csinders | 0:8d98d7f7d672 | 148 | int presBufSize = 6; |
csinders | 0:8d98d7f7d672 | 149 | double temp = pressure_sensor.getTemperature(); |
csinders | 0:8d98d7f7d672 | 150 | char tempBuf[tempBufSize]; |
csinders | 0:8d98d7f7d672 | 151 | double pres = pressure_sensor.getPressure(); |
csinders | 0:8d98d7f7d672 | 152 | char presBuf[presBufSize]; |
csinders | 0:8d98d7f7d672 | 153 | pc.printf("Current temp is %f\n\r",temp); |
csinders | 0:8d98d7f7d672 | 154 | pc.printf("Current pres is %f\n\r",pres); |
csinders | 0:8d98d7f7d672 | 155 | sprintf(tempBuf, "%2.1f",temp); |
csinders | 0:8d98d7f7d672 | 156 | sprintf(presBuf, "%4.1f",pres); |
csinders | 0:8d98d7f7d672 | 157 | pc.printf("After convert with sprintf temp = %s\n\r",tempBuf); |
csinders | 0:8d98d7f7d672 | 158 | pc.printf("After convert with sprintf pres = %s\n\r",presBuf); |
csinders | 0:8d98d7f7d672 | 159 | sendAtMessageNACK("AT+SEND=",8); |
csinders | 0:8d98d7f7d672 | 160 | sendAtMessageNACK(tempBuf,tempBufSize); |
csinders | 0:8d98d7f7d672 | 161 | sendAtMessageNACK(",",1); |
csinders | 0:8d98d7f7d672 | 162 | sendAtMessageNACK(presBuf,presBufSize); |
csinders | 0:8d98d7f7d672 | 163 | wait_ms(500); |
csinders | 0:8d98d7f7d672 | 164 | if (sendAtMessage("\n")) { |
csinders | 0:8d98d7f7d672 | 165 | pc.printf("message succesfully sent\n\r"); |
csinders | 0:8d98d7f7d672 | 166 | } else { |
csinders | 0:8d98d7f7d672 | 167 | pc.printf("Failed to send the message for some reason\r\n"); |
csinders | 0:8d98d7f7d672 | 168 | } |
csinders | 0:8d98d7f7d672 | 169 | } |
csinders | 0:8d98d7f7d672 | 170 | } |
csinders | 0:8d98d7f7d672 | 171 | } |
csinders | 0:8d98d7f7d672 | 172 | */ |
csinders | 0:8d98d7f7d672 | 173 | |
csinders | 0:8d98d7f7d672 | 174 | /* |
csinders | 0:8d98d7f7d672 | 175 | while(1) { |
csinders | 0:8d98d7f7d672 | 176 | if(pc.readable()) { |
csinders | 0:8d98d7f7d672 | 177 | device.putc(pc.getc()); |
csinders | 0:8d98d7f7d672 | 178 | myled = !myled; |
csinders | 0:8d98d7f7d672 | 179 | } |
csinders | 0:8d98d7f7d672 | 180 | if(device.readable()) { |
csinders | 0:8d98d7f7d672 | 181 | pc.putc(device.getc()); |
csinders | 0:8d98d7f7d672 | 182 | myled = !myled; |
csinders | 0:8d98d7f7d672 | 183 | } |
csinders | 0:8d98d7f7d672 | 184 | } |
csinders | 0:8d98d7f7d672 | 185 | */ |
csinders | 0:8d98d7f7d672 | 186 | } |