Smart parking system using ESP-8266 Wi-Fi module with infrared sensor and servo motor
Dependencies: Servo mbed-os FXOS8700Q
Revision 0:b2db5e8bd926, committed 2020-04-29
- Comitter:
- sahilkasana
- Date:
- Wed Apr 29 00:17:28 2020 +0000
- Commit message:
- smart parking system using ESP-8266 Wi-Fi module with Thingspeak.; Infrared sensors are used in the project along with servo motor.
Changed in this revision
diff -r 000000000000 -r b2db5e8bd926 ESP8266/ESP8266.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP8266/ESP8266.cpp Wed Apr 29 00:17:28 2020 +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 b2db5e8bd926 ESP8266/ESP8266.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP8266/ESP8266.h Wed Apr 29 00:17:28 2020 +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 b2db5e8bd926 FXOS8700Q.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FXOS8700Q.lib Wed Apr 29 00:17:28 2020 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/Freescale/code/FXOS8700Q/#aee7dea904e2
diff -r 000000000000 -r b2db5e8bd926 Servo.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Servo.lib Wed Apr 29 00:17:28 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/simon/code/Servo/#36b69a7ced07
diff -r 000000000000 -r b2db5e8bd926 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Apr 29 00:17:28 2020 +0000 @@ -0,0 +1,134 @@ +#include "mbed.h" +#include "Servo.h" +#include "ESP8266.h" +#include "math.h" +#define CloudIP "184.106.153.149" //Raw IP Address of ThingSpeak Cloud Server + +DigitalOut RLed(LED1); //when Garage has no spot for parking +DigitalOut GLed(LED2); //when Garage has spot available +DigitalOut BLed(LED3); //Onboard Blue LED for Wifi Tx Indication + +ESP8266 wifi(PTC17, PTC16, 9600); //Tx Pin:PTC17; Rx Pin:PTC17; Baud rate:115200 +DigitalIn IR1(A0); +DigitalIn IR2(A1); +DigitalIn IR3(A2); +DigitalIn GS(A3); +Servo myservo(D7); +void wifi_send(void); //Connect and Push Data Channel to Cloud Server +int f_out= 3; //variable to count for no of slots available +int num = 0; +int open = 1 ; +char snd[255],rcv[1000]; //snd: send command to ESP8266 +//rcv: receive response from ESP8266 +int main() +{ + printf("WELCOME TO PARKING SYSTEM\n\r"); + printf("Cloud synchronization number : %d", num); + printf("Syncing Data with Cloud, Please Wait.\n\r"); + printf("Initial Setup\r\n"); + wifi.SetMode(1); //Set ESP mode to 1 + wifi.RcvReply(rcv, 1000); //Receive a response from ESP + printf("%s\r", rcv); + + printf("Conneting to WiFi\r\n"); //AP Setup Initialization + wifi.Join("Kuch Bhi Daal Do", "paiselagenge"); //wifi address details + wifi.RcvReply(rcv, 1000); + printf("%s\n", rcv); + wait(8); + + wifi.GetIP(rcv); //Obtains an IP address from the AP + + while (1) { + wifi_send(); + + RLed = 1; + GLed = 1; + BLed = 0; + wait(2.0f); + } +} + +void wifi_send(void) +{ + while(num<1000000000000) { + num=num + 1; + + GLed=0; + BLed=1; + + + + if(GS==0) { + printf("Car is available at parking entrance..checking for slot availability\n\r"); + if(IR1==0 && IR2==0 && IR3==0) { + printf("Parking full try next garage..\n\r"); + open=0; + RLed=0; + GLed=1; + f_out = 0; + } else if (IR1==0 && IR2==0 && IR3==1) { + printf(" 1 spot available at slot 3\n\r"); + f_out = 1; + } else if (IR1==0 && IR2==1 && IR3==0) { + printf(" 1 spot avaialble at slot 2\n\r"); + f_out = 1; + } else if (IR1==0 && IR2==1 && IR3==1) { + printf("Two spots available at slot 2 and 3\n\r"); + f_out = 2; + } else if (IR1==1 && IR2==0 && IR3==0) { + printf("One spot available at slot 1\n\r"); + f_out = 1; + } else if (IR1==1 && IR2==0 && IR3==1) { + printf("Two spot avaialble at slot 1 and 3\n\r"); + f_out = 2; + } else if (IR1==1 && IR2==1 && IR3==0) { + printf("Two spot avaialble at slot 1 and 2\n\r"); + f_out = 2; + } else if (IR1==1 && IR2==1 && IR3==1) { + printf("Garage Empty\n\r"); + f_out = 3; + } + + if(open == 1) { + for(int i=0; i<100; i++) { + myservo = i/100.0; + wait(0.01); + } + for(int i=100; i>0; i--) { + myservo = i/100.0; + wait(0.01); + } + } + } + else + printf("No car to enter\n\r"); + + //Sending Data to the Cloud Server via ESP8266 WiFi Module + strcpy(snd,"AT+CIPMUX=0\n\r"); //AT+CIPMUX: Enabling Single Channel Mode + wifi.SendCMD(snd); + wait(1); + wifi.RcvReply(rcv, 1000); + wait(1); + + sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80\n",CloudIP); //Establish TCP connection w/ Cloud Server + wait(1); + wifi.RcvReply(rcv, 1000); + wait(1); + + strcpy(snd,"AT+CIPSEND=100\n\r"); //Set length of the data that will be sent + wifi.SendCMD(snd); + printf("%s\r", rcv); + wait(1); + wifi.RcvReply(rcv, 1000); + printf("%s\r", rcv); + wait(1); + + // Pushing the data to Cloud Server via API + sprintf(snd,"GET https://api.thingspeak.com/update?api_key=M2RJX3971TCLP0S7&field1=%d\n\r", f_out); + wifi.SendCMD(snd); + printf("%s\r",snd); + wait(1); + wifi.RcvReply(rcv, 1000); + printf("%s\r", rcv); + } + }
diff -r 000000000000 -r b2db5e8bd926 mbed-os.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Wed Apr 29 00:17:28 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/teams/EL4121-Embedded-System/code/mbed-os/#b74591d5ab33