119 lines
2.7 KiB
C++
Raw Normal View History

2021-06-10 18:36:16 +02:00
#include <Arduino.h>
2021-06-22 20:25:27 +02:00
#ifdef WITH_DEBUGGING_ON
2021-06-23 16:49:53 +02:00
#include <GDBStub.h> // debugging support via GDBStub over UART
#endif
2021-06-10 20:58:48 +02:00
#include "PCA9685.h"
2021-06-23 16:49:53 +02:00
extern "C" {
#include "user_interface.h"
2021-06-22 20:25:27 +02:00
}
// OTA & WEB
2021-06-23 16:49:53 +02:00
#include "ota.h"
#include "wifi_credentials.h"
2021-06-23 15:22:38 +02:00
#include "httpserver.h"
2021-06-10 18:36:16 +02:00
2021-06-23 16:49:53 +02:00
// BOARD
2021-06-21 15:32:38 +02:00
#define ESP12_LED 2
#define NODEMCU_LED 16
2021-06-23 16:49:53 +02:00
// PWM
2021-06-23 21:23:31 +02:00
#include "pwm.h"
2021-06-22 20:36:53 +02:00
os_timer_t timer1;
2021-06-23 16:49:53 +02:00
uint8_t timer_flag = 0;
2021-06-23 21:23:31 +02:00
Treppe stairs(13);
2021-06-23 16:49:53 +02:00
// WIFI
const char* ssid = STASSID;
const char* password = STAPSK;
2021-06-21 13:11:18 +02:00
2021-06-21 12:26:52 +02:00
void timerCallback(void *pArg)
2021-06-21 13:11:24 +02:00
{
2021-06-21 12:26:52 +02:00
*((int *) pArg) += 1;
2021-06-23 21:23:31 +02:00
stairs.task();
2021-06-21 15:32:38 +02:00
}
2021-06-21 12:26:52 +02:00
2021-06-23 16:49:53 +02:00
// HTTP
2021-06-21 09:43:06 +02:00
void handleNotFound();
2021-06-23 16:12:19 +02:00
HTTPServer httpServer(80, "/");
2021-06-21 09:43:06 +02:00
2021-06-23 17:12:40 +02:00
uint32_t _t=0;
#define SP_US(_str,_a) Serial.print(_str); Serial.print(" took: "); Serial.print(_a); Serial.println("us")
#define TIMEIF_US(_f, _l, _str) _t=micros(); _f; _t=micros()-_t; if(_t > _l) { SP_US(_str, _t); }
2021-06-23 16:49:53 +02:00
// ===============================================
2021-06-10 18:36:16 +02:00
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
2021-06-23 16:12:19 +02:00
message += httpServer.uri();
2021-06-10 18:36:16 +02:00
message += "\nMethod: ";
2021-06-23 16:12:19 +02:00
message += (httpServer.method() == HTTP_GET) ? "GET" : "POST";
2021-06-10 18:36:16 +02:00
message += "\nArguments: ";
2021-06-23 16:12:19 +02:00
message += httpServer.args();
2021-06-10 18:36:16 +02:00
message += "\n";
2021-06-23 16:12:19 +02:00
for (uint8_t i = 0; i < httpServer.args(); i++) {
message += " " + httpServer.argName(i) + ": " + httpServer.arg(i) + "\n";
2021-06-10 18:36:16 +02:00
}
2021-06-23 16:12:19 +02:00
httpServer.send(404, "text/plain", message);
2021-06-21 09:43:06 +02:00
}
2021-06-10 20:19:25 +02:00
2021-06-23 17:12:40 +02:00
void setup() {
#ifdef WITH_DEBUGGING_ON
Serial.begin(460800);
gdbstub_init();
#else
Serial.begin(115200);
#endif
2021-06-21 10:48:27 +02:00
2021-06-23 17:12:40 +02:00
Serial.println(F("Booting ...."));
pinMode(NODEMCU_LED, OUTPUT);
pinMode(ESP12_LED, OUTPUT);
Wire.begin(); // Wire must be started first
2021-06-24 19:46:24 +02:00
Wire.setClock(1000000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz
2021-06-23 17:12:40 +02:00
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
ota_setup();
httpServer.start();
httpServer.onNotFound(handleNotFound);
Serial.println("HTTP server started !");
2021-06-23 21:23:31 +02:00
stairs.setup();
2021-06-23 17:12:40 +02:00
Serial.println("PCA9685 connected !");
os_timer_setfn(&timer1, timerCallback, &timer_flag);
os_timer_arm(&timer1, 20, true);
2021-06-23 21:23:31 +02:00
stairs.setState(1);
stairs.setDirection(1);
2021-06-23 17:12:40 +02:00
}
void loop() {
2021-06-23 21:23:31 +02:00
if(millis() > 25000 && stairs.getState() == 1 && stairs.getDirection() == 1) stairs.setState(0);
if(millis() > 45000 && stairs.getDirection() == 1){
stairs.setState(1);
stairs.setDirection(0);
2021-06-21 10:48:27 +02:00
}
2021-06-23 01:30:28 +02:00
TIMEIF_US(ArduinoOTA.handle(), 1000, "OTA");
2021-06-23 15:22:38 +02:00
TIMEIF_US(httpServer.handleClient(), 1000, "HTTP");
2021-06-10 18:36:16 +02:00
}