![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
master to control sensor node
Dependencies: mbed
gateway.cpp@4:5518470323de, 2015-11-10 (annotated)
- Committer:
- mfrede
- Date:
- Tue Nov 10 23:44:36 2015 +0000
- Revision:
- 4:5518470323de
- Parent:
- 3:646a628b8656
- Child:
- 5:85fea4ceaa1f
fix compile errors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfrede | 1:9de46f7e559e | 1 | #include "mbed.h" |
mfrede | 1:9de46f7e559e | 2 | #include "MRF24J40.h" |
mfrede | 1:9de46f7e559e | 3 | #include <string> |
mfrede | 2:5d88c51f53dc | 4 | #include "packet.h" |
mfrede | 1:9de46f7e559e | 5 | |
ashrekhi | 3:646a628b8656 | 6 | #define NODE 1 |
ashrekhi | 3:646a628b8656 | 7 | |
ashrekhi | 3:646a628b8656 | 8 | //unsigned short TYPE = 4; |
mfrede | 1:9de46f7e559e | 9 | |
mfrede | 1:9de46f7e559e | 10 | // RF tranceiver to link with handheld. |
mfrede | 1:9de46f7e559e | 11 | MRF24J40 mrf(p11, p12, p13, p14, p21); |
mfrede | 1:9de46f7e559e | 12 | |
mfrede | 1:9de46f7e559e | 13 | DigitalOut myled(LED1); |
mfrede | 1:9de46f7e559e | 14 | |
mfrede | 1:9de46f7e559e | 15 | // Serial port for showing RX data. |
mfrede | 1:9de46f7e559e | 16 | Serial pc(USBTX, USBRX); |
mfrede | 1:9de46f7e559e | 17 | |
mfrede | 1:9de46f7e559e | 18 | // Used for sending and receiving |
mfrede | 1:9de46f7e559e | 19 | char txBuffer[128]; |
mfrede | 1:9de46f7e559e | 20 | char rxBuffer[128]; |
mfrede | 1:9de46f7e559e | 21 | int rxLen; |
mfrede | 1:9de46f7e559e | 22 | |
ashrekhi | 3:646a628b8656 | 23 | packet* sxBuffer; |
ashrekhi | 3:646a628b8656 | 24 | packet* gxBuffer; |
ashrekhi | 3:646a628b8656 | 25 | |
mfrede | 1:9de46f7e559e | 26 | //***************** Do not change these methods (please) *****************// |
mfrede | 1:9de46f7e559e | 27 | |
mfrede | 1:9de46f7e559e | 28 | /** |
mfrede | 1:9de46f7e559e | 29 | * Receive data from the MRF24J40. |
mfrede | 1:9de46f7e559e | 30 | * |
mfrede | 1:9de46f7e559e | 31 | * @param data A pointer to a char array to hold the data |
mfrede | 1:9de46f7e559e | 32 | * @param maxLength The max amount of data to read. |
mfrede | 1:9de46f7e559e | 33 | */ |
mfrede | 1:9de46f7e559e | 34 | int rf_receive(char *data, uint8_t maxLength) |
mfrede | 1:9de46f7e559e | 35 | { |
mfrede | 1:9de46f7e559e | 36 | uint8_t len = mrf.Receive((uint8_t *)data, maxLength); |
mfrede | 1:9de46f7e559e | 37 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
mfrede | 1:9de46f7e559e | 38 | |
mfrede | 1:9de46f7e559e | 39 | if(len > 10) { |
mfrede | 1:9de46f7e559e | 40 | //Remove the header and footer of the message |
mfrede | 1:9de46f7e559e | 41 | for(uint8_t i = 0; i < len-2; i++) { |
mfrede | 1:9de46f7e559e | 42 | if(i<8) { |
mfrede | 1:9de46f7e559e | 43 | //Make sure our header is valid first |
mfrede | 1:9de46f7e559e | 44 | if(data[i] != header[i]) |
mfrede | 1:9de46f7e559e | 45 | return 0; |
mfrede | 1:9de46f7e559e | 46 | } else { |
mfrede | 1:9de46f7e559e | 47 | data[i-8] = data[i]; |
mfrede | 1:9de46f7e559e | 48 | } |
mfrede | 1:9de46f7e559e | 49 | } |
mfrede | 1:9de46f7e559e | 50 | |
mfrede | 1:9de46f7e559e | 51 | //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10); |
mfrede | 1:9de46f7e559e | 52 | } |
mfrede | 1:9de46f7e559e | 53 | return ((int)len)-10; |
mfrede | 1:9de46f7e559e | 54 | } |
mfrede | 1:9de46f7e559e | 55 | |
mfrede | 1:9de46f7e559e | 56 | /** |
mfrede | 1:9de46f7e559e | 57 | * Send data to another MRF24J40. |
mfrede | 1:9de46f7e559e | 58 | * |
mfrede | 1:9de46f7e559e | 59 | * @param data The string to send |
mfrede | 1:9de46f7e559e | 60 | * @param maxLength The length of the data to send. |
mfrede | 1:9de46f7e559e | 61 | * If you are sending a null-terminated string you can pass strlen(data)+1 |
mfrede | 1:9de46f7e559e | 62 | */ |
ashrekhi | 3:646a628b8656 | 63 | void rf_send(packet *data, uint8_t len) |
mfrede | 1:9de46f7e559e | 64 | { |
mfrede | 1:9de46f7e559e | 65 | //We need to prepend the message with a valid ZigBee header |
ashrekhi | 3:646a628b8656 | 66 | //uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
ashrekhi | 3:646a628b8656 | 67 | //uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len) ); |
ashrekhi | 3:646a628b8656 | 68 | //for(uint8_t i = 0; i < len+8; i++) { |
mfrede | 1:9de46f7e559e | 69 | //prepend the 8-byte header |
ashrekhi | 3:646a628b8656 | 70 | //send_buf[i] = (i<8) ? header[i] : data[i-8]; |
mfrede | 4:5518470323de | 71 | // } |
mfrede | 1:9de46f7e559e | 72 | //pc.printf("Sent: %s\r\n", send_buf+8); |
ashrekhi | 3:646a628b8656 | 73 | |
ashrekhi | 3:646a628b8656 | 74 | //for(unit8_t i = 0; i < len; i++){ |
ashrekhi | 3:646a628b8656 | 75 | //} |
ashrekhi | 3:646a628b8656 | 76 | |
mfrede | 4:5518470323de | 77 | mrf.Send((uint8_t *) data, (len*4)+4); |
ashrekhi | 3:646a628b8656 | 78 | //free(send_buf); |
mfrede | 1:9de46f7e559e | 79 | } |
mfrede | 1:9de46f7e559e | 80 | |
mfrede | 1:9de46f7e559e | 81 | int main() { |
mfrede | 2:5d88c51f53dc | 82 | mrf.SetChannel(15); |
mfrede | 1:9de46f7e559e | 83 | int nodes = 6; |
mfrede | 4:5518470323de | 84 | // char buffer[128]; |
ashrekhi | 3:646a628b8656 | 85 | |
ashrekhi | 3:646a628b8656 | 86 | packet buffer(NODE,2,HELLO_TYPE,4.0,5.0); |
mfrede | 4:5518470323de | 87 | rf_send(&buffer, 2); |
ashrekhi | 3:646a628b8656 | 88 | /* while(1) { |
mfrede | 1:9de46f7e559e | 89 | pc.printf("Select operation: \r\n"); |
mfrede | 1:9de46f7e559e | 90 | pc.printf("1) get node list\r\n"); |
mfrede | 1:9de46f7e559e | 91 | pc.printf("2) Update data rates\r\n"); |
mfrede | 1:9de46f7e559e | 92 | pc.gets(buffer,1); |
mfrede | 1:9de46f7e559e | 93 | switch (buffer[0]) { |
mfrede | 1:9de46f7e559e | 94 | case '1': |
mfrede | 1:9de46f7e559e | 95 | |
mfrede | 1:9de46f7e559e | 96 | for(int i=0;i<nodes;i++) |
mfrede | 1:9de46f7e559e | 97 | { |
mfrede | 1:9de46f7e559e | 98 | pc.printf("Node %d Neighbors:\r\n"); |
mfrede | 1:9de46f7e559e | 99 | for (int j=0;j<neighbors[i][0];j++) |
mfrede | 1:9de46f7e559e | 100 | { |
mfrede | 1:9de46f7e559e | 101 | pc.printf("Node %d: %d cycles\r\n",neighbors[i][j],cycles[i][j]); |
mfrede | 1:9de46f7e559e | 102 | } |
mfrede | 1:9de46f7e559e | 103 | } |
mfrede | 1:9de46f7e559e | 104 | break; |
mfrede | 1:9de46f7e559e | 105 | case '2': |
mfrede | 1:9de46f7e559e | 106 | pc.printf("How many seconds between updates? "); |
mfrede | 1:9de46f7e559e | 107 | pc.gets(buffer,2); |
mfrede | 1:9de46f7e559e | 108 | int secs = atoi(buffer); |
mfrede | 1:9de46f7e559e | 109 | //build data packet |
mfrede | 1:9de46f7e559e | 110 | |
mfrede | 1:9de46f7e559e | 111 | //broadcast type |
mfrede | 1:9de46f7e559e | 112 | |
mfrede | 1:9de46f7e559e | 113 | //send data packet |
mfrede | 1:9de46f7e559e | 114 | |
mfrede | 1:9de46f7e559e | 115 | break; |
mfrede | 1:9de46f7e559e | 116 | default: |
mfrede | 1:9de46f7e559e | 117 | pc.printf("unknown command\r\n"); |
mfrede | 1:9de46f7e559e | 118 | break; |
mfrede | 1:9de46f7e559e | 119 | } |
ashrekhi | 3:646a628b8656 | 120 | */ |
mfrede | 4:5518470323de | 121 | //} |
mfrede | 1:9de46f7e559e | 122 | } |