Elementic logo

NoBrand

ESP32-C3 Development

Elementic-compatible device record with linked microcontroller, GPIO map and starter Arduino code.

3910876540GD5V3VRXTX21

Device information

Controller
ESP32-C3
Category
Not set
Pins
11

Controller details

ESP32-C3

Espressif · ESP32-C3 · RISC-V single-core

400 KB RAM · 22 GPIO

Starter Arduino code

Device specific
#include <Arduino.h>
#include <U8g2lib.h>
#include <WiFi.h>

extern const uint16_t ELEMENTIC_DEVICEID = 8; // NodeMCU V2/V3 ESP8266 ESP-12E

#include <Elementic.h>

namespace {
constexpr uint8_t OLED_SDA_PIN = 5;
constexpr uint8_t OLED_SCL_PIN = 6;
constexpr uint8_t OLED_I2C_ADDRESS = 0x3C;
constexpr uint32_t OLED_REFRESH_MS = 1000;

U8G2_SSD1306_72X40_ER_F_SW_I2C oled(U8G2_R0, OLED_SCL_PIN, OLED_SDA_PIN, U8X8_PIN_NONE);

uint32_t lastOledRefreshMs = 0;

const char* mqttStatusText() {
  if (mqttClient.connected() || mqttClient.state() == MQTT_CONNECTED) {
    return "MQTT OK";
  }

  switch (mqttClient.state()) {
    case MQTT_CONNECTION_TIMEOUT: return "MQTT timeout";
    case MQTT_CONNECTION_LOST: return "MQTT lost";
    case MQTT_CONNECT_FAILED: return "MQTT failed";
    case MQTT_DISCONNECTED: return "MQTT offline";
    case MQTT_CONNECT_BAD_PROTOCOL: return "MQTT bad proto";
    case MQTT_CONNECT_BAD_CLIENT_ID: return "MQTT bad id";
    case MQTT_CONNECT_UNAVAILABLE: return "MQTT unavail";
    case MQTT_CONNECT_BAD_CREDENTIALS: return "MQTT bad login";
    case MQTT_CONNECT_UNAUTHORIZED: return "MQTT no auth";
    default: return "MQTT wait";
  }
}

void drawClippedText(uint8_t x, uint8_t y, const String& text, uint8_t maxWidth) {
  String clipped = text;
  while (clipped.length() > 0 && oled.getStrWidth(clipped.c_str()) > maxWidth) {
    clipped.remove(clipped.length() - 1);
  }
  oled.drawStr(x, y, clipped.c_str());
}

void updateOledStatus(bool force = false) {
  const uint32_t now = millis();
  if (!force && (uint32_t)(now - lastOledRefreshMs) < OLED_REFRESH_MS) {
    return;
  }
  lastOledRefreshMs = now;

  const bool wifiConnected = WIFIConnected || (WiFi.status() == WL_CONNECTED);
  const bool networkOnline = wifiConnected || EthernetConnected || networkConnected;
  const int32_t rssi = wifiConnected ? WiFi.RSSI() : 0;
  String ssid = WiFi.SSID();
  if (ssid.length() == 0) {
    ssid = GetStringValue("SSID");
  }
  if (ssid.length() == 0) {
    ssid = "-";
  }

  oled.clearBuffer();
  oled.setFont(u8g2_font_5x7_tf);
  oled.drawStr(0, 7, "Power ON");

  char line[24];
  if (wifiConnected) {
    snprintf(line, sizeof(line), "WiFi %lddBm", (long)rssi);
  } else if (networkOnline) {
    snprintf(line, sizeof(line), "Net online");
  } else {
    snprintf(line, sizeof(line), "WiFi offline");
  }
  oled.drawStr(0, 17, line);

  drawClippedText(0, 27, ssid, 72);
  drawClippedText(0, 38, mqttStatusText(), 72);

  oled.sendBuffer();
}
}

void setup() {
  
  oled.setI2CAddress(OLED_I2C_ADDRESS << 1);
  oled.begin();
  updateOledStatus(true);

  ElementicSetup();
  updateOledStatus(true);
}

void loop() {

  ElementicLoop();
  updateOledStatus();

  // ESP8266: be nice to watchdog if ElementicLoop can be heavy
  
}

Pin summary

GPIO Name Flags
0 0 Binary in, Digital out, Binary out
1 1 Binary in, Digital out, Binary out
1 3 Analog in, Binary in, Digital out, Binary out, Interrupt
2 2 Binary in, Digital out, Binary out
4 4 Analog in, Binary in, Digital out, Binary out, Interrupt
5 5 Analog in, Binary in, Digital out, Binary out, Interrupt
6 6 Analog in, Binary in, Digital out, Binary out, Interrupt
7 7 Analog in, Binary in, Digital out, Binary out, Interrupt
8 8 Binary in, Digital out, Binary out
9 9 Binary in, Digital out, Binary out
10 10 Binary in, Digital out, Binary out