here's one of my programs I use to set the node ID in EEPROM:
#define Ver 1.0
#include <EEPROM.h>
// ------------------------------------------------------------------------------------clearSerialBuffer
void clearSerialBuffer(){
while(Serial.available()){
Serial.read();
delay(50);
}
}
// ------------------------------------------------------------------------------------header
void header(){
uint8_t EEpromInt = EEPROM.read(0);
Serial.print(F("Current value at address (0) is "));
Serial.println(EEpromInt);
clearSerialBuffer();
Serial.print(F("Enter a an unsigned integer >0 and <254: "));
}
// ------------------------------------------------------------------------------------setup
void setup() {
Serial.begin(115200); Serial.print(F("\n\rAddress As uint8_t ver ")); Serial.println(Ver);
clearSerialBuffer();
header();
}
// ------------------------------------------------------------------------------------loop
void loop() {
if (Serial.available()){
long inInt = Serial.parseInt();
if (inInt <= 0 || inInt >=254){
Serial.print(F(" I don't like '"));
Serial.print(inInt);
Serial.println(F("'"));
}
else {
EEPROM.write(0,uint8_t(inInt));
}
header();
}
}
One would run it once in the Arduino, then load the actual program..
This boiler plate show how to set the variable (and set up for data coming from Home Assistant
#define VER "01"
#define PROGRAM_NAME "PROGRAM NAME" // " " is required
// compiler directive to select development vs production environment
// comment out directive for production environment
#define DEVELOPMENT // MySensors Gateway on channel 86 otherwise 121
// Setting Node ID from EEPROM
#include <EEPROM.h>
unsigned short MY_NODE_ID = EEPROM.read(0); // get NODE ID
unsigned short dispMY_NODE_ID = MY_NODE_ID;
/*
ver 01
Make notes for each version change
*/
#include <LibPrintf.h>
//--------------------------------------------------MySensors parameters
#define MY_DEBUG
// radio type, radio control pins and channel
#define MY_RADIO_RF24
// this matches the nRF24 nano
#define MY_RF24_CS_PIN 9
#define MY_RF24_CE_PIN 10
#ifdef DEVELOPMENT
#define MY_RF24_CHANNEL 86
#else
#define MY_RF24_CHANNEL 121
#endif
#define MY_RF24_PA_LEVEL (RF24_PA_MAX)
#include <MySensors.h>
// these wait periods seem to be necessary
#define WAIT_AFTER_SEND_MESSAGE 300,0 // ms to wait after send message
#define WAIT_AFTER_PRESENTATION 5000,0 // ms to wait after presentation message or echo was returned
// configure communication protocol to Home Assistant for power, energy, voltage, current, VA, and powerfactor
#define CHILD_ID_SENSOR_0 0 // Id of the sensor child
MyMessage msgPOWER(CHILD_ID_SENSOR_0, V_WATT);
// HA can only send text data. If the text sent is a number, Arduino can read it as a number.
// The MySensors integration into HA cannot send numbers, but can send text. The Arduino must define a “sensor” as V_TEXT, present it as S_INFO, and as above any value sent as it ends up at HA as text.
#define CHILD_ID_receive_from_HA 7 // ID of entity that receives numeric data from HA sent as text
MyMessage msgFrom_HA(CHILD_ID_receive_from_HA,V_TEXT); // for receiving number of counts as text
//------------------------------------------------------before (starting MySensors)
// This will display program information before attempting nRF24 network connection
void before(){ // this happens before MySensors starts
analogReference(EXTERNAL); // this needs to be done very near the start so ADC doesn't get buggered
// usual program information at very start
Serial.begin(115200);
#ifdef DEVELOPMENT
Serial.println("---------------------------------- DEVELOPMENT ----------------------------------");
#endif
Serial.print(PROGRAM_NAME);Serial.print(" version ");Serial.println(VER);
Serial.print("Channel ");Serial.println(MY_RF24_CHANNEL);
Serial.print("Node: ");Serial.println(dispMY_NODE_ID);
}
//-------------------------------------------------------presentation
void presentation(){
sendSketchInfo(PROGRAM_NAME, VER,true); // "true" means send echo request
wait(WAIT_AFTER_PRESENTATION);
present(CHILD_ID_SENSOR_0,S_POWER,"Sensor_0_name",true);
wait(WAIT_AFTER_PRESENTATION);
present(CHILD_ID_receive_from_HA,S_INFO,"Data_for_MySensors_sensor",true);
wait(WAIT_AFTER_PRESENTATION);
}
//------------------------------------------------------setup
void setup() {
// request last known kWh value from gw/HA
send(msgPOWER.set(-2147483646),true); // an ENERGY of 1 is ignored, used to sent the energy to no zero
wait(WAIT_AFTER_SEND_MESSAGE);
wait(WAIT_AFTER_SEND_MESSAGE);
send(msgFrom_HA.set(0),true); // Ensures that HA sees the "sensor"
wait(WAIT_AFTER_SEND_MESSAGE);
wait(WAIT_AFTER_SEND_MESSAGE);
}
volatile bool MsgReceived = false;
volatile long NumberFromHA = 0;
//------------------------------------------------------loop
void loop() {
//------------------------cumulative energy one-time initialization
if (MsgReceived){ // message came in from HA
MsgReceived = false; // note that this message was processed
// Deal with message received from HA
}
// Other loop stuff
}
//------------------------------------------------------receive
void receive(const MyMessage &message) {
if (message.getType()==V_TEXT) {
if(message.getSensor() == CHILD_ID_receive_from_HA) { // filters out ACK messages and ensures that the data is what was intended
MsgReceived = true;
NumberFromHA = message.getLong(); // the reads the text sent and stores as a long integer
}
}
//printf("Received Message --- command: %d Type: %d\r\n",message.getCommand(),message.getPayloadType());
}
I hope this helps
OSD