9.10.2024

foto Petr Bravenec

Petr Bravenec
Twitter: @BravenecPetr
+420 777 566 384
petr.bravenec@hobrasoft.cz

Introduction

Meshtastic (more here: Meshtastic - Network for the Zombie Apocalypse) is a network used for communication, at least in my area. However, various hardware can be easily connected to Meshtastic devices – these are the "things" in the commonly used phrase "Internet of Things."

We tested how to send information from a PIR sensor (motion detector) via the Meshtastic network. The PIR sensor is typically used in automatically activated lights or alarm systems, and is ideal for detecting movement cheaply (including humans, cats, martens... or even mosquitoes if misconfigured). While activating a light through Meshtastic may be overkill, using it for a basic alarm system in a garden shed without electricity is a perfect solution.

The entire Meshtastic device chain could look like this:

  • The first device, in "silent client" mode, with a connected PIR sensor, starts sending an event notification to the network upon motion detection.
  • The second device, in "client" or "silent client" mode, with an attached siren, begins sounding the alarm upon receiving the event.
  • If there's no direct connection between the devices, a third device can act as a router, relaying the information from the detector to the siren.

Setting Up the Communication Channel

Meshtastic sends information over a single frequency channel. This channel is shared, and without additional configuration, Meshtastic devices broadcast to everyone. PIR sensor data shouldn’t be sent publicly because it could annoy others and cause a privacy breach. Meshtastic allows for the creation of a private encrypted channel. The easiest way to set this up is via the mobile app.

  • Connect to the PIR device via Bluetooth and find the "Channels" page. You should see at least one channel (by default, "LongFast"; in the Czech Republic, "MediumFast" is often used).
  • Create a new channel — name it as you wish, for example, PIR — and generate an encryption key (PSK, with "AQ==" meaning no encryption). You need this channel in the first position (number zero) so that all data from the connected hardware (here, the PIR sensor) is sent through it. Delete any channels with a lower number, probably "MediumFast."
  • Copy the PSK key for this channel to your clipboard to use on the second device.
  • Without the "MediumFast" channel, you won’t be able to receive public messages, but this usually isn’t an issue for the PIR detector. If needed, you can recreate the "MediumFast" channel at this point; it will appear at the end of the list.
  • Connect to the second device (with the siren) via Bluetooth. Create a channel with the same name and copy the PSK key from the PIR device. Since this device will receive information, the channel order isn’t as important, so you can leave "MediumFast" in the first position.
  • This is also a good time to create a remote administration channel: create a channel named "admin" (it must be exactly this name) and copy its PSK key.
  • Reconnect to the PIR device via Bluetooth and create the "admin" channel there as well, ensuring the PSK key is identical to the second device’s.
  • After testing communication on the PIR and admin channels, you can turn off WiFi, Bluetooth, enable power-saving mode, and switch the PIR device to "silent client" mode (CLIENT_MUTE). In this configuration, the device’s power consumption should be minimal.

With remote administration set up, you can connect permanently via Bluetooth to the device with the siren and make any necessary adjustments from there.

Device with Connected Detector

The PIR sensor has its own settings page: "Detection Sensor." A detailed description is available on the Meshtastic website: Detection Sensor Module Usage. Three critical options are:

  • GPIO pin to monitor – Specifies which pin the PIR sensor is connected to. On our Heltec V3, pin numbers match the board, but this might differ on other devices.
  • Detection is triggered on HIGH – Activates when the sensor detects movement.
  • Use INPUT_PULLUP mode – Strengthens the signal from the PIR sensor, especially necessary if using a switching transistor.

Settings can be tested without the PIR sensor attached. In a resting state, the device should not send any messages. When the GPIO pin is connected to ground, the device should send a detection message. This behavior corresponds to the following settings:

  • Detection triggered on HIGH – off
  • Use INPUT_PULLUP mode – on

This ensures proper functionality when using a transistor to boost the PIR sensor signal and maintain correct logic levels.

Connecting the PIR Sensor

We have three different PIR sensors available. The best in practice is the largest one (HC-SR501). Smaller, simpler sensors can't be adjusted, and if the sensitivity is too high, the sensor will trigger from every tiny movement, like a passing insect, causing many false alarms. The HC-SR501 allows adjustments for sensitivity, activation duration, and detection mode. The sensor is clearly designed for switching lights and offers two modes: The first mode: the light stays on as long as movement is detected. The second mode: the light turns on with motion but switches off after a preset time, regardless of further movement.

When the sensor detects movement, it sets the output to a logical 1. You can measure this signal with a modern digital voltmeter (an older analog voltmeter may have too low an internal resistance, which can affect the accuracy of the measurement), but the signal strength is too weak to drive the circuits in the Heltec Meshtastic device directly. Therefore, a switching transistor is needed to boost the signal. We used a BS170 (a switching FET), but many other bipolar switching transistors would likely work just as well.

The transistor operates against the internal pull-up resistor in the Heltec device. If the device lacks an internal pull-up, an external resistor can be used. In our case, the external resistor shown in the schematic wasn't necessary.

Connecting the Siren

For experiments with the PIR sensor, a siren isn't necessary—you can use the Meshtastic device connected via Bluetooth to a mobile phone, which can notify you gently. However, a loud siren is a good addition to a security alarm system in regular use.

The siren is connected to the second, receiving device. We used a simple miniature speaker salvaged from an old PC. However, you can also use a real siren, as they often look the same but behave differently. Both can be used effectively.

  • Siren – When connected with the correct polarity, it emits a loud screech. It’s usually much louder than a speaker but will only make a continuous, unpleasant noise without any melody.
  • Speaker – When connected to a power source, it make scratching noises, it may overheat, and eventually burn out. However, Meshtastic can work with a speaker to send simple melodies, making it more versatile despite the risk of damage if misused.

In addition to a siren, you can connect a signal LED or a vibration motor (from an old phone) to the Meshtastic device. Both the LED and siren can be connected directly, although using a switching transistor as an amplifier might be useful. For the vibration motor, a transistor is recommended (though untested).

We encountered a key issue: the speaker only worked on GPIO 0 (likely the only pin supporting PWM).

Settings can be found under "External Notification" on the Meshtastic site: External Notification Module Configuration.

The critical settings are:

  • Output buzzer (GPIO) – We found only GPIO 1 worked, with output on pin 0 of the Heltec. It's likely that the siren will work on any GPIO (as it did with the LED).
  • Use PWM buzzer – Enable this for a speaker. For a siren, it should be off.
  • Ringtone – You can set an appropriate melody for the alarm. For example, the code for a sufficiently alarming sound might look like this: "siren =8,o=5,b=100 ,e,d,e,d,e,d,e". The description on Meshtastic is brief, but additional details can be found on ESPHome: Rtttl Buzzer.
  • The remaining options are fairly straightforward, and you’ll likely figure out their usage on your own.

Similarly, you can connect an LED, a relay to activate a real ship siren for serious alarms, or any other alerting device.

An interesting option is connecting an I²S device, which sends an audio signal digitally (similar to MIDI or I²C). AliExpress offers various amplifiers for around one or two dollars, which can be connected to a speaker and power supply. However, we haven't tested this setup.

Odkazy

Hobrasoft s.r.o. | Contact