![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
multihop protocol
Dependencies: mbed
remote.cpp@6:1fe84e29b486, 2015-11-11 (annotated)
- Committer:
- mfrede
- Date:
- Wed Nov 11 00:46:15 2015 +0000
- Revision:
- 6:1fe84e29b486
- Parent:
- 5:387b2ebf6b91
- Child:
- 7:67b40640ddb6
added headers back in...not sure if we need them
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfrede | 1:019a3be50b96 | 1 | #include "mbed.h" |
mfrede | 1:019a3be50b96 | 2 | #include "MRF24J40.h" |
mfrede | 1:019a3be50b96 | 3 | #include <string> |
mfrede | 2:9365aaf34637 | 4 | #include "packet.h" |
mfrede | 1:019a3be50b96 | 5 | |
mfrede | 3:1b37d57a4e41 | 6 | //define a node number |
mfrede | 3:1b37d57a4e41 | 7 | #define NODE 2 |
mfrede | 3:1b37d57a4e41 | 8 | |
mfrede | 3:1b37d57a4e41 | 9 | // RF tranceiver to link with gateway. |
mfrede | 1:019a3be50b96 | 10 | MRF24J40 mrf(p11, p12, p13, p14, p21); |
mfrede | 1:019a3be50b96 | 11 | |
mfrede | 1:019a3be50b96 | 12 | // LEDs you can treat these as variables (led2 = 1 will turn led2 on!) |
mfrede | 1:019a3be50b96 | 13 | DigitalOut led1(LED1); |
mfrede | 1:019a3be50b96 | 14 | DigitalOut led2(LED2); |
mfrede | 1:019a3be50b96 | 15 | DigitalOut led3(LED3); |
mfrede | 1:019a3be50b96 | 16 | DigitalOut led4(LED4); |
mfrede | 1:019a3be50b96 | 17 | |
mfrede | 1:019a3be50b96 | 18 | // Timer |
mfrede | 1:019a3be50b96 | 19 | Timer timer; |
mfrede | 1:019a3be50b96 | 20 | |
mfrede | 1:019a3be50b96 | 21 | // Serial port for showing RX data. |
mfrede | 1:019a3be50b96 | 22 | Serial pc(USBTX, USBRX); |
mfrede | 1:019a3be50b96 | 23 | |
mfrede | 1:019a3be50b96 | 24 | // Used for sending and receiving |
mfrede | 5:387b2ebf6b91 | 25 | //char txBuffer[128]; |
mfrede | 5:387b2ebf6b91 | 26 | //char rxBuffer[128]; |
mfrede | 1:019a3be50b96 | 27 | int rxLen; |
mfrede | 5:387b2ebf6b91 | 28 | packet* txBuffer; |
mfrede | 5:387b2ebf6b91 | 29 | packet* rxBuffer; |
mfrede | 4:024e613151a5 | 30 | |
mfrede | 1:019a3be50b96 | 31 | //***************** Do not change these methods (please) *****************// |
mfrede | 1:019a3be50b96 | 32 | |
mfrede | 1:019a3be50b96 | 33 | /** |
mfrede | 1:019a3be50b96 | 34 | * Receive data from the MRF24J40. |
mfrede | 1:019a3be50b96 | 35 | * |
mfrede | 1:019a3be50b96 | 36 | * @param data A pointer to a char array to hold the data |
mfrede | 1:019a3be50b96 | 37 | * @param maxLength The max amount of data to read. |
mfrede | 1:019a3be50b96 | 38 | */ |
mfrede | 4:024e613151a5 | 39 | int rf_receive(packet *data, uint8_t maxLength) |
mfrede | 1:019a3be50b96 | 40 | { |
mfrede | 1:019a3be50b96 | 41 | uint8_t len = mrf.Receive((uint8_t *)data, maxLength); |
mfrede | 6:1fe84e29b486 | 42 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
mfrede | 1:019a3be50b96 | 43 | |
mfrede | 6:1fe84e29b486 | 44 | if(len > 10) { |
mfrede | 6:1fe84e29b486 | 45 | pc.printf("got data\r\n"); |
mfrede | 6:1fe84e29b486 | 46 | //Remove the header and footer of the message |
mfrede | 6:1fe84e29b486 | 47 | for(uint8_t i = 0; i < len-2; i++) { |
mfrede | 6:1fe84e29b486 | 48 | if(i<8) { |
mfrede | 6:1fe84e29b486 | 49 | //Make sure our header is valid first |
mfrede | 6:1fe84e29b486 | 50 | if(((uint8_t *)data)[i] != header[i]) |
mfrede | 6:1fe84e29b486 | 51 | return 0; |
mfrede | 6:1fe84e29b486 | 52 | } else { |
mfrede | 6:1fe84e29b486 | 53 | ((uint8_t *)data)[i-8] = ((uint8_t *)data)[i]; |
mfrede | 6:1fe84e29b486 | 54 | } |
mfrede | 6:1fe84e29b486 | 55 | } |
mfrede | 6:1fe84e29b486 | 56 | len = len - 8; |
mfrede | 6:1fe84e29b486 | 57 | } |
mfrede | 4:024e613151a5 | 58 | if(len > 4) { |
mfrede | 4:024e613151a5 | 59 | //valid data with full header |
mfrede | 5:387b2ebf6b91 | 60 | if(data->to != NODE) |
mfrede | 4:024e613151a5 | 61 | { |
mfrede | 4:024e613151a5 | 62 | //throw out the data |
mfrede | 4:024e613151a5 | 63 | return 0; |
mfrede | 1:019a3be50b96 | 64 | } |
mfrede | 5:387b2ebf6b91 | 65 | pc.printf("Received data for us from node: %d TTL: %d\r\n",data->from,data->TTL); |
mfrede | 5:387b2ebf6b91 | 66 | |
mfrede | 5:387b2ebf6b91 | 67 | switch(data->TYPE) |
mfrede | 5:387b2ebf6b91 | 68 | { |
mfrede | 5:387b2ebf6b91 | 69 | case HELLO_TYPE: |
mfrede | 5:387b2ebf6b91 | 70 | pc.printf("HELLO\r\n"); |
mfrede | 5:387b2ebf6b91 | 71 | } |
mfrede | 4:024e613151a5 | 72 | |
mfrede | 1:019a3be50b96 | 73 | //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10); |
mfrede | 1:019a3be50b96 | 74 | } |
mfrede | 4:024e613151a5 | 75 | |
mfrede | 4:024e613151a5 | 76 | |
mfrede | 4:024e613151a5 | 77 | return ((int)len); |
mfrede | 1:019a3be50b96 | 78 | } |
mfrede | 1:019a3be50b96 | 79 | |
mfrede | 1:019a3be50b96 | 80 | /** |
mfrede | 1:019a3be50b96 | 81 | * Send data to another MRF24J40. |
mfrede | 1:019a3be50b96 | 82 | * |
mfrede | 1:019a3be50b96 | 83 | * @param data The string to send |
mfrede | 1:019a3be50b96 | 84 | * @param maxLength The length of the data to send. |
mfrede | 1:019a3be50b96 | 85 | * If you are sending a null-terminated string you can pass strlen(data)+1 |
mfrede | 1:019a3be50b96 | 86 | */ |
mfrede | 4:024e613151a5 | 87 | void rf_send(packet *data, uint8_t len) |
mfrede | 1:019a3be50b96 | 88 | { |
mfrede | 1:019a3be50b96 | 89 | |
mfrede | 5:387b2ebf6b91 | 90 | data->from = NODE; |
mfrede | 5:387b2ebf6b91 | 91 | |
mfrede | 1:019a3be50b96 | 92 | |
mfrede | 5:387b2ebf6b91 | 93 | mrf.Send((uint8_t *) data, (len*4)+4); |
mfrede | 1:019a3be50b96 | 94 | } |
mfrede | 1:019a3be50b96 | 95 | |
mfrede | 1:019a3be50b96 | 96 | |
mfrede | 1:019a3be50b96 | 97 | //***************** You can start coding here *****************// |
mfrede | 1:019a3be50b96 | 98 | |
mfrede | 1:019a3be50b96 | 99 | int main (void) |
mfrede | 1:019a3be50b96 | 100 | { |
mfrede | 1:019a3be50b96 | 101 | //Set the channel of the slave |
mfrede | 1:019a3be50b96 | 102 | uint8_t channel = 15; |
mfrede | 1:019a3be50b96 | 103 | mrf.SetChannel(channel); |
mfrede | 1:019a3be50b96 | 104 | |
mfrede | 1:019a3be50b96 | 105 | //Analog input |
mfrede | 1:019a3be50b96 | 106 | AnalogIn light(p20); //Analog input from the light sensor |
mfrede | 1:019a3be50b96 | 107 | AnalogIn temp(p19); //Analog input from the temp snesor |
mfrede | 1:019a3be50b96 | 108 | |
mfrede | 1:019a3be50b96 | 109 | //Variable to read values |
mfrede | 1:019a3be50b96 | 110 | float val = 1; |
mfrede | 1:019a3be50b96 | 111 | int begin = 0; |
mfrede | 1:019a3be50b96 | 112 | int end = 0; |
mfrede | 1:019a3be50b96 | 113 | int current = 0; |
mfrede | 1:019a3be50b96 | 114 | |
mfrede | 1:019a3be50b96 | 115 | //Start the timer |
mfrede | 1:019a3be50b96 | 116 | timer.start(); |
mfrede | 1:019a3be50b96 | 117 | |
mfrede | 1:019a3be50b96 | 118 | while(true) { |
mfrede | 1:019a3be50b96 | 119 | |
mfrede | 1:019a3be50b96 | 120 | //Try to receive some data |
mfrede | 6:1fe84e29b486 | 121 | rxLen = rf_receive(rxBuffer, 128); |
mfrede | 1:019a3be50b96 | 122 | |
mfrede | 1:019a3be50b96 | 123 | if(rxLen > 0) { |
mfrede | 1:019a3be50b96 | 124 | |
mfrede | 1:019a3be50b96 | 125 | //Switch on the Led and start the timer |
mfrede | 1:019a3be50b96 | 126 | printf("Received input\n"); |
mfrede | 1:019a3be50b96 | 127 | led1 = 1; |
mfrede | 1:019a3be50b96 | 128 | begin = timer.read_ms(); |
mfrede | 1:019a3be50b96 | 129 | end = begin + 1000; |
mfrede | 5:387b2ebf6b91 | 130 | //strcpy(txBuffer, "n"); |
mfrede | 1:019a3be50b96 | 131 | |
mfrede | 1:019a3be50b96 | 132 | |
mfrede | 1:019a3be50b96 | 133 | while(current < end) { |
mfrede | 1:019a3be50b96 | 134 | |
mfrede | 1:019a3be50b96 | 135 | current = timer.read_ms(); |
mfrede | 5:387b2ebf6b91 | 136 | // val = button.read(); |
mfrede | 5:387b2ebf6b91 | 137 | // //printf("val: %f\r\n",val); |
mfrede | 5:387b2ebf6b91 | 138 | // if(val < 0.1f ) { |
mfrede | 5:387b2ebf6b91 | 139 | // strcpy(txBuffer, "y"); ///send "y" if press |
mfrede | 5:387b2ebf6b91 | 140 | // break; |
mfrede | 5:387b2ebf6b91 | 141 | // } |
mfrede | 1:019a3be50b96 | 142 | |
mfrede | 1:019a3be50b96 | 143 | }//end of 1 sec while loop |
mfrede | 1:019a3be50b96 | 144 | |
mfrede | 1:019a3be50b96 | 145 | led1 = 0; //Switch of the Led |
mfrede | 5:387b2ebf6b91 | 146 | // rf_send(txBuffer, strlen(txBuffer) + 1); // send data |
mfrede | 1:019a3be50b96 | 147 | pc.printf("Sent: %s\r\n", txBuffer); |
mfrede | 1:019a3be50b96 | 148 | |
mfrede | 1:019a3be50b96 | 149 | }//end of if loop |
mfrede | 1:019a3be50b96 | 150 | |
mfrede | 1:019a3be50b96 | 151 | |
mfrede | 1:019a3be50b96 | 152 | |
mfrede | 1:019a3be50b96 | 153 | }// end of while true |
mfrede | 1:019a3be50b96 | 154 | |
mfrede | 1:019a3be50b96 | 155 | }//end of main |