Giter Site home page Giter Site logo

arduinoha-examples's Introduction

Using Arduino and Home Assistant

home-assistant-integration

Github

Github Issues

ArduinoHA

HADevice

HADevice represents the physical device where the library is installed. Logically it’s a group of types like sensors, switches, lights and so on. In the Home Assistant, it’s listed with properties that may be configured using the library’s API.

Each property except the unique ID is optional. Setting optional properties increases flash and RAM usage so it’s not recommended to set them on lower-spec MCUs. The supported properties are:

  • unique ID*
  • name
  • software version
  • manufacturer
  • model

HADevice unique ID needs to be unique in a scope of a Home Assistant instance. The safest solution is to use the MAC address of an Ethernet or Wi-Fi chip but you can also implement your own solution.

// use your own unique bytes sequence or use mac of WiFi
byte myId[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A};
HADevice device(myId, sizeof(myId));

void setup() {
    device.setName("Bedroom Light Controller");
    device.setSoftwareVersion("1.0.0");
    device.setManufacturer("Developer Corp.");
    device.setModel("ABC-123");
    // ...
    device.enableSharedAvailability();
    device.setAvailability(false); // changes default state to offline
    // MQTT LWT
    device.enableLastWill();
    // Discovery
    mqtt.setDiscoveryPrefix("homeassistant");
    mqtt.setDataPrefix("aha");
}

Availability reporting

Home Assistant allows to track online/offline states of devices and device types. In this way controls available in the panel will be displayed as disabled if a device is offline.

The library allows to expose state of the entire device (i.e. shared availability) or specific type (sensor, switch, light, etc.). By default this feature is not enabled to save resources (RAM and flash) but you can easily turn it on as shown below.

Shared availability

It is highly recommend to use shared availability feature as it allows to utilize MQTT LWT. Basically, shared availability allows to control availability of all types related to a specific device. For example: if your device has 5 switches and 2 buttons you can control their availability in the HA panel using a single method call.

See example above showing how to enable shared availability of the device. By default, the device is considered online but you can control its state manually using HADevice::setAvailability(bool online) method. In most cases you won’t need to control availability manually as the library takes care of availability as long as the device is powered on.

MQTT LWT

When LWT feature is enabled the device becomes offline in the HA panel even if you cut off power supply. This solution is implemented by MQTT broker that automatically publishes the message when the TCP connection to the device is lost.

Discovery

The library automatically maintains connection to the MQTT broker and takes care of the discovery process. Each device type that you create (sensor, switch, light, fan, etc.) is automatically registered in MQTT manager. Whenever connection with the MQTT broker is acquired the configuration of all device types is pushed to the Home Assistant.

In some cases you may need to change prefix of MQTT topics. There are two types of topics utilized by the library:

  • discovery topic - used for publishing device types’ configuration (default: homeassistant)
  • data topic - used for publishing states, data, etc. (default: aha)

Sensor Types (Device types)

In the library, HABaseDeviceType is the type of Sensor(s) which are attached to Your physical device (for example ESP-01 board) can have multiple sensor types (device types) assigned.

Represents a single entity in the Home Assistant panel. Registering a new device type requires some flash and RAM memory to be utilized. On less powerful units like Arduino Uno, you may quickly hit the limit of resources, so keeping the device simple is recommended. Hitting the resource limit will result in random reboots of the device.

Device types API describes the current "Sensor Types" supported.

By default, the maximum number of device types is 6.

// change the number of Device Types
HAMqtt mqtt(client, device, 12); // <------------ 12 is a new limit of device types

arduinoha-examples's People

Contributors

jwilleke avatar

Watchers

 avatar

arduinoha-examples's Issues

Need to add fill tube

We have all the parts to add the automatic fill to the tank.

Just need to add the tank fitting and connect to water line.

Controlling fan speeds

It appears the direct 12V is too fast to spin Fans for Stirrers. The PTFE Magnetic Stirrer Mixer Stir Bars get spun to the side of the containers.

Been difficult to find any solution to use PWM from Arduino (UNO R4 wifi) to control fans.

Put item in forum.arduino.cc.

Activate 110VAC controlls

  • Grow Light - Zigbee / BLE smart plug with power (3RSP02028BZ) by Third Reality
  • Fill switch - Shelly device
  • Aeration pump ?

Reading of Sensors appear to stop

The sensors:

  • ORB
  • Temperature
  • pH
  • TDS
  • EC
  • Level
    Are supposed to be read every 2,000 milliseconds.
((millis() - lastUpdateAt) > THRESHOLD)

Where

  • THRESHOLD=2,000
  • unsigned long lastUpdateAt - is set in loop
  • millis() is the

When Imonitor MQTT with a broker it appears that they are not read at all?

The expression (millis() - lastUpdateAt) > THRESHOLD in Arduino code typically won't overflow itself, even if millis() overflows. Here's why:

Overflow of millis(): millis() is an unsigned long variable, which on most Arduino boards has a maximum value of 4,294,967,295 milliseconds (approximately 50 days). After reaching this limit, it wraps around to zero and starts counting again.

Subtraction handling overflow: In your code, even if millis() overflows and becomes zero, the subtraction (millis() - lastUpdateAt) usually won't cause an overflow because it considers the relative difference between the two values.

Here's a breakdown of why overflow is avoided:

Assuming overflow: Let's say millis() overflows to zero, and lastUpdateAt has a previous high value (close to the maximum).
Subtraction: When you subtract the new millis() (zero) from lastUpdateAt, the result will be a large positive number because lastUpdateAt was much bigger.
Comparison: This large positive number is then compared to THRESHOLD. As long as THRESHOLD is a reasonable value (less than the maximum millis() value), the condition (millis() - lastUpdateAt) > THRESHOLD will still evaluate correctly.

It appears the loop

if ((millis() - lastUpdateAt) > THRESHOLD)
{
  ....various code here....
    // reset loop timer
    lastUpdateAt = millis();
}

stops executing the code inside the loop.

Suspension of pump lines

THe pump lines must not reach the bottom of the Chemical containers and need to be suspended above the Nutrient Tank and the stirrers.

fans and pumps run on startup

The fans and pumps run on startup until the Arduino code is initialized we need two relays on for 5V and one for 12V supplies to keep them off until the code is initialized.

readCount is a counter that counts the number of reads performed and in the loop() function we use

  if (readCount > INITIAL_READER_COUNTER )
  {...}

To prevent readings until things settle down.
The sensors run all the time power is applied to the Arduino, but we just do not read them until this counter reaches the INITIAL_READER_COUNTER.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.