Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A8 Ventilation control
#7
(02-07-2024, 12:34 AM)admin Wrote: ok, good idea.

Hi! so, i have solution - it is works for me.

I use another ESP8266 to ping HomeAssistant and if it accessible i can switch on one of Digital Inputs.

Here is source for Arduino IDE 1.8 (i tryed to do it on Arduino IDE 2.x but it's failed):


 
Code:
/*
   This example show how to ping a remote machine using it's hostname
   Based on ESP8266-ping project

   https://github.com/bluemurder/esp8266-ping/tree/master

   NOTE:
   bool accessible=pinger.Ping(remote_host);
   This construction ALWAYS return "true" when using IP address
   in my tests this construction return "false" only when DNS name could not be resolved, that why i use response analise to set variable HAAccesible "true" or "false" in function pinger.OnReceive([](const PingerResponse& response)
   
   NOTE2:
   this project does not work in Arduino IDE v2.x, in Arduino IDE v1.8.x it is works properly
*/

#include <Pinger.h>
#include <ESP8266WiFi.h>
extern "C"
{
#include <lwip/icmp.h> // needed for icmp packet definitions
}

// Set global to avoid object removing after setup() routine
Pinger pinger;

bool HAAccessible = false;
const char* ssid     = "stranger";
const char* password = "1234567890abcdef1234567890";
const char*remote_host = "192.168.7.6"; //Home Assistant host
int pinOut = 13; // D7 ESP8266 GPIO pin
bool stationConnected = false;
void setup()
{
  pinMode(pinOut, OUTPUT);
  // Connect to WiFi access point
  stationConnected = WiFi.begin(ssid, password);
  // Begin serial connection at 9600 baud
  Serial.begin(9600);

  // Check if connection errors
  if (!stationConnected)
  {
    Serial.println("Error, unable to connect specified WiFi network.");
  }

  // Wait connection completed
  Serial.print("Connecting to AP...");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.print("WiFi connected \n");
}
  void loop()
  {
    //==========================
     pinger.OnReceive([](const PingerResponse& response)
  {
    if (response.ReceivedResponse)
    {
      Serial.printf(
        "Reply from %s: bytes=%d time=%lums TTL=%d\n",
        response.DestIPAddress.toString().c_str(),
        response.EchoMessageSize - sizeof(struct icmp_echo_hdr),
        response.ResponseTime,
        response.TimeToLive);
        HAAccessible=true;
    }
    else
    {
      Serial.printf("Request timed out.\n");
      HAAccessible=false;
    }

    // Return true to continue the ping sequence.
    // If current event returns false, the ping sequence is interrupted.
    return true;
  });
 
  pinger.OnEnd([](const PingerResponse& response)
  {
    // Evaluate lost packet percentage
    float loss = 100;
    if(response.TotalReceivedResponses > 0)
    {
      loss = (response.TotalSentRequests - response.TotalReceivedResponses) * 100 / response.TotalSentRequests;
    }
   
    // Print packet trip data
    Serial.printf(
      "Ping statistics for %s:\n",
      response.DestIPAddress.toString().c_str());
    Serial.printf(
      "    Packets: Sent = %lu, Received = %lu, Lost = %lu (%.2f%% loss),\n",
      response.TotalSentRequests,
      response.TotalReceivedResponses,
      response.TotalSentRequests - response.TotalReceivedResponses,
      loss);

    // Print time information
    if(response.TotalReceivedResponses > 0)
    {
      Serial.printf("Approximate round trip times in milli-seconds:\n");
      Serial.printf(
        "    Minimum = %lums, Maximum = %lums, Average = %.2fms\n",
        response.MinResponseTime,
        response.MaxResponseTime,
        response.AvgResponseTime);
    }
   
    // Print host data
    Serial.printf("Destination host data:\n");
    Serial.printf(
      "    IP address: %s\n",
      response.DestIPAddress.toString().c_str());
    if(response.DestMacAddress != nullptr)
    {
      Serial.printf(
        "    MAC address: " MACSTR "\n",
        MAC2STR(response.DestMacAddress->addr));
    }
    if(response.DestHostname != "")
    {
      Serial.printf(
        "    DNS name: %s\n",
        response.DestHostname.c_str());
    }

    return true;
  });
  //================================================
   
    while (WiFi.status() == WL_CONNECTED)
    {
      pinger.Ping(remote_host);
      Serial.println(HAAccessible);
      if (HAAccessible==false)
      {
         digitalWrite(pinOut, LOW);
        Serial.println("HA NOT Accessible.");
        delay (10000);
      }
      else
      {
        digitalWrite(pinOut, HIGH);
        Serial.println("HA Accessible.");
        delay (10000);
      }
    }
     digitalWrite(pinOut, LOW);
     HAAccessible=false;
      Serial.println("HA NOT Accessible because WiFi disconnected.");
     
      //reconnect to wi-fi;
      stationConnected = WiFi.begin(ssid, password);
      // Check if connection errors
      if (!stationConnected)
      {
        Serial.println("Error, unable to connect specified WiFi network.");
      }

      // Wait connection completed
      Serial.print("Reconnecting to AP...");
      while (WiFi.status() != WL_CONNECTED)
      {
        delay(500);
        Serial.print(".");
      }
      Serial.print("WiFi connected \n");

  }

Now, when HA accessible relays 1-4 is switching on, and when HA not accessible - switching off.
It works when WiFi failed (when WiFi restored it will be reconected)
It works when HA disconnected from LAN


Attached Files Image(s)
           
Reply


Messages In This Thread
A8 Ventilation control - by a.demenev@limeup.ru - 02-04-2024, 12:38 PM
RE: A8 Ventilation control - by admin - 02-04-2024, 01:37 PM
RE: A8 Ventilation control - by admin - 02-06-2024, 12:52 AM
RE: A8 Ventilation control - by admin - 02-07-2024, 12:34 AM
RE: A8 Ventilation control - by a.demenev@limeup.ru - 02-11-2024, 04:37 PM
RE: A8 Ventilation control - by admin - 02-12-2024, 02:00 PM

Forum Jump:


Users browsing this thread:
1 Guest(s)