소프트웨어
프로젝트 부품
아두이노에서, “Additional boards manager URLs:” 필드에 다음 링크를 사용하여 ESP32 지원을 추가합니다.
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
아두이노 Library Manager를 통해 다음 라이브러리를 추가합니다:
전체 소스:
#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();
}
}