Smart parking system using ESP-8266 Wi-Fi module with infrared sensor and servo motor

Dependencies:   Servo mbed-os FXOS8700Q

Committer:
sahilkasana
Date:
Wed Apr 29 00:17:28 2020 +0000
Revision:
0:b2db5e8bd926
smart parking system using ESP-8266 Wi-Fi module with Thingspeak.; Infrared sensors are used in the project along with servo motor.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilkasana 0:b2db5e8bd926 1 #include "mbed.h"
sahilkasana 0:b2db5e8bd926 2 #include "Servo.h"
sahilkasana 0:b2db5e8bd926 3 #include "ESP8266.h"
sahilkasana 0:b2db5e8bd926 4 #include "math.h"
sahilkasana 0:b2db5e8bd926 5 #define CloudIP "184.106.153.149" //Raw IP Address of ThingSpeak Cloud Server
sahilkasana 0:b2db5e8bd926 6
sahilkasana 0:b2db5e8bd926 7 DigitalOut RLed(LED1); //when Garage has no spot for parking
sahilkasana 0:b2db5e8bd926 8 DigitalOut GLed(LED2); //when Garage has spot available
sahilkasana 0:b2db5e8bd926 9 DigitalOut BLed(LED3); //Onboard Blue LED for Wifi Tx Indication
sahilkasana 0:b2db5e8bd926 10
sahilkasana 0:b2db5e8bd926 11 ESP8266 wifi(PTC17, PTC16, 9600); //Tx Pin:PTC17; Rx Pin:PTC17; Baud rate:115200
sahilkasana 0:b2db5e8bd926 12 DigitalIn IR1(A0);
sahilkasana 0:b2db5e8bd926 13 DigitalIn IR2(A1);
sahilkasana 0:b2db5e8bd926 14 DigitalIn IR3(A2);
sahilkasana 0:b2db5e8bd926 15 DigitalIn GS(A3);
sahilkasana 0:b2db5e8bd926 16 Servo myservo(D7);
sahilkasana 0:b2db5e8bd926 17 void wifi_send(void); //Connect and Push Data Channel to Cloud Server
sahilkasana 0:b2db5e8bd926 18 int f_out= 3; //variable to count for no of slots available
sahilkasana 0:b2db5e8bd926 19 int num = 0;
sahilkasana 0:b2db5e8bd926 20 int open = 1 ;
sahilkasana 0:b2db5e8bd926 21 char snd[255],rcv[1000]; //snd: send command to ESP8266
sahilkasana 0:b2db5e8bd926 22 //rcv: receive response from ESP8266
sahilkasana 0:b2db5e8bd926 23 int main()
sahilkasana 0:b2db5e8bd926 24 {
sahilkasana 0:b2db5e8bd926 25 printf("WELCOME TO PARKING SYSTEM\n\r");
sahilkasana 0:b2db5e8bd926 26 printf("Cloud synchronization number : %d", num);
sahilkasana 0:b2db5e8bd926 27 printf("Syncing Data with Cloud, Please Wait.\n\r");
sahilkasana 0:b2db5e8bd926 28 printf("Initial Setup\r\n");
sahilkasana 0:b2db5e8bd926 29 wifi.SetMode(1); //Set ESP mode to 1
sahilkasana 0:b2db5e8bd926 30 wifi.RcvReply(rcv, 1000); //Receive a response from ESP
sahilkasana 0:b2db5e8bd926 31 printf("%s\r", rcv);
sahilkasana 0:b2db5e8bd926 32
sahilkasana 0:b2db5e8bd926 33 printf("Conneting to WiFi\r\n"); //AP Setup Initialization
sahilkasana 0:b2db5e8bd926 34 wifi.Join("Kuch Bhi Daal Do", "paiselagenge"); //wifi address details
sahilkasana 0:b2db5e8bd926 35 wifi.RcvReply(rcv, 1000);
sahilkasana 0:b2db5e8bd926 36 printf("%s\n", rcv);
sahilkasana 0:b2db5e8bd926 37 wait(8);
sahilkasana 0:b2db5e8bd926 38
sahilkasana 0:b2db5e8bd926 39 wifi.GetIP(rcv); //Obtains an IP address from the AP
sahilkasana 0:b2db5e8bd926 40
sahilkasana 0:b2db5e8bd926 41 while (1) {
sahilkasana 0:b2db5e8bd926 42 wifi_send();
sahilkasana 0:b2db5e8bd926 43
sahilkasana 0:b2db5e8bd926 44 RLed = 1;
sahilkasana 0:b2db5e8bd926 45 GLed = 1;
sahilkasana 0:b2db5e8bd926 46 BLed = 0;
sahilkasana 0:b2db5e8bd926 47 wait(2.0f);
sahilkasana 0:b2db5e8bd926 48 }
sahilkasana 0:b2db5e8bd926 49 }
sahilkasana 0:b2db5e8bd926 50
sahilkasana 0:b2db5e8bd926 51 void wifi_send(void)
sahilkasana 0:b2db5e8bd926 52 {
sahilkasana 0:b2db5e8bd926 53 while(num<1000000000000) {
sahilkasana 0:b2db5e8bd926 54 num=num + 1;
sahilkasana 0:b2db5e8bd926 55
sahilkasana 0:b2db5e8bd926 56 GLed=0;
sahilkasana 0:b2db5e8bd926 57 BLed=1;
sahilkasana 0:b2db5e8bd926 58
sahilkasana 0:b2db5e8bd926 59
sahilkasana 0:b2db5e8bd926 60
sahilkasana 0:b2db5e8bd926 61 if(GS==0) {
sahilkasana 0:b2db5e8bd926 62 printf("Car is available at parking entrance..checking for slot availability\n\r");
sahilkasana 0:b2db5e8bd926 63 if(IR1==0 && IR2==0 && IR3==0) {
sahilkasana 0:b2db5e8bd926 64 printf("Parking full try next garage..\n\r");
sahilkasana 0:b2db5e8bd926 65 open=0;
sahilkasana 0:b2db5e8bd926 66 RLed=0;
sahilkasana 0:b2db5e8bd926 67 GLed=1;
sahilkasana 0:b2db5e8bd926 68 f_out = 0;
sahilkasana 0:b2db5e8bd926 69 } else if (IR1==0 && IR2==0 && IR3==1) {
sahilkasana 0:b2db5e8bd926 70 printf(" 1 spot available at slot 3\n\r");
sahilkasana 0:b2db5e8bd926 71 f_out = 1;
sahilkasana 0:b2db5e8bd926 72 } else if (IR1==0 && IR2==1 && IR3==0) {
sahilkasana 0:b2db5e8bd926 73 printf(" 1 spot avaialble at slot 2\n\r");
sahilkasana 0:b2db5e8bd926 74 f_out = 1;
sahilkasana 0:b2db5e8bd926 75 } else if (IR1==0 && IR2==1 && IR3==1) {
sahilkasana 0:b2db5e8bd926 76 printf("Two spots available at slot 2 and 3\n\r");
sahilkasana 0:b2db5e8bd926 77 f_out = 2;
sahilkasana 0:b2db5e8bd926 78 } else if (IR1==1 && IR2==0 && IR3==0) {
sahilkasana 0:b2db5e8bd926 79 printf("One spot available at slot 1\n\r");
sahilkasana 0:b2db5e8bd926 80 f_out = 1;
sahilkasana 0:b2db5e8bd926 81 } else if (IR1==1 && IR2==0 && IR3==1) {
sahilkasana 0:b2db5e8bd926 82 printf("Two spot avaialble at slot 1 and 3\n\r");
sahilkasana 0:b2db5e8bd926 83 f_out = 2;
sahilkasana 0:b2db5e8bd926 84 } else if (IR1==1 && IR2==1 && IR3==0) {
sahilkasana 0:b2db5e8bd926 85 printf("Two spot avaialble at slot 1 and 2\n\r");
sahilkasana 0:b2db5e8bd926 86 f_out = 2;
sahilkasana 0:b2db5e8bd926 87 } else if (IR1==1 && IR2==1 && IR3==1) {
sahilkasana 0:b2db5e8bd926 88 printf("Garage Empty\n\r");
sahilkasana 0:b2db5e8bd926 89 f_out = 3;
sahilkasana 0:b2db5e8bd926 90 }
sahilkasana 0:b2db5e8bd926 91
sahilkasana 0:b2db5e8bd926 92 if(open == 1) {
sahilkasana 0:b2db5e8bd926 93 for(int i=0; i<100; i++) {
sahilkasana 0:b2db5e8bd926 94 myservo = i/100.0;
sahilkasana 0:b2db5e8bd926 95 wait(0.01);
sahilkasana 0:b2db5e8bd926 96 }
sahilkasana 0:b2db5e8bd926 97 for(int i=100; i>0; i--) {
sahilkasana 0:b2db5e8bd926 98 myservo = i/100.0;
sahilkasana 0:b2db5e8bd926 99 wait(0.01);
sahilkasana 0:b2db5e8bd926 100 }
sahilkasana 0:b2db5e8bd926 101 }
sahilkasana 0:b2db5e8bd926 102 }
sahilkasana 0:b2db5e8bd926 103 else
sahilkasana 0:b2db5e8bd926 104 printf("No car to enter\n\r");
sahilkasana 0:b2db5e8bd926 105
sahilkasana 0:b2db5e8bd926 106 //Sending Data to the Cloud Server via ESP8266 WiFi Module
sahilkasana 0:b2db5e8bd926 107 strcpy(snd,"AT+CIPMUX=0\n\r"); //AT+CIPMUX: Enabling Single Channel Mode
sahilkasana 0:b2db5e8bd926 108 wifi.SendCMD(snd);
sahilkasana 0:b2db5e8bd926 109 wait(1);
sahilkasana 0:b2db5e8bd926 110 wifi.RcvReply(rcv, 1000);
sahilkasana 0:b2db5e8bd926 111 wait(1);
sahilkasana 0:b2db5e8bd926 112
sahilkasana 0:b2db5e8bd926 113 sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80\n",CloudIP); //Establish TCP connection w/ Cloud Server
sahilkasana 0:b2db5e8bd926 114 wait(1);
sahilkasana 0:b2db5e8bd926 115 wifi.RcvReply(rcv, 1000);
sahilkasana 0:b2db5e8bd926 116 wait(1);
sahilkasana 0:b2db5e8bd926 117
sahilkasana 0:b2db5e8bd926 118 strcpy(snd,"AT+CIPSEND=100\n\r"); //Set length of the data that will be sent
sahilkasana 0:b2db5e8bd926 119 wifi.SendCMD(snd);
sahilkasana 0:b2db5e8bd926 120 printf("%s\r", rcv);
sahilkasana 0:b2db5e8bd926 121 wait(1);
sahilkasana 0:b2db5e8bd926 122 wifi.RcvReply(rcv, 1000);
sahilkasana 0:b2db5e8bd926 123 printf("%s\r", rcv);
sahilkasana 0:b2db5e8bd926 124 wait(1);
sahilkasana 0:b2db5e8bd926 125
sahilkasana 0:b2db5e8bd926 126 // Pushing the data to Cloud Server via API
sahilkasana 0:b2db5e8bd926 127 sprintf(snd,"GET https://api.thingspeak.com/update?api_key=M2RJX3971TCLP0S7&field1=%d\n\r", f_out);
sahilkasana 0:b2db5e8bd926 128 wifi.SendCMD(snd);
sahilkasana 0:b2db5e8bd926 129 printf("%s\r",snd);
sahilkasana 0:b2db5e8bd926 130 wait(1);
sahilkasana 0:b2db5e8bd926 131 wifi.RcvReply(rcv, 1000);
sahilkasana 0:b2db5e8bd926 132 printf("%s\r", rcv);
sahilkasana 0:b2db5e8bd926 133 }
sahilkasana 0:b2db5e8bd926 134 }