Le BetaRobot

De BetaWiki
Aller à la navigation Aller à la recherche

Il s'agit d'un ESP 32 sur lequel est branché un interrupteur (le gros disjoncteur) relié sur GPIO12 et GND. Celui ci envoie une notification sur Slack pour prévenir de la présence dans le lab

 #include <ArduinoJson.h>
 #include <HTTPClient.h>
 #include <WiFiMulti.h>
  
 const char *AP_SSID = "Betamachine_Core";
 const char *AP_PWD = "Azertyuiop";
   
 WiFiMulti wifiMulti;
  const int buttonPin = 12;
 int pinvalue =  2;
 int lastPinValue = 2;
 void setup() {
   Serial.begin(115200);
    
   delay(4000);
   wifiMulti.addAP(AP_SSID, AP_PWD);
    pinMode(buttonPin, INPUT_PULLUP);
 }
  
 void loop() {
       pinvalue = digitalRead(buttonPin);
       if(lastPinValue != pinvalue)
       {
         lastPinValue = pinvalue;
 
       
         if (wifiMulti.run() == WL_CONNECTED) {
      
     HTTPClient http;        
     http.begin("https://hooks.slack.com/services/xxxxxxxxxxxxxxxxxxxx");  
       http.addHeader("Content-Type", "application/json");
 String text = "";
         if(!pinvalue)
         {
           Serial.println("ouvert");
        text = "{\"text\":\"Le Lab est ouvert\"}";        }
         else
         {
         Serial.println("ferme");
          text = "{\"text\":\"Le Lab est fermé\"}";
         }
     int httpResponseCode = http.POST(text);
 
  
     if(httpResponseCode>0){
        
       String response = http.getString();                 
       Serial.println(httpResponseCode);   
       Serial.println(response);
      
     }
     else {     
       Serial.printf("Error occurred while sending HTTP POST: %s\n", http.errorToString(httpResponseCode).c_str());       
     }
      
   }
       
       
       }
       else
       {
       delay(5000);
       Serial.println("Pas de changement, on attend 5s");
       }
 
 }