![](/media/cache/group/WeatherStation.jpg.50x50_q85.jpg)
This is the code for the mbed which receives the weather data, pushes serially to screen
Dependencies: ID12RFID NRF2401P mbed
main.cpp@0:ef33698150d6, 2015-06-12 (annotated)
- Committer:
- lewisjfish
- Date:
- Fri Jun 12 13:20:33 2015 +0000
- Revision:
- 0:ef33698150d6
receiving weather data and pushing to receiver/screen serially. RFID cmmnted out
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lewisjfish | 0:ef33698150d6 | 1 | #include "mbed.h" |
lewisjfish | 0:ef33698150d6 | 2 | #include "ID12RFID.h" |
lewisjfish | 0:ef33698150d6 | 3 | #include "NRF2401P.h" |
lewisjfish | 0:ef33698150d6 | 4 | #include "nRF24l01.h" |
lewisjfish | 0:ef33698150d6 | 5 | |
lewisjfish | 0:ef33698150d6 | 6 | Serial infoout(PTA2,PTA1); //define the serial port, connected to the screen mbed |
lewisjfish | 0:ef33698150d6 | 7 | |
lewisjfish | 0:ef33698150d6 | 8 | Serial weatherIn(PTE22,PTE23); |
lewisjfish | 0:ef33698150d6 | 9 | |
lewisjfish | 0:ef33698150d6 | 10 | |
lewisjfish | 0:ef33698150d6 | 11 | //DigitalOut myled(LED1); |
lewisjfish | 0:ef33698150d6 | 12 | char message[9]; //character array to store the data we send |
lewisjfish | 0:ef33698150d6 | 13 | |
lewisjfish | 0:ef33698150d6 | 14 | int tagNumber; //stores the tagnumber read from the RFID reader |
lewisjfish | 0:ef33698150d6 | 15 | |
lewisjfish | 0:ef33698150d6 | 16 | char c; |
lewisjfish | 0:ef33698150d6 | 17 | int i; |
lewisjfish | 0:ef33698150d6 | 18 | |
lewisjfish | 0:ef33698150d6 | 19 | |
lewisjfish | 0:ef33698150d6 | 20 | |
lewisjfish | 0:ef33698150d6 | 21 | ID12RFID rfid(PTE1); //Initialise RFID reader and associated functions |
lewisjfish | 0:ef33698150d6 | 22 | |
lewisjfish | 0:ef33698150d6 | 23 | |
lewisjfish | 0:ef33698150d6 | 24 | |
lewisjfish | 0:ef33698150d6 | 25 | int main() { |
lewisjfish | 0:ef33698150d6 | 26 | |
lewisjfish | 0:ef33698150d6 | 27 | long long addr1=0xAB01CD; // setup address - any 5 byte number - same as TX |
lewisjfish | 0:ef33698150d6 | 28 | int channel =0x10; // [0-126] setup channel, must be same as TX |
lewisjfish | 0:ef33698150d6 | 29 | bool txOK; |
lewisjfish | 0:ef33698150d6 | 30 | char msg[32]; |
lewisjfish | 0:ef33698150d6 | 31 | char ackData[32]; |
lewisjfish | 0:ef33698150d6 | 32 | char len; |
lewisjfish | 0:ef33698150d6 | 33 | |
lewisjfish | 0:ef33698150d6 | 34 | // Setup |
lewisjfish | 0:ef33698150d6 | 35 | NRF2401P nrf1(PTD2,PTD3, PTD1,PTD5,PTD0); //mosi, miso, sclk, csn, ce) |
lewisjfish | 0:ef33698150d6 | 36 | nrf1.quickRxSetup(channel, addr1); // sets nrf24l01+ as receiver, using pipe 1 |
lewisjfish | 0:ef33698150d6 | 37 | printf("Set up complete!\n\r"); |
lewisjfish | 0:ef33698150d6 | 38 | |
lewisjfish | 0:ef33698150d6 | 39 | // set ack data |
lewisjfish | 0:ef33698150d6 | 40 | sprintf(ackData,"Acknowledge data"); |
lewisjfish | 0:ef33698150d6 | 41 | printf("Ack data set.\n\r"); |
lewisjfish | 0:ef33698150d6 | 42 | |
lewisjfish | 0:ef33698150d6 | 43 | while(1){ |
lewisjfish | 0:ef33698150d6 | 44 | |
lewisjfish | 0:ef33698150d6 | 45 | // receive |
lewisjfish | 0:ef33698150d6 | 46 | while (! nrf1.isRxData()); // note this blocks until RX data |
lewisjfish | 0:ef33698150d6 | 47 | |
lewisjfish | 0:ef33698150d6 | 48 | len= nrf1.getRxData(msg); // gets the message, len is length of msg |
lewisjfish | 0:ef33698150d6 | 49 | msg[len] = 0; |
lewisjfish | 0:ef33698150d6 | 50 | printf("%s",msg); |
lewisjfish | 0:ef33698150d6 | 51 | nrf1.acknowledgeData(ackData, strlen(ackData),1); // ack for pipe 1 |
lewisjfish | 0:ef33698150d6 | 52 | |
lewisjfish | 0:ef33698150d6 | 53 | for(int n =0; n<sizeof(msg) ; n++){ |
lewisjfish | 0:ef33698150d6 | 54 | //send the data to the second mbed(screen) using putc |
lewisjfish | 0:ef33698150d6 | 55 | infoout.putc(msg[n]); |
lewisjfish | 0:ef33698150d6 | 56 | //,waiting 0.05 seconds after each character |
lewisjfish | 0:ef33698150d6 | 57 | wait(0.05); |
lewisjfish | 0:ef33698150d6 | 58 | } |
lewisjfish | 0:ef33698150d6 | 59 | // |
lewisjfish | 0:ef33698150d6 | 60 | int i=0; |
lewisjfish | 0:ef33698150d6 | 61 | /* |
lewisjfish | 0:ef33698150d6 | 62 | if(rfid.readable()){ |
lewisjfish | 0:ef33698150d6 | 63 | //if we can read a tag, get the tagnumber |
lewisjfish | 0:ef33698150d6 | 64 | tagNumber = rfid.read(); |
lewisjfish | 0:ef33698150d6 | 65 | sprintf(message,"a%d",tagNumber); //turn the tagnumber into a string, string starts with a to identify string as an RFID tagnumber |
lewisjfish | 0:ef33698150d6 | 66 | |
lewisjfish | 0:ef33698150d6 | 67 | |
lewisjfish | 0:ef33698150d6 | 68 | |
lewisjfish | 0:ef33698150d6 | 69 | for(int n =0; n<9 ; n++){ |
lewisjfish | 0:ef33698150d6 | 70 | //send the data to the second mbed(screen) using putc |
lewisjfish | 0:ef33698150d6 | 71 | infoout.putc(message[i]); |
lewisjfish | 0:ef33698150d6 | 72 | i=(i+1)%9; //modulo 9 to ensure only 9 characters are sent |
lewisjfish | 0:ef33698150d6 | 73 | |
lewisjfish | 0:ef33698150d6 | 74 | //,waiting 0.05 seconds after each character |
lewisjfish | 0:ef33698150d6 | 75 | wait(0.05); |
lewisjfish | 0:ef33698150d6 | 76 | } |
lewisjfish | 0:ef33698150d6 | 77 | |
lewisjfish | 0:ef33698150d6 | 78 | if(i==0){ |
lewisjfish | 0:ef33698150d6 | 79 | infoout.putc('A'); //end the message with 'A' to denote the end of an RFID |
lewisjfish | 0:ef33698150d6 | 80 | |
lewisjfish | 0:ef33698150d6 | 81 | wait(0.01); |
lewisjfish | 0:ef33698150d6 | 82 | } |
lewisjfish | 0:ef33698150d6 | 83 | } |
lewisjfish | 0:ef33698150d6 | 84 | |
lewisjfish | 0:ef33698150d6 | 85 | |
lewisjfish | 0:ef33698150d6 | 86 | if(weatherIn.readable()){ |
lewisjfish | 0:ef33698150d6 | 87 | |
lewisjfish | 0:ef33698150d6 | 88 | c=weatherIn.getc(); |
lewisjfish | 0:ef33698150d6 | 89 | infoout.putc(message[i]); |
lewisjfish | 0:ef33698150d6 | 90 | message[i] = c; |
lewisjfish | 0:ef33698150d6 | 91 | i=(i+1)%9; |
lewisjfish | 0:ef33698150d6 | 92 | |
lewisjfish | 0:ef33698150d6 | 93 | wait(0.05); |
lewisjfish | 0:ef33698150d6 | 94 | } |
lewisjfish | 0:ef33698150d6 | 95 | |
lewisjfish | 0:ef33698150d6 | 96 | |
lewisjfish | 0:ef33698150d6 | 97 | |
lewisjfish | 0:ef33698150d6 | 98 | |
lewisjfish | 0:ef33698150d6 | 99 | */ |
lewisjfish | 0:ef33698150d6 | 100 | } |
lewisjfish | 0:ef33698150d6 | 101 | |
lewisjfish | 0:ef33698150d6 | 102 | |
lewisjfish | 0:ef33698150d6 | 103 | } |
lewisjfish | 0:ef33698150d6 | 104 |