Project Electronic - WIFI, oled, AT, Pire

  • Explication

 

  • Prerequisites
    • Dossier partager all user

 

  • Manipulation
//Librairie
#include <OLED_I2C.h>
#include<Wire.h>

//Declaration
////Led
int lightRelay = 6;
int lightStart = 7;
int lightStop = 8;
int lightError = 9;
int lightProg = 10;
////Relay
int relay = 5;
boolean stateRelay = false;
//void switchrealy(void);
////Boutton
void bp(void);
int buttonReturn = A1;
int buttonRight = A2;
int buttonValid = A3;
const byte interruptPin = 2;
////Menu
void menu(void);
void sroll(void);
void menu2(void);
void menu3(void);
void menu4(void);
void menu5(void);
void menu6(void);
boolean b1 = false;
boolean b2 = false;
boolean b3 = false;
int etat = 0;
int nbMenu = 6;
////Oled
OLED  myOLED(SDA, SCL);
extern uint8_t SmallFont[];
extern uint8_t MediumNumbers[];
extern uint8_t BigNumbers[];
//////ESP
String ChainR;
boolean OK = false;
int error = 0;
int iESP = 0;
boolean Me = 0;
///////TCP
int stateTCP = 0;
void tcprelay(void);
//////Gyro
const int MPU_addr = 0x68;
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
int minVal = 265;
int maxVal = 402;
double x;
double y;
double z;
//////PIR
int pir = 12;
boolean statePIR = false;
//////SecuBox
int secuBox = 11;
//////TIME
unsigned long int temps, temps1 = 0, temps2, temps3 = 0;
int tempstempo = 3000;
//////Security
boolean stateSecurity = false;
boolean securityState = false;
void security(void);
boolean milSecu = false;
//////Capt
void capt(void);

