Santosh Pawan Kumar Bodapati
/
Major_Rx_Phant
c
Revision 0:2ae429a5b336, committed 2016-12-15
- Comitter:
- bspk96
- Date:
- Thu Dec 15 08:05:18 2016 +0000
- Commit message:
- Check
Changed in this revision
diff -r 000000000000 -r 2ae429a5b336 ESP8266/ESP8266.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP8266/ESP8266.cpp Thu Dec 15 08:05:18 2016 +0000 @@ -0,0 +1,202 @@ +#include "ESP8266.h" +#define HTTPCMD "GET " +#define protocol " HTTP/1.0\n\n" + + +// Constructor +ESP8266::ESP8266(PinName tx, PinName rx, int br) : comm(tx, rx) { + comm.baud(br); +} + +// Destructor +ESP8266::~ESP8266() { } + +// Add <CR> + <LF> at the end of the string +void ESP8266::AddEOL(char * s) { + char k; + k = strlen(s); // Finds position of NULL character + s[k] = 0x0D; // switch NULL for <CR> + s[k + 1] = 0x0A; // Add <LF> + s[k + 2] = 0; // Add NULL at the end +} + +// Add one ASCII character at the end of the string +void ESP8266::AddChar(char * s, char c) { + char k; + k = strlen(s); + s[k] = c; + s[k + 1] = 0; +} + +// Converts integer number to null-terminated string +void ESP8266::itoa(int n, char * s) { + char k = 0; + char r[11]; + + if(n == 0) { + s[0] = '0'; + s[1] = 0; + } else { + while(n != 0) { + r[k]= (n % 10) + '0'; + n = n / 10; + k++; + } + while(k > 0) { + s[n] = r[k - 1] + '0'; + n++; + k--; + } + s[n] = 0; + } +} + +// Sends command to ESP8266. Receives the command string +void ESP8266::SendCMD(char * s) { + AddEOL(s); + comm.printf("%s", s); +} + +// Resets the ESP8266 +void ESP8266::Reset(void) { + char rs[10]; + strcpy(rs, "AT+RST"); + SendCMD(rs); +} + +// Receive reply until no character is received after a given timeout in miliseconds +bool ESP8266::RcvReply(char * r, int to) { + Timer t; + bool ended = 0; + char c; + + strcpy(r, ""); + t.start(); + while(!ended) { + if(comm.readable()) { + c = comm.getc(); + AddChar(r, c); + t.start(); + } + if(t.read_ms() > to) { + ended = 1; + } + } + AddChar(r, 0x00); + return ended; +} + +// Gets the AP list. Parameter: the string to receive the list +void ESP8266::GetList(char * l) { + char rs[15]; + strcpy(rs, "AT+CWLAP"); + SendCMD(rs); + RcvReply(l, 5000); // Needs big timeout because it takes long to start replying +} + +// Joins a Wifi AP. Parameters: SSID and Password (strings) +void ESP8266::Join(char * id, char * pwd) { + char cmd[255]; + strcpy(cmd, "AT+CWJAP="); + AddChar(cmd, 0x22); + strcat(cmd, id); + AddChar(cmd, 0x22); + AddChar(cmd, 0x2C); + AddChar(cmd, 0x22); + strcat(cmd, pwd); + AddChar(cmd, 0x22); + SendCMD(cmd); +} + +// Gets ESP IP. Parameter: string to contain IP +void ESP8266::GetIP(char * ip) { + char cmd[15]; + strcpy(cmd, "AT+CIFSR"); + SendCMD(cmd); + RcvReply(ip, 2000); +} + +//Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both +void ESP8266::SetMode(char mode) { + char cmd[15]; + strcpy(cmd, "AT+CWMODE="); + mode = mode + 0x30; // Converts number into corresponding ASCII character + AddChar(cmd, mode); // Completes command string + SendCMD(cmd); +} + +// Quits the AP +void ESP8266::Quit(void) { + char cmd[15]; + strcpy(cmd, "AT+CWQAP"); + SendCMD(cmd); +} + +// Sets single connection +void ESP8266::SetSingle(void) { + char cmd[15]; + strcpy(cmd, "AT+CIPMUX=0"); + SendCMD(cmd); +} + +// Sets multiple connection +void ESP8266::SetMultiple(void) { + char rs[15]; + strcpy(rs, "AT+CIPMUX=1"); + SendCMD(rs); +} + +// Gets connection status. Parameter: string to contain status +void ESP8266::GetConnStatus(char * st) { + char cmd[15]; + strcpy(cmd, "AT+CIPSTATUS"); + SendCMD(cmd); + RcvReply(st, 2000); +} + +// Starts server mode. Parameter: port to be used +void ESP8266::StartServerMode(int port) { + char rs[25]; + char t[4]; + strcpy(rs, "AT+CIPSERVER=1,"); + itoa(port, t); + strcat(rs, t); + SendCMD(rs); +} + +// Close server mode. +void ESP8266::CloseServerMode(void) { + char rs[20]; + strcpy(rs, "AT+CIPSERVER=0"); + SendCMD(rs); +} + +void ESP8266::setTransparent(void){ + char rs[20]; + strcpy(rs, "AT+CIPMODE=0"); + SendCMD(rs); +} + +void ESP8266::startTCPConn(char *IP, int port){ + char rs[100]; + sprintf(rs, "AT+CIPSTART=\"TCP\",\"%s\",%d", IP, port); + SendCMD(rs); +} + +void ESP8266::sendURL(char *URL, char *command){ + char url[300], snd[300], http_cmd[300]; + + strcpy(http_cmd, HTTPCMD); + + strcat(http_cmd, URL); + strcat(http_cmd, protocol); + + strcpy(url, http_cmd); + sprintf(snd,"AT+CIPSENDEX=%d",strlen(url)); + strcpy(command, url); + SendCMD(snd); + wait(3); + SendCMD(url); +} + + \ No newline at end of file
diff -r 000000000000 -r 2ae429a5b336 ESP8266/ESP8266.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP8266/ESP8266.h Thu Dec 15 08:05:18 2016 +0000 @@ -0,0 +1,70 @@ +#ifndef ESP8266_H +#define ESP8266_H + +#include <string> +#include "mbed.h" + +class ESP8266 +{ +public: +/** + * ESP8266 constructor + * + * @param tx TX pin + * @param rx RX pin + * @param br Baud Rate + */ + ESP8266(PinName tx, PinName rx, int br); + + /** + * ESP8266 destructor + */ + ~ESP8266(); + +void SendCMD(char * s); +void Reset(void); +bool RcvReply(char * r, int to); +void GetList(char * l); +void Join(char * id, char * pwd); +void GetIP(char * ip); +void SetMode(char mode); +void Quit(void); +void SetSingle(void); +void SetMultiple(void); +void GetConnStatus(char * st); +void StartServerMode(int port); +void CloseServerMode(void); +void setTransparent(void); +void startTCPConn(char * IP, int port); +void sendURL(char *URL, char *command); + +private: +Serial comm; +void AddEOL(char * s); +void AddChar(char * s, char c); +void itoa(int c, char s[]); + +}; + +#endif +/* + COMMAND TABLE + Basic: + AT: Just to generate "OK" reply + Wifi: + AT+RST: restart the module + AT+CWMODE: define wifi mode; AT+CWMODE=<mode> 1= Sta, 2= AP, 3=both; Inquiry: AT+CWMODE? or AT+CWMODE=? + AT+CWJAP: join the AP wifi; AT+ CWJAP =<ssid>,< pwd > - ssid = ssid, pwd = wifi password, both between quotes; Inquiry: AT+ CWJAP? + AT+CWLAP: list the AP wifi + AT+CWQAP: quit the AP wifi; Inquiry: AT+CWQAP=? + * AT+CWSAP: set the parameters of AP; AT+CWSAP= <ssid>,<pwd>,<chl>,<ecn> - ssid, pwd, chl = channel, ecn = encryption; Inquiry: AT+CWJAP? + TCP/IP: + AT+CIPSTATUS: get the connection status + * AT+CIPSTART: set up TCP or UDP connection 1)single connection (+CIPMUX=0) AT+CIPSTART= <type>,<addr>,<port>; 2) multiple connection (+CIPMUX=1) AT+CIPSTART= <id><type>,<addr>, <port> - id = 0-4, type = TCP/UDP, addr = IP address, port= port; Inquiry: AT+CIPSTART=? + * AT+CIPSEND: send data; 1)single connection(+CIPMUX=0) AT+CIPSEND=<length>; 2) multiple connection (+CIPMUX=1) AT+CIPSEND= <id>,<length>; Inquiry: AT+CIPSEND=? + * AT+CIPCLOSE: close TCP or UDP connection; AT+CIPCLOSE=<id> or AT+CIPCLOSE; Inquiry: AT+CIPCLOSE=? + AT+CIFSR: Get IP address; Inquiry: AT+ CIFSR=? + AT+CIPMUX: set mutiple connection; AT+ CIPMUX=<mode> - 0 for single connection 1 for mutiple connection; Inquiry: AT+CIPMUX? + AT+CIPSERVER: set as server; AT+ CIPSERVER= <mode>[,<port> ] - mode 0 to close server mode, mode 1 to open; port = port; Inquiry: AT+CIFSR=? + * +IPD: received data +*/ \ No newline at end of file
diff -r 000000000000 -r 2ae429a5b336 Rx_Phant_Rx.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Rx_Phant_Rx.cpp Thu Dec 15 08:05:18 2016 +0000 @@ -0,0 +1,171 @@ +#include "mbed.h" +#include "ESP8266.h" +#include "nRF24L01P.h" + +Serial pc(USBTX,USBRX); +//nRF24L01P Intialisation +nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTD0, PTD5, PTD4); // mosi, miso, sck, csn, ce, irq +DigitalOut gled(LED2); +//PwmOut RedLed(LED1); + + +//wifi UART port and baud rate +ESP8266 wifi(PTE0, PTE1, 115200); + +//buffers for wifi library +char snd[255],resp[1000]; +char http_cmd[300], comm[300]; + +int timeout = 3000; //timeout for wifi commands + +//SSID and password for connection +#define SSID "Major"//Change this as well +#define PASS "chikku123"//Also the password + +//Remote IP +#define IP "192.168.2.3"//IP of your computer on the network, so as to connect to phant thru the computer + +//Define global variables +char count[2]; +char RxDataCnt; +char temp; +float value,value1; + +//Public and private keys for phant +char* Public_Key = "4J1LAGyxXOF6MN8r9G5x"; +char* Private_Key = "4J1LAGyxXOF6MN8r9G5x"; + +//Wifi init function +void wifi_initialize(void){ + + pc.printf("******** Resetting wifi module ********\r\n"); + wifi.Reset(); + + //wait for 5 seconds for response, else display no response receiveed + if (wifi.RcvReply(resp, 5000)) + pc.printf("%s",resp); + else + pc.printf("No response"); + + pc.printf("******** Setting Station mode of wifi with AP ********\r\n"); + wifi.SetMode(1); // set transparent mode + if (wifi.RcvReply(resp, timeout)) //receive a response from ESP + pc.printf("%s",resp); //Print the response onscreen + else + pc.printf("No response while setting mode. \r\n"); + + pc.printf("******** Joining network with SSID and PASS ********\r\n"); + wifi.Join(SSID, PASS); + if (wifi.RcvReply(resp, timeout)) + pc.printf("%s",resp); + else + pc.printf("No response while connecting to network \r\n"); + + pc.printf("******** Getting IP and MAC of module ********\r\n"); + wifi.GetIP(resp); + if (wifi.RcvReply(resp, timeout)) + pc.printf("%s",resp); + else + pc.printf("No response while getting IP \r\n"); + +} + +void wifi_send(void){ + + pc.printf("******** Setting WIFI UART passthrough ********\r\n"); + wifi.setTransparent(); + if (wifi.RcvReply(resp, timeout)) + pc.printf("%s",resp); + else + pc.printf("No response while setting wifi passthrough. \r\n"); + wait(1); + + pc.printf("******** Setting single connection mode ********\r\n"); + wifi.SetSingle(); + wifi.RcvReply(resp, timeout); + if (wifi.RcvReply(resp, timeout)) + pc.printf("%s",resp); + else + pc.printf("No response while setting single connection \r\n"); + wait(1); + + pc.printf("******** Starting TCP connection on IP and port ********\r\n"); + wifi.startTCPConn(IP, 8080); //cipstart + wifi.RcvReply(resp, timeout); + if (wifi.RcvReply(resp, timeout)) + pc.printf("%s",resp); + else + pc.printf("No response while starting TCP connection \r\n"); + wait(1); + + //create link + sprintf(http_cmd,"/input/%s?private_key=%s&pot=%.2f&ldr=%.2f",Public_Key,Private_Key,value,value1); + + pc.printf("******** Sending URL to wifi ********\r\n"); + wifi.sendURL(http_cmd, comm); //cipsend and get command + if (wifi.RcvReply(resp, timeout)) + pc.printf("%s",resp); + else + pc.printf("No response while sending URL \r\n"); + + //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server + //wifi.RcvReply(resp, timeout); + //pc.printf("%s", resp); +} + +int main () +{ + float tmp[2]; + //Intializing the values + count[0] = 0x01; + count[1] = 0x01; + + value = 0x01; + value1 = 0x01; + + //Intialise WiFi Module + wifi_initialize(); + + //Intialise nRF module + my_nrf24l01p.powerUp(); + my_nrf24l01p.setRfFrequency(2440); + + // Display the (default) setup of the nRF24L01+ chip + pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); + pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); + pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); + pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); + pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); + + pc.printf( "Recieveing data from several sensors\r\n" ); + + RxDataCnt = 2; + my_nrf24l01p.setTransferSize( RxDataCnt ); + + my_nrf24l01p.setReceiveMode(); + my_nrf24l01p.enable(); + + while (1) + { + if(my_nrf24l01p.readable()) + { + //Read data into the recieve buffer + temp = my_nrf24l01p.read(NRF24L01P_PIPE_P0,count,RxDataCnt); + pc.printf("cnt %d = %d %d \r \n",temp,count[0],count[1]); + tmp[0] = count[0]/255; + tmp[1] = count[1]/255; + + value = 3.3*tmp[0]; + value1 = 3.3*tmp[1]; + + pc.printf("Pot = %dmV LDR = %dmV \r \n",value,value1); + +// Togle LED2 (to help debug nRF24L01+ -> Host communication) + gled = !gled; + wait_ms(10); + } + //Send the data over wifi + wifi_send(); + wait(1); + } +} \ No newline at end of file
diff -r 000000000000 -r 2ae429a5b336 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Dec 15 08:05:18 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/abea610beb85 \ No newline at end of file
diff -r 000000000000 -r 2ae429a5b336 nRF24L01P.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nRF24L01P.lib Thu Dec 15 08:05:18 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/pabloamr/code/nRF24L01P/#8d55f1f49a33