Customize platform and src so the code can be used on both ESP32 and ESP8266
This commit is contained in:
parent
8310fd0a17
commit
b1b307b4c3
2
include/esp32.h
Normal file
2
include/esp32.h
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
#define BUTTONPIN 22 // 22/GPIO22
|
||||||
|
#define BOARD "ESP32"
|
||||||
2
include/esp8266.h
Normal file
2
include/esp8266.h
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
#define BUTTONPIN 14 // D5/GPIO14
|
||||||
|
#define BOARD "ESP8266"
|
||||||
|
|
@ -8,14 +8,38 @@
|
||||||
; Please visit documentation for the other options and examples
|
; Please visit documentation for the other options and examples
|
||||||
; https://docs.platformio.org/page/projectconf.html
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
|
[platformio]
|
||||||
|
default_envs = esp32doit-devkit-v1
|
||||||
|
;default_envs = nodemcuv2
|
||||||
|
|
||||||
|
[common]
|
||||||
|
build_flags =
|
||||||
|
-D DEBUG=false
|
||||||
|
|
||||||
[env:esp32doit-devkit-v1]
|
[env:esp32doit-devkit-v1]
|
||||||
platform = espressif32
|
platform = espressif32
|
||||||
board = esp32doit-devkit-v1
|
board = esp32doit-devkit-v1
|
||||||
framework = arduino
|
framework = arduino
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
upload_protocol = espota
|
upload_protocol = espota
|
||||||
upload_port = 192.168.2.232
|
upload_port = 192.168.2.221
|
||||||
|
;upload_port = COM6
|
||||||
|
build_flags =
|
||||||
|
${common.build_flags}
|
||||||
lib_compat_mode = strict
|
lib_compat_mode = strict
|
||||||
lib_deps =
|
lib_deps =
|
||||||
tzapu/WiFiManager@^2.0.17
|
tzapu/WiFiManager@^2.0.17
|
||||||
lostincompilation/PrettyOTA@^1.1.2
|
|
||||||
|
[env:nodemcuv2]
|
||||||
|
platform = https://github.com/platformio/platform-espressif8266.git
|
||||||
|
board = nodemcuv2
|
||||||
|
framework = arduino
|
||||||
|
upload_protocol = espota
|
||||||
|
upload_port = 192.168.2.238
|
||||||
|
;upload_port = COM6
|
||||||
|
upload_speed = 115200
|
||||||
|
monitor_speed = 115200
|
||||||
|
build_flags =
|
||||||
|
${common.build_flags}
|
||||||
|
lib_deps =
|
||||||
|
tzapu/WiFiManager@^2.0.17
|
||||||
|
|
|
||||||
87
src/main.cpp
87
src/main.cpp
|
|
@ -1,41 +1,50 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <WiFiManager.h>
|
#include <WiFiManager.h>
|
||||||
#include <WiFi.h>
|
#include <ArduinoOTA.h>
|
||||||
#include <HTTPClient.h>
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
#include <PrettyOTA.h>
|
#include <esp32.h>
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <HTTPClient.h>
|
||||||
|
#elif defined(ARDUINO_ARCH_ESP8266)
|
||||||
|
#include <esp8266.h>
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ESP8266HTTPClient.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#define BOARDFIRMWARE "2.2"
|
#define BOARDFIRMWARE "2.5"
|
||||||
|
|
||||||
#define BUTTONPIN 22
|
|
||||||
#define DEBOUNCEMS 15
|
#define DEBOUNCEMS 15
|
||||||
#define BOARD "ESP32"
|
|
||||||
#define SERVICE "LEDsActionButton"
|
#define SERVICE "LEDsActionButton"
|
||||||
#define STATUSINTERVAL 600000
|
#define STATUSINTERVAL 600000 // 10 minutes
|
||||||
|
|
||||||
|
String hostAddress = "moon.basement.lan";
|
||||||
|
unsigned int hostPort = 80;
|
||||||
|
String actionURL = "/l/actionbutton.php?state=";
|
||||||
|
String statusURL = "/l/boardstatus.php";
|
||||||
|
|
||||||
String actionURL = "http://moon.basement.lan/l/actionbutton.php?state=";
|
|
||||||
String statusURL = "http://moon.basement.lan/l/boardstatus.php";
|
|
||||||
unsigned short buttonState = HIGH, lastButtonState = HIGH;
|
unsigned short buttonState = HIGH, lastButtonState = HIGH;
|
||||||
unsigned long lastStatusTime = 0;
|
unsigned long lastStatusTime = 0;
|
||||||
|
|
||||||
AsyncWebServer server(80);
|
|
||||||
PrettyOTA OTAUpdates;
|
|
||||||
|
|
||||||
void sendActionMessage(bool state) {
|
void sendActionMessage(bool state) {
|
||||||
Serial.println("Action button triggered");
|
if ( DEBUG ) Serial.println("Action button triggered");
|
||||||
char webMsg[255] = "";
|
String msgURL = actionURL + (state ? "true" : "false");
|
||||||
strcat(webMsg, actionURL.c_str());
|
|
||||||
strcat(webMsg, (state) ? "true" : "false");
|
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
http.begin(webMsg);
|
if ( BOARD == "ESP32" ) {
|
||||||
|
msgURL = "http://" + hostAddress + ":" + String(hostPort) + msgURL;
|
||||||
|
http.begin(msgURL.c_str());
|
||||||
|
} else {
|
||||||
|
WiFiClient client;
|
||||||
|
http.begin(client, hostAddress.c_str(), hostPort, msgURL.c_str());
|
||||||
|
}
|
||||||
http.GET();
|
http.GET();
|
||||||
http.end();
|
http.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long sendDeviceStatus() {
|
unsigned long sendDeviceStatus() {
|
||||||
Serial.println("Sending device status");
|
if ( DEBUG ) Serial.println("Sending device status");
|
||||||
WiFiClient client;
|
WiFiClient client;
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
http.begin(client, statusURL.c_str());
|
http.begin(client, ("http://" + hostAddress + ":" + String(hostPort) + statusURL).c_str());
|
||||||
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||||
String httpData = "mac=" + WiFi.macAddress() + "&board=" + BOARD + "&firmware=" + BOARDFIRMWARE + "&service=" + SERVICE;
|
String httpData = "mac=" + WiFi.macAddress() + "&board=" + BOARD + "&firmware=" + BOARDFIRMWARE + "&service=" + SERVICE;
|
||||||
int httpResponseCode = http.POST(httpData);
|
int httpResponseCode = http.POST(httpData);
|
||||||
|
|
@ -43,46 +52,14 @@ unsigned long sendDeviceStatus() {
|
||||||
return millis();
|
return millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets called when update starts
|
|
||||||
// updateMode can be FILESYSTEM or FIRMWARE
|
|
||||||
void OnOTAStart(NSPrettyOTA::UPDATE_MODE updateMode) {
|
|
||||||
Serial.println("OTA update started");
|
|
||||||
if(updateMode == NSPrettyOTA::UPDATE_MODE::FIRMWARE)
|
|
||||||
Serial.println("Mode: Firmware");
|
|
||||||
else if(updateMode == NSPrettyOTA::UPDATE_MODE::FILESYSTEM)
|
|
||||||
Serial.println("Mode: Filesystem");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gets called while update is running
|
|
||||||
// currentSize: Number of bytes already processed
|
|
||||||
// totalSize: Total size of new firmware in bytes
|
|
||||||
void OnOTAProgress(uint32_t currentSize, uint32_t totalSize) {
|
|
||||||
Serial.printf("OTA Progress Current: %u bytes, Total: %u bytes\n", currentSize, totalSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gets called when update finishes
|
|
||||||
void OnOTAEnd(bool successful) {
|
|
||||||
if (successful)
|
|
||||||
Serial.println("OTA update finished successfully");
|
|
||||||
else
|
|
||||||
Serial.println("OTA update failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
pinMode(BUTTONPIN, INPUT_PULLUP);
|
pinMode(BUTTONPIN, INPUT_PULLUP);
|
||||||
Serial.println("MAC Address: " + WiFi.macAddress());
|
if ( DEBUG ) Serial.println("MAC Address: " + WiFi.macAddress());
|
||||||
WiFiManager wifiManager;
|
WiFiManager wifiManager;
|
||||||
wifiManager.autoConnect("LEDsActionButton");
|
wifiManager.autoConnect("LEDsActionButton");
|
||||||
lastStatusTime = sendDeviceStatus();
|
lastStatusTime = sendDeviceStatus();
|
||||||
OTAUpdates.Begin(&server);
|
ArduinoOTA.begin();
|
||||||
OTAUpdates.SetHardwareID(SERVICE);
|
|
||||||
OTAUpdates.OverwriteAppVersion(BOARDFIRMWARE);
|
|
||||||
PRETTY_OTA_SET_CURRENT_BUILD_TIME_AND_DATE();
|
|
||||||
OTAUpdates.OnStart(OnOTAStart);
|
|
||||||
OTAUpdates.OnProgress(OnOTAProgress);
|
|
||||||
OTAUpdates.OnEnd(OnOTAEnd);
|
|
||||||
server.begin();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long lastDebounceTime = 0;
|
unsigned long lastDebounceTime = 0;
|
||||||
|
|
@ -92,9 +69,6 @@ void loop() {
|
||||||
if ( millis() > (lastStatusTime + STATUSINTERVAL) ) {
|
if ( millis() > (lastStatusTime + STATUSINTERVAL) ) {
|
||||||
lastStatusTime = sendDeviceStatus();
|
lastStatusTime = sendDeviceStatus();
|
||||||
}
|
}
|
||||||
if ( millis() > 86400000 ) {
|
|
||||||
ESP.restart();
|
|
||||||
}
|
|
||||||
unsigned short buttonCheck = digitalRead(BUTTONPIN);
|
unsigned short buttonCheck = digitalRead(BUTTONPIN);
|
||||||
if ( buttonCheck != lastButtonState ) {
|
if ( buttonCheck != lastButtonState ) {
|
||||||
debouncing = true;
|
debouncing = true;
|
||||||
|
|
@ -111,4 +85,5 @@ void loop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lastButtonState = buttonCheck;
|
lastButtonState = buttonCheck;
|
||||||
|
ArduinoOTA.handle();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user