Category Archives: IOT / Domoticz

My configs to set and get domoticz-433Mhz from Home Assistant

I’ve posted about some integrations here:

There are three kinds of 433 connections I’m using:

  • Switches
  • Sensors (read-only)
  • Dimmers

Mqtt Home Assistant Config Yaml

Here are 3 mqtt examples using the Node-Red rewriter (see above post)

mqtt:
 light:    
  - name: "KitchenOutside"
    payload_on: "99"
    payload_off: "0"
    unique_id: "KitchenOutside"
    brightness_scale: "99"
    brightness_state_topic: ha433/kitchenoutside/brightcontrolstate
    brightness_command_topic: ha433/kitchenoutside/brightcontrol
    state_topic: ha433/kitchenoutside/brightcontrolstate
    command_topic: ha433/kitchenoutside/control
    optimistic: false
    on_command_type: brightness
 binary_sensor:
  - name: "AtelierDoor"
    state_topic: "home2/3331/payload"
    value_template: "{{ value_json.nvalue }}"
    off_delay: 5
    payload_off: "0"
 sensor:
  - name: "LivingTemperature"
    state_topic: "home/9999/payload"
    unit_of_measurement: "°C"
    value_template: "{{ value_json.svalue1 }}"
  - name: "LivingHumidity"
    state_topic: "home/9999/payload"
    unit_of_measurement: "%"
    value_template: "{{ value_json.svalue2 }}"

Here is a virtual switch sensor using curl commands

command_line:
  - switch:
      name: PatioSlinger
      command_on: >
        curl "http://domoticz:pass@192.168.1.1:8080/json.htm?type=command&param=switchlight&idx=6&switchcmd=On"
      command_off: >
        curl "http://domoticz:pass@192.168.1.1:8080/json.htm?type=command&param=switchlight&idx=6&switchcmd=Off"
      command_state: > 
        curl "http://domoticz:pass@192.168.1.1:8080/json.htm?type=command&param=getdevices&rid=6" | grep Status | cut -f4 -d\"
      value_template: >
#        "{{ value_json.result[0].Status }}"
        "{{ value_json.result[0].Status == 'On'}}"
      icon: >
        {% if value_json.result[0].Status == 'On' %} mdi:toggle-switch
        {% else %} mdi:toggle-switch-off
        {% endif %}
  - switch:
      name: DoorChimeManual
      command_on: >
        curl "http://domoticz:pass@192.168.1.1:8080/json.htm?type=command&param=switchlight&idx=9999&switchcmd=On"

Nodered mqtt payload rewriter for dimmer
Note: I still need to write the state part

Function code

var idx = 9999;
var bright = msg.payload;
msg.payload = {};
msg.payload = {"command": "switchlight", "idx": idx, "switchcmd": "Set Level", "level": bright};
return msg;

Adding a rotary encoder to Home Assistant to control dimmers using EspHome

Config for mqtt-433 and home assistant entities.
Maybe I’ll add a display to select which dimmer to change.

ESPHome Config for direct communication to a MQTT enabled 443mhz dimmer.

When using GND to the rotary you have to use a pullup entry in your yaml

esphome:
  name: rotarywhite
  friendly_name: RotaryWhite

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx="

ota:
  password: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Rotarywhite Fallback Hotspot"
    password: "xxxxxxxxxxxxxxxxxxx"

captive_portal:
    
sensor:
  - platform: rotary_encoder
    name: "WhiteRotaryEncoder"
    id: rotvalue
    min_value: 0
    max_value: 50
    resolution: 1
    pin_a:
      number: GPIO0
      inverted: true
      mode:
        input: true
        pullup: true
    pin_b:
      number: GPIO2
      inverted: true
      mode:
        input: true
        pullup: true
    on_value:    
      - mqtt.publish:
          topic: "ha433/Living5Spots/brightcontrol"
          payload: !lambda |-
              return to_string(id(rotvalue).state);
mqtt:
  discovery: false
  broker: 192.168.1.1
  port: 1883
  discovery_prefix: homeassistant

Config part to change Home Assistant entities.
WARNING YOU HAVE TO CHANGE RIGHTS!

