Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32-Based Wi-Fi Controlled LED System

View and Edit Project In Cirkit DesignerImage of ESP32-Based Wi-Fi Controlled LED System

Circuit Documentation

Summary

This circuit involves two ESP32 microcontrollers, a resistor, and a red LED. The ESP32 microcontrollers communicate with each other and control the LED through a series of connections. The circuit also includes a resistor to limit the current flowing through the LED.

Component List

ESP32 38 PINS

Pocket ESP32-C3

Resistor

LED: Two Pin (red)

Wiring Details

ESP32 38 PINS

Pocket ESP32-C3

Resistor

LED: Two Pin (red)

Documented Code

ESP32 38 PINS Code (Slave1.ino)

//Boards Manager esp32 by Espressif Systems Ver 3.0.4
#include <EEPROM.h>
#include "Wire.h"

#define slaveno 1
#define I2C_DEV_ADDR 0x30+slaveno  //0x11=Slave 1, 0x12=Slave 2, 0x13=Slave 3

#define LED 12                      //Slave1 to Slave10 LED=12, Slave0=2
#define stbt 2                    //Slave1 to Slave10 LED=2, Slave0=12
#define RXD2 16
#define TXD2 17

String inputString = "";
String inputCommands = "";
bool stringComplete = false;
char inString[] = "";
int temp[10];

String inputString2 = "";
String inputCommands2 = "";
bool stringComplete2 = false;
char inString2[] = "";
int temp2[10];

int buff[4] = {0,0,0,0};  
int value;
int coutp = 0;
int coutn = 0;
int sendcout = 0;
int after4bytecout = 0;
bool triggerFlag = 0;           

int triggercouter = 1;
int triggerBuff[100];
bool startflag = 0;
bool svstartflag = 0;
bool wutflag = 0;
bool testpir = 0;

void serialEvent() {  
  while (Serial.available()) {     
    char inChar = (char)Serial.read();        
    inputString += inChar;    
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

void serialEvent2() {
  while (Serial2.available()) {
    char inChar = (char)Serial2.read();        
    inputString2 += inChar;
    if (inChar == '\n') {
      stringComplete2 = true;
    }
  }
}

uint32_t i = 0;

void send4byte(int value){
  byte b = 0;
  b |= value;
  Wire.write(b);
  b = 0;
  b |= value >> 8;
  Wire.write(b);
  b = 0;
  b |= value >> 16;
  Wire.write(b);
  b = 0;
  b |= value >> 24;
  Wire.write(b);
}

void onRequest(){
  uint8_t error = 1;
  Serial.print("triggerBuff[1] = b'");
  for(int i=0;i<triggerBuff[1]+2;i++){
    send4byte(triggerBuff[i]);
  }
  error = Wire.endTransmission(true);
  Serial.print("onRequest erroe = ");
  Serial.println(error);
  Serial.println("");
}

void onReceive(int len){
  char str[len];
  Serial.printf("onReceive[%d]: ", len); 
  for(int i=0;i<len;i++){
    str[i] = Wire.read();
  }
  for(int i=0;i<len;i++){
    Serial.print(str[i]);
  }
  Serial.println("");
  if(str[0] == 115 && str[1] == 116 && str[2] == 111 && str[3] == 112){
    startflag = 0; 
    wutflag = 0;
    if(triggercouter > 0)digitalWrite(LED, HIGH);
  }
  if(str[0] == 119 && str[1] == 117 && str[2] == 116){
    testpir = 0;   
    triggercouter = 0;
    wutflag = 1;
    digitalWrite(LED, LOW);
  }
  if(str[0] == 115 && str[1] == 116 && str[2] == 97 && str[3] == 114 && str[4] == 116){
    triggercouter = 0;
    for(int i = 1;i<100;i++){
      triggerBuff[i] = 0;
    }
    startflag = 1;
    after4bytecout = 0;
    digitalWrite(LED, LOW);
  }
  if (str[0] == 99 && str[1] == 111 && str[2] == 117 && str[3] == 116){
      String s1 = "";
      String s2 = "-";
      for (int i = 5;i <= 10; i++){
        if (str[i] == 0)break;
        s1 += char(str[i]);      
      }
      coutp = s1.toInt();
      triggerBuff[0] = coutp;
      EEPROM.put(0, coutp); 
      EEPROM.commit();
      s2 += s1;
      coutn = s2.toInt();
      EEPROM.put(sizeof(coutp), coutn); 
      EEPROM.commit();
      Serial.println(coutp);
      Serial.println(coutn);
    }
  if(str[0] == 116 && str[1] == 114 && str[2] == 103){
    if (str[4] == 49)testpir = 1;
    if (str[4] == 48)testpir = 0;    
  }
}

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  Serial2.begin(57600, SERIAL_8N1, RXD2, TXD2);
  EEPROM.begin(32);
  EEPROM.get(0, coutp);
  EEPROM.get(sizeof(coutp), coutn);
  triggerBuff[0] = coutp;
  Serial.println(coutp);
  Serial.println(coutn);
  Wire.onReceive(onReceive);
  Wire.onRequest(onRequest);
  Wire.begin((uint8_t)I2C_DEV_ADDR);

  #if CONFIG_IDF_TARGET_ESP32
    char message[64];
    snprintf(message, 64, "%u Packets.", i++);
    Wire.slaveWrite((uint8_t *)message, strlen(message));
  #endif
  pinMode(LED, OUTPUT);
  pinMode(stbt, INPUT_PULLUP);
  pinMode(TXD2, INPUT_PULLUP);
  digitalWrite(LED, LOW);
}

void loop() {  
  uart_in();
  if (startflag || svstartflag){
    signal_in();
  }

  if(triggerc