c

Dependencies:   mbed nRF24L01P

Committer:
bspk96
Date:
Thu Dec 15 08:05:18 2016 +0000
Revision:
0:2ae429a5b336
Check

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bspk96 0:2ae429a5b336 1 #include "mbed.h"
bspk96 0:2ae429a5b336 2 #include "ESP8266.h"
bspk96 0:2ae429a5b336 3 #include "nRF24L01P.h"
bspk96 0:2ae429a5b336 4
bspk96 0:2ae429a5b336 5 Serial pc(USBTX,USBRX);
bspk96 0:2ae429a5b336 6 //nRF24L01P Intialisation
bspk96 0:2ae429a5b336 7 nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTD0, PTD5, PTD4); // mosi, miso, sck, csn, ce, irq
bspk96 0:2ae429a5b336 8 DigitalOut gled(LED2);
bspk96 0:2ae429a5b336 9 //PwmOut RedLed(LED1);
bspk96 0:2ae429a5b336 10
bspk96 0:2ae429a5b336 11
bspk96 0:2ae429a5b336 12 //wifi UART port and baud rate
bspk96 0:2ae429a5b336 13 ESP8266 wifi(PTE0, PTE1, 115200);
bspk96 0:2ae429a5b336 14
bspk96 0:2ae429a5b336 15 //buffers for wifi library
bspk96 0:2ae429a5b336 16 char snd[255],resp[1000];
bspk96 0:2ae429a5b336 17 char http_cmd[300], comm[300];
bspk96 0:2ae429a5b336 18
bspk96 0:2ae429a5b336 19 int timeout = 3000; //timeout for wifi commands
bspk96 0:2ae429a5b336 20
bspk96 0:2ae429a5b336 21 //SSID and password for connection
bspk96 0:2ae429a5b336 22 #define SSID "Major"//Change this as well
bspk96 0:2ae429a5b336 23 #define PASS "chikku123"//Also the password
bspk96 0:2ae429a5b336 24
bspk96 0:2ae429a5b336 25 //Remote IP
bspk96 0:2ae429a5b336 26 #define IP "192.168.2.3"//IP of your computer on the network, so as to connect to phant thru the computer
bspk96 0:2ae429a5b336 27
bspk96 0:2ae429a5b336 28 //Define global variables
bspk96 0:2ae429a5b336 29 char count[2];
bspk96 0:2ae429a5b336 30 char RxDataCnt;
bspk96 0:2ae429a5b336 31 char temp;
bspk96 0:2ae429a5b336 32 float value,value1;
bspk96 0:2ae429a5b336 33
bspk96 0:2ae429a5b336 34 //Public and private keys for phant
bspk96 0:2ae429a5b336 35 char* Public_Key = "4J1LAGyxXOF6MN8r9G5x";
bspk96 0:2ae429a5b336 36 char* Private_Key = "4J1LAGyxXOF6MN8r9G5x";
bspk96 0:2ae429a5b336 37
bspk96 0:2ae429a5b336 38 //Wifi init function
bspk96 0:2ae429a5b336 39 void wifi_initialize(void){
bspk96 0:2ae429a5b336 40
bspk96 0:2ae429a5b336 41 pc.printf("******** Resetting wifi module ********\r\n");
bspk96 0:2ae429a5b336 42 wifi.Reset();
bspk96 0:2ae429a5b336 43
bspk96 0:2ae429a5b336 44 //wait for 5 seconds for response, else display no response receiveed
bspk96 0:2ae429a5b336 45 if (wifi.RcvReply(resp, 5000))
bspk96 0:2ae429a5b336 46 pc.printf("%s",resp);
bspk96 0:2ae429a5b336 47 else
bspk96 0:2ae429a5b336 48 pc.printf("No response");
bspk96 0:2ae429a5b336 49
bspk96 0:2ae429a5b336 50 pc.printf("******** Setting Station mode of wifi with AP ********\r\n");
bspk96 0:2ae429a5b336 51 wifi.SetMode(1); // set transparent mode
bspk96 0:2ae429a5b336 52 if (wifi.RcvReply(resp, timeout)) //receive a response from ESP
bspk96 0:2ae429a5b336 53 pc.printf("%s",resp); //Print the response onscreen
bspk96 0:2ae429a5b336 54 else
bspk96 0:2ae429a5b336 55 pc.printf("No response while setting mode. \r\n");
bspk96 0:2ae429a5b336 56
bspk96 0:2ae429a5b336 57 pc.printf("******** Joining network with SSID and PASS ********\r\n");
bspk96 0:2ae429a5b336 58 wifi.Join(SSID, PASS);
bspk96 0:2ae429a5b336 59 if (wifi.RcvReply(resp, timeout))
bspk96 0:2ae429a5b336 60 pc.printf("%s",resp);
bspk96 0:2ae429a5b336 61 else
bspk96 0:2ae429a5b336 62 pc.printf("No response while connecting to network \r\n");
bspk96 0:2ae429a5b336 63
bspk96 0:2ae429a5b336 64 pc.printf("******** Getting IP and MAC of module ********\r\n");
bspk96 0:2ae429a5b336 65 wifi.GetIP(resp);
bspk96 0:2ae429a5b336 66 if (wifi.RcvReply(resp, timeout))
bspk96 0:2ae429a5b336 67 pc.printf("%s",resp);
bspk96 0:2ae429a5b336 68 else
bspk96 0:2ae429a5b336 69 pc.printf("No response while getting IP \r\n");
bspk96 0:2ae429a5b336 70
bspk96 0:2ae429a5b336 71 }
bspk96 0:2ae429a5b336 72
bspk96 0:2ae429a5b336 73 void wifi_send(void){
bspk96 0:2ae429a5b336 74
bspk96 0:2ae429a5b336 75 pc.printf("******** Setting WIFI UART passthrough ********\r\n");
bspk96 0:2ae429a5b336 76 wifi.setTransparent();
bspk96 0:2ae429a5b336 77 if (wifi.RcvReply(resp, timeout))
bspk96 0:2ae429a5b336 78 pc.printf("%s",resp);
bspk96 0:2ae429a5b336 79 else
bspk96 0:2ae429a5b336 80 pc.printf("No response while setting wifi passthrough. \r\n");
bspk96 0:2ae429a5b336 81 wait(1);
bspk96 0:2ae429a5b336 82
bspk96 0:2ae429a5b336 83 pc.printf("******** Setting single connection mode ********\r\n");
bspk96 0:2ae429a5b336 84 wifi.SetSingle();
bspk96 0:2ae429a5b336 85 wifi.RcvReply(resp, timeout);
bspk96 0:2ae429a5b336 86 if (wifi.RcvReply(resp, timeout))
bspk96 0:2ae429a5b336 87 pc.printf("%s",resp);
bspk96 0:2ae429a5b336 88 else
bspk96 0:2ae429a5b336 89 pc.printf("No response while setting single connection \r\n");
bspk96 0:2ae429a5b336 90 wait(1);
bspk96 0:2ae429a5b336 91
bspk96 0:2ae429a5b336 92 pc.printf("******** Starting TCP connection on IP and port ********\r\n");
bspk96 0:2ae429a5b336 93 wifi.startTCPConn(IP, 8080); //cipstart
bspk96 0:2ae429a5b336 94 wifi.RcvReply(resp, timeout);
bspk96 0:2ae429a5b336 95 if (wifi.RcvReply(resp, timeout))
bspk96 0:2ae429a5b336 96 pc.printf("%s",resp);
bspk96 0:2ae429a5b336 97 else
bspk96 0:2ae429a5b336 98 pc.printf("No response while starting TCP connection \r\n");
bspk96 0:2ae429a5b336 99 wait(1);
bspk96 0:2ae429a5b336 100
bspk96 0:2ae429a5b336 101 //create link
bspk96 0:2ae429a5b336 102 sprintf(http_cmd,"/input/%s?private_key=%s&pot=%.2f&ldr=%.2f",Public_Key,Private_Key,value,value1);
bspk96 0:2ae429a5b336 103
bspk96 0:2ae429a5b336 104 pc.printf("******** Sending URL to wifi ********\r\n");
bspk96 0:2ae429a5b336 105 wifi.sendURL(http_cmd, comm); //cipsend and get command
bspk96 0:2ae429a5b336 106 if (wifi.RcvReply(resp, timeout))
bspk96 0:2ae429a5b336 107 pc.printf("%s",resp);
bspk96 0:2ae429a5b336 108 else
bspk96 0:2ae429a5b336 109 pc.printf("No response while sending URL \r\n");
bspk96 0:2ae429a5b336 110
bspk96 0:2ae429a5b336 111 //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
bspk96 0:2ae429a5b336 112 //wifi.RcvReply(resp, timeout);
bspk96 0:2ae429a5b336 113 //pc.printf("%s", resp);
bspk96 0:2ae429a5b336 114 }
bspk96 0:2ae429a5b336 115
bspk96 0:2ae429a5b336 116 int main ()
bspk96 0:2ae429a5b336 117 {
bspk96 0:2ae429a5b336 118 float tmp[2];
bspk96 0:2ae429a5b336 119 //Intializing the values
bspk96 0:2ae429a5b336 120 count[0] = 0x01;
bspk96 0:2ae429a5b336 121 count[1] = 0x01;
bspk96 0:2ae429a5b336 122
bspk96 0:2ae429a5b336 123 value = 0x01;
bspk96 0:2ae429a5b336 124 value1 = 0x01;
bspk96 0:2ae429a5b336 125
bspk96 0:2ae429a5b336 126 //Intialise WiFi Module
bspk96 0:2ae429a5b336 127 wifi_initialize();
bspk96 0:2ae429a5b336 128
bspk96 0:2ae429a5b336 129 //Intialise nRF module
bspk96 0:2ae429a5b336 130 my_nrf24l01p.powerUp();
bspk96 0:2ae429a5b336 131 my_nrf24l01p.setRfFrequency(2440);
bspk96 0:2ae429a5b336 132
bspk96 0:2ae429a5b336 133 // Display the (default) setup of the nRF24L01+ chip
bspk96 0:2ae429a5b336 134 pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() );
bspk96 0:2ae429a5b336 135 pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() );
bspk96 0:2ae429a5b336 136 pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
bspk96 0:2ae429a5b336 137 pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
bspk96 0:2ae429a5b336 138 pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
bspk96 0:2ae429a5b336 139
bspk96 0:2ae429a5b336 140 pc.printf( "Recieveing data from several sensors\r\n" );
bspk96 0:2ae429a5b336 141
bspk96 0:2ae429a5b336 142 RxDataCnt = 2;
bspk96 0:2ae429a5b336 143 my_nrf24l01p.setTransferSize( RxDataCnt );
bspk96 0:2ae429a5b336 144
bspk96 0:2ae429a5b336 145 my_nrf24l01p.setReceiveMode();
bspk96 0:2ae429a5b336 146 my_nrf24l01p.enable();
bspk96 0:2ae429a5b336 147
bspk96 0:2ae429a5b336 148 while (1)
bspk96 0:2ae429a5b336 149 {
bspk96 0:2ae429a5b336 150 if(my_nrf24l01p.readable())
bspk96 0:2ae429a5b336 151 {
bspk96 0:2ae429a5b336 152 //Read data into the recieve buffer
bspk96 0:2ae429a5b336 153 temp = my_nrf24l01p.read(NRF24L01P_PIPE_P0,count,RxDataCnt);
bspk96 0:2ae429a5b336 154 pc.printf("cnt %d = %d %d \r \n",temp,count[0],count[1]);
bspk96 0:2ae429a5b336 155 tmp[0] = count[0]/255;
bspk96 0:2ae429a5b336 156 tmp[1] = count[1]/255;
bspk96 0:2ae429a5b336 157
bspk96 0:2ae429a5b336 158 value = 3.3*tmp[0];
bspk96 0:2ae429a5b336 159 value1 = 3.3*tmp[1];
bspk96 0:2ae429a5b336 160
bspk96 0:2ae429a5b336 161 pc.printf("Pot = %dmV LDR = %dmV \r \n",value,value1);
bspk96 0:2ae429a5b336 162
bspk96 0:2ae429a5b336 163 // Togle LED2 (to help debug nRF24L01+ -> Host communication)
bspk96 0:2ae429a5b336 164 gled = !gled;
bspk96 0:2ae429a5b336 165 wait_ms(10);
bspk96 0:2ae429a5b336 166 }
bspk96 0:2ae429a5b336 167 //Send the data over wifi
bspk96 0:2ae429a5b336 168 wifi_send();
bspk96 0:2ae429a5b336 169 wait(1);
bspk96 0:2ae429a5b336 170 }
bspk96 0:2ae429a5b336 171 }