Which I have working, I changed the states as follows:
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
int status;
//send message every 2 second
if (now - lastMsg > 2000) {
lastMsg = now;
status=digitalRead(BUTTON_PIN);
String msg="Button status: ";
if(status==HIGH )
{
msg= msg+ “Pressed”;
char message[58];
msg.toCharArray(message,58);
Serial.println(message);
//publish sensor data to MQTT broker
client.publish(“buttondata”, message);
}
else
{
msg= msg+ “No Press”;
char message[58];
msg.toCharArray(message,58);
Serial.println(message);
//publish sensor data to MQTT broker
client.publish(“buttondata”, message);
}
I am trying to trigger a basic script with an automation but this doesn’t appear to be working and I am sure its because I am not using the proper topic but I am not sure of the correct format… is the topic buttondata and the payload “pressed”? thanks!
This line sends a message with topic ‘buttondata’ and a payload with whatever is in the message variable. Looking back through the code, it looks like this is either Button status: Pressed or Button status: No Press
The documentation for triggering automations from MQTT messages is here
follow up question if you don’t mind… to make a state for button held, I assume I should make another else (if) statement that reads the state for a different amount of time and then reports it? Do you have any suggestions?
I am not sure what you want to achieve, but I probably can’t help much as I do all of my automations in appdaemon, so I don’t know too much about yaml.
on the ESP (arduino) code. I want to have a MQTT message sent when the button is pressed and held for lets say 3 seconds. A different message then “Pressed”… etc
I suppose I have to augment this: if(status==HIGH )
with an int so that if status==high for 3000 then do this…