Updating Readme file, and src files
This commit is contained in:
1 parent
2ea33655a5
commit
400a727892
10 files changed
+239
-272
No files matched your search
+12
-54
@@ -1,65 +1,23 @@
|
||||
#include <Wire.h>
|
||||
#include <I2CRelay.h>
|
||||
#include <I2CMasterUtils.h>
|
||||
|
||||
I2CRelay relayMaster(0x08); // Default address, can be changed via setAddress()
|
||||
I2CRelay relay;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
relayMaster.begin();
|
||||
Serial.println("Enter:");
|
||||
Serial.println(" s - to scan I2C bus");
|
||||
Serial.println(" ch <old> <new> - to change slave address");
|
||||
Serial.println(" <relay> <on/off> - to set relay (e.g. 3 on)");
|
||||
relay.begin();
|
||||
Serial.println("8-Relay I2C Master Ready.");
|
||||
Serial.println("Commands:");
|
||||
Serial.println(" r <n> <0|1> - Set relay n (1-8) OFF/ON");
|
||||
Serial.println(" all <0|1> - Set all relays OFF/ON");
|
||||
Serial.println(" name <newname> - Set device name");
|
||||
Serial.println(" addr <newaddr> - Set I2C address (8-119)");
|
||||
Serial.println(" id - Query identification string");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (Serial.available()) {
|
||||
String input = Serial.readStringUntil('\n');
|
||||
input.trim();
|
||||
|
||||
if (input == "s") {
|
||||
I2CMasterUtils::scanI2C();
|
||||
} else if (input.startsWith("ch")) {
|
||||
uint8_t oldAddr, newAddr;
|
||||
if (I2CMasterUtils::parseChangeAddressCommand(input, oldAddr, newAddr)) {
|
||||
I2CMasterUtils::changeSlaveAddress(oldAddr, newAddr);
|
||||
relayMaster.setAddress(newAddr); // Update master's address
|
||||
Serial.print("Changed address to 0x");
|
||||
Serial.println(newAddr, HEX);
|
||||
} else {
|
||||
Serial.println("Invalid command format. Use: ch <old_addr> <new_addr>");
|
||||
}
|
||||
} else {
|
||||
int spaceIndex = input.indexOf(' ');
|
||||
if (spaceIndex > 0) {
|
||||
String relayStr = input.substring(0, spaceIndex);
|
||||
String stateStr = input.substring(spaceIndex + 1);
|
||||
|
||||
int relayNum = relayStr.toInt();
|
||||
bool state = false;
|
||||
|
||||
stateStr.toLowerCase();
|
||||
if (stateStr == "on") state = true;
|
||||
else if (stateStr == "off") state = false;
|
||||
else {
|
||||
Serial.println("Invalid state! Use 'on' or 'off'.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (relayNum >= 1 && relayNum <= 8) {
|
||||
relayMaster.setRelay(relayNum, state);
|
||||
Serial.print("Relay ");
|
||||
Serial.print(relayNum);
|
||||
Serial.print(" turned ");
|
||||
Serial.println(state ? "ON" : "OFF");
|
||||
} else {
|
||||
Serial.println("Invalid relay number! Use 1-8.");
|
||||
}
|
||||
} else {
|
||||
Serial.println("Invalid command! Format: <relay_number> <on/off>");
|
||||
}
|
||||
}
|
||||
String cmd = Serial.readStringUntil('\n');
|
||||
cmd.trim();
|
||||
relay.handleSerialCommand(cmd);
|
||||
}
|
||||
}
|
||||
+91
-51
@@ -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();
|
||||
}
|
||||
Reference in new issue
Block a user