Machinechat with ESP32 and WebOTA Support

Software:

Project Parts:

In Arduino, use this link in the “Additional Boards Managers URLs” field: to add ESP32 support.

https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

Add these libraries thru Arduino’s Library Manager:

Full Source:

#include <WebOTA.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>

#ifdef __cplusplus
extern "C" {
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif
uint8_t temprature_sens_read();

uint8_t ESP32_tempF;

// Create a unique ID for the data from each NodeMCU running this code
const char* jediID = "ESP32-Temp";

// Wi-Fi settings - replace with your Wi-Fi SSID and password
const char* mdns     = "ESP32-OTA"; // Used for MDNS resolution
//const char* ssid     = "DemoWiFi";
//const char* password = "DemoWiFi";

// IP address of server running Machinechat JEDI software
// If you changed the JEDI port number, replace 8100 with the new port
//const char* host = "192.168.1.210:8100";

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(115200);

  Serial.println();
  Serial.println();
  Serial.println();

  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\r\n", t);
    Serial.flush();
    delay(500);
  }

  init_wifi(ssid, password, mdns);

  // Wait for Wi-Fi connection and show progress on serial monitor
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.flush();

  // Defaults to 8080 and "/webota"
  //webota.init(80, "/update");
}

// the loop function runs over and over again forever
void loop() {
  String postData;
  int md = 1000;

  webota.delay(md);
  webota.handle();

  ESP32_tempF = temprature_sens_read();

  Serial.print(" | ESP32 Temp[F]: ");
  Serial.print(ESP32_tempF);
  Serial.println(" |");

  //Following code creates the serialized JSON string to send to JEDI One
  //using ArduinoJson library
  StaticJsonDocument <200> doc;

  JsonObject context = doc.createNestedObject("context");
  context["target_id"] = String(jediID);

  JsonObject data = doc.createNestedObject("data");
  data["ESP32_tempF"] = ESP32_tempF;

  serializeJson(doc, postData);

  //This prints the JSON to the serial monitor screen
  Serial.println(postData);

  if (WiFi.status() == WL_CONNECTED) {
    WiFiClient client;
    HTTPClient http;

    String address = String("http://") + String(host) + String("/v1/data/mc");
    http.begin(client, address);
    http.addHeader("Content-Type", "application/json");

    int httpResponseCode = http.POST(postData);

    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);

    // Free resources
    http.end();
  }
}
1 Like