final sketch and functional with MyController
/*
* DESCRIPTION : 4 inputs and 4 outputs on i2C bus with a PCF8574
*
* LIBRAIRIES:
* - Rob Tillaart pour PCF8574 https://github.com/RobTillaart/Arduino/tree/master/libraries/PCF8574
* - MySensors / Arduino Uno https://github.com/mysensors/MySensors
*
* ELEMENTS:
* - Expander PCF8574 avec module Metasys JC / 4di 4do - 0X03D i2c adress
*
* MODIFICATION:
* -x- RAS : Fonctionnelle
* -o-
* -o-
*
* (36%) de l'espace de stockage de programmes
*/
//----------- 9 Mai 2022 ---------------- PCF8574 -4DI-4DO / MyS RS485 et MyC -----------------//
//------------------- MyS ---------------------------------
//#define MY_DEBUG /*Enable debug prints to serial monitor*/
//#define MY_DEBUG_OTA_DISABLE_ECHO //testing
#define MY_TRANSPORT_WAIT_READY_MS 3000 /*Tempo de mis en Com (millisecondes) Γ placer avant Mysensors.h*/
#define MY_NODE_ID 40 /*Node en ID static*/
/* ----- Module TTL-RS485 ----*/
#define MY_RS485 /*Apl du transport RS485 (protocol?)*/
#define MY_RS485_DE_PIN 10 /*Cmd DE pin*/
#define MY_RS485_BAUD_RATE 9600 /*Set RS485 baud rate to use*/
#define MY_REPEATER_FEATURE /*Activer fonctionnalitΓ© de rΓ©pΓ©teur du nΕud*/
#include <MySensors.h>
//---------------- PCF -------------------------------
#include "PCF8574.h"
PCF8574 PCF27a (0x3D); // adjust addresses i2c pcf8574
// ------ objet ------
#define CHILD_ID_B0 0 /*Id IN - bp */
#define CHILD_ID_B1 1
#define CHILD_ID_B2 2
#define CHILD_ID_B3 3
#define CHILD_ID_R4 4 /*Id OUT - relay */
#define CHILD_ID_R5 5
#define CHILD_ID_R6 6
#define CHILD_ID_R7 7
#define RELAY_ON 0 // 1 valeur
#define RELAY_OFF 1 // 0 invers
//---------- MyS -------------
MyMessage msg0(CHILD_ID_B0,V_TRIPPED); /*Boton*/
MyMessage msg1(CHILD_ID_B1,V_TRIPPED);
MyMessage msg2(CHILD_ID_B2,V_TRIPPED);
MyMessage msg3(CHILD_ID_B3,V_TRIPPED);
MyMessage msg4(CHILD_ID_R4,V_STATUS); /*Relay*/
MyMessage msg5(CHILD_ID_R5,V_STATUS);
MyMessage msg6(CHILD_ID_R6,V_STATUS);
MyMessage msg7(CHILD_ID_R7,V_STATUS);
bool info; // pour info GW sur MyC
bool state = false; // output
// bool state4 = false; bool state5 = false; bool state6 = false; bool state7 = false;
int oldValue0=-1; int oldValue1=-1; int oldValue2=-1; int oldValue3=-1; // Input
//---------------- SETUP -------------------------
void setup() {
// Serial.begin(115200); // debug
//--------- testing IsConnected i2c PCfx ------------
Serial.println(__FILE__);
Serial.print("PCF8574_LIB_VERSION:\t");
Serial.println(PCF8574_LIB_VERSION);
if (!PCF27a.begin()) {
Serial.println("could not initialize...");
}
if (!PCF27a.isConnected()) {
Serial.println("=> not connected");
}
else {
Serial.println("=> connected!!");
}
// ---- info send to gateway ----
send(msg0.set(info)); send(msg1.set(info)); //input
wait(200);
send(msg2.set(info)); send(msg3.set(info)); //input
wait(200); //delays for frames, depending on system load and bus type (wireless or wired)
send(msg4.set(info)); send(msg5.set(info)); //output
wait(200);
send(msg6.set(info)); send(msg7.set(info)); //output
}
//----------------- MyS ----------------------------
void presentation() {
sendSketchInfo("PCF8574 x4in/out - node40", "2.0"); /*info version sketch*/
/*Mysenors Enregistre Child sur la Gw*/
present(CHILD_ID_B0, S_DOOR, "boton0"); /*Boton*/
present(CHILD_ID_B1, S_DOOR, "boton1");
wait(200);
present(CHILD_ID_B2, S_DOOR, "boton2");
present(CHILD_ID_B3, S_DOOR, "boton3");
wait(200); //delays for frames, depending on system load and bus type (wireless or wired)
present(CHILD_ID_R4, S_BINARY, "relay4"); /*Relay*/
present(CHILD_ID_R5, S_BINARY, "relay5");
wait(200);
present(CHILD_ID_R6, S_BINARY, "relay6");
present(CHILD_ID_R7, S_BINARY, "relay7");
wait(200);
// metric = getControllerConfig().isMetric;
}
//----------------- LOOP ----------------------------
void loop() {
//----------- MyS input -------------------------------
int value0 = PCF27a.read(0);
if (value0 != oldValue0) {
send(msg0.set(value0==HIGH ? 1 : 0)); //Send new value
oldValue0 = value0;
}
int value1 = PCF27a.read(1);
if (value1 != oldValue1) {
send(msg1.set(value1==HIGH ? 1 : 0));
oldValue1 = value1;
}
int value2 = PCF27a.read(2);
if (value2 != oldValue2) {
send(msg2.set(value2==HIGH ? 1 : 0));
oldValue2 = value2;
}
int value3 = PCF27a.read(3);
if (value3 != oldValue3) {
send(msg3.set(value3==HIGH ? 1 : 0));
oldValue3 = value3;
}
}
//----------- MyS out -------------------------------
void receive(const MyMessage &message) {
if (message.getType()==V_STATUS) {
switch (message.sensor) {
case 4:
state= message.getBool();
PCF27a.write(4, state?RELAY_ON:RELAY_OFF);
break;
case 5:
state = message.getBool();
PCF27a.write(5, state?RELAY_ON:RELAY_OFF);
break;
case 6:
state = message.getBool();
PCF27a.write(6, state?RELAY_ON:RELAY_OFF);
break;
case 7:
state = message.getBool();
PCF27a.write(7, state?RELAY_ON:RELAY_OFF);
break;
}
}
}
//------------------------------ End pgm -------------------------------------