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@2:ac9f576fbcb7, 2018-04-19 (annotated)
- Committer:
- csinders
- Date:
- Thu Apr 19 14:54:48 2018 +0000
- Revision:
- 2:ac9f576fbcb7
- Parent:
- 1:fb25b13e6ab3
Have it set up using DR1. 6 transmissions with 4 data points each.
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 | 2:ac9f576fbcb7 | 74 | /* |
csinders | 2:ac9f576fbcb7 | 75 | Copies copyFrom into the CopyTo string. |
csinders | 2:ac9f576fbcb7 | 76 | /StartTo is the starting index of Copyto |
csinders | 2:ac9f576fbcb7 | 77 | copyFromSize is the size of the string being copied |
csinders | 2:ac9f576fbcb7 | 78 | //EX: Copyfrom = "HI", Copyto = "____", startTo = 1, copyFromSize = 2 |
csinders | 2:ac9f576fbcb7 | 79 | // end result of copyTo = "_HI_" |
csinders | 2:ac9f576fbcb7 | 80 | */ |
csinders | 2:ac9f576fbcb7 | 81 | void copyString(char *copyTo, char *copyFrom, int startTo, int copyFromSize) { |
csinders | 2:ac9f576fbcb7 | 82 | int i; |
csinders | 2:ac9f576fbcb7 | 83 | for (i = 0; i < copyFromSize; i++) { |
csinders | 2:ac9f576fbcb7 | 84 | copyTo[i+startTo] = copyFrom[i]; |
csinders | 2:ac9f576fbcb7 | 85 | } |
csinders | 2:ac9f576fbcb7 | 86 | } |
csinders | 2:ac9f576fbcb7 | 87 | |
csinders | 2:ac9f576fbcb7 | 88 | |
csinders | 0:8d98d7f7d672 | 89 | int main() { |
csinders | 0:8d98d7f7d672 | 90 | pc.baud(115200); |
csinders | 0:8d98d7f7d672 | 91 | device.baud(115200); |
csinders | 0:8d98d7f7d672 | 92 | myled = 1; |
csinders | 0:8d98d7f7d672 | 93 | wait(1); |
csinders | 0:8d98d7f7d672 | 94 | myled = 0; |
csinders | 0:8d98d7f7d672 | 95 | |
csinders | 2:ac9f576fbcb7 | 96 | //Array to hold data |
csinders | 2:ac9f576fbcb7 | 97 | int dataArraySize = 24; |
csinders | 2:ac9f576fbcb7 | 98 | int lightBufSize = 8; |
csinders | 2:ac9f576fbcb7 | 99 | char dataArray[dataArraySize][lightBufSize]; |
csinders | 2:ac9f576fbcb7 | 100 | |
csinders | 0:8d98d7f7d672 | 101 | pc.printf("Starting program\n\r"); |
csinders | 1:fb25b13e6ab3 | 102 | |
csinders | 0:8d98d7f7d672 | 103 | if (!sendAtMessage("AT\n")) { |
csinders | 0:8d98d7f7d672 | 104 | pc.printf("xDot not responding to message\n\r"); |
csinders | 0:8d98d7f7d672 | 105 | } |
csinders | 0:8d98d7f7d672 | 106 | // Provice network name |
csinders | 0:8d98d7f7d672 | 107 | char networkName[] = "AT+NI=1,MTCDT-19400691\n"; |
csinders | 0:8d98d7f7d672 | 108 | if (!sendAtMessage(networkName)) { |
csinders | 0:8d98d7f7d672 | 109 | pc.printf("Unable to set network name\n\r"); |
csinders | 0:8d98d7f7d672 | 110 | } |
csinders | 0:8d98d7f7d672 | 111 | //Provide network passphrase |
csinders | 0:8d98d7f7d672 | 112 | char networkPassphrase[] = "AT+NK=1,MTCDT-19400691\n"; |
csinders | 0:8d98d7f7d672 | 113 | if (!sendAtMessage(networkPassphrase)) { |
csinders | 0:8d98d7f7d672 | 114 | pc.printf("Network passphrase not successfully set\n\r"); |
csinders | 0:8d98d7f7d672 | 115 | } |
csinders | 0:8d98d7f7d672 | 116 | //Set the frequency sub band |
csinders | 0:8d98d7f7d672 | 117 | char frequencySubBand[] = "AT+FSB=1\n"; |
csinders | 0:8d98d7f7d672 | 118 | if (!sendAtMessage(frequencySubBand)) { |
csinders | 0:8d98d7f7d672 | 119 | pc.printf("Network frequency SubBand not successfully set\n\r"); |
csinders | 0:8d98d7f7d672 | 120 | } |
csinders | 1:fb25b13e6ab3 | 121 | |
csinders | 2:ac9f576fbcb7 | 122 | //Set data transmission rate |
csinders | 2:ac9f576fbcb7 | 123 | if (!sendAtMessage("AT+TXDR=1\n")) { |
csinders | 0:8d98d7f7d672 | 124 | pc.printf("Unable to set dataRate\n\r"); |
csinders | 0:8d98d7f7d672 | 125 | } |
csinders | 0:8d98d7f7d672 | 126 | |
csinders | 2:ac9f576fbcb7 | 127 | if (!sendAtMessage("AT+ACK=8\n")) { |
csinders | 2:ac9f576fbcb7 | 128 | pc.printf("Unable to set acknowledgement requirement\n\r"); |
csinders | 2:ac9f576fbcb7 | 129 | } |
csinders | 2:ac9f576fbcb7 | 130 | pc.printf("I got here\n\r"); |
csinders | 1:fb25b13e6ab3 | 131 | |
csinders | 0:8d98d7f7d672 | 132 | //Join the network |
csinders | 0:8d98d7f7d672 | 133 | char joinNetwork[] = "AT+JOIN\n"; // Unable to join network atm. |
csinders | 2:ac9f576fbcb7 | 134 | while (!sendAtMessage(joinNetwork)) { |
csinders | 0:8d98d7f7d672 | 135 | pc.printf("Failed to joined the network\n\r"); |
csinders | 2:ac9f576fbcb7 | 136 | wait(1); |
csinders | 0:8d98d7f7d672 | 137 | } |
csinders | 0:8d98d7f7d672 | 138 | |
csinders | 2:ac9f576fbcb7 | 139 | while (!sendAtMessage("AT+SEND=TestMessage\n")) { |
csinders | 2:ac9f576fbcb7 | 140 | myled=0; |
csinders | 2:ac9f576fbcb7 | 141 | } |
csinders | 2:ac9f576fbcb7 | 142 | myled = 1; |
csinders | 0:8d98d7f7d672 | 143 | |
csinders | 0:8d98d7f7d672 | 144 | while(1) { |
csinders | 2:ac9f576fbcb7 | 145 | //Collect data |
csinders | 2:ac9f576fbcb7 | 146 | int i = 0; |
csinders | 2:ac9f576fbcb7 | 147 | for (i = 0; i < dataArraySize; i++) { |
csinders | 2:ac9f576fbcb7 | 148 | double light = lightsensor.lux(); |
csinders | 2:ac9f576fbcb7 | 149 | pc.printf("Current light is %f\n\r",light); |
csinders | 2:ac9f576fbcb7 | 150 | sprintf(dataArray[i], "%2.5f",light); |
csinders | 2:ac9f576fbcb7 | 151 | pc.printf("After convert with sprintf light = %s\n\r",dataArray[i]); |
csinders | 2:ac9f576fbcb7 | 152 | wait_ms(500); |
csinders | 2:ac9f576fbcb7 | 153 | } |
csinders | 2:ac9f576fbcb7 | 154 | //pc.printf("%s\n\r",dataArray); |
csinders | 2:ac9f576fbcb7 | 155 | |
csinders | 2:ac9f576fbcb7 | 156 | |
csinders | 2:ac9f576fbcb7 | 157 | for (i = 0; i < 6; i++) { |
csinders | 2:ac9f576fbcb7 | 158 | int j; |
csinders | 2:ac9f576fbcb7 | 159 | sendAtMessageNACK("AT+SEND=",8); |
csinders | 2:ac9f576fbcb7 | 160 | for (j = 0; j < 4; j++) { |
csinders | 2:ac9f576fbcb7 | 161 | sendAtMessageNACK(dataArray[(i*4) + j], lightBufSize); |
csinders | 2:ac9f576fbcb7 | 162 | sendAtMessageNACK(";",1); |
csinders | 0:8d98d7f7d672 | 163 | } |
csinders | 2:ac9f576fbcb7 | 164 | if(sendAtMessage("\n")) { |
csinders | 2:ac9f576fbcb7 | 165 | pc.printf("message succesfully sent\n\r"); |
csinders | 2:ac9f576fbcb7 | 166 | //myled = 1; |
csinders | 2:ac9f576fbcb7 | 167 | } |
csinders | 2:ac9f576fbcb7 | 168 | wait_ms(1000); |
csinders | 0:8d98d7f7d672 | 169 | } |
csinders | 0:8d98d7f7d672 | 170 | } |
csinders | 0:8d98d7f7d672 | 171 | |
csinders | 1:fb25b13e6ab3 | 172 | |
csinders | 0:8d98d7f7d672 | 173 | while(1) { |
csinders | 0:8d98d7f7d672 | 174 | if(pc.readable()) { |
csinders | 0:8d98d7f7d672 | 175 | device.putc(pc.getc()); |
csinders | 0:8d98d7f7d672 | 176 | myled = !myled; |
csinders | 0:8d98d7f7d672 | 177 | } |
csinders | 0:8d98d7f7d672 | 178 | if(device.readable()) { |
csinders | 0:8d98d7f7d672 | 179 | pc.putc(device.getc()); |
csinders | 0:8d98d7f7d672 | 180 | myled = !myled; |
csinders | 0:8d98d7f7d672 | 181 | } |
csinders | 0:8d98d7f7d672 | 182 | } |
csinders | 1:fb25b13e6ab3 | 183 | |
csinders | 0:8d98d7f7d672 | 184 | } |