Compare commits
2 Commits
0625a098da
...
a5300f34e6
| Author | SHA1 | Date | |
|---|---|---|---|
| a5300f34e6 | |||
| 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,13 +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
|
||||||
|
|
||||||
|
[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
|
||||||
|
|
|
||||||
43
src/main.cpp
43
src/main.cpp
|
|
@ -1,40 +1,50 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <WiFiManager.h>
|
#include <WiFiManager.h>
|
||||||
|
#include <ArduinoOTA.h>
|
||||||
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
|
#include <esp32.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
#include <ArduinoOTA.h>
|
#elif defined(ARDUINO_ARCH_ESP8266)
|
||||||
|
#include <esp8266.h>
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ESP8266HTTPClient.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#define BOARDFIRMWARE "2.5"
|
#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
|
||||||
|
|
||||||
const char hostAddress[] = "moon.basement.lan";
|
String hostAddress = "moon.basement.lan";
|
||||||
unsigned int hostPort = 80;
|
unsigned int hostPort = 80;
|
||||||
String actionURL = "http://moon.basement.lan/l/actionbutton.php?state=";
|
String actionURL = "/l/actionbutton.php?state=";
|
||||||
String statusURL = "http://moon.basement.lan/l/boardstatus.php";
|
String statusURL = "/l/boardstatus.php";
|
||||||
|
|
||||||
unsigned short buttonState = HIGH, lastButtonState = HIGH;
|
unsigned short buttonState = HIGH, lastButtonState = HIGH;
|
||||||
unsigned long lastStatusTime = 0;
|
unsigned long lastStatusTime = 0;
|
||||||
|
|
||||||
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);
|
||||||
|
|
@ -45,7 +55,7 @@ unsigned long sendDeviceStatus() {
|
||||||
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();
|
||||||
|
|
@ -59,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;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user