Commit 33c8cdb0 authored by Jochen Rößner's avatar Jochen Rößner

add rest interface, add buildtime and version

parent 6cbebf23
echo -n "-D BUILDTIME='\"$(date +%F-%H:%M:%S)\"'"
\ No newline at end of file
......@@ -12,6 +12,11 @@
platform = espressif8266
board = esp12e
framework = arduino
; build_flags = -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG
build_flags =
-DCORE_DEBUG_LEVEL=4
!sh build_timestamp.sh
; build_flags = -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
src_filter = +<*> -<*.ino*>
......
......@@ -90,6 +90,29 @@ int led(int value)
return value;
}
int ledtoggle(int value)
{
DEBUGLOG(PSTR("led: %u\n"), value);
switch (value)
{
case 1:
digitalWrite(LED_TOP_PIN, !digitalRead(LED_TOP_PIN));
digitalWrite(LED_BOTTOM_PIN, 0);
break;
case 2:
digitalWrite(LED_TOP_PIN, 0);
digitalWrite(LED_BOTTOM_PIN, !digitalRead(LED_BOTTOM_PIN));
break;
default:
Serial.println(F("Do nothing"));
break;
}
broadcastWS("led", value);
return value;
}
int ledtop(int value)
{
DEBUGLOG(PSTR("ledtop: %u\n"), value);
......
......@@ -37,6 +37,16 @@ extern void sendDebugWS(String name, String type, String value);
#define DEFAULT_HOSTNAME "ledlight"
#define MDNSSERVICENAME DEFAULT_HOSTNAME
#define XSTR(x) STR(x)
#define STR(x) #x
#ifndef BUILDTIME
#define BUILDTIME "none"
#endif
#pragma message("BUILDTIME=" XSTR(BUILDTIME))
const char *version = "0.2";
const char *buildtime = (const char *)BUILDTIME;
//with starting slash
#define INTOPIC_SUFFIX "/ledlight/setpercent"
#define OUTTOPIC_SUFFIX "/ledlight/percent"
......@@ -72,6 +82,7 @@ extern void loopDebug();
//extern int setPosition(int value);
//extern int getPosition(int value);
extern int led(int value);
extern int ledtoggle(int value);
extern int temperature(int value);
extern int maxtemperature(int value);
extern int ledtop(int value);
......@@ -80,6 +91,7 @@ extern int ledbottom(int value);
std::map<std::string, std::function<int(int)>> wsMap =
{
{"led", led},
{"ledtoggle", ledtoggle},
{"temperature", temperature},
{"maxtemperature", maxtemperature},
{"ledtop", ledtop},
......@@ -565,6 +577,42 @@ void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties
}
}
void handleRestCall(AsyncWebServerRequest *request)
{
String argfunc;
String argvalue;
if (request->hasArg("func"))
{
argfunc = request->arg("func");
}
else
{
argvalue = String("");
}
if (request->hasArg("value"))
{
argvalue = request->arg("value");
}
else
{
argvalue = String("");
}
DEBUGLOG("handleRestCall: %s => %s\r\n", argfunc.c_str(), argvalue.c_str());
const char *wstype = argfunc.c_str();
if (wstype && wsMap.count(wstype) == 1)
{
int value = argvalue.toInt();
int wsres = wsMap[wstype](value);
Serial.printf("value: %i, wsres: %i\n", value, wsres);
request->send(200, "text/json", String(String("{ \"func\": \"") + wstype + "\", \"result\": " + String(wsres) + " }"));
}
else{
request->send(404, "text/plain", "Function Not Found");
}
}
bool setupWifi(bool force)
{
......@@ -625,7 +673,6 @@ bool setupWifi(bool force)
void setup()
{
// put your setup code here, to run once:
Serial.begin(74880);
Serial.println(F("Booting"));
EEPROM.begin(sizeof(hostConfig) + sizeof(appConfig));
......@@ -662,15 +709,12 @@ void setup()
Serial.print(F("IP address: "));
Serial.println(WiFi.localIP());
if (WiFi.isConnected())
if (MDNS.begin(myhostConfig.hostName))
{
if (MDNS.begin(myhostConfig.hostName))
{
Serial.println(F("MDNS responder started"));
MDNS.addService("http", "tcp", 80);
MDNS.addService(MDNSSERVICENAME, "tcp", 80);
Serial.println(F("MDNS services added"));
}
Serial.println(F("MDNS responder started"));
MDNS.addService("http", "tcp", 80);
MDNS.addService(MDNSSERVICENAME, "tcp", 80);
Serial.println(F("MDNS services added"));
}
SPIFFS.begin();
......@@ -701,6 +745,10 @@ void setup()
handleFileList(request);
});
server.on("/rest", HTTP_GET, [](AsyncWebServerRequest *request) {
handleRestCall(request);
});
// attach AsyncWebSocket
wscontrol.onEvent(wsEvent);
server.addHandler(&wscontrol);
......@@ -742,7 +790,7 @@ void setup()
void loop()
{
// put your main code here, to run repeatedly:
MDNS.update();
if (shouldReboot)
{
Serial.println("Rebooting...");
......
......@@ -2,6 +2,10 @@
hostname=$1
curl -F 'name=update' -F 'filename=@.pio/build/esp12e/firmware.bin' "http://$hostname/update"
curl --progress-bar \
-F 'name=update' \
-F 'filename=@.pio/build/esp12e/firmware.bin' \
"http://$hostname/update" \
| tee /dev/null
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment