Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <Servo.h>
- Servo myservo;
- const char *ssid = "NOMEWIFI";
- const char *password = "PASSWORDWIFI";
- const int triggerPin = 12; //D6
- const int echoPin = 13; //D7
- const int servoPin = 2; //D9
- const long searchwifi = 1000UL*30; //secondi prima di rinunciare alla connessione WiFi
- const long towait = 1000UL*60*240; //minuti prima di poter dare cibo una seconda volta
- const int servospeed = 10; //velocità del servo 1,2,5,10
- const int distance = 25; //distanza in centimetri per rilevare il gatto
- const int opened = 0;
- const int closed = 90;
- long remaining = 0;
- ESP8266WebServer server ( 80 );
- void handleRoot() {
- char temp[400];
- int rmin = remaining/(1000UL*60);
- int emin = (towait - remaining)/(1000UL*60);
- snprintf ( temp, 400,
- "<html>\
- <head>\
- <meta http-equiv='refresh' content='5'/>\
- <title>ESP8266 Demo</title>\
- </head>\
- <body>\
- <h1>Distributore di cibo per il gatto</h1>\
- <p>Ho distribuito cibo almeno %02d minuti fa, ne posso distribuire di nuovo tra %02d minuti.</p>\
- <p>Se vuoi dare cibo al gatto <a href=\"/cibo\">clicca qui</a>.</p>\
- </body>\
- </html>",
- emin, rmin
- );
- server.send ( 200, "text/html", temp );
- }
- void handleNotFound() {
- String message = "File Not Found\n\n";
- message += "URI: ";
- message += server.uri();
- message += "\nMethod: ";
- message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
- message += "\nArguments: ";
- message += server.args();
- message += "\n";
- for ( uint8_t i = 0; i < server.args(); i++ ) {
- message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
- }
- server.send ( 404, "text/plain", message );
- }
- void setup ( void ) {
- myservo.attach(servoPin);
- myservo.write(closed);
- pinMode(triggerPin, OUTPUT);
- pinMode(echoPin, INPUT);
- WiFi.begin ( ssid, password );
- // Wait for connection
- long wifitime = 0;
- while ( WiFi.status() != WL_CONNECTED ) {
- delay ( 500 );
- wifitime = wifitime + 500;
- if (wifitime > searchwifi) break;
- }
- if (WiFi.status() == WL_CONNECTED) {
- MDNS.begin( "esp8266" );
- server.on ( "/", handleRoot );
- server.on ( "/cibo", apriWeb );
- server.onNotFound ( handleNotFound );
- server.begin();
- }
- }
- void loop ( void ) {
- if (WiFi.status() == WL_CONNECTED) server.handleClient();
- //Invio impulso 10 us
- digitalWrite(triggerPin, LOW );
- digitalWrite(triggerPin, HIGH );
- delayMicroseconds( 10 );
- digitalWrite(triggerPin, LOW );
- //ascolto eco
- long pulseEcho = pulseIn(echoPin, HIGH );
- int thisdistance = 0.034 * pulseEcho / 2;
- if (thisdistance < distance) {
- if (remaining == 0) apri();
- }
- delay(200);
- remaining -= 200;
- if (remaining < 0) remaining = 0;
- }
- void apri() {
- int pos;
- int add = servospeed;
- if (closed > opened) add = servospeed*-1;
- for(pos = closed; pos != opened; pos = pos + add)
- {
- myservo.write(pos);
- delay(15);
- }
- delay(10*servospeed); //aspetto che scendano un po' di croccantini
- add = add *-1;
- for(pos = opened; pos != closed; pos = pos + add)
- {
- myservo.write(pos);
- delay(15);
- }
- remaining = towait;
- }
- void apriWeb() {
- server.send ( 200, "text/html", "<html><p>Cibo distribuito, <a href=\"/\">torna alla home</a>.</p></html>" );
- if (remaining == 0) apri();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement