187 lines
4.5 KiB
C++
187 lines
4.5 KiB
C++
#include <WiFi.h>
|
|
|
|
#include <ESPmDNS.h>
|
|
#include <WiFiUdp.h>
|
|
#include <ArduinoOTA.h>
|
|
#include <Adafruit_NeoPixel.h>
|
|
|
|
|
|
|
|
#define PIN_WS2812 15
|
|
#define PIN_T_SENS 35
|
|
#define PIN_FAN 5
|
|
#define PIN_STATUS1 18
|
|
#define PIN_STATUS2 19
|
|
|
|
//#define NUM_LEDS 144
|
|
#define NUM_LEDS 150
|
|
Adafruit_NeoPixel strip(NUM_LEDS, PIN_WS2812, NEO_GRB + NEO_KHZ800);
|
|
|
|
|
|
enum class ConnState {
|
|
DISCONNECTED = 0,
|
|
STA_CONNECTING,
|
|
STA_CONNECTED,
|
|
WIFI_AP,
|
|
};
|
|
|
|
static ConnState connState = ConnState::DISCONNECTED;
|
|
static uint32_t connStateSecs = 0;
|
|
|
|
static const char* wifiSTAssid = "...";
|
|
static const char* wifiSTApassword = "...";
|
|
|
|
static const char* wifiAPssid = "...";
|
|
static const char* wifiAPpassword = "...";
|
|
|
|
static const char* mdnsName = "...";
|
|
static const char* otaPassword = "...";
|
|
|
|
|
|
void StaConnect() {
|
|
Serial.printf("Connecting to WiFi %s\n", wifiSTAssid);
|
|
WiFi.mode(WIFI_OFF);
|
|
// WiFi.end();
|
|
connState = ConnState::STA_CONNECTING;
|
|
connStateSecs = 0;
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(wifiSTAssid, wifiSTApassword);
|
|
}
|
|
|
|
void ApInit() {
|
|
Serial.printf("Opening WiFi AP %s\n", wifiAPssid);
|
|
WiFi.mode(WIFI_OFF);
|
|
connState = ConnState::WIFI_MODE_AP;
|
|
connStateSecs = 0;
|
|
WiFi.softAP(wifiAPssid, wifiAPpassword);
|
|
IPAddress myIP = WiFi.softAPIP();
|
|
Serial.print("AP IP address: ");
|
|
Serial.println(myIP);
|
|
}
|
|
|
|
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info)
|
|
{
|
|
Serial.printf("WiFiStationConnected\r\n");
|
|
connState = ConnState::STA_CONNECTED;
|
|
}
|
|
|
|
|
|
static void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info)
|
|
{
|
|
Serial.printf("WiFiStationDisconnected\r\n");
|
|
if(connState == ConnState::STA_CONNECTED) {
|
|
connState = ConnState::DISCONNECTED;
|
|
StaConnect();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Serial.println("Booting");
|
|
|
|
|
|
WiFi.onEvent(WiFiStationConnected, SYSTEM_EVENT_STA_CONNECTED);
|
|
WiFi.onEvent(WiFiStationDisconnected, SYSTEM_EVENT_STA_DISCONNECTED);
|
|
|
|
StaConnect();
|
|
|
|
// while (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
|
// Serial.println("Connection Failed! Rebooting...");
|
|
// delay(5000);
|
|
// ESP.restart();
|
|
// }
|
|
// put your setup code here, to run once:
|
|
|
|
pinMode(PIN_STATUS1, OUTPUT);
|
|
pinMode(PIN_STATUS2, OUTPUT);
|
|
pinMode(PIN_FAN, OUTPUT);
|
|
pinMode(PIN_T_SENS, ANALOG);
|
|
strip.begin();
|
|
for(uint16_t i = 0; i < NUM_LEDS; i++) {
|
|
strip.setPixelColor(i, 127,127,127);
|
|
}
|
|
strip.show();
|
|
|
|
// Hostname defaults to esp3232-[MAC]
|
|
ArduinoOTA.setHostname(mdnsName);
|
|
|
|
// No authentication by default
|
|
ArduinoOTA.setPassword(otaPassword);
|
|
|
|
ArduinoOTA
|
|
.onStart([]() {
|
|
String type;
|
|
if (ArduinoOTA.getCommand() == U_FLASH)
|
|
type = "sketch";
|
|
else // U_SPIFFS
|
|
type = "filesystem";
|
|
|
|
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
|
|
Serial.println("Start updating " + type);
|
|
})
|
|
.onEnd([]() {
|
|
Serial.println("\nEnd");
|
|
})
|
|
.onProgress([](unsigned int progress, unsigned int total) {
|
|
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
|
|
})
|
|
.onError([](ota_error_t error) {
|
|
Serial.printf("Error[%u]: ", error);
|
|
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
|
|
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
|
|
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
|
|
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
|
|
else if (error == OTA_END_ERROR) Serial.println("End Failed");
|
|
});
|
|
|
|
ArduinoOTA.begin();
|
|
}
|
|
|
|
static void DelayWithOta(uint32_t msecs)
|
|
{
|
|
uint32_t mstart = millis();
|
|
uint32_t mnow;
|
|
do {
|
|
mnow = millis();
|
|
if(mnow < mstart) break; //overflow
|
|
ArduinoOTA.handle();
|
|
delay(1);
|
|
} while((mstart + msecs) > mnow);
|
|
}
|
|
|
|
static bool fanOn = false;
|
|
void loop() {
|
|
strip.show();
|
|
|
|
digitalWrite(PIN_STATUS1, HIGH);
|
|
DelayWithOta(100);
|
|
digitalWrite(PIN_STATUS1, LOW);
|
|
DelayWithOta(900);
|
|
|
|
uint32_t tmp = connStateSecs;
|
|
tmp += 1;
|
|
if(tmp > connStateSecs) {
|
|
connStateSecs = tmp;
|
|
}
|
|
if(connState == ConnState::STA_CONNECTING && connStateSecs > 30) {
|
|
ApInit();
|
|
} else if(connState == ConnState::WIFI_MODE_AP && connStateSecs > 3600) {
|
|
StaConnect();
|
|
}
|
|
|
|
int sensorValue = analogRead(PIN_T_SENS);
|
|
Serial.printf("tsens=%i\n", sensorValue);
|
|
|
|
if(sensorValue < 2100 && !fanOn) {
|
|
fanOn = true;
|
|
digitalWrite(PIN_FAN, HIGH);
|
|
digitalWrite(PIN_STATUS2, HIGH);
|
|
} else if(sensorValue > 2400 && fanOn) {
|
|
fanOn = false;
|
|
digitalWrite(PIN_FAN, LOW);
|
|
digitalWrite(PIN_STATUS2, LOW);
|
|
}
|
|
}
|