
Hexiware_WiFi_ESP8266_and_sensor_code
Revision 0:5fcbe9cb4f5b, committed 2017-03-22
- Comitter:
- agaikwad
- Date:
- Wed Mar 22 01:15:16 2017 +0000
- Commit message:
- Hexiware_WiFi_ESP8266_and_sensor_code
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.gitignore Wed Mar 22 01:15:16 2017 +0000 @@ -0,0 +1,4 @@ +.build +.mbed +projectfiles +*.py*
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP8266/ESP8266.cpp Wed Mar 22 01:15:16 2017 +0000 @@ -0,0 +1,168 @@ +#include "ESP8266.h" + +// 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 +void 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); +} + +// 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); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP8266/ESP8266.h Wed Mar 22 01:15:16 2017 +0000 @@ -0,0 +1,67 @@ +#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); +void 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); + +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 +*/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Wed Mar 22 01:15:16 2017 +0000 @@ -0,0 +1,87 @@ +# Getting started with Blinky on mbed OS + +This guide reviews the steps required to get Blinky working on an mbed OS platform. + +Please install [mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli). + +## Import the example application + +From the command-line, import the example: + +``` +mbed import mbed-os-example-blinky +cd mbed-os-example-blinky +``` + +### Now compile + +Invoke `mbed compile`, and specify the name of your platform and your favorite toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the ARM Compiler 5: + +``` +mbed compile -m K64F -t ARM +``` + +Your PC may take a few minutes to compile your code. At the end, you see the following result: + +``` +[snip] ++----------------------------+-------+-------+------+ +| Module | .text | .data | .bss | ++----------------------------+-------+-------+------+ +| Misc | 13939 | 24 | 1372 | +| core/hal | 16993 | 96 | 296 | +| core/rtos | 7384 | 92 | 4204 | +| features/FEATURE_IPV4 | 80 | 0 | 176 | +| frameworks/greentea-client | 1830 | 60 | 44 | +| frameworks/utest | 2392 | 512 | 292 | +| Subtotals | 42618 | 784 | 6384 | ++----------------------------+-------+-------+------+ +Allocated Heap: unknown +Allocated Stack: unknown +Total Static RAM memory (data + bss): 7168 bytes +Total RAM memory (data + bss + heap + stack): 7168 bytes +Total Flash memory (text + data + misc): 43402 bytes +Image: .\.build\K64F\ARM\mbed-os-example-blinky.bin +``` + +### Program your board + +1. Connect your mbed device to the computer over USB. +1. Copy the binary file to the mbed device. +1. Press the reset button to start the program. + +The LED on your platform turns on and off. + +## Export the project to Keil MDK, and debug your application + +From the command-line, run the following command: + +``` +mbed export -m K64F -i uvision +``` + +To debug the application: + +1. Start uVision. +1. Import the uVision project generated earlier. +1. Compile your application, and generate an `.axf` file. +1. Make sure uVision is configured to debug over CMSIS-DAP (From the Project menu > Options for Target '...' > Debug tab > Use CMSIS-DAP Debugger). +1. Set breakpoints, and start a debug session. + + + +## Troubleshooting + +1. Make sure `mbed-cli` is working correctly and its version is `>1.0.0` + + ``` + mbed --version + ``` + + If not, you can update it: + + ``` + pip install mbed-cli --upgrade + ``` + +2. If using Keil MDK, make sure you have a license installed. [MDK-Lite](http://www.keil.com/arm/mdk.asp) has a 32 KB restriction on code size. \ No newline at end of file
Binary file img/uvision.png has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Mar 22 01:15:16 2017 +0000 @@ -0,0 +1,213 @@ + +#include "mbed.h" +#include "ESP8266.h" // Include header file from Author: Antonio Quevedo +#include "math.h" +#include <string> + +#define APIKEY 45IE5JPN8ZIK2W2M //Put "Write key" of your channel in thingspeak.com +#define IP "184.106.153.149" // IP Address of "api.thingspeak.com\" +#define WIFI_SSID "Redmi" +#define WIFI_PASS "akash12345" + +Serial pc(USBTX,USBRX); +SPI spi(PTC6,PTC7,PTC5); // (MOSI MISO CLK)setup SPI interface on pins PTC5,PTC6,PTC7 +ESP8266 esp(PTC17, PTC16, 115200); // baud rate for wifi + +DigitalOut cs1(PTC4); +DigitalOut cs2(PTC3); +I2C i2c(PTD9,PTD8); + +//AnalogIn check(PTB6); +const int addr = 0x88; + +char snd[255],rcv[1000],snd_Data[255]; //snd= string used to send command to ESP 8266 wii and rcv = string used to receive response from ESP8266 wifi module + +int cnt = 0; // counter for number of motion detected +int cnt1 = 0; // counter for number of motion detected +int cnt2 = 0; // counter for number of motion detected + +float ammeter_out = 0; +float voltmeter_out = 0; +float light_out = 0; + +void esp_initialize(void); // Function used to initialize ESP8266 wifi module +void esp_send(void); // Function used to connect with thingspeak.com and update channel using ESP8266 wifi module + +int main() +{ + + pc.baud(115200); // Baud rate used for communicating with Tera-term on PC + + pc.printf("START\r\n"); // Starting point + + esp_initialize(); + + //ammeter + voltmeter code + spi.format(8,0); + spi.frequency(1000000); + pc.printf("Analog read test from spi.\n"); + pc.printf("\n\r"); + + //light code + int exp,exp1,l=1; + i2c.frequency(100000); // set required i2c frequency + //pc.baud(9600); //set baud rate + pc.printf("I2C started!\r\n"); + pc.printf("\n\r"); + char cmd[3]; //for byte transfer + + while (1) + { + wait(15); + //ammeter code + + cs1=0; + //spi.write(); + //pc.printf("%d\r\n",k); + int high_byte = spi.write(0); + int low_byte = spi.write(0); + cs1=1; + float m = ((high_byte & 0x1f) << 7) | ((low_byte >> 1)); + float k= (float)((m*1)/4096); // show value in volts. + ammeter_out = (float)((k-0.5)*1000); + pc.printf("Current value: %f mA\r\n", ammeter_out); + //pc.printf("Analog Value of current : %.2f\n\r",m); + wait_ms(100); + + cs2=0; + //spi.write(); + //pc.printf("%d\r\n",k); + int hi_byte = spi.write(0); + int lo_byte = spi.write(0); + cs2=1; + float x = ((hi_byte & 0x1f) << 7) | ((lo_byte >> 1)); + + float r= (float)((x*33)/4096); // show value in volts. + voltmeter_out = (float)(r-16.5); + pc.printf("AD Voltage channel value: %f V\r\n", voltmeter_out); + //pc.printf("Voltage Analog Value: %.2f\n\r",x); + wait_ms(100); + + //ambient light sensor code + + cmd[0] = 0x01; //configuration register + cmd[1]= 0xCC; //configuration data + cmd[2]= 0x01; //configuration data + i2c.write(addr, cmd, 3); + cmd[0] = 0x00; // data register + i2c.write(addr, cmd, 1); + wait_ms(100); + i2c.read(addr, cmd, 2); + + exp= cmd[0]>>4; + exp1= (cmd[0]-(exp<<4))*256+cmd[1]; + // pc.printf("exponent = 0x%x\n\r", exp); + // pc.printf("fraction = %d\n\r", exp1); + l=1; + for(int r=0;r<exp;r++){l=l*2;}; + // pc.printf("value = %d\n\r", l); + light_out= (exp1*l)/100; + pc.printf("Lux = %.2f\n\r", light_out); // printing LUX value + pc.printf("\n\r"); + wait_ms(100); + + pc.printf("Sending this information to thingspeak.com = %d\r\n",cnt); + esp_send(); + + } +} + + +void esp_initialize(void) +{ + pc.printf("Initializing ESP\r\n"); + + pc.printf("Reset ESP\r\n"); + esp.Reset(); //RESET ESP + esp.RcvReply(rcv, 400); //receive a response from ESP + //pc.printf(rcv); //Print the response onscreen + wait(2); + + strcpy(snd,"AT"); + esp.SendCMD(snd); + pc.printf(snd); + //wait(2); + esp.RcvReply(rcv, 400); + pc.printf(rcv); + wait(0.1); + + strcpy(snd,"AT+CWMODE=1"); + esp.SendCMD(snd); + pc.printf(snd); + wait(2); + + strcpy(snd,"AT+CWJAP=\""); + strcat(snd,WIFI_SSID); + strcat(snd,"\",\""); + strcat(snd,WIFI_PASS); + strcat(snd,"\""); + + esp.SendCMD(snd); + pc.printf(snd); + wait(5); + esp.RcvReply(rcv, 400); + pc.printf("\n %s \n", rcv); + + strcpy(snd,"AT+CIPMUX=0"); + esp.SendCMD(snd); + pc.printf(snd); + //wait(2); + esp.RcvReply(rcv, 400); + pc.printf("\n %s \n", rcv); + +} + + +void esp_send(void) +{ + + //ESP updates the Status of Thingspeak channel// + + strcpy(snd,"AT+CIPSTART="); + strcat(snd,"\"TCP\",\""); + strcat(snd,IP); + strcat(snd,"\",80"); + + esp.SendCMD(snd); + pc.printf("S\r\n%s",snd); + //wait(2); + esp.RcvReply(rcv, 1000); + pc.printf("R\r\n%s",rcv); + wait(1); + + sprintf(snd,"GET https://api.thingspeak.com/update?key=45IE5JPN8ZIK2W2M&field1=%f&field2=%f&field3=%f\r\n",ammeter_out,voltmeter_out,light_out); + + int i=0; + for(i=0;snd[i]!='\0';i++); + i++; + char cmd[255]; + + sprintf(cmd,"AT+CIPSEND=%d",i); //Send Number of open connection and Characters to send + esp.SendCMD(cmd); + pc.printf("S\r\n%s",cmd); + while(i<=20 || rcv == ">") + { + esp.RcvReply(rcv, 1000); + wait(100); + i++; + } + pc.printf("R\r\n%s",rcv); + + esp.SendCMD(snd); //Post value to thingspeak channel + pc.printf("S\r\n%s",snd); + + while(i<=20 || rcv == "OK") + { + esp.RcvReply(rcv, 1000); + wait(100); + i++; + } + pc.printf("R\r\n%s",rcv); + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Wed Mar 22 01:15:16 2017 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#f4864dc6429e1ff5474111d4e0f6bee36a759b1c