DÉTECTEUR de CHAT CONNECTÉ

vidéo

Liste des composants

1 capteur de mouvement – Amazon

2 esp01 – amazon

2 Régulateurs ams1117 – amazon

1 module usb – amazon

1 bouton poussoir – amazon

2 plaques pcb – amazon

2 prises pin femelle – amazon

5 leds bleu – amazon

Les schémas

Les fichiers 3D

Le code Adresse MAc

//------------------------------------------------------
//    Code pour trouver l'adresse mac d'un esp8266
//------------------------------------------------------
#include "WiFi.h"
 
void setup(){
  Serial.begin(115200);
  WiFi.mode(WIFI_MODE_STA);
  Serial.println(WiFi.macAddress());
}
 
void loop(){
} 

Le code Émetteur

//------------------------------------------------------
//                https://gammatroniques.fr
//                    08/2020 - by Xmow
//------------------------------------------------------

#include <ESP8266WiFi.h>
#include <espnow.h>

#define SENSORPIN 2

uint8_t broadcastAddress[] = {0x68, 0xC6, 0x3A, 0xF8, 0xB7, 0x39};

typedef struct struct_message {
  bool motion;
} struct_message;

struct_message myData;
bool alreadySend = false;
bool motion;
void setup() {
  pinMode(SENSORPIN, INPUT);
  WiFi.mode(WIFI_STA);

  if (esp_now_init() != 0) {
    return;
  }
  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
  esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}

void loop() {
  delay(100);
  motion = digitalRead(SENSORPIN);
  if (motion) {
    if (!alreadySend) {
      myData.motion = motion;
      esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
      alreadySend = true;
    }
  }
  else {
    delay(100);
    if (alreadySend) {
      myData.motion = false;
      esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
      alreadySend = false;
    }
  }
}
 

Le code Récepteur

//------------------------------------------------------
//                https://gammatroniques.fr
//                    08/2020 - by Xmow
//------------------------------------------------------

#include <ESP8266WiFi.h>
#include <espnow.h>

#define LED 3
typedef struct struct_message {
  bool motion;
} struct_message;

struct_message myData;

bool alreadyFlash = false;
bool motion;
void flashLed(int n) {
  for (int i = 1; i <= n; i++) {
    analogWrite(LED, 150);
    delay(200);
    digitalWrite(LED, LOW);
    delay(200);
  }
  analogWrite(LED, 150);

}

void offLed() {
  digitalWrite(LED, LOW);
}

void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
  memcpy(&myData, incomingData, sizeof(myData));

  /*Serial.print("Bytes received: ");
    Serial.println(len);
    Serial.print("Bool: ");
    Serial.println(myData.motion);
    Serial.println();*/

  motion = myData.motion;
}

void setup() {
  //Serial.begin(115200);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);

  WiFi.mode(WIFI_STA);

  flashLed(2);
  delay(500);

  if (esp_now_init() != 0) {
    return; // ERROR
  }
  esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
  esp_now_register_recv_cb(OnDataRecv);

}

void loop() {
  if (motion) {
    if (!alreadyFlash) {
      flashLed(5);
      alreadyFlash = true;
    }
  } else {
    offLed();
    alreadyFlash = false;
  }
}