SHARE
TWEET

distributore croccantini

TringaliLuca Apr 29th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ESP8266mDNS.h>
  5.  
  6. #include <Servo.h>
  7.  
  8. Servo myservo;
  9.  
  10. const char *ssid = "NOMEWIFI";
  11. const char *password = "PASSWORDWIFI";
  12.  
  13. const int triggerPin = 12;  //D6
  14. const int echoPin = 13;  //D7
  15. const int servoPin = 2;  //D9
  16.  
  17. const long searchwifi = 1000UL*30;  //secondi prima di rinunciare alla connessione WiFi
  18. const long towait = 1000UL*60*240;   //minuti prima di poter dare cibo una seconda volta
  19. const int servospeed = 10;  //velocit√† del servo 1,2,5,10
  20. const int distance = 25;  //distanza in centimetri per rilevare il gatto
  21.  
  22. const int opened = 0;
  23. const int closed = 90;
  24.  
  25. long remaining = 0;
  26.  
  27. ESP8266WebServer server ( 80 );
  28.  
  29. void handleRoot() {
  30.   char temp[400];
  31.   int rmin = remaining/(1000UL*60);
  32.   int emin = (towait - remaining)/(1000UL*60);
  33.  
  34.   snprintf ( temp, 400,
  35.  
  36. "<html>\
  37.  <head>\
  38.    <meta http-equiv='refresh' content='5'/>\
  39.    <title>ESP8266 Demo</title>\
  40.  </head>\
  41.  <body>\
  42.    <h1>Distributore di cibo per il gatto</h1>\
  43.    <p>Ho distribuito cibo almeno %02d minuti fa, ne posso distribuire di nuovo tra %02d minuti.</p>\
  44.    <p>Se vuoi dare cibo al gatto <a href=\"/cibo\">clicca qui</a>.</p>\
  45.  </body>\
  46. </html>",
  47.  
  48.     emin, rmin
  49.   );
  50.   server.send ( 200, "text/html", temp );
  51. }
  52.  
  53. void handleNotFound() {
  54.   String message = "File Not Found\n\n";
  55.   message += "URI: ";
  56.   message += server.uri();
  57.   message += "\nMethod: ";
  58.   message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
  59.   message += "\nArguments: ";
  60.   message += server.args();
  61.   message += "\n";
  62.  
  63.   for ( uint8_t i = 0; i < server.args(); i++ ) {
  64.     message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
  65.   }
  66.  
  67.   server.send ( 404, "text/plain", message );
  68. }
  69.  
  70. void setup ( void ) {
  71.   myservo.attach(servoPin);
  72.   myservo.write(closed);
  73.   pinMode(triggerPin, OUTPUT);
  74.   pinMode(echoPin, INPUT);
  75.  
  76.   WiFi.begin ( ssid, password );
  77.  
  78.   // Wait for connection
  79.   long wifitime = 0;
  80.   while ( WiFi.status() != WL_CONNECTED ) {
  81.     delay ( 500 );
  82.     wifitime = wifitime + 500;
  83.     if (wifitime > searchwifi) break;
  84.   }
  85.  
  86.   if (WiFi.status() == WL_CONNECTED) {
  87.     MDNS.begin( "esp8266" );
  88.     server.on ( "/", handleRoot );
  89.     server.on ( "/cibo", apriWeb );
  90.     server.onNotFound ( handleNotFound );
  91.     server.begin();
  92.   }
  93. }
  94.  
  95. void loop ( void ) {
  96.   if (WiFi.status() == WL_CONNECTED) server.handleClient();
  97.  
  98.   //Invio impulso 10 us
  99.   digitalWrite(triggerPin, LOW );
  100.   digitalWrite(triggerPin, HIGH );
  101.   delayMicroseconds( 10 );
  102.   digitalWrite(triggerPin, LOW );
  103.   //ascolto eco
  104.   long pulseEcho = pulseIn(echoPin, HIGH );
  105.   int thisdistance = 0.034 * pulseEcho / 2;
  106.   if (thisdistance < distance) {
  107.     if (remaining == 0) apri();
  108.   }
  109.  
  110.   delay(200);
  111.   remaining -= 200;
  112.   if (remaining < 0) remaining = 0;
  113. }
  114.  
  115. void apri() {
  116.   int pos;
  117.  
  118.   int add = servospeed;
  119.   if (closed > opened) add = servospeed*-1;
  120.   for(pos = closed; pos != opened; pos = pos + add)
  121.   {                                  
  122.     myservo.write(pos);              
  123.     delay(15);                      
  124.   }
  125.   delay(10*servospeed); //aspetto che scendano un po' di croccantini
  126.   add = add *-1;
  127.   for(pos = opened; pos != closed; pos = pos + add)
  128.   {                                  
  129.     myservo.write(pos);              
  130.     delay(15);                      
  131.   }
  132.   remaining = towait;
  133. }
  134.  
  135. void apriWeb() {
  136.   server.send ( 200, "text/html", "<html><p>Cibo distribuito, <a href=\"/\">torna alla home</a>.</p></html>" );
  137.   if (remaining == 0) apri();
  138. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top