this is 2/2 of the project this is the master board which has a wifi module ESP8266, an RF transceiver nrf module , and an LCD screen with an I2C back pack all communicating with the slave board
Dependencies: Group10_master mbed nRF24L01P
main.cpp@0:f3eceaf51647, 2018-04-30 (annotated)
- Committer:
- imomoh
- Date:
- Mon Apr 30 06:19:48 2018 +0000
- Revision:
- 0:f3eceaf51647
new commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
imomoh | 0:f3eceaf51647 | 1 | // Hello World! for the TextLCD |
imomoh | 0:f3eceaf51647 | 2 | |
imomoh | 0:f3eceaf51647 | 3 | #include "mbed.h" |
imomoh | 0:f3eceaf51647 | 4 | #include "TextLCD.h" |
imomoh | 0:f3eceaf51647 | 5 | #include "nRF24L01P.h" |
imomoh | 0:f3eceaf51647 | 6 | |
imomoh | 0:f3eceaf51647 | 7 | |
imomoh | 0:f3eceaf51647 | 8 | |
imomoh | 0:f3eceaf51647 | 9 | |
imomoh | 0:f3eceaf51647 | 10 | //nrf24l01p transiever setup |
imomoh | 0:f3eceaf51647 | 11 | nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTD0, PTC4, PTC12); |
imomoh | 0:f3eceaf51647 | 12 | //nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTC3,PTC2 , PTA2); //Slave board |
imomoh | 0:f3eceaf51647 | 13 | DigitalOut myled1(LED_GREEN); |
imomoh | 0:f3eceaf51647 | 14 | DigitalOut myled2(LED_RED); |
imomoh | 0:f3eceaf51647 | 15 | #define TRANSFER_SIZE 4 |
imomoh | 0:f3eceaf51647 | 16 | |
imomoh | 0:f3eceaf51647 | 17 | // Host PC Communication channels |
imomoh | 0:f3eceaf51647 | 18 | Serial pc(USBTX, USBRX); // tx, rx |
imomoh | 0:f3eceaf51647 | 19 | //LCD SETUP |
imomoh | 0:f3eceaf51647 | 20 | // I2C Communication |
imomoh | 0:f3eceaf51647 | 21 | I2C i2c_lcd(PTE25,PTE24); // SDA, SCL |
imomoh | 0:f3eceaf51647 | 22 | //I2C Portexpander PCF8574 |
imomoh | 0:f3eceaf51647 | 23 | TextLCD_I2C lcd(&i2c_lcd, 0x7e , TextLCD::LCD16x2, TextLCD::WS0010); // I2C bus, PCF8574 addr, LCD Type, Ctrl Type |
imomoh | 0:f3eceaf51647 | 24 | |
imomoh | 0:f3eceaf51647 | 25 | |
imomoh | 0:f3eceaf51647 | 26 | //ESP8266 SETUP |
imomoh | 0:f3eceaf51647 | 27 | Serial esp(D1, D0); // tx, rx |
imomoh | 0:f3eceaf51647 | 28 | DigitalOut reset(D3); |
imomoh | 0:f3eceaf51647 | 29 | Timer t; |
imomoh | 0:f3eceaf51647 | 30 | int count,ended,timeout; |
imomoh | 0:f3eceaf51647 | 31 | char buf[1024]; |
imomoh | 0:f3eceaf51647 | 32 | char snd[255]; |
imomoh | 0:f3eceaf51647 | 33 | char ssid[32] = "IU DeviceNet"; // enter WiFi router ssid inside the quotes |
imomoh | 0:f3eceaf51647 | 34 | char pwd [32] = ""; // enter WiFi router password inside the quotes |
imomoh | 0:f3eceaf51647 | 35 | |
imomoh | 0:f3eceaf51647 | 36 | |
imomoh | 0:f3eceaf51647 | 37 | |
imomoh | 0:f3eceaf51647 | 38 | void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(), ESPconnection(),nrfmodule(); |
imomoh | 0:f3eceaf51647 | 39 | |
imomoh | 0:f3eceaf51647 | 40 | |
imomoh | 0:f3eceaf51647 | 41 | |
imomoh | 0:f3eceaf51647 | 42 | int main() { |
imomoh | 0:f3eceaf51647 | 43 | |
imomoh | 0:f3eceaf51647 | 44 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
imomoh | 0:f3eceaf51647 | 45 | int txDataCnt = 0; |
imomoh | 0:f3eceaf51647 | 46 | int rxDataCnt = 0; |
imomoh | 0:f3eceaf51647 | 47 | |
imomoh | 0:f3eceaf51647 | 48 | my_nrf24l01p.powerUp(); |
imomoh | 0:f3eceaf51647 | 49 | |
imomoh | 0:f3eceaf51647 | 50 | lcd.cls(); |
imomoh | 0:f3eceaf51647 | 51 | lcd.locate(3,0); |
imomoh | 0:f3eceaf51647 | 52 | lcd.printf("connecting"); |
imomoh | 0:f3eceaf51647 | 53 | lcd.locate(3,1); |
imomoh | 0:f3eceaf51647 | 54 | lcd.printf("to wifi..."); |
imomoh | 0:f3eceaf51647 | 55 | |
imomoh | 0:f3eceaf51647 | 56 | |
imomoh | 0:f3eceaf51647 | 57 | // Show cursor as blinking character |
imomoh | 0:f3eceaf51647 | 58 | lcd.setCursor(TextLCD::CurOff_BlkOn); |
imomoh | 0:f3eceaf51647 | 59 | lcd.setBacklight(TextLCD::LightOn); |
imomoh | 0:f3eceaf51647 | 60 | |
imomoh | 0:f3eceaf51647 | 61 | |
imomoh | 0:f3eceaf51647 | 62 | pc.baud(115200); |
imomoh | 0:f3eceaf51647 | 63 | wait(0.5); |
imomoh | 0:f3eceaf51647 | 64 | esp.baud(115200);// change this to the new ESP8266 baudrate if it is changed at any time. |
imomoh | 0:f3eceaf51647 | 65 | |
imomoh | 0:f3eceaf51647 | 66 | nrfmodule(); |
imomoh | 0:f3eceaf51647 | 67 | ESPconnection(); |
imomoh | 0:f3eceaf51647 | 68 | |
imomoh | 0:f3eceaf51647 | 69 | lcd.cls(); |
imomoh | 0:f3eceaf51647 | 70 | lcd.locate(3,0); |
imomoh | 0:f3eceaf51647 | 71 | lcd.printf("SYSTEM ARMED"); |
imomoh | 0:f3eceaf51647 | 72 | pc.printf("\nwaiting to be triggered\n"); |
imomoh | 0:f3eceaf51647 | 73 | |
imomoh | 0:f3eceaf51647 | 74 | while(1){ |
imomoh | 0:f3eceaf51647 | 75 | |
imomoh | 0:f3eceaf51647 | 76 | wait(1); |
imomoh | 0:f3eceaf51647 | 77 | ////this is for the recieving part of the code part of the code |
imomoh | 0:f3eceaf51647 | 78 | // If we've received anything in the nRF24L01+... |
imomoh | 0:f3eceaf51647 | 79 | |
imomoh | 0:f3eceaf51647 | 80 | if ( my_nrf24l01p.readable() ) { |
imomoh | 0:f3eceaf51647 | 81 | lcd.cls(); |
imomoh | 0:f3eceaf51647 | 82 | pc.printf("message recieved\n\r"); |
imomoh | 0:f3eceaf51647 | 83 | // ...read the data into the receive buffer |
imomoh | 0:f3eceaf51647 | 84 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); |
imomoh | 0:f3eceaf51647 | 85 | |
imomoh | 0:f3eceaf51647 | 86 | // Display the receive buffer contents via the host serial link |
imomoh | 0:f3eceaf51647 | 87 | for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { |
imomoh | 0:f3eceaf51647 | 88 | |
imomoh | 0:f3eceaf51647 | 89 | pc.putc( rxData[i] ); |
imomoh | 0:f3eceaf51647 | 90 | pc.printf("data to recieve\r"); |
imomoh | 0:f3eceaf51647 | 91 | if(rxData[i]=='w'){ |
imomoh | 0:f3eceaf51647 | 92 | |
imomoh | 0:f3eceaf51647 | 93 | lcd.locate(3,0); |
imomoh | 0:f3eceaf51647 | 94 | lcd.printf ("WARNING!!!"); |
imomoh | 0:f3eceaf51647 | 95 | lcd.locate(0,1); |
imomoh | 0:f3eceaf51647 | 96 | lcd.printf ("ALARM TRIGGERED"); |
imomoh | 0:f3eceaf51647 | 97 | }else if(rxData){ |
imomoh | 0:f3eceaf51647 | 98 | lcd.locate(3,0); |
imomoh | 0:f3eceaf51647 | 99 | lcd.printf ("SAFE"); |
imomoh | 0:f3eceaf51647 | 100 | lcd.locate(3,1); |
imomoh | 0:f3eceaf51647 | 101 | lcd.printf ("SYSTEM ARMED"); |
imomoh | 0:f3eceaf51647 | 102 | } |
imomoh | 0:f3eceaf51647 | 103 | } |
imomoh | 0:f3eceaf51647 | 104 | |
imomoh | 0:f3eceaf51647 | 105 | // Toggle LED2 (to help debug nRF24L01+ -> Host communication) |
imomoh | 0:f3eceaf51647 | 106 | myled2 = !myled2; |
imomoh | 0:f3eceaf51647 | 107 | } |
imomoh | 0:f3eceaf51647 | 108 | |
imomoh | 0:f3eceaf51647 | 109 | |
imomoh | 0:f3eceaf51647 | 110 | } |
imomoh | 0:f3eceaf51647 | 111 | |
imomoh | 0:f3eceaf51647 | 112 | |
imomoh | 0:f3eceaf51647 | 113 | |
imomoh | 0:f3eceaf51647 | 114 | } |
imomoh | 0:f3eceaf51647 | 115 | |
imomoh | 0:f3eceaf51647 | 116 | //FUNCTIONS |
imomoh | 0:f3eceaf51647 | 117 | |
imomoh | 0:f3eceaf51647 | 118 | void ESPconnection() |
imomoh | 0:f3eceaf51647 | 119 | { |
imomoh | 0:f3eceaf51647 | 120 | pc.printf("\n---------- Get IP and MAC ----------\r\n"); |
imomoh | 0:f3eceaf51647 | 121 | strcpy(snd, "AT+CIFSR\r\n"); |
imomoh | 0:f3eceaf51647 | 122 | SendCMD(); |
imomoh | 0:f3eceaf51647 | 123 | timeout=10; |
imomoh | 0:f3eceaf51647 | 124 | getreply(); |
imomoh | 0:f3eceaf51647 | 125 | pc.printf(buf); |
imomoh | 0:f3eceaf51647 | 126 | wait(2); |
imomoh | 0:f3eceaf51647 | 127 | |
imomoh | 0:f3eceaf51647 | 128 | |
imomoh | 0:f3eceaf51647 | 129 | pc.printf("\n---------- Get Connection Status ----------\r\n"); |
imomoh | 0:f3eceaf51647 | 130 | strcpy(snd, "AT+CIPSTATUS\r\n"); |
imomoh | 0:f3eceaf51647 | 131 | SendCMD(); |
imomoh | 0:f3eceaf51647 | 132 | timeout=5; |
imomoh | 0:f3eceaf51647 | 133 | getreply(); |
imomoh | 0:f3eceaf51647 | 134 | pc.printf(buf); |
imomoh | 0:f3eceaf51647 | 135 | lcd.cls(); |
imomoh | 0:f3eceaf51647 | 136 | lcd.locate(3,0); |
imomoh | 0:f3eceaf51647 | 137 | lcd.printf("connected"); |
imomoh | 0:f3eceaf51647 | 138 | wait(1); |
imomoh | 0:f3eceaf51647 | 139 | |
imomoh | 0:f3eceaf51647 | 140 | |
imomoh | 0:f3eceaf51647 | 141 | } |
imomoh | 0:f3eceaf51647 | 142 | |
imomoh | 0:f3eceaf51647 | 143 | |
imomoh | 0:f3eceaf51647 | 144 | void ESPsetbaudrate() |
imomoh | 0:f3eceaf51647 | 145 | { |
imomoh | 0:f3eceaf51647 | 146 | strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate |
imomoh | 0:f3eceaf51647 | 147 | SendCMD(); |
imomoh | 0:f3eceaf51647 | 148 | } |
imomoh | 0:f3eceaf51647 | 149 | |
imomoh | 0:f3eceaf51647 | 150 | |
imomoh | 0:f3eceaf51647 | 151 | void SendCMD() |
imomoh | 0:f3eceaf51647 | 152 | { |
imomoh | 0:f3eceaf51647 | 153 | esp.printf("%s", snd); |
imomoh | 0:f3eceaf51647 | 154 | } |
imomoh | 0:f3eceaf51647 | 155 | |
imomoh | 0:f3eceaf51647 | 156 | void getreply() |
imomoh | 0:f3eceaf51647 | 157 | { |
imomoh | 0:f3eceaf51647 | 158 | memset(buf, '\0', sizeof(buf)); |
imomoh | 0:f3eceaf51647 | 159 | t.start(); |
imomoh | 0:f3eceaf51647 | 160 | ended=0; |
imomoh | 0:f3eceaf51647 | 161 | count=0; |
imomoh | 0:f3eceaf51647 | 162 | while(!ended) { |
imomoh | 0:f3eceaf51647 | 163 | if(esp.readable()) { |
imomoh | 0:f3eceaf51647 | 164 | buf[count] = esp.getc(); |
imomoh | 0:f3eceaf51647 | 165 | count++; |
imomoh | 0:f3eceaf51647 | 166 | } |
imomoh | 0:f3eceaf51647 | 167 | if(t.read() > timeout) { |
imomoh | 0:f3eceaf51647 | 168 | ended = 1; |
imomoh | 0:f3eceaf51647 | 169 | t.stop(); |
imomoh | 0:f3eceaf51647 | 170 | t.reset(); |
imomoh | 0:f3eceaf51647 | 171 | } |
imomoh | 0:f3eceaf51647 | 172 | } |
imomoh | 0:f3eceaf51647 | 173 | } |
imomoh | 0:f3eceaf51647 | 174 | void nrfmodule(){ |
imomoh | 0:f3eceaf51647 | 175 | |
imomoh | 0:f3eceaf51647 | 176 | // Display the (default) setup of the nRF24L01+ chip |
imomoh | 0:f3eceaf51647 | 177 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
imomoh | 0:f3eceaf51647 | 178 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
imomoh | 0:f3eceaf51647 | 179 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
imomoh | 0:f3eceaf51647 | 180 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
imomoh | 0:f3eceaf51647 | 181 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
imomoh | 0:f3eceaf51647 | 182 | |
imomoh | 0:f3eceaf51647 | 183 | pc.printf( "nRF24L01 module has been set up" ); |
imomoh | 0:f3eceaf51647 | 184 | |
imomoh | 0:f3eceaf51647 | 185 | |
imomoh | 0:f3eceaf51647 | 186 | my_nrf24l01p.setReceiveMode(); |
imomoh | 0:f3eceaf51647 | 187 | my_nrf24l01p.enable(); |
imomoh | 0:f3eceaf51647 | 188 | |
imomoh | 0:f3eceaf51647 | 189 | |
imomoh | 0:f3eceaf51647 | 190 | |
imomoh | 0:f3eceaf51647 | 191 | } |