Tags
Pages dans la catégorie :
Tags
Pages dans la catégorie :
Ceci est une ancienne révision du document !
l'idée est d'automatiser l'ouverture et la fermeture de de la porte du poulailler Poulpidou… le matin , le soir… avec une commande OSC en WiFi
Je pars maintenant sur la L298N
Connexion
Commande OSC par exemple ./osc.py send 192.168.1.31:9081 /ouvre
Reste à faire le flow nodered ou une commande cron/bash sur le serveur
/* Poulpidou gepeto@du-libre.org 2018 commande de porte de poulaillé , commande par Wifi / OSC la commande OSC /ouvre /ferme /status // a cause de OSC sur purdata on garde que des float ! sorties : 1, 2, 4, 5, 6, 7 //ok pour 1,2,4,5,6,7 Matériel: ESP Wemos + driver wemos moteur + platine wemos alimentation */ #include <ESP8266WiFi.h> //#include <ESP8266WiFiMulti.h> #include <ESP8266mDNS.h> #include <WiFiUdp.h> //#include <DNSServer.h> // OSC #include <OSCMessage.h> #include <OSCBundle.h> #include <OSCData.h> // //ESP8266WiFiMulti wifiMulti; //Brochage // Driver moteur - NodeCU const int enA = D7; //wemos D7 const int in1 = D6; //wemos D6 const int in2 = D5; //wemos D5 int noporte = 1; IPAddress porte_ip(192, 168, 43, 121); //IPAddress porte_ip(192, 168, 0, 121); const char* nomhost = "porte-1"; IPAddress MonitIP(192, 168, 0, 99); // IP et port du moniteur char* OSCVar = "/LOCO-XX "; const unsigned int inPort = 9081; const unsigned int MonitPort = 9998; // suivi normal const char* ssid1 = "NORD"; const char* password1 = "blabla"; const char* ssid2 = "SUD"; const char* password2 = "blabla"; const char* ssid3 = "kerminy"; const char* password3 = "blabla"; const char* ssid4 = "BIBLIOTEQUE"; const char* password4 = "blabla"; /////// FIN ////////////////////////////////////////////// ////////////////////////////////////////////////////////// IPAddress mask(255, 255, 255, 0); //IPAddress passerelle(192, 168, 1, 80); IPAddress passerelle(192, 168, 1, 13); const char DEVICE_NAME[] = "PORTE"; //D0-16 //D1 5 //D2 4 up (led) //D1 0 up bouton ferme //D2 2 up bouton ouvre //D5 14 in2 / //D6 12 in1 / -> L298N platine //D7 13 enA / //D8 15 down //└─ $ ▶ ./osc.py send 192.168.0.121:9005 /ouvre ... bool ouvert = false; bool ferme = false; bool ouvre_en_cours = false; bool ferme_en_cours = false; float temps_maxi = 30000; // temps maxi ouverture porte float temps_milli = 0; // uint64_t minute_dodo = 5; // min ESP.deepSleep(1000000 * 60 * (uint64_t)min); float temps_debut = 0; WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP void dodo() { digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH Serial.print("dodo min :"); Serial.println((float)minute_dodo); // Connect D0 to RST to wake up ! ESP.deepSleep(minute_dodo * 60 * 1000000); } //converts the pin to an osc address char * numToOSCAddress(int pin) { static char s[10]; int i = 9; s[i--] = '\0'; do { s[i] = "0123456789"[pin % 10]; --i; pin /= 10; } while (pin && i); s[i] = '/'; return &s[i]; } void MonitMsg(char debugbla[], int debugint) { sprintf(OSCVar, "/PORTE-%d/%s", noporte, debugbla); OSCMessage envoi(OSCVar); //envoi.add(debugbla); envoi.add(debugint); Udp.beginPacket(MonitIP, MonitPort); envoi.send(Udp); Udp.endPacket(); envoi.empty(); } void Envoyer_status(OSCMessage & msg) { if (ouvert == true ) MonitMsg("OUVERT OK", 1); if (ferme == true ) MonitMsg("FERME OK", 1); } void Ouvre(OSCMessage & msg) { Serial.print("Ouvre"); if (digitalRead(D2) == LOW && ouvert == false) { analogWrite(enA, 0); // stop MonitMsg("OUVRE OK", temps_debut); ouvert = true; ferme = false; Serial.println(" en butee"); } else { digitalWrite(in1, LOW); digitalWrite(in2, HIGH); analogWrite(enA, 100); temps_debut = millis(); // on arme le temps pour stop si trop long MonitMsg("OUVRE", temps_debut); ouvre_en_cours = true; ferme_en_cours = false; } } void Ferme(OSCMessage & msg) { Serial.print("Ferme"); if (digitalRead(D1) == LOW && ferme == false) { analogWrite(enA, 0); // stop MonitMsg("FERME OK", temps_debut); ouvert = false; ferme = true; Serial.println(" en butee"); } else { digitalWrite(in1, HIGH); digitalWrite(in2, LOW); analogWrite(enA, 100); temps_debut = millis(); // on arme le temps pour stop si trop long MonitMsg("FERME", temps_debut); ouvre_en_cours = false; ferme_en_cours = true; } } void Arreter(OSCMessage & msg) { analogWrite(enA, 0); digitalWrite(in1, LOW); digitalWrite(in2, LOW); Serial.println("Stop"); ouvre_en_cours = false; ferme_en_cours = false; } void setup(void) { Serial.begin(115200); //set led pin as output pinMode(enA, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(D2, INPUT_PULLUP); pinMode(D1, INPUT_PULLUP); Serial.println((int32_t)ESP.getChipId()); delay(1000); //int BoutonParam = analogRead(A0); // start ticker with 0.5 because we start in AP mode and try to connect //ticker.attach(0.6, tick);// clignote // a cause de OSC sur purdata on garde que des float ! Serial.println(F("/ouvert")); Serial.println(F("/ferme")); Serial.println(F("/status")); temps_milli = millis(); pinMode(D0, WAKEUP_PULLUP); WiFi.disconnect(); WiFi.mode(WIFI_STA); /*wifiMulti.addAP(ssid1, password1); wifiMulti.addAP(ssid2, password2); wifiMulti.addAP(ssid3, password3); wifiMulti.addAP(ssid4, password4); Serial.print("tente "); Serial.println(ssid1); Serial.println(ssid2); Serial.println(ssid3); Serial.println(ssid4); */ WiFi.hostname(nomhost); // DHCP Hostname (useful for finding device for static lease) WiFi.begin(ssid2, password1); WiFi.config(porte_ip, passerelle, mask); // (DNS not required) //while (wifiMulti.run() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(F("TOUT OK WiFi connected! IP address: ")); Serial.println(WiFi.localIP()); Serial.println(F(" ")); Serial.print(WiFi.hostname()); // Serial.println(F(".local"));porte-1.kerminy.org Serial.println(F(".local ou porte-1.kerminy.org")); Serial.print(F("sur port :")); Serial.println(inPort); delay(100); Udp.begin(inPort); Serial.print(F("Local port: ")); Serial.println(Udp.localPort()); // Set up mDNS responder: // - first argument is the domain name, in this example // the fully-qualified domain name is "esp8266.local" // - second argument is the IP address to advertise // we send our IP address on the WiFi network if (!MDNS.begin(WiFi.hostname())) { Serial.println("Error setting up MDNS responder!"); } Serial.println("mDNS responder started"); if (analogRead(A0) > 10) Ouvre; } void loop() { //////////// OSC //////////////////////// //reads and dispatches the incoming message OSCMessage msgIN; int size; //char puce_char[] = " "; if ( (size = Udp.parsePacket()) > 0) { while (size--) msgIN.fill(Udp.read()); if (!msgIN.hasError()) { msgIN.dispatch("/ouvre", Ouvre); msgIN.dispatch("/ferme", Ferme); msgIN.dispatch("/status", Envoyer_status); msgIN.dispatch("/stop", Arreter); Serial.println(size); } else { Serial.print("error: "); Serial.println(msgIN.getError()); } Udp.flush(); } if (digitalRead(D2) == LOW ) { analogWrite(enA, 0); // stop ouvre_en_cours = false; MonitMsg("OUVERT OK", temps_debut); ouvert = true; ferme = false; } if (digitalRead(D1) == LOW ) { analogWrite(enA, 0); // stop ferme_en_cours = false; MonitMsg("FERME OK", temps_debut); ferme = true; ouvert = false; } //if (ouvre_en_cours || ferme_en_cours) Serial.println(millis() - temps_debut); if (digitalRead(D2) == LOW ) Serial.println("OUVERT"); if (digitalRead(D1) == LOW ) Serial.println("FERME"); if (ouvre_en_cours == true) Serial.println ("0uvre en cours"); if (ferme_en_cours == true) Serial.println ("Ferme en cours"); delay(500); }
J'etais parti sur la platine wemos sur TB6612 qui encaisse le moteur choisi (0,5A 12V) avec une autre platine d'alimentation DC shield le tout sur une wemos D1 pro, plus simple compacte et avec une bonne portée.
Mais je l'abandonne suite a de nombreux bug, elle se plante trop souvent
Pinout:
VM………Motor Power Supply +(Max 15Vdc)
GND…….Motor Power Supply -
A1 A2…..Motor A
B1 B2…..Motor B
S………..Standby Control Mode
I2C Mode: Control TB6612's STBY with I2C protocol
IO Mode: Control TB6612's STBY with “S” pin
/* Poulpidou gepeto@du-libre.org 2018 commande de porte de poulaillé , commande par Wifi / OSC la commande OSC /ouvre /ferme /status // a cause de OSC sur purdata on garde que des float ! sorties : 1, 2, 4, 5, 6, 7 //ok pour 1,2,4,5,6,7 Matériel: ESP Wemos + driver wemos moteur + platine wemos alimentation */ #include <ESP8266WiFi.h> //#include <ESP8266WiFiMulti.h> #include <ESP8266mDNS.h> #include <WiFiUdp.h> //#include <DNSServer.h> // OSC #include <OSCMessage.h> #include <OSCBundle.h> #include <OSCData.h> // #include "WEMOS_Motor.h" // Shiel motor V1.0 int pwm; //Motor shiled I2C Address: 0x30 //PWM frequency: 1000Hz(1kHz) Motor M1(0x30, _MOTOR_A, 1000); //Motor A //ESP8266WiFiMulti wifiMulti; int noporte = 1; IPAddress porte_ip(192, 168, 43, 121); //IPAddress porte_ip(192, 168, 0, 121); const char* nomhost = "porte-1"; IPAddress MonitIP(192, 168, 0, 99); // IP et port du moniteur char* OSCVar = "/LOCO-XX "; const unsigned int inPort = 9081; const unsigned int MonitPort = 9998; // suivi normal const char* ssid6 = "NORD"; const char* passworD6 = "blabla"; const char* ssid7 = "SUD"; const char* passworD7 = "blabla"; const char* ssid3 = "kerminy"; const char* password3 = "blabla"; const char* ssid4 = "BIBLIOTEQUE"; const char* password4 = "blabla"; /////// FIN ////////////////////////////////////////////// ////////////////////////////////////////////////////////// IPAddress mask(255, 255, 255, 0); //IPAddress passerelle(192, 168, 1, 80); IPAddress passerelle(192, 168, 1, 13); const char DEVICE_NAME[] = "PORTE"; //D1 / i2C //D2 / //D7 4 up (led) //D6 0 up bouton ferme //D7 2 up bouton ouvre //└─ $ ▶ ./osc.py send 192.168.0.121:9005 /ouvre ... bool ouvert = false; bool ferme = false; bool ouvre_en_cours = false; bool ferme_en_cours = false; float temps_maxi = 30000; // temps maxi ouverture porte float temps_milli = 0; // uint64_t minute_dodo = 5; // min ESP.deepSleep(1000000 * 60 * (uint64_t)min); float temps_debut = 0; WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP void dodo() { digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH M1.setmotor(_STANDBY);//Both Motor standby Serial.print("dodo min :"); Serial.println((float)minute_dodo); // Connect D0 to RST to wake up ! ESP.deepSleep(minute_dodo * 60 * 1000000); } //converts the pin to an osc address char * numToOSCAddress(int pin) { static char s[10]; int i = 9; s[i--] = '\0'; do { s[i] = "0123456789"[pin % 10]; --i; pin /= 10; } while (pin && i); s[i] = '/'; return &s[i]; } void MonitMsg(char debugbla[], int debugint) { sprintf(OSCVar, "/PORTE-%d/%s", noporte, debugbla); OSCMessage envoi(OSCVar); //envoi.add(debugbla); envoi.add(debugint); Udp.beginPacket(MonitIP, MonitPort); envoi.send(Udp); Udp.endPacket(); envoi.empty(); } void Envoyer_status(OSCMessage & msg) { Serial.println("Stop"); if (ouvert == true ) MonitMsg("OUVERT OK", 1); if (ferme == true ) MonitMsg("FERME OK", 1); } void Ouvre(OSCMessage & msg) { Serial.println("Ouvre"); M1.setmotor(_SHORT_BRAKE); delay(200); if (digitalRead(D7) == LOW && ouvert == false) { MonitMsg("OUVRE OK", temps_debut); ouvert = true; ferme = false; Serial.println(" en butee"); } else { M1.setmotor( _CW, 0); // ouvre delay(200); M1.setmotor( _CCW, 0); // ferme delay(200); M1.setmotor( _CW, 10); // ouvre temps_debut = millis(); // on arme le temps pour stop si trop long MonitMsg("moteur", temps_debut); ouvre_en_cours = true; ferme_en_cours = false; Serial.println("moteur"); } } void Ferme(OSCMessage & msg) { Serial.println("Ferme"); M1.setmotor(_SHORT_BRAKE); delay(200); if (digitalRead(D6) == LOW && ferme == false) { MonitMsg("FERME OK", temps_debut); ouvert = false; ferme = true; Serial.println(" en butee"); } else { M1.setmotor( _CW, 0); // ouvre delay(200); M1.setmotor( _CCW, 0); // ferme delay(200); M1.setmotor( _CCW, 10); // ferme temps_debut = millis(); // on arme le temps pour stop si trop long MonitMsg("moteur", temps_debut); ouvre_en_cours = false; ferme_en_cours = true; Serial.println("moteur"); } } void Arreter(OSCMessage & msg) { M1.setmotor(_SHORT_BRAKE); delay(200); ouvre_en_cours = false; ferme_en_cours = false; Serial.println("stop"); } void setup(void) { Serial.begin(115200); //set led pin as output pinMode(D7, INPUT_PULLUP); pinMode(D6, INPUT_PULLUP); Serial.println((int32_t)ESP.getChipId()); delay(1000); //int BoutonParam = analogRead(A0); // start ticker with 0.5 because we start in AP mode and try to connect //ticker.attach(0.6, tick);// clignote // a cause de OSC sur purdata on garde que des float ! Serial.println(F("/ouvert")); Serial.println(F("/ferme")); Serial.println(F("/status")); temps_milli = millis(); pinMode(D0, WAKEUP_PULLUP); WiFi.disconnect(); WiFi.mode(WIFI_STA); /*wifiMulti.addAP(ssid6, passworD6); wifiMulti.addAP(ssid7, passworD7); wifiMulti.addAP(ssid3, password3); wifiMulti.addAP(ssid4, password4); Serial.print("tente "); Serial.println(ssid6); Serial.println(ssid7); Serial.println(ssid3); Serial.println(ssid4); */ WiFi.hostname(nomhost); // DHCP Hostname (useful for finding device for static lease) WiFi.begin(ssid7, passworD6); WiFi.config(porte_ip, passerelle, mask); // (DNS not required) //while (wifiMulti.run() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(F("TOUT OK WiFi connected! IP address: ")); Serial.println(WiFi.localIP()); Serial.println(F(" ")); Serial.print(WiFi.hostname()); // Serial.println(F(".local"));porte-1.kerminy.org Serial.println(F(".local ou porte-1.kerminy.org")); Serial.print(F("sur port :")); Serial.println(inPort); delay(100); Udp.begin(inPort); Serial.print(F("Local port: ")); Serial.println(Udp.localPort()); // Set up mDNS responder: // - first argument is the domain name, in this example // the fully-qualified domain name is "esp8266.local" // - second argument is the IP address to advertise // we send our IP address on the WiFi network if (!MDNS.begin(WiFi.hostname())) { Serial.println("Error setting up MDNS responder!"); } Serial.println("mDNS responder started"); ouvre_en_cours = false; ferme_en_cours = false; if (analogRead(A0) > 10) Ouvre; } void loop() { //////////// OSC //////////////////////// //reads and dispatches the incoming message OSCMessage msgIN; int size; //char puce_char[] = " "; if ( (size = Udp.parsePacket()) > 0) { while (size--) msgIN.fill(Udp.read()); if (!msgIN.hasError()) { msgIN.dispatch("/ouvre", Ouvre); msgIN.dispatch("/ferme", Ferme); msgIN.dispatch("/status", Envoyer_status); msgIN.dispatch("/stop", Arreter); Serial.println(size); } else { Serial.print("error: "); Serial.println(msgIN.getError()); } Udp.flush(); } if (digitalRead(D7) == LOW ) { // M1.setmotor(_STOP); // stop // delay(200); M1.setmotor(_STANDBY);//Both Motor standby delay(200); ouvre_en_cours = false; MonitMsg("OUVERT OK", temps_debut); ouvert = true; ferme = false; } if (digitalRead(D6) == LOW ) { // M1.setmotor(_STOP); // stop // delay(200); M1.setmotor(_STANDBY);//Both Motor standby delay(200); ferme_en_cours = false; MonitMsg("FERME OK", temps_debut); ferme = true; ouvert = false; } //if (ouvre_en_cours || ferme_en_cours) Serial.println(millis() - temps_debut); if (digitalRead(D7) == LOW ) Serial.println("OUVERT"); if (digitalRead(D6) == LOW ) Serial.println("FERME"); if (ouvre_en_cours == true) Serial.println ("0uvre en cours"); if (ferme_en_cours == true) Serial.println ("Ferme en cours"); delay(500); }