IoT A — ESP32 simulation

Aquaculture monitoring system with temperature, pH, and motor control.

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

Components

Sketch code

#include <WiFi.h>
#include <HTTPClient.h>

// Velxio Wi-Fi
const char* WIFI_SSID = "Espressif";
const char* WIFI_PASSWORD = "";

// ThingSpeak Write API Key
const char* API_KEY = "0XS21RTGTBSTLCK8";

void connectWiFi() {
  Serial.println("\nConnecting to Wi-Fi...");

  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  unsigned long startTime = millis();

  while (WiFi.status() != WL_CONNECTED &&
         millis() - startTime < 15000) {
    delay(500);
    Serial.print(".");
  }

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nWi-Fi Connected!");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println("\nWi-Fi Connection Failed!");
  }
}

void sendToThingSpeak() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Wi-Fi disconnected!");
    return;
  }

  // Sample values only, not actual sensor readings
  float temperature = 29.5;
  float ph = 7.4;
  float salinity = 15.2;

  int aeratorStatus = 1;
  int feederStatus = 0;

  String url = "http://api.thingspeak.com/update?api_key=";
  url += API_KEY;
  url += "&field1=" + String(temperature, 1);
  url += "&field2=" + String(ph, 1);
  url += "&field3=" + String(salinity, 1);
  url += "&field4=" + String(aeratorStatus);
  url += "&field5=" + String(feederStatus);

  HTTPClient http;
  http.setTimeout(15000);

  Serial.println("\nSending data to ThingSpeak...");

  if (!http.begin(url)) {
    Serial.println("HTTP initialization failed!");
    return;
  }

  int responseCode = http.GET();

  Serial.print("HTTP response code: ");
  Serial.println(responseCode);

  if (responseCode == 200) {
    String response = http.getString();
    Serial.print("ThingSpeak entry ID: ");
    Serial.println(response);

    if (response.toInt() > 0) {
      Serial.println("Upload successful!");
    } else {
      Serial.println("Upload failed. Check API key and channel fields.");
    }
  } else {
    Serial.println(http.errorToString(responseCode));
  }

  http.end();
}

void setup() {
  Serial.begin(115200);
  delay(2000);

  connectWiFi();
  delay(1000);
  sendToThingSpeak();
}

void loop() {
  delay(20000);
  sendToThingSpeak();
}

More projects by balu