Odessa Sinclair
Published

Wi-Fi HaLow-Powered Micro-Climate & Crowd Sensor for Island

BaliSense Node is a low-cost, autonomous sensor unit powered by ESP32 and Wi-Fi HaLow (802.11ah), ts

BeginnerWork in progressOver 4 days154
Wi-Fi HaLow-Powered Micro-Climate & Crowd Sensor for Island

Things used in this project

Hardware components

WROOMBEE -  ESP32 WROOM Xbee form factor
EngeBOT Technology WROOMBEE - ESP32 WROOM Xbee form factor
×1
Core MCU + Wi-Fi
×1
SparkFun Atmospheric Sensor Breakout - BME280
SparkFun Atmospheric Sensor Breakout - BME280
×1

Software apps and online services

Arduino IDE
Arduino IDE
Node-RED
Node-RED
Google Sheets API
MQTT broker
Balinese Event API

Hand tools and fabrication machines

Multimeter
3D Printer (generic)
3D Printer (generic)

Story

Read more

Code

Code (ESP32 + DHT + MQTT, PlatformIO / Arduino IDE)

C/C++
Code (ESP32 + DHT + MQTT, PlatformIO / Arduino IDE)
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>

#define DHTPIN 4           // GPIO where the DHT is connected
#define DHTTYPE DHT22      // DHT 22 (AM2302)

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

const char* mqtt_server = "broker.hivemq.com"; // Use public broker or your own
const char* mqtt_topic = "esp32/envdata";

WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE);

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect("ESP32Client")) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 sec");
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  dht.begin();
  setup_wifi();
  client.setServer(mqtt_server, 1883);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (!isnan(h) && !isnan(t)) {
    String payload = "{\"temperature\": " + String(t) + ", \"humidity\": " + String(h) + "}";
    client.publish(mqtt_topic, payload.c_str());
    Serial.println("Published: " + payload);
  } else {
    Serial.println("Failed to read from DHT sensor!");
  }

  delay(10000); // Publish every 10 seconds
}

Credits

Odessa Sinclair
1 project • 2 followers

Comments