I have an Arduino with 2 sensors and 2 switches that I integrated in Home Assistant using ArduinoHA.
I did everything over 3 months ago and everything went well and worked perfectly.
Please note that I had a custom panel in which I was able to see the value form sensor and activate switches. When I disconnected the Arduino from PC of course everything went offline (not available but when I restored the power (not USB, just power) everything went back online.
As you all know, there were a lot of update in HA and I did all of them.
Today I tried to connect the device to PC to update the sketch in Arduino but I was not able to have it integrated in Home Assistant again.
PLEASE NOTE: I did not update the sketch. It goes offline and did not shows up online again.
Any idea what this should be due to? Any idea how to fix it?
For instance, please fine hereafter the full arduino sketch if may be useful.
#include <WiFiNINA.h>
#include <ArduinoHA.h>
char ssid[]= SECRET_SSID;
char pass[] = SECRET_PASS;
const char broker[] = SECRET_MY_BROKER_IP;
const char mqtt_user[] = SECRET_MY_BROKER_USER;
const char mqtt_pass[] = SECRET_MY_BROKER_PASS;
const int port = SECRET_MY_BROKER_PORT;
#define sw1 11 //pin 11 di comando del relè1
#define sw2 12 //pin 12 di comando del relè2
#define SPENTO HIGH //funzionano in logica inversa
#define ACCESO LOW //funzionano in logica inversa
#define LO_Threshold 500 //DA REGOLARE, soglia di apertura carico acqua
#define HI_Threshold 800 //DA REGOLARE, soglia di chiusura carico acqua
#define SENSORE A4 //DA CORREGGERE, mettere il sensore di pressione
unsigned long lastUpdateAt = 0;
const unsigned int sogliaControllo = 5000; //controllo il dato ogni 5 secondi
WiFiClient client;
HADevice device("Ard-CaricoH2O");
HAMqtt mqtt(client, device);
HASwitch elettrovalvola("elettrovalvola");
HASensorNumber analogSensor("myAnalogInput", HASensorNumber::PrecisionP1);
HASensorNumber analogSensor2("letturaDigitale");
void onSwitchCommand(bool state, HASwitch* sender)
{
digitalWrite(sw1, (state ? ACCESO : SPENTO));
digitalWrite(sw2, (state ? ACCESO : SPENTO));
sender->setState(state); // report state back to the Home Assistant
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; //aspetto di connettermi alla seriale
}
device.enableSharedAvailability();
device.enableLastWill();
device.setName("Arduino Carico Automatico");
device.setSoftwareVersion("1.0.0");
device.setManufacturer("DoItYourself Corp.");
device.setModel("Arduino Uno WiFi Rev.2");
elettrovalvola.setName("Elettrovalvola");
analogSensor.setIcon("mdi:water-pump");
analogSensor.setName("Pressione Acqua");
analogSensor.setUnitOfMeasurement("atm");
analogSensor2.setIcon("mdi:numeric");
analogSensor2.setName("Lettura Digitale");
analogSensor2.setUnitOfMeasurement("(0->1023)");
pinMode(A5, INPUT);
pinMode(A4, INPUT);
pinMode(sw1, OUTPUT);
pinMode(sw2, OUTPUT);
digitalWrite(sw1, SPENTO);
digitalWrite(sw2, SPENTO);
Serial.print("Connessione in corso alla WiFi ");
Serial.println(ssid);
while (WiFi.begin(ssid,pass) != WL_CONNECTED) {
Serial.print(".");
delay(5000);
}
//qui sono connesso alla WiFi
Serial.println();
Serial.println("WiFi CONNESSA.");
Serial.println();
//digitalWrite(sw1, SPENTO);
//digitalWrite(sw2, SPENTO);
elettrovalvola.onCommand(onSwitchCommand);
Serial.println("Connessione in corso al broker ");
Serial.println(broker);
mqtt.begin(broker, port, mqtt_user, mqtt_pass);
mqtt.setDiscoveryPrefix("homeassistant");
}
void loop() {
// put your main code here, to run repeatedly:
mqtt.loop();
delay(500);
if ((millis() - lastUpdateAt) > sogliaControllo) {
uint16_t reading = analogRead(SENSORE);
float lettura = reading * 5.f / 1023.f; //0.0 - 5.0
Serial.print("\nLettura analogica pari a ");
Serial.println(lettura);
Serial.print("Lettura digitale pari a ");
Serial.println(reading);
analogSensor.setValue(lettura);
analogSensor2.setValue(reading);
lastUpdateAt = millis();
//controllo automatico
if (reading < LO_Threshold) {
digitalWrite(sw1, ACCESO);
digitalWrite(sw2, ACCESO);
elettrovalvola.setState(true);
}
if (reading > HI_Threshold) {
digitalWrite(sw1, SPENTO);
digitalWrite(sw2, SPENTO);
elettrovalvola.setState(false);
}
}
}