Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 95451

MQTT topic from arduino to Hassio issue

$
0
0

@Dmosq wrote:

Hi Guys, i’m new here and in hassio.
i’m following a tutorial to create an MQTT window sensor in arduino ESP01 and conect it to home assistant.

the thing is, i am getting the mqtt_topic in node red, the arduino is working as expected as you can see in the image i’m getting the toppic:

but i do not know how to publish it into an entity in the lovelace, i put this configuration in my configuration.yaml

sensor:
  platform: mqtt
  name: 'Window'
  state_topic: 'test/window1'
  unique_id: 'test/window1'

and indeed it is creating and entity called sensor.window_1 but the state doesn’t change, it shows “Unknown”

Capture

does anybody here can help me to solve this? is it something that i’m not doing?

thank you

code in arduino:

#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
ADC_MODE(ADC_VCC);

//USER CONFIGURED SECTION START//
const char* ssid = "MY_SSID";
const char* password = "MY_WIFI_PASSWORD";
const char* mqtt_server = "192.168.1.100";
const int mqtt_port = 1883;
const char *mqtt_user = "mqtt_user";
const char *mqtt_pass = "mqtt_password";
const char *mqtt_client_name = "windows"; // Client connections cant have the same connection name
const char *mqtt_topic = "test/window1";
IPAddress ip(192, 168, 1, 50); 
IPAddress gateway(192, 168, 1, 1); 
IPAddress subnet(255, 255, 255, 0);
//USER CONFIGURED SECTION END//


WiFiClient espClient;
PubSubClient client(espClient);

// Variables
bool boot = true;
char batteryVoltageMQTT[50];

//Functions

void setup_wifi() 
{
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
    {
      delay(50);
    }
}

void reconnect() 
{
  while (!client.connected()) 
  {
      int battery_Voltage = ESP.getVcc() + 600;
      String temp_str = String(battery_Voltage);
      String mqttString = temp_str + "mV Replace Battery";
      mqttString.toCharArray(batteryVoltageMQTT, mqttString.length() + 1);
      if (battery_Voltage <= 2900)
      {
        //boolean connect (clientID, username, password, willTopic, willQoS, willRetain, willMessage)
        if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass, mqtt_topic, 0, 1, batteryVoltageMQTT)) 
        {
        
          if(boot == true)
          {
            client.publish(mqtt_topic,"open");
            boot = false;
          }
        } 
        else 
        {
          ESP.restart();
        }
      }
      if (battery_Voltage > 2900)
      {
        if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass, mqtt_topic, 0, 1, "closed")) 
        {
          if(boot == true)
          {
            client.publish(mqtt_topic,"open");
            boot = false;
          }
        } 
        else 
        {
          ESP.restart();
        }
      }
  }
}

void setup() 
{
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
}

void loop() 
{
  
  if (boot == true) 
  {
    reconnect();
  }
  else
  {
  ESP.deepSleep(0);
  }
}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 95451

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>