/* * ReddWatch community mini-node — ESP32 WiFi direct-ingest firmware. * The ~$150 "build your own" path: no LoRa gateway needed, posts readings * straight to /api/ingest over HTTPS every 15 minutes. * * Hardware (see BOM): * ESP32 dev board (any — tested layout for ESP32-WROOM/S3) * DS18B20 waterproof temperature probe → OneWire, pin 4 (+ 4.7kΩ pull-up) * DFRobot SEN0189 analog turbidity → ADC1 pin 34 * analog pressure transducer 0–5m / 0.5–4.5V → ADC1 pin 35 (optional) * USB power or small solar + 18650 + TP4056/CN3791 * * PROVISIONING — three lines below. Your node ID + key come from * /stations ("My stations") after your site proposal is approved. * Keep NODE_KEY private — it signs your station's data. * * Libraries (Arduino Library Manager): OneWire, DallasTemperature. * Board: "ESP32 Dev Module" or your variant. HTTPS via WiFiClientSecure. */ #include #include #include #include #include #include #include // ---------- YOUR PROVISIONED VALUES (from /stations) ---------- const char* WIFI_SSID = "YOUR_WIFI"; const char* WIFI_PASS = "YOUR_PASSWORD"; const char* NODE_ID = "com-XXXXXX"; const char* NODE_KEY = "PASTE-YOUR-KEY"; const char* API_URL = "https://reddwatch.duckdns.org/api/ingest"; // ------------------------------------------------------------ #define PIN_ONEWIRE 4 #define PIN_TURB 34 // ADC1_CH6 #define PIN_LEVEL 35 // ADC1_CH7 (0.5–4.5 V pressure transducer) #define PIN_BATT 36 // ADC1_CH0 battery divider (optional) static const uint32_t SLEEP_MIN = 15; static const uint8_t TURB_AVG = 40; // analog samples to average OneWire ow(PIN_ONEWIRE); DallasTemperature dallas(&ow); float readTemp() { dallas.requestTemperatures(); float t = dallas.getTempCByIndex(0); return (t > -50 && t < 85) ? t : NAN; } float readTurbidity() { // DFRobot SEN0189: higher voltage = cleaner water. Map raw ADC → NTU // with a two-point field calibration (clear water + 100 NTU standard). // Defaults below are a sane starting point — calibrate yours. const float V_CLEAR = 4.1f, V_100NTU = 3.0f; // volts at 0 / 100 NTU uint32_t acc = 0; for (int i = 0; i < TURB_AVG; i++) { acc += analogRead(PIN_TURB); delay(5); } float v = (acc / (float)TURB_AVG) * (3.3f / 4095.0f); float ntu = (V_CLEAR - v) * (100.0f / (V_CLEAR - V_100NTU)); return constrain(ntu, 0, 3000); } float readLevel() { // 0.5–4.5 V transducer → 0–5 m head. Adjust SPAN_M for your sensor. const float SPAN_M = 5.0f; uint32_t acc = 0; for (int i = 0; i < TURB_AVG; i++) { acc += analogRead(PIN_LEVEL); delay(5); } float v = (acc / (float)TURB_AVG) * (3.3f / 4095.0f); if (v < 0.4f) return NAN; // unplugged / fault return constrain((v - 0.5f) / 4.0f * SPAN_M, 0, SPAN_M); } bool wifiUp(uint32_t ms = 20000) { WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID, WIFI_PASS); for (uint32_t t = millis(); WiFi.status() != WL_CONNECTED && millis() - t < ms;) delay(250); return WiFi.status() == WL_CONNECTED; } void setup() { Serial.begin(115200); analogReadResolution(12); analogSetAttenuation(ADC_11db); dallas.begin(); float temp = readTemp(); float turb = readTurbidity(); float lvl = readLevel(); int batt = analogRead(PIN_BATT); if (!wifiUp()) { Serial.println("[ingest] wifi failed — skipping cycle (no store-forward on mini)"); esp_sleep_enable_timer_wakeup(SLEEP_MIN * 60ULL * 1000000ULL); esp_deep_sleep_start(); } // build JSON by hand — no ArduinoJson dependency String body = String("{\"node_id\":\"") + NODE_ID + "\",\"readings\":{"; bool first = true; auto add = [&](const char* k, float v, int dp) { if (isnan(v)) return; if (!first) body += ','; body += '"'; body += k; body += "\":"; body += String(v, dp); first = false; }; add("temp_c", temp, 2); add("turbidity_ntu", turb, 1); add("level_m", lvl, 3); if (batt > 200) add("battery_v", batt * (3.3f / 4095.0f) * 2.0f, 2); // 1:1 divider add("rssi_dbm", WiFi.RSSI(), 0); body += "}}"; WiFiClientSecure client; client.setInsecure(); // public API over TLS; cert pin optional HTTPClient http; http.begin(client, API_URL); http.addHeader("Content-Type", "application/json"); http.addHeader("X-Node-Token", NODE_KEY); int code = http.POST(body); Serial.printf("[ingest] %d %s\n", code, body.c_str()); http.end(); esp_sleep_enable_timer_wakeup(SLEEP_MIN * 60ULL * 1000000ULL); esp_deep_sleep_start(); } void loop() {} // everything happens in setup(), then deep sleep