as been checked (in my capacity) for I don’t know how many times.
CASE: I have a particle photon with a photoresistor connected between two pins, which publishes the value of the resistance placed in front of the “end” light of a washing machine under my own WIFI. The part of Particle works, so much so that with IFTT I can start Pushbullet when the light comes on with the message “the washing machine has run out”.
I therefore created a mqtt sensor on Hassio, with also the states of unavailability (AFAIK)
[code]
sensor:
- platform: mqtt
name: "Lavatrice"
state_topic: "tele/LAVATRICE/status"
icon: mdi:washing-machine
payload_available: "online"
payload_not_available: "offline"
[/code]
and…
nothing, mosquitto refuses -o, better, truncates-the connection with
[Code]
1556805730: New client connected from 192.168.0.15 as WASHING MACHINE (c1, k15, u'MQTT ').
1556805744: Socket error on client WASHING MACHINE, disconnecting.
[/ Code]
and the sensor remains in “unknown”
This is the Photon code. I’m sure I made a trivial mistake (or maybe not, I’m not very well on MQTT) but I don’t see it.
Thank you in advance.
[code]
#include "MQTT.h"
// Lavatrice
int photoresistor = A0; // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below).
int power = A5; // This is the other end of your photoresistor. The other side is plugged into the "photoresistor" pin (above).
int analogvalue;
void callback(char* topic, byte* payload, unsigned int length);
byte server[] = { 192,168,0,xx }; //IP del HA+raspi
MQTT client(server, 1883, callback);
// receive message UNUSED BUT I HAVE NOT REMOVED IT FOR POSSIBLE UPGRADES
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
if (!strcmp(p, "RED"))
RGB.color(255, 0, 0);
else if (!strcmp(p, "GREEN"))
RGB.color(0, 255, 0);
else if (!strcmp(p, "BLUE"))
RGB.color(0, 0, 255);
else
RGB.color(255, 255, 255);
delay(1000);
}
void setup() {
pinMode(photoresistor,INPUT); // photoresistor pin input (reading the photoresistor)
pinMode(power,OUTPUT); // pin powering the photoresistor is output (sending out consistent power)
digitalWrite(power,HIGH);
Spark.variable("analogvalue", &analogvalue, INT); //variable to be published on cloud particle
RGB.control(true); //led control
// connect to the server
client.connect("LAVATRICE", "MQTTUSER", "MQTTpwd"); //login to broker
// publish/subscribe
if (client.isConnected()) {
client.publish("tele/LAVATRICE/status","CONNESSA"); //I don't already see this: it should put the sensor as CONNECTed as soon as the connection is made.
}
}
void loop() {
analogvalue = analogRead(photoresistor); //reads the resistor
if (client.isConnected()) //if it is still connected to the MQTT ...
{
delay(10000); //...after ten sec...
RGB.color(0, 255, 0); // ... the LED turns green
if (analogvalue>3750) //.....e if the light is on ...
{
Spark.publish("analogvalue"); //....publicishes the value of the resistor in the cloud ...
client.publish("tele/LAVATRICE/status","FINITO"); // ...and"FINITO" on MQTTT...
delay(300000); // ...and wait 5 minutes before the next reading
}
else
{
Spark.publish("analogvalue"); //... otherwise it only publishes the value of the resistance in the cloud (AS IF!)
delay(20000); // ...and wait only 20 seconds
}
client.loop(); // I found this, I suppose it's the keepalive
}
else // Instead, if it's not connected to the broker ....
{
RGB.color(255, 0, 0); //....the LED turns red ...
client.publish("tele/LAVATRICE/status","DISCONNESSA"); //.....for security post the disconnection even if it will never arrive ...
if (analogvalue>3750) //...e if the LED is on ...
{
Spark.publish("analogvalue"); //.... posts the value on the cloud ...
delay(300000); // ...and waits 5 minutes before the next reading
}
else
{
Spark.publish("analogvalue"); //... otherwise only publish the value of the resistance in the cloud
delay(20000); // ...and wait only 20 seconds
}
//(sorry for the rubberducking).
}
}
[/code]
The problem is that the sensor on HASSIO does not even present the “online” and “offline” payloads.
I’m afraid I got something wrong between PHOTON and MQTT, but …
Again, thanks.