Beehive scale and hive temperature (MQTT)

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

⚠ Safety

Li-ion cells: watch polarity, use protected cells and a charger with protection, never leave a damaged cell charging unattended.

What you need

Buy the parts kit · Estimated total: $19.30

ESP32 DevKit V1 (30 pin, WROOM-32) × 1
$4.00
Buy at AliExpress Buy at Amazon.com
HX711 + 4 load cells (platform scale kit) × 1
$6.00
Buy at AliExpress Buy at Amazon.com
DS18B20 waterproof probe × 1
$1.50
Buy at AliExpress Buy at Amazon.com
18650 Li-ion cell + holder × 1
$4.00
Buy at AliExpress Buy at Amazon.com
TP4056 USB-C Li-ion charger with protection × 1
$0.80
Buy at AliExpress Buy at Amazon.com
Plastic project enclosure × 1
$3.00
Buy at AliExpress Buy at Amazon.com
🇺🇦 Ukraine🌍 Other countries

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.

Wiring

ModuleModule pinESP32 pinNote
HX711 DT / SCKdigitalGPIO16 / GPIO17VCC 3V3
HX711 E+ E- A+ A-load cellsfour cells in a bridgefollow the colour code of your cells
DS18B20 DATA1-WireGPIO44.7 kΩ pull-up to 3V3

Libraries

Arduino IDE → Tools → Manage Libraries, then install:

  • HX711 (bogde)
  • OneWire
  • DallasTemperature
  • PubSubClient

Code

beehive-scale.ino
#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

Build tips

  • Calibrate with a known weight: CAL_FACTOR = raw difference / weight in kg.
  • Shield the electronics from rain and sun, and re-zero after adding a super.

← ← All projects

Related ideas

⚠ Caution

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.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●○○ Beginner
  • Build time 1–2 h
Pick parts / kit
⛔ High risk

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.

Night-tariff water heater scheduler

Heats the water heater only in the cheap night tariff zone or from surplus solar power, with a temperature limit.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●● Advanced
  • Build time 3–5 h
Pick parts / kit
⚠ Caution

Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.

Orchard and greenhouse frost alert

A battery sensor that wakes up rarely and sends an alert when the temperature falls toward freezing.

  • Board ESP32-C3 SuperMini
  • Level ●○○ Beginner
  • Build time 1–2 h
Pick parts / kit

Comments

No comments yet. Share your experience or ask a question.

Comments are moderated.