dron — ESP32 simulation

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by dronrajdas.

Components

Sketch code

#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>

// ========================================
// POLARSENSE AI
// POTENTIOMETER → ESP32 → MQTT
// ========================================

// -------- POTENTIOMETER PINS --------
#define WIND_PIN    34
#define WAVE_PIN    35
#define BATTERY_PIN 32

// -------- WIFI --------
const char* WIFI_SSID = "Velxio-GUEST";

// -------- MQTT --------
const char* MQTT_SERVER = "broker.hivemq.com";
const int MQTT_PORT = 1883;

const char* MQTT_TOPIC = "polarsenseai/buoy/telemetry";

WiFiClient espClient;
PubSubClient mqttClient(espClient);


// ========================================
// CONNECT TO WIFI
// ========================================

void connectWiFi() {

  WiFi.mode(WIFI_STA);

  Serial.print("Connecting to Wi-Fi");

  WiFi.begin(WIFI_SSID);

  int attempts = 0;

  while (WiFi.status() != WL_CONNECTED && attempts < 40) {

    delay(500);
    Serial.print(".");

    attempts++;
  }

  Serial.println();

  if (WiFi.status() == WL_CONNECTED) {

    Serial.println("WIFI CONNECTED");

    Serial.print("IP ADDRESS: ");
    Serial.println(WiFi.localIP());

  } else {

    Serial.println("WIFI CONNECTION FAILED");
  }
}


// ========================================
// CONNECT TO MQTT
// ========================================

void connectMQTT() {

  while (!mqttClient.connected()) {

    Serial.print("Connecting to MQTT...");

    String clientId = "PolarSenseESP32-";
    clientId += String((uint32_t)ESP.getEfuseMac(), HEX);

    if (mqttClient.connect(clientId.c_str())) {

      Serial.println("CONNECTED");

    } else {

      Serial.print("FAILED, STATE=");
      Serial.println(mqttClient.state());

      delay(2000);
    }
  }
}


// ========================================
// SETUP
// ========================================

void setup() {

  Serial.begin(115200);

  delay(1000);

  analogReadResolution(12);

  Serial.println();
  Serial.println("================================");
  Serial.println("POLARSENSE AI");
  Serial.println("POTENTIOMETER TELEMETRY TEST");
  Serial.println("================================");

  // Wi-Fi
  connectWiFi();

  // MQTT
  mqttClient.setServer(
    MQTT_SERVER,
    MQTT_PORT
  );

  // Give MQTT enough space for JSON
  mqttClient.setBufferSize(512);

  connectMQTT();

  Serial.println("--------------------------------");
  Serial.println("BUOY TELEMETRY LINK ONLINE");
  Serial.println("================================");
}


// ========================================
// MAIN LOOP
// ========================================

void loop() {

  // Reconnect MQTT if necessary
  if (!mqttClient.connected()) {
    connectMQTT();
  }

  mqttClient.loop();


  // --------------------------------------
  // READ POTENTIOMETERS
  // --------------------------------------

  int windRaw = analogRead(WIND_PIN);
  int waveRaw = analogRead(WAVE_PIN);
  int batteryRaw = analogRead(BATTERY_PIN);


  // --------------------------------------
  // CONVERT VALUES

More projects by dronrajdas