Settings > Addons > EspHome > Configuration
(press configure to change service calls)

sensor:
  - platform: rotary_encoder
    name: "WhiteRotaryEncoder"
    id: rotvalue
    min_value: 0
    max_value: 50
    resolution: 1
    pin_a:
      number: GPIO0
      inverted: true
      mode:
        input: true
        pullup: true
    pin_b:
      number: GPIO2
      inverted: true
      mode:
        input: true
        pullup: true
    on_value:    
      - homeassistant.service:
          service: light.turn_on
          data_template:
                entity_id: light.bedroomdimmer  
                brightness: "{{ brightness_1 | int }}"    
          variables:
              brightness_1: !lambda 'return id(rotvalue).state * 4;'

Home Assistant Speech and More

I made my own Mqtt to speech thingy in the past.
Sending a text to a mqtt topic would be picked up by my domoticz raspberry and using a bash script the topic payload was converted to speech and being played on a connected speaker.

This speaker migrated to my Home Assistant NUC.
So i changed the speech engine.

Beside this migration, i’ve started using the HA voice assistant capabilities.
This was a major impact/project in 2023.

I’m not going to talk about configuring this .. There are many good YT tutorials and forum topics about this.

https://www.home-assistant.io/voice_control/thirteen-usd-voice-remote/

This is the device: a ESP32 pico, Microphone, leds and Speaker are being used for this sound assistant.
(It uses ESPHOME)

Back to the speaker being hooked-up to my HA NUC.

Install the addon PicoTTS (speech synthesis)

configuration.yaml

# Text to speech
tts:
  - platform: picotts
# My part
input_text:
  mqttspeech:
    name: mqttspeech
    initial: ""
    

Then install notified addon

Add a text field to your dashboard …

HLK-LD2410B with a Wemos Mini D1 (v4) connected to Home Assistant using ESPHome

LD2410 is a high-sensitivity 24GHz human presence status sensing module developed by Hi-link. Its working principle is to use FMCW frequency-modulated continuous waves to detect human targets in the set space. Combined with radar signal processing and precise human body sensing algorithms, it realizes high-sensitivity human presence status sensing, and can identify human bodies in motion and stationary states. And auxiliary information such as the distance of the target can be calculated.

This product is mainly used in indoor scenes to sense whether there is a moving or micro-moving human body in the area, and output the detection results in real time. The farthest sensing distance can reach 5 meters, and the distance resolution is 0.75m. Provides a visual configuration tool, which can easily configure the sensing distance range, sensing sensitivity in different intervals and unmanned delay time, etc., to adapt to different specific application needs.

Support GPIO and UART output, plug and play, and can be flexibly applied to different smart scenarios and terminal products.

There are 3 versions: Without Bluetooth, with Bluetooth (B version) and a C version which uses the standard pin distance.
The other ones are a pain in the *ss to solder!

Got a Bluetooth version? See end of post!

When searching for examples, I noticed that many had issues getting this working.
Let me be clear, it wasn’t working for me the first time.
Things i’ve learned:

  • Flash the first initial ESPHome using a USB cable, after that you can connect the module and flash OTA
  • Do not use the standard Uart RX/TX, it didn’t work for me. And messes-up the logging over USB (See baudrate: 0 to turn this off)
  • When connecting D7/D8 and this signal gets pulled down, the wemos won’t boot. (Running wifi connections gets interrupted)
    This is also a sign that RX/TX is switched around!
  • Measure and make sure you have a good, stable 5V power to your LD2410

Here is a post about the RCWL-0516, a similar project, but this one can’t measure distances and person detection won’t work when a person is not moving.

https://www.henriaanstoot.nl/2022/11/14/radar-module-rcwl-0516-with-mqtt/

Steps to take:

  • Flash ESPHome over USB
  • Detect and add using Home Assistant
  • Edit and upload code (below)
  • Connect the sensor

Wire it up like this:

UPDATE : TURN YELLOW AND BLUE AROUND

ESPHome YAML:

Parts i’ve changed:
board: Changed from esp-1?? to d1_mini
logger: baud_rate: 0
tx_pin and rx_pin

