Updating Readme file, and src files

This commit is contained in:
fatimaxsen committed 2025-05-12 11:24:46 +03:00
1 parent 2ea33655a5
commit 400a727892
10 files changed
+239 -272

No files matched your search

+91 -51
View File
@@ -1,65 +1,105 @@
#include <Wire.h>
#include <EEPROM.h>
// EEPROM settings
#define ADDR_EEPROM_LOC 0
#define EEPROM_SIZE 64
#define DEFAULT_ADDRESS 0x08
#define MAX_NAME_LENGTH 24
// Relay pins: GPIO0 to GPIO7
// EEPROM Structure
struct Config {
uint8_t address;
char deviceName[MAX_NAME_LENGTH+1];
uint8_t relayStates;
};
Config config;
const int relayPins[8] = {0, 1, 2, 3, 4, 5, 6, 7}; // GPIOs 0-7
uint8_t i2cAddress;
// Unified I2C receive event
void receiveEvent(int howMany) {
if (howMany >= 2) {
uint8_t first = Wire.read();
uint8_t second = Wire.read();
// Check for address change command
if (first == 0xAA) {
// Change I2C address and store in EEPROM
uint8_t newAddr = second;
EEPROM.write(ADDR_EEPROM_LOC, newAddr);
EEPROM.commit();
Serial.print("I2C address changed to: 0x");
Serial.println(newAddr, HEX);
delay(100);
ESP.restart();
return;
}
// Otherwise, treat as relay control
int relay = first; // Relay number: 1-8
int state = second; // State: 1=ON, 0=OFF
if (relay >= 1 && relay <= 8) {
digitalWrite(relayPins[relay - 1], state ? LOW : HIGH); // Active LOW relays
}
}
}
void handleCommand(int numBytes);
void saveConfig();
void loadConfig();
void emergencyStop();
void setup() {
Serial.begin(115200);
EEPROM.begin(EEPROM_SIZE);
// Read I2C address from EEPROM or use default
i2cAddress = EEPROM.read(ADDR_EEPROM_LOC);
if (i2cAddress == 0xFF || i2cAddress == 0) i2cAddress = 0x08; // Default address
Serial.print("Using I2C address: 0x");
Serial.println(i2cAddress, HEX);
// Initialize relay pins
for (int i = 0; i < 8; i++) {
EEPROM.begin(sizeof(Config));
loadConfig();
// Initialize relays
for(int i=0; i<8; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], HIGH); // Relays OFF initially (active LOW)
digitalWrite(relayPins[i], HIGH); // Active-low logic
}
// Start I2C slave
Wire.begin(i2cAddress);
Wire.onReceive(receiveEvent);
Wire.onReceive(handleCommand);
Wire.onRequest([]() {
String response = String(config.address) + ",8-relay," +
config.deviceName + "," +
String(config.relayStates, BIN);
Wire.print(response);
});
Wire.begin((uint8_t)config.address);
}
void loop() {
// Nothing needed here for basic operation
void loop() { /* Empty */ }
void handleCommand(int numBytes) {
uint8_t cmd = Wire.read();
switch(cmd) {
case 0xA0: // Set relay state
if(numBytes == 3) {
uint8_t relay = Wire.read();
uint8_t state = Wire.read();
if(relay < 8) {
digitalWrite(relayPins[relay], state ? LOW : HIGH);
bitWrite(config.relayStates, relay, state);
}
}
break;
case 0xB0: // Emergency stop
emergencyStop();
break;
case 0xC0: // Set address
if(numBytes == 2) {
config.address = Wire.read();
saveConfig();
Wire.begin((uint8_t)config.address);
}
break;
case 0xD0: // Set device name
if(numBytes > 1) {
uint8_t i = 0;
while(Wire.available() && i < MAX_NAME_LENGTH) {
config.deviceName[i++] = Wire.read();
}
config.deviceName[i] = '\0';
saveConfig();
}
break;
}
}
void emergencyStop() {
for(int i=0; i<8; i++) {
digitalWrite(relayPins[i], HIGH);
}
config.relayStates = 0;
}
void loadConfig() {
EEPROM.get(0, config);
if(config.address < 0x08 || config.address > 0x77) {
config.address = DEFAULT_ADDRESS;
strcpy(config.deviceName, "8-Relay Module");
saveConfig();
}
}
void saveConfig() {
EEPROM.put(0, config);
EEPROM.commit();
}