Smart Relay Design Requirements
Waiting for input
Table
| Function | ESP32-C3 Pin | Net | Connected To |
|---|---|---|---|
| USB D- | GPIO18 | USB_DN | J1 D-, D1 ESD |
| USB D+ | GPIO19 | USB_DP | J1 D+, D1 ESD |
| I2C SDA | GPIO6 | I2C_SDA | U2 SDA, R5 4.7 kΩ pull-up |
| I2C SCL | GPIO7 | I2C_SCL | U2 SCL, R6 4.7 kΩ pull-up |
| Status LED | GPIO10 | STATUS_LED | R7 -> LED1 |
| BOOT | GPIO9 | ESP_BOOT | R4 pull-up, SW1 to GND |
| Reset/Enable | EN | ESP_EN | R3 pull-up, C6, SW2 to GND |
Ini
[env:esp32-c3-devkitm-1] platform = espressif32 board = esp32-c3-devkitm-1 framework = arduino monitor_speed = 115200 lib_deps = sensirion/Sensirion I2C SHT4x@^1.0.0
Cpp
#include <Arduino.h> #include <Wire.h> #include <WiFi.h> #include <SensirionI2CSht4x.h> #define I2C_SDA_PIN 6 #define I2C_SCL_PIN 7 #define STATUS_LED_PIN 10 #define SENSOR_READ_INTERVAL_MS 5000 const char* WIFI_SSID = "YOUR_SSID"; const char* WIFI_PASSWORD = "YOUR_PASSWORD"; SensirionI2CSht4x sht4x; unsigned long lastReadMs = 0; void initWiFi() { WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.print("Connecting to WiFi"); for (int i = 0; i < 20 && WiFi.status() != WL_CONNECTED; ++i) { digitalWrite(STATUS_LED_PIN, !digitalRead(STATUS_LED_PIN)); delay(500); Serial.print('.'); } Serial.println(); if (WiFi.status() == WL_CONNECTED) { Serial.print("WiFi IP: "); Serial.println(WiFi.localIP()); digitalWrite(STATUS_LED_PIN, HIGH); } else { Serial.println("WiFi offline; continuing sensor logging over USB serial."); digitalWrite(STATUS_LED_PIN, LOW); } } void setup() { pinMode(STATUS_LED_PIN, OUTPUT); digitalWrite(STATUS_LED_PIN, LOW); Serial.begin(115200); delay(500); Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN); sht4x.begin(Wire); uint32_t serialNumber = 0; int16_t error = sht4x.serialNumber(serialNumber); if (error) { Serial.print("SHT4x serial read failed: "); Serial.println(error); } else { Serial.print("SHT4x serial: "); Serial.println(serialNumber); } initWiFi(); } void loop() { if (millis() - lastReadMs >= SENSOR_READ_INTERVAL_MS) { lastReadMs = millis(); float temperatureC = 0.0f; float humidityRh = 0.0f; int16_t error = sht4x.measureHighPrecision(temperatureC, humidityRh); if (error) { Serial.print("SHT4x read failed: "); Serial.println(error); digitalWrite(STATUS_LED_PIN, LOW); } else { Serial.printf("T=%.2f C, RH=%.2f %%\n", temperatureC, humidityRh); digitalWrite(STATUS_LED_PIN, HIGH); // Production firmware can publish this payload via MQTT/HTTPS/BLE GATT. } } if (WiFi.status() != WL_CONNECTED) { static unsigned long lastReconnect = 0; if (millis() - lastReconnect > 30000) { lastReconnect = millis(); initWiFi(); } } }
pio runpio run -t uploadpio device monitorPlatform
Pin Mapping
platformio.ini
src/main.cpp
Build and Flash