esphome:
  name: ld2410-1
  friendly_name: ld2410-1

esp8266:
  board: d1_mini

# Enable logging
logger:
  baud_rate: 0

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx="

ota:
  password: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Ld2410-1 Fallback Hotspot"
    password: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

captive_portal:
    
ld2410:
  id: ld2410_radar
  
uart:
  tx_pin: GPIO15
  rx_pin: GPIO13
  baud_rate: 256000
  parity: NONE
  stop_bits: 1

number:
  - platform: ld2410
    timeout:
      name: Radar Timeout
    max_move_distance_gate:
      name: Radar Max Move Distance
    max_still_distance_gate:
      name: Radar Max Still Distance
    g0:
      move_threshold:
        name: g0 move threshold
      still_threshold:
        name: g0 still threshold
    g1:
      move_threshold:
        name: g1 move threshold
      still_threshold:
        name: g1 still threshold
    g2:
      move_threshold:
        name: g2 move threshold
      still_threshold:
        name: g2 still threshold
    g3:
      move_threshold:
        name: g3 move threshold
      still_threshold:
        name: g3 still threshold
    g4:
      move_threshold:
        name: g4 move threshold
      still_threshold:
        name: g4 still threshold
    g5:
      move_threshold:
        name: g5 move threshold
      still_threshold:
        name: g5 still threshold
    g6:
      move_threshold:
        name: g6 move threshold
      still_threshold:
        name: g6 still threshold
    g7:
      move_threshold:
        name: g7 move threshold
      still_threshold:
        name: g7 still threshold
    g8:
      move_threshold:
        name: g8 move threshold
      still_threshold:
        name: g8 still threshold

binary_sensor:
  - platform: ld2410
    has_target:
      name: Radar Target
      id: radar_has_target
    has_moving_target:
      name: Radar Moving Target
    has_still_target:
      name: Radar Still Target
button:
  - platform: ld2410
    factory_reset:
      name: "factory reset"
    restart:
      name: "restart"
    query_params:
      name: query params

sensor:
  - platform: ld2410
    moving_distance:
      name: Radar Moving Distance
      id: moving_distance
    still_distance:
      name: Radar Still Distance
      id: still_distance
    moving_energy:
      name: Radar Move Energy
    still_energy:
      name: Radar Still Energy
    detection_distance:
      name: Radar Detection Distance
      id: radar_detection_distance
    g0:
      move_energy:
        name: g0 move energy
      still_energy:
        name: g0 still energy
    g1:
      move_energy:
        name: g1 move energy
      still_energy:
        name: g1 still energy
    g2:
      move_energy:
        name: g2 move energy
      still_energy:
        name: g2 still energy
    g3:
      move_energy:
        name: g3 move energy
      still_energy:
        name: g3 still energy
    g4:
      move_energy:
        name: g4 move energy
      still_energy:
        name: g4 still energy
    g5:
      move_energy:
        name: g5 move energy
      still_energy:
        name: g5 still energy
    g6:
      move_energy:
        name: g6 move energy
      still_energy:
        name: g6 still energy
    g7:
      move_energy:
        name: g7 move energy
      still_energy:
        name: g7 still energy
    g8:
      move_energy:
        name: g8 move energy
      still_energy:
        name: g8 still energy

Bluetooth:

I’ve connected this HLK-DL2410B to Home Assistant before using Bluetooth. But I wanted to get them connected using Wifi.

You can install a App on you phone to connect to the sensor when powered on.
This way you can test the device, but also upgrade the firmware and make adjustments!

Just enable engineering mode and click more.

Testing another baud rate and upgrading the firmware:

Home Assistant ESPHome with toggle for interval

Posted because I could not find a good example on the interwebs.

Below creates a virtual HA button which toggles a blinking led.
(button and variables are called eprint for another function, change to something meaningful. )

Home Assistant virtual mqtt switch (configuration.yml)

See switch part

