Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.
Door / window sensor
A battery sensor with a reed switch that wakes from deep sleep only when the door opens.
Weighs the hive with four load cells, measures the temperature and publishes to MQTT every 15 minutes, then sleeps.
Level: Intermediate Board: ESP32 DevKit V1 (WROOM-32) Climate
Li-ion cells: watch polarity, use protected cells and a charger with protection, never leave a damaged cell charging unattended.
Buy the parts kit · Estimated total: $19.30
Other countries: order these parts on AliExpress with the links below.
Some links are affiliate links: we may earn a commission at no extra cost to you.
| Module | Module pin | ESP32 pin | Note |
|---|---|---|---|
| HX711 DT / SCK | digital | GPIO16 / GPIO17 | VCC 3V3 |
| HX711 E+ E- A+ A- | load cells | four cells in a bridge | follow the colour code of your cells |
| DS18B20 DATA | 1-Wire | GPIO4 | 4.7 kΩ pull-up to 3V3 |
Arduino IDE → Tools → Manage Libraries, then install:
#include <WiFi.h>
const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";
bool connectWiFi(uint32_t timeoutMs = 20000) {
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
uint32_t t0 = millis();
while (WiFi.status() != WL_CONNECTED && millis() - t0 < timeoutMs) {
delay(250);
Serial.print(".");
}
Serial.println();
if (WiFi.status() == WL_CONNECTED) {
Serial.print("IP: ");
Serial.println(WiFi.localIP());
return true;
}
return false;
}
#include <PubSubClient.h>
const char* MQTT_HOST = "192.168.1.10"; // IP of your Raspberry Pi / mini-PC
const uint16_t MQTT_PORT = 1883;
const char* MQTT_USER = "esp32";
const char* MQTT_PASS = "mqtt-password";
WiFiClient netClient;
PubSubClient mqtt(netClient);
bool mqttConnect(const char* clientId) {
mqtt.setServer(MQTT_HOST, MQTT_PORT);
uint32_t t0 = millis();
while (!mqtt.connected() && millis() - t0 < 10000) {
if (mqtt.connect(clientId, MQTT_USER, MQTT_PASS)) return true;
delay(500);
}
return mqtt.connected();
}
#include <HX711.h>
#include <OneWire.h>
#include <DallasTemperature.h>
HX711 scale; OneWire ow(4); DallasTemperature ds(&ow);
float CAL_FACTOR = 22000.0; // raw units per kg: calibrate with a known weight (see tips)
const uint64_t SLEEP_US = 15ULL * 60ULL * 1000000ULL;
RTC_DATA_ATTR float tareOffset = 0;
void setup() {
Serial.begin(115200); ds.begin();
scale.begin(16, 17);
if (scale.wait_ready_timeout(2000)) {
if (tareOffset == 0) { scale.tare(10); tareOffset = scale.get_offset(); } else scale.set_offset(tareOffset); // first boot with an empty platform = zero
scale.set_scale(CAL_FACTOR);
float kg = scale.get_units(10);
ds.requestTemperatures(); float t = ds.getTempCByIndex(0);
if (connectWiFi() && mqttConnect("beehive1")) {
String j = String("{\"weight_kg\":") + String(kg, 2) + ",\"temp_c\":" + String(t, 1) + "}";
mqtt.publish("home/beehive1/state", j.c_str(), true); mqtt.loop(); delay(200);
}
}
scale.power_down();
esp_sleep_enable_timer_wakeup(SLEEP_US); esp_deep_sleep_start();
}
void loop() {}Flash a ready firmware from the browser Paste your error into the AI Error Doctor
Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.
A battery sensor with a reed switch that wakes from deep sleep only when the door opens.
Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.
Heats the water heater only in the cheap night tariff zone or from surplus solar power, with a temperature limit.
Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.
A battery sensor that wakes up rarely and sends an alert when the temperature falls toward freezing.
Comments
No comments yet. Share your experience or ask a question.