void setup() {
  //Serial
  Serial.begin(115200);

  //Out
  pinMode(lightRelay, OUTPUT);
  pinMode(lightStart, OUTPUT);
  pinMode(lightStop, OUTPUT);
  pinMode(lightError, OUTPUT);
  pinMode(lightProg, OUTPUT);
  pinMode(relay, OUTPUT);
  //In
  pinMode(buttonReturn, INPUT);
  pinMode(buttonRight, INPUT);
  pinMode(buttonValid, INPUT);
  pinMode(secuBox, INPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  pinMode(pir, INPUT_PULLUP);

  //Oled
  if (!myOLED.begin(SSD1306_128X64))
    while (1);
  myOLED.setFont(SmallFont);
  //Logo
  myOLED.print("CONOIR.COM", CENTER, 30);
  myOLED.drawRoundRect(20, 20, 110, 45);
  myOLED.update();
  delay(3000);
  myOLED.clrScr();
  myOLED.print("SECURITY 2.0", CENTER, 30);
  myOLED.drawRoundRect(20, 20, 110, 45);
  myOLED.update();
  delay(3000);



  //Test
  myOLED.clrScr();
  myOLED.print("TEST CARTE", LEFT, 1);
  myOLED.update();
  
      ////LED
    for (int i = 6; i < 11; i++) {
      myOLED.clrScr();
      myOLED.print("TEST LED", LEFT, 10);
      myOLED.printNumI(i, RIGHT, 10);
      myOLED.update();
      digitalWrite(i, HIGH);
      delay(500);
      digitalWrite(i, LOW);
    }

  ////ESP
  myOLED.clrScr();
  myOLED.print("TEST ESP", RIGHT, 1);
  myOLED.print("TEST CARTE", LEFT, 1);
  myOLED.update();

  while (OK == false) {
    Serial.println("AT");
    delay(500);
    serialEvent();
    myOLED.print(ChainR, LEFT, 20);
    myOLED.update();
    ChainR = "";
  }
  OK = false;
  //////CWMODE
    while (OK == false) {
    Serial.println("AT+CWMODE=1");
    delay(500);
    serialEvent();
    myOLED.print(ChainR, LEFT, 30);
    myOLED.update();
    ChainR = "";
    }
    OK = false;
    //////CWJAP
    while (OK == false) {
    Serial.println("AT+CWJAP=\"LABO03-AP\",\"Tigrou007Labo03\"");
    delay(3000);
    serialEvent();
    myOLED.print(ChainR, LEFT, 40);
    myOLED.update();
    ChainR = "";
    }
    OK = false;
  ////FIN esp
  //Connection TCP
  myOLED.clrScr();
  myOLED.print("TEST CARTE", LEFT, 1);
  myOLED.print("Connect TCP", RIGHT, 1);
  myOLED.update();
  Serial.println("AT+CIPSTART=\"TCP\",\"10.101.103.153\",2222");
  myOLED.print(ChainR, LEFT, 10);
  myOLED.update();

  //Interrupt
  attachInterrupt(digitalPinToInterrupt(interruptPin), bp, FALLING);
}

void loop() {
  menu();
  switchrealy();
  capt();
  security();
  digitalWrite(lightProg, HIGH);
}

//Event
void serialEvent(void) {
  while (Serial.available()) {
    char CaractR = Serial.read();
    ChainR += CaractR;
    if (ChainR.indexOf("OK") >= 0) {
      OK = true;
    }

    if (ChainR.indexOf("+IPD") >= 0) {
      //int 2p = ChainR.indexOf(":");
      ChainR = ChainR.substring(ChainR.indexOf(":"));
      myOLED.clrScr();
      myOLED.print(ChainR, LEFT, 10);
      myOLED.update();
      tcp();
    }
  }
}

//Relay
void switchrealy(void) {
  if (stateRelay) {
    digitalWrite(relay, HIGH);
    stateRelay = true;
    digitalWrite(lightRelay, stateRelay);
  }
  else {
    if (!stateRelay) {
      digitalWrite(relay, LOW);
      stateRelay = false;
      digitalWrite(lightRelay, stateRelay);
    }
  }

  if (statePIR) {
    temps = millis();
    if (temps > (temps1 + 3000)) {
      temps1 = temps;
      stateRelay = false;
      statePIR = false;
    }
  }
}

//TCP
void tcp(void) {
  if (ChainR.indexOf("relayon") >= 0) {
    stateRelay = true;
    ChainR = "";
  }
  else {
    if (ChainR.indexOf("relayoff") >= 0) {
      stateRelay = false;
      ChainR = "";
    }
    else {
      if (ChainR.indexOf("security") >= 0) {
        stateSecurity = true;
        ChainR = "";
      }
      else {
        if (ChainR.indexOf("securoff") >= 0) {
          stateSecurity = false;
          ChainR = "";
        }
      }
    }
  }
}

//SECURITY
void security(void) {
  if (stateSecurity) {
    digitalWrite(lightStop, LOW);
    digitalWrite(lightStart, HIGH);
    if (securityState) {
      digitalWrite(lightError, HIGH);
    }
  }
  else {
    if (!stateSecurity) {
      digitalWrite(lightStop, HIGH);
      digitalWrite(lightStart, LOW);
      digitalWrite(lightError, LOW);
      securityState = false;
    }


  }
}

//CAPT
void capt(void) {
  //secuBOX
  if (!digitalRead(secuBox)) {
    securityState = true;
  }
  //PIR
  if (digitalRead(pir)) {
    securityState = true;
    if (!stateRelay) {
      stateRelay = true;
      statePIR = true;
    }
  }/*
  //GYRO
  if (z < 10 || z > 360) {
    //securityState = true;Serial.println("Gyro");
  }
  
      Wire.beginTransmission(MPU_addr);
      Wire.write(0x3B);
      Wire.endTransmission(false);
      //Wire.requestFrom(MPU_addr, 14, true);
      AcX = Wire.read() << 8 | Wire.read();
      AcY = Wire.read() << 8 | Wire.read();
      AcZ = Wire.read() << 8 | Wire.read();
      int xAng = map(AcX, minVal, maxVal, -90, 90);
      int yAng = map(AcY, minVal, maxVal, -90, 90);
      int zAng = map(AcZ, minVal, maxVal, -90, 90);

      x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
      y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
      z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);
  */
}

//bp
void bp(void) {
  //delay(50);
  if (analogRead(buttonReturn) < 800)
  {
    if (b1 == 0)
      etat --;
  }

  if (analogRead(buttonRight) < 800)
  {
    if (b2 == 0)
      b2 = 1;
  }

  if (analogRead(buttonValid) < 800 && etat > 0)
  {
    if (b3 == 0)
      b3 = 1;
    else b3 = 0;
  }
}

//menu
void menu(void) {
  if (b1) {
    if (etat == 1) {
      etat = 0;
      b1 = 0;
    }
    else etat -- ;
  }

  if (b2) {
    if (etat < nbMenu) {
      etat ++;
      b2 = 0;
    }
    else {
      etat = 0;
      b2 = 0;
    }
  }

  if (etat == 0 ) {
    scroll();
  }
  if (etat == 1 ) {
    menu2();
  }
  if (etat == 2 ) {
    menu3();
  }
  if (etat == 3 ) {
    menu4();
  }
  if (etat == 4 ) {
    menu5();
  }
  if (etat == 5 ) {
    menu6();
  }

}

//Affiche Menu Home
void scroll(void) {
  myOLED.clrScr();
  myOLED.setFont(SmallFont);
  myOLED.print("Conoir.com", LEFT, 0); myOLED.print("Security 2.0", RIGHT, 0);
  myOLED.print("Programme", LEFT, 20);   if (!stateSecurity)myOLED.print("Stop", RIGHT, 20); if (stateSecurity)myOLED.print("Start", RIGHT, 20);
  myOLED.print("Relay", LEFT, 10); if (!stateRelay)myOLED.print("Stop", RIGHT, 10); if (stateRelay)myOLED.print("Start", RIGHT, 10);
  myOLED.print("ERROR", LEFT, 30);   if (!securityState)myOLED.print("NO ERROR", RIGHT, 30); if (securityState)myOLED.print("ERROR !!!!", RIGHT, 30);
  myOLED.print("Return", LEFT, 54); myOLED.print("RIGHT", CENTER, 54); myOLED.print("VALID", RIGHT, 54);
  myOLED.update();
}

//Affiche Menu2 Security
void menu2(void) {
  myOLED.clrScr();
  myOLED.setFont(SmallFont);
  myOLED.print("2 - Security", LEFT, 0); myOLED.print("Security 2.0", RIGHT, 10);
  myOLED.print("Programme Security", LEFT, 10);
  myOLED.print(temps, LEFT, 30);
  myOLED.print("Return", LEFT, 54); myOLED.print("RIGHT", CENTER, 54); myOLED.print("VALID", RIGHT, 54);
  myOLED.update();
  if (b3 && stateSecurity == false) {

    b3 = 0;
    stateSecurity = true;

    temps2 = millis();
    if (temps2 > (temps3 + 3000)) {
      temps3 = temps2;
      milSecu = true;
    }
  }


  else {
    if (b3 && stateSecurity) {

      b3 = 0;
      stateSecurity = false;
      milSecu = false;
    }

  }
  if (!stateSecurity)myOLED.print("Stop", RIGHT, 20);
  if (stateSecurity)myOLED.print("Start", RIGHT, 20);
  myOLED.update();
}

//Affiche Menu3 TCP
void menu3(void) {
  myOLED.clrScr();
  myOLED.setFont(SmallFont);
  myOLED.print("3 - TCP", LEFT, 0); myOLED.print("Security 2.0", RIGHT, 0);
  myOLED.print("Envoi tcp", LEFT, 10);
  myOLED.print("Return", LEFT, 54); myOLED.print("RIGHT", CENTER, 54); myOLED.print("VALID", RIGHT, 54);
  myOLED.update();
  if (b3 && stateTCP == 0) {
    b3 = 0;
    stateTCP = 1;
    myOLED.print("Initialize", LEFT, 30);
    myOLED.update();
    Serial.println("AT+CIPSEND=4");
    delay(1000);

    myOLED.print("Send", LEFT, 40);
    myOLED.update();
    Serial.println("toto");
    delay(1000);
    stateTCP = 0;
  }

  if (stateTCP == 0)myOLED.print("Send ?", RIGHT, 10);

  myOLED.update();
}

//Affiche Menu4 Relay
void menu4(void) {
  myOLED.clrScr();
  myOLED.setFont(SmallFont);
  myOLED.print("4 - Relay", LEFT, 0); myOLED.print("Security 2.0", RIGHT, 0);
  myOLED.print("Relay", LEFT, 10);
  myOLED.print("Return", LEFT, 54); myOLED.print("RIGHT", CENTER, 54); myOLED.print("VALID", RIGHT, 54);
  if (b3 && stateRelay == 0) {
    b3 = 0;
    stateRelay = 1;
  }
  else {
    if (b3 && stateRelay == 1) {
      b3 = 0;
      stateRelay = 0;
    }
  }
  if (stateRelay == 0)myOLED.print("Stop", RIGHT, 10);
  if (stateRelay == 1)myOLED.print("Start", RIGHT, 10);
  myOLED.update();
}

//Affiche Menu5 GYRO
void menu5(void) {
  myOLED.clrScr();
  myOLED.setFont(SmallFont);
  myOLED.print("5 - GYRO", LEFT, 0); myOLED.print("Security 2.0", RIGHT, 0);
  myOLED.printNumI(x, LEFT, 10);
  myOLED.printNumI(y, LEFT, 20);
  myOLED.printNumI(z, LEFT, 30);
  myOLED.print("Return", LEFT, 54); myOLED.print("RIGHT", CENTER, 54); myOLED.print("VALID", RIGHT, 54);
  myOLED.update();
}

//Affiche Menu6 AFFICHE
void menu6(void) {
  myOLED.clrScr();
  myOLED.setFont(SmallFont);
  myOLED.print("6 - GYRO", LEFT, 0); myOLED.print("Security 2.0", RIGHT, 0);
  myOLED.print(ChainR, RIGHT, 10);
  myOLED.print("Return", LEFT, 54); myOLED.print("RIGHT", CENTER, 54); myOLED.print("VALID", RIGHT, 54);
  myOLED.update();
}
  • Conclusion