mqtt:
 light:    
  - name: "KitchenOutside"
    payload_on: "99"
    payload_off: "0"
    unique_id: "KitchenOutside"
    brightness_scale: "99"
    brightness_state_topic: ha433/kitchenoutside/brightcontrol
    brightness_command_topic: ha433/kitchenoutside/brightcontrol
    state_topic: ha433/kitchenoutside/brightcontrol
    command_topic: ha433/kitchenoutside/control
    optimistic: false
    on_command_type: brightness
#-----8<-------------snip ########### EXAMPLES light and sensor
 sensor:
  - name: "LivingTemperature"
    state_topic: "home/8461/payload"
    unit_of_measurement: "°C"
    value_template: "{{ value_json.svalue1 }}"
  - name: "LivingHumidity"
    state_topic: "home/8461/payload"
    unit_of_measurement: "%"
    value_template: "{{ value_json.svalue2 }}"
#--------8<--- snip ################################ ONLY PART BELOW NEEDED 
 switch:
    unique_id: esphome_switch
    name: "Esp Home Switch"
    state_topic: "esphome/eprint/tmpstate"
    command_topic: "esphome/eprint/state"
    payload_on: "ON"
    payload_off: "OFF"
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 0
    retain: true

ESP Home config for a ESP32

esphome:
  name: lolin32litemqttled
  friendly_name: lolin32litemqttled

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx="

ota:
  password: "xxxxxxxxxxxxxxxxxxxxxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Lolin32Litemqttled"
    password: "xxxxxxxxxxxxxxxxxxx"

captive_portal:
    
switch:
  - platform: gpio
    pin:
      number: 22
      mode: output
    id: blueLED
  - platform: template
    name: "eprint"
    id: eprint_enable
    optimistic: on

interval:
  - interval: 1000ms
    then:
      if:
        condition:
          switch.is_on: eprint_enable
        then:
          - switch.toggle: blueLED

mqtt:
  broker: 192.168.1.2
  on_message:
  - topic: esphome/eprint/state
    qos: 0
    payload: "OFF"
    then:
      - switch.turn_off: eprint_enable
  - topic: esphome/eprint/state
    qos: 0
    payload: "ON"
    then:
      - switch.turn_off: eprint_enable

Raspberry – Arduino Collection

Display case almost done.

Raspberry 1-3 different versions RP400, compute module, zero’s, pico, RP2040 (trinkey). (2012-2023)

I’ve got to many arduino clones, so only atmeta328p, uno, mega, nano ..
And a esp32 esp8066 plus a wemos mini (i use these a lot)

The Arduino’s will have to make place for other Raspberry stuff at a later time.

Custom made case with acrylic protection cover.

These all work, so this is also my stock.
When needed for a project, I will have some spares.

Raspberry Pi History Case

I started a little case for a collection of Raspberry devices.

Over the years, many Raspberries were designed and made.
It all started in 2012
I want to have a case with all RP’s i’ve used.

There are many iterations of the RPi, I’m missing a lot now. If you want to help me, send me old/broken raspberries to get the collection complete!

This is the case at the moment

Case with some Raspberries and Arduino’s I found lying around, I’m not going to dismantle projects. Only the RPi 1A, 4B, Zero, Zero W, Pico and RPi 400 are displayed.
Plastic sheet as protective layer still on there, should be clear as glass.

The case isn’t glued together yet. I’m not sure how and what to include.

  • Horizontal wooden bars to place the devices on?
  • Include a history of Arduino’s for now?
  • Put little notes in the case with information? Like my SDK-85 case?
  • ?

Get information about your RPi version

curl -L https://perturb.org/rpi?rev=$(awk '/^Revision/ { print $3 }' /proc/cpuinfo)

Micropython Pico W Internal Led Test

  • Download firmware from here (uf2 file)
    https://micropython.org/download/RPI_PICO_W/
    (Make sure you use W version if you have a pico-w)
  • Press bootsel and plug your pico into an usb port.
  • A folder should be accessible
  • cp RPI_PICO_W-20231005-v1.21.0.uf2 /media/$USER/RPI-RP2/
    (pico reboots and installs firmware)

Install and start Thonny.

Tools > Options > Interpreter

Python test code (1 second blink)
Note: for the normal pico without Wi-Fi, it is GP25 instead of LED.

from machine import Pin
import utime

led_onboard = Pin('LED', Pin.OUT)
while True:
        led_onboard.on()
        utime.sleep(1)
        led_onboard.off()
        utime.sleep(1)

Run at boottime?

File > save as:
Select device and name the python script main.py

Ultrasonic Sensor HC-SR04 + RP2040 (waveshare) Auto screen lock

Point the sensor at yourself when behind your computer.
When you leave your computer for some seconds, it wil automatically lock your screen. (Windows-L keypress)
The RP2040 is configured as HID so it emulates a keyboard.
Just connect via an usb-cable to your machine

Arduino Code

File > Preferences > Additional Board URLS
https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

Change USB Stack!

Download and install Adafruit_TinyUSB.zip

#include "Adafruit_TinyUSB.h"
// defines pins numbers
const int trigPin = D4;
const int echoPin = D5;
// defines variables
long duration;
int distance;
int maxcounter;
uint8_t const desc_hid_report[] =
{
  TUD_HID_REPORT_DESC_KEYBOARD()
};

// D0-D3 NOT USED AT THE MOMENT, I'VE GOT IDEAS FOR EXTRA FUNCTIONALLITY!

// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval, use out endpoint
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_KEYBOARD, 2, false);

//------------- Input Pins -------------//
// Array of pins and its keycode.
  uint8_t pins[] = { D0, D1, D2, D3 };


// number of pins
uint8_t pincount = sizeof(pins)/sizeof(pins[0]);

// For keycode definition check out https://github.com/hathach/tinyusb/blob/master/src/class/hid/hid.h
uint8_t hidcode[] = { HID_KEY_0, HID_KEY_1, HID_KEY_2, HID_KEY_3 , HID_KEY_4, HID_KEY_5 };

#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY) || defined(ARDUINO_FUNHOUSE_ESP32S2)
  bool activeState = true;
#else
  bool activeState = false;
#endif

void setup()
{
  // Setting pins for Ultrasonic Sensor HC-SR04
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
  // Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
  TinyUSB_Device_Init(0);
#endif

  // Set up output report (on control endpoint) for Capslock indicator
  // Not used .. yet
  usb_hid.setReportCallback(NULL, hid_report_callback);

  usb_hid.begin();

  // overwrite input pin with PIN_BUTTONx
  // NOT USED 
#ifdef PIN_BUTTON1
  pins[0] = PIN_BUTTON1;
#endif

#ifdef PIN_BUTTON2
  pins[1] = PIN_BUTTON2;
#endif

#ifdef PIN_BUTTON3
  pins[2] = PIN_BUTTON3;
#endif

#ifdef PIN_BUTTON4
  pins[3] = PIN_BUTTON4;
#endif

  // Set up pin as input
  for (uint8_t i=0; i<pincount; i++)
  {
    pinMode(pins[i], activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
  }

  // wait until device mounted
  while( !TinyUSBDevice.mounted() ) delay(1);

maxcounter =0;
}

void loop()
{
  
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  // Prints the distance on the Serial Monitor - DEBUG
  //Serial.print("Distance: ");
  //Serial.println(distance);

  // Below will wait for more than 100 measurements with a distance of 100
  // Then it will send a WINDOWS-L (lock) keyboard combination 
  if (distance > 100)
  {
    maxcounter +=1; 
  }
  else
  {
    maxcounter = 0;
  }
  if (maxcounter > 100 && maxcounter < 150)
  {
    maxcounter = 200;
       // Send report if there is key pressed
    uint8_t const report_id = 0;


    uint8_t  modifier = KEYBOARD_MODIFIER_LEFTGUI;
          uint8_t keycode[6] = { 0 };
      keycode[0] = HID_KEY_L;


    usb_hid.keyboardReport(report_id, modifier, keycode);
    delay(10);
    // Un-press keys :)
    usb_hid.keyboardRelease(0);
    
  }

  
  // poll gpio once each 2 ms
  delay(20);

  // used to avoid send multiple consecutive zero report for keyboard
  static bool keyPressedPreviously = false;

  uint8_t count=0;
  uint8_t keycode[6] = { 0 };

  // scan normal key and send report
  for(uint8_t i=0; i < pincount; i++)
  {
    if ( activeState == digitalRead(pins[i]) )
    {
      // if pin is active (low), add its hid code to key report
      keycode[count++] = hidcode[i];

      // 6 is max keycode per report
      if (count == 6) break;
    }
  }

  if ( TinyUSBDevice.suspended() && count )
  {
    // Wake up host if we are in suspend mode
    // and REMOTE_WAKEUP feature is enabled by host
    TinyUSBDevice.remoteWakeup();
  }

  // skip if hid is not ready e.g still transferring previous report
  if ( !usb_hid.ready() ) return;

  if ( count )
  {
    // Send report if there is key pressed
    uint8_t const report_id = 0;
    uint8_t const modifier = 0;

    keyPressedPreviously = true;
    usb_hid.keyboardReport(report_id, modifier, keycode);
  }else
  {
    // Send All-zero report to indicate there is no keys pressed
    // Most of the time, it is, though we don't need to send zero report
    // every loop(), only a key is pressed in previous loop()
    if ( keyPressedPreviously )
    {
      keyPressedPreviously = false;
      usb_hid.keyboardRelease(0);
    }
  }
}

// Output report callback for LED indicator such as Caplocks
void hid_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize)
{
  (void) report_id;
  (void) bufsize;

}

Arduino with Huskylens detecting learned face

Arduino Uno with Husky Lens using I2C

The HuskyLens is an easy-to-use AI machine vision sensor. It is equipped with multiple functions such as:

  • Face recognition
  • Object tracking
  • Object recognition
  • Line trace
  • Color recognition
  • Tag recognition (QR code).

Via the UART / I2C port you can among others: boards connect:

  • Arduino
  • micro:bit
  • Raspberry Pi

Steps to take: Press Face detection, when a cross in a square is displayed, press the button on your HuskyLens

Set your husky protocol to I2C in the settings.

Minimal Code needed

/***************************************************
 HUSKYLENS An Easy-to-use AI Machine Vision Sensor
 <https://www.dfrobot.com/product-1922.html> 
****************************************************/

#include "HUSKYLENS.h"

HUSKYLENS huskylens;
//HUSKYLENS green line >> SDA; blue line >> SCL
int ID0 = 0; //not learned results. Grey result on HUSKYLENS screen
int ID1 = 1; //first learned results. colored result on HUSKYLENS screen
int ID2 = 2; //second learned results. colored result on HUSKYLENS screen
// and so on.....

int arjprevious = 0;
void printResult(HUSKYLENSResult result);
void setup() {
    Serial.begin(115200);
    Wire.begin();
    while (!huskylens.begin(Wire))
    {
        Serial.println(F("Begin failed!"));
        Serial.println(F("1.Please recheck the \"Protocol Type\" in HUSKYLENS (General Settings>>Protocol Type>>I2C)"));
        Serial.println(F("2.Please recheck the connection."));
        delay(100);
    }
    
     huskylens.writeAlgorithm(ALGORITHM_FACE_RECOGNITION);
}

void loop() {
   if (huskylens.requestLearned())          //request blocks and arrows tangged ID != 0 from HUSKYLENS
   if (huskylens.requestBlocksLearned())    //request blocks tangged ID != ID0 from HUSKYLENS

    {
        for (int i = 0; i < huskylens.countArrows(ID0); i++)
        {
            HUSKYLENSResult result = huskylens.getArrow(ID0, i);  
        }
        int arj = huskylens.count(ID1);
        if ( arj != arjprevious )
        {
        if ( arj == 1 )
            {
            Serial.println("Learned face detected");
            }
            else
            {
            Serial.println("Learned face not detected");
            }
            arjprevious = arj;
        }
    }
    else
    {
        Serial.println("Fail to request objects from Huskylens!");
    }
}
Learned face detected ID1
Learned face not detected
Learned face detected ID1
Learned face not detected
Learned face detected ID1
Learned face not detected