Auto Pets Feeder

Dependencies:   mbed RangeFinder ESP8266 HC_SR04_Ultrasonic_Library

Revision:
2:c2e622db40aa
Parent:
1:8ab009672555
diff -r 8ab009672555 -r c2e622db40aa main.cpp
--- a/main.cpp	Sat Jun 17 09:53:27 2017 +0000
+++ b/main.cpp	Tue Apr 30 13:20:00 2019 +0000
@@ -1,32 +1,42 @@
 #include "mbed.h"
 #include "ESP8266.h"
+#include "RangeFinder.h"
+ 
+//Rangefinder Init
+RangeFinder rf(D2, 10, 5800.0, 100000);
  
 Serial pc(USBTX,USBRX);
 
-//POT sensor 
-AnalogIn pot(PTB0);
+//sensor 
+AnalogIn lightSensor(A1);
+PwmOut pwm(PTD0);
 
 //wifi UART port and baud rate
-ESP8266 wifi(PTE0, PTE1, 115200); 
+ESP8266 wifi(PTC17, PTC16, 115200); 
+
 
 //buffers for wifi library
 char resp[1000];
 char http_cmd[300], comm[300];
 
+//lightsensor reading
+float lightRead = 0.0f;
+
 int timeout = 8000; //timeout for wifi commands
 
+//system state
+int state = 0;
+
 //SSID and password for connection
-#define SSID "Eduvance" 
-#define PASS "eduvance123"  
+#define SSID "ATT4fpt8dY" 
+#define PASS "8k#ua9zy%4ue"  
 
 //Remote IP
 #define IP "184.106.153.149"
 
-//global variable
-float potvalue = 0; 
 
 //Update key for thingspeak
-char* Update_Key = "O1VOWGC3WXI981H6";
+char* Update_Key = "H67JROEREPXPGPFZ";
  
 //Wifi init function
 void wifi_initialize(void){
@@ -79,8 +89,8 @@
     wait(1);
 }
 
-void wifi_send(void){
-    
+void wifi_send(void){  
+
     pc.printf("******** Starting TCP connection on IP and port ********\r\n");
     wifi.startTCPConn(IP,80);    //cipstart
     wifi.RcvReply(resp, timeout);
@@ -89,10 +99,10 @@
     else
         pc.printf("No response while starting TCP connection \r\n");
     wait(1);
+    //create link 
+    sprintf(http_cmd,"/update?api_key=%s&field1=%f",Update_Key,lightRead); 
+    pc.printf(http_cmd);
     
-    //create link 
-    sprintf(http_cmd,"/update?api_key=%s&field1=%f",Update_Key,potvalue); 
-    pc.printf(http_cmd);
     
     pc.printf("******** Sending URL to wifi ********\r\n");
     wifi.sendURL(http_cmd, comm);   //cipsend and get command
@@ -103,15 +113,35 @@
 }
 
 int main () {
+    pwm.period_ms(20) ;
+    wifi_initialize();
+    float d;
+    int count = 0;
     
     
-    wifi_initialize();
     
     while (1) {
-        potvalue = 255*pot.read();
-        pc.printf("Current potvalue is = %.3f \r\n", potvalue);
+        printf("running\n");
+        lightRead = 1 - lightSensor.read();
+        d = rf.read_m();
         
-        wifi_send();
-        wait(30);
+        switch(state){
+            case 0:
+                if(d < 0.2f){
+                    state = 1;    
+                }break;
+            case 1:
+                pwm.pulsewidth_us(2400);
+                wait(1);
+                pwm.pulsewidth_us(1000);
+                wait(1);
+                state = 2;
+                break;
+            case 2:
+                if(d > 0.6f){state = 0;}break;
+        }
+        if(count == 500){wifi_send();count = 0;}
+        count++;
+        //
     }
 }
\ No newline at end of file