Coordinator_1

Dependencies:   ESP8266 mbed nRF24L01P

Committer:
monish
Date:
Wed Jul 06 06:26:16 2016 +0000
Revision:
0:e246677dfb3b
Child:
1:7f3a0c145a08
Coordinator;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
monish 0:e246677dfb3b 1 /*****Multiceiver wireless network with IoT*****/
monish 0:e246677dfb3b 2
monish 0:e246677dfb3b 3 #include "mbed.h"
monish 0:e246677dfb3b 4 #include "nRF24L01P.h"
monish 0:e246677dfb3b 5 #include "ESP8266.h"
monish 0:e246677dfb3b 6
monish 0:e246677dfb3b 7 //Serial Transmission
monish 0:e246677dfb3b 8 Serial pc(USBTX, USBRX); // tx, rx
monish 0:e246677dfb3b 9
monish 0:e246677dfb3b 10 //wifi UART port and baud rate
monish 0:e246677dfb3b 11 ESP8266 wifi(PTE0, PTE1, 115200);
monish 0:e246677dfb3b 12
monish 0:e246677dfb3b 13 //nRF module
monish 0:e246677dfb3b 14 nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTD0, PTD5, PTD4); // mosi, miso, sck, csn, ce, irq
monish 0:e246677dfb3b 15
monish 0:e246677dfb3b 16 //blue for pipe1 and green for pipe0
monish 0:e246677dfb3b 17 DigitalOut GreenLED(LED2);
monish 0:e246677dfb3b 18 DigitalOut RedLED(LED1);
monish 0:e246677dfb3b 19
monish 0:e246677dfb3b 20 //buffers for wifi library
monish 0:e246677dfb3b 21 char snd[255],resp[1000];
monish 0:e246677dfb3b 22 char http_cmd[300], comm[300];
monish 0:e246677dfb3b 23 int timeout = 3000; //timeout for wifi commands
monish 0:e246677dfb3b 24
monish 0:e246677dfb3b 25 char count[1];
monish 0:e246677dfb3b 26 char RxDataCnt_PIPE0, RxDataCnt_PIPE1;
monish 0:e246677dfb3b 27 char temp;
monish 0:e246677dfb3b 28
monish 0:e246677dfb3b 29 //potvalue and ldrvalue
monish 0:e246677dfb3b 30 char pot_val, ldr_val;
monish 0:e246677dfb3b 31 float potval, ldrval;
monish 0:e246677dfb3b 32 float opv=0;
monish 0:e246677dfb3b 33 float olv=0;
monish 0:e246677dfb3b 34
monish 0:e246677dfb3b 35 //Public and private keys for phant
monish 0:e246677dfb3b 36 char* Public_Key = "3I4DFZQSYETVGFM9";
monish 0:e246677dfb3b 37 char* Private_Key = "3CVTRI3P2PMCQI6M";
monish 0:e246677dfb3b 38
monish 0:e246677dfb3b 39 //SSID and password for connection
monish 0:e246677dfb3b 40 #define SSID "RIFT"
monish 0:e246677dfb3b 41 #define PASS "rift2015"
monish 0:e246677dfb3b 42
monish 0:e246677dfb3b 43 //Remote IP
monish 0:e246677dfb3b 44 #define IP "184.106.153.149" //184.106.153.149-ThingsSpeak IP
monish 0:e246677dfb3b 45
monish 0:e246677dfb3b 46 //Wifi init function
monish 0:e246677dfb3b 47 void wifi_initialize(void){
monish 0:e246677dfb3b 48
monish 0:e246677dfb3b 49 pc.printf("******** Resetting wifi module ********\r\n");
monish 0:e246677dfb3b 50 wifi.Reset();
monish 0:e246677dfb3b 51
monish 0:e246677dfb3b 52 //wait for 5 seconds for response, else display no response receiveed
monish 0:e246677dfb3b 53 if (wifi.RcvReply(resp, 5000))
monish 0:e246677dfb3b 54 pc.printf("%s",resp);
monish 0:e246677dfb3b 55 else
monish 0:e246677dfb3b 56 pc.printf("No response");
monish 0:e246677dfb3b 57
monish 0:e246677dfb3b 58 pc.printf("******** Setting Station mode of wifi with AP ********\r\n");
monish 0:e246677dfb3b 59 wifi.SetMode(1); // set transparent mode
monish 0:e246677dfb3b 60 if (wifi.RcvReply(resp, timeout)) //receive a response from ESP
monish 0:e246677dfb3b 61 pc.printf("%s",resp); //Print the response onscreen
monish 0:e246677dfb3b 62 else
monish 0:e246677dfb3b 63 pc.printf("No response while setting mode. \r\n");
monish 0:e246677dfb3b 64
monish 0:e246677dfb3b 65 pc.printf("******** Joining network with SSID and PASS ********\r\n");
monish 0:e246677dfb3b 66 wifi.Join(SSID, PASS);
monish 0:e246677dfb3b 67 if (wifi.RcvReply(resp, timeout))
monish 0:e246677dfb3b 68 pc.printf("%s",resp);
monish 0:e246677dfb3b 69 else
monish 0:e246677dfb3b 70 pc.printf("No response while connecting to network \r\n");
monish 0:e246677dfb3b 71
monish 0:e246677dfb3b 72 pc.printf("******** Getting IP and MAC of module ********\r\n");
monish 0:e246677dfb3b 73 wifi.GetIP(resp);
monish 0:e246677dfb3b 74 if (wifi.RcvReply(resp, timeout))
monish 0:e246677dfb3b 75 pc.printf("%s",resp);
monish 0:e246677dfb3b 76 else
monish 0:e246677dfb3b 77 pc.printf("No response while getting IP \r\n");
monish 0:e246677dfb3b 78
monish 0:e246677dfb3b 79 pc.printf("******** Setting WIFI UART passthrough ********\r\n");
monish 0:e246677dfb3b 80 wifi.setTransparent();
monish 0:e246677dfb3b 81 if (wifi.RcvReply(resp, timeout))
monish 0:e246677dfb3b 82 pc.printf("%s",resp);
monish 0:e246677dfb3b 83 else
monish 0:e246677dfb3b 84 pc.printf("No response while setting wifi passthrough. \r\n");
monish 0:e246677dfb3b 85 wait(1);
monish 0:e246677dfb3b 86
monish 0:e246677dfb3b 87 pc.printf("******** Setting single connection mode ********\r\n");
monish 0:e246677dfb3b 88 wifi.SetSingle();
monish 0:e246677dfb3b 89 wifi.RcvReply(resp, timeout);
monish 0:e246677dfb3b 90 if (wifi.RcvReply(resp, timeout))
monish 0:e246677dfb3b 91 pc.printf("%s",resp);
monish 0:e246677dfb3b 92 else
monish 0:e246677dfb3b 93 pc.printf("No response while setting single connection \r\n");
monish 0:e246677dfb3b 94 wait(1);
monish 0:e246677dfb3b 95 }
monish 0:e246677dfb3b 96
monish 0:e246677dfb3b 97 void wifi_send(void){
monish 0:e246677dfb3b 98
monish 0:e246677dfb3b 99 pc.printf("******** Starting TCP connection on IP and port ********\r\n");
monish 0:e246677dfb3b 100 wifi.startTCPConn(IP, 80); //cipstart
monish 0:e246677dfb3b 101 wifi.RcvReply(resp, timeout);
monish 0:e246677dfb3b 102 if (wifi.RcvReply(resp, timeout))
monish 0:e246677dfb3b 103 pc.printf("%s",resp);
monish 0:e246677dfb3b 104 else
monish 0:e246677dfb3b 105 pc.printf("No response while starting TCP connection \r\n");
monish 0:e246677dfb3b 106 wait(1);
monish 0:e246677dfb3b 107
monish 0:e246677dfb3b 108 //create link
monish 0:e246677dfb3b 109 sprintf(http_cmd,"/update?api_key=%s&field1=%.2f&field2=%.2f",Public_Key,ldrval,potval);
monish 0:e246677dfb3b 110 pc.printf(http_cmd);
monish 0:e246677dfb3b 111
monish 0:e246677dfb3b 112 pc.printf("******** Sending URL to wifi ********\r\n");
monish 0:e246677dfb3b 113 wifi.sendURL(http_cmd, comm); //cipsend and get command
monish 0:e246677dfb3b 114 if (wifi.RcvReply(resp, timeout))
monish 0:e246677dfb3b 115 pc.printf("%s",resp);
monish 0:e246677dfb3b 116 else
monish 0:e246677dfb3b 117 pc.printf("No response while sending URL \r\n");
monish 0:e246677dfb3b 118
monish 0:e246677dfb3b 119 }
monish 0:e246677dfb3b 120
monish 0:e246677dfb3b 121 int main() {
monish 0:e246677dfb3b 122
monish 0:e246677dfb3b 123
monish 0:e246677dfb3b 124 //specifying address same as transmitter for pipe0 and pipe1
monish 0:e246677dfb3b 125 long long RxAddress_PIPE1 = 0xE2E2E2E2E2;
monish 0:e246677dfb3b 126 long long RxAddress_PIPE0 = 0xC2C2C2C2C2;
monish 0:e246677dfb3b 127
monish 0:e246677dfb3b 128 my_nrf24l01p.powerUp();
monish 0:e246677dfb3b 129 my_nrf24l01p.setRfFrequency(2475);
monish 0:e246677dfb3b 130
monish 0:e246677dfb3b 131 //set rx address with default address and for specified pipe
monish 0:e246677dfb3b 132 my_nrf24l01p.setRxAddress(RxAddress_PIPE1, DEFAULT_NRF24L01P_ADDRESS_WIDTH, NRF24L01P_PIPE_P1);
monish 0:e246677dfb3b 133 my_nrf24l01p.setRxAddress(RxAddress_PIPE0, DEFAULT_NRF24L01P_ADDRESS_WIDTH, NRF24L01P_PIPE_P0);
monish 0:e246677dfb3b 134
monish 0:e246677dfb3b 135 // Display the (default) setup of the nRF24L01+ chip
monish 0:e246677dfb3b 136 pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() );
monish 0:e246677dfb3b 137 pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() );
monish 0:e246677dfb3b 138 pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
monish 0:e246677dfb3b 139
monish 0:e246677dfb3b 140 //display rx address for both pipes
monish 0:e246677dfb3b 141 pc.printf( "nRF24L01+ RX Address - PIPE0 : 0x%010llX\r\n", my_nrf24l01p.getRxAddress(NRF24L01P_PIPE_P0) );
monish 0:e246677dfb3b 142 pc.printf( "nRF24L01+ RX Address - PIPE1 : 0x%010llX\r\n", my_nrf24l01p.getRxAddress(NRF24L01P_PIPE_P1) );
monish 0:e246677dfb3b 143 pc.printf( "Wireless Sensor Network - Multiceiver\r\n" );
monish 0:e246677dfb3b 144
monish 0:e246677dfb3b 145 RxDataCnt_PIPE0 = 1;
monish 0:e246677dfb3b 146 RxDataCnt_PIPE1 = 1;
monish 0:e246677dfb3b 147
monish 0:e246677dfb3b 148 //set transfer size explicitly for both pipes
monish 0:e246677dfb3b 149 my_nrf24l01p.setTransferSize(RxDataCnt_PIPE1, NRF24L01P_PIPE_P1);
monish 0:e246677dfb3b 150 my_nrf24l01p.setTransferSize(RxDataCnt_PIPE0, NRF24L01P_PIPE_P0);
monish 0:e246677dfb3b 151
monish 0:e246677dfb3b 152 my_nrf24l01p.setReceiveMode();
monish 0:e246677dfb3b 153 my_nrf24l01p.enable();
monish 0:e246677dfb3b 154
monish 0:e246677dfb3b 155 wifi_initialize();
monish 0:e246677dfb3b 156
monish 0:e246677dfb3b 157 while (1) {
monish 0:e246677dfb3b 158
monish 0:e246677dfb3b 159 //check if data is available in pipe0
monish 0:e246677dfb3b 160 if ( my_nrf24l01p.readable(NRF24L01P_PIPE_P0) ) {
monish 0:e246677dfb3b 161
monish 0:e246677dfb3b 162 // ...read the data into the receive buffer
monish 0:e246677dfb3b 163 temp = my_nrf24l01p.read( NRF24L01P_PIPE_P0, count, RxDataCnt_PIPE0 );
monish 0:e246677dfb3b 164
monish 0:e246677dfb3b 165 pot_val = count[0];
monish 0:e246677dfb3b 166 potval=pot_val/255;
monish 0:e246677dfb3b 167
monish 0:e246677dfb3b 168 pc.printf("Received: %d bytes from PIPE0; POT=%d\r\n",temp, potval);
monish 0:e246677dfb3b 169
monish 0:e246677dfb3b 170 // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
monish 0:e246677dfb3b 171 GreenLED = !GreenLED;
monish 0:e246677dfb3b 172 }
monish 0:e246677dfb3b 173
monish 0:e246677dfb3b 174 //check if data is there in pipe1
monish 0:e246677dfb3b 175 if ( my_nrf24l01p.readable(NRF24L01P_PIPE_P1) ) {
monish 0:e246677dfb3b 176
monish 0:e246677dfb3b 177 // ...read the data into the receive buffer
monish 0:e246677dfb3b 178 temp = my_nrf24l01p.read( NRF24L01P_PIPE_P1, count, RxDataCnt_PIPE1 );
monish 0:e246677dfb3b 179
monish 0:e246677dfb3b 180 ldr_val = count[0];
monish 0:e246677dfb3b 181 ldrval=ldr_val/255;
monish 0:e246677dfb3b 182 pc.printf("Received: %d bytes from PIPE1; LDR=%d\r\n",temp, ldrval);
monish 0:e246677dfb3b 183
monish 0:e246677dfb3b 184 // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
monish 0:e246677dfb3b 185 RedLED = !RedLED;
monish 0:e246677dfb3b 186 }
monish 0:e246677dfb3b 187 if(olv!=ldrval || opv!=potval)
monish 0:e246677dfb3b 188 {
monish 0:e246677dfb3b 189 wifi_send();
monish 0:e246677dfb3b 190 }
monish 0:e246677dfb3b 191 olv=ldrval;
monish 0:e246677dfb3b 192 opv=potval;
monish 0:e246677dfb3b 193 }
monish 0:e246677dfb3b 194 }