diff --git a/Lumix.cpp b/Lumix.cpp new file mode 100644 index 0000000..10c1a6d --- /dev/null +++ b/Lumix.cpp @@ -0,0 +1,110 @@ +#include "Lumix.h" + +Lumix::Lumix(String ip){ + this->ip = ip; + connectionStatus = 0; + deviceName = ""; + lastKeepAlive = 0; +} + +void Lumix::updateDeviceName(String deviceName){ + this->deviceName = deviceName; +} + +void Lumix::updateConnectionStatus(int connectionStatus){ + this->connectionStatus = connectionStatus; +} + +void Lumix::updateIp(String ip){ + this->ip = ip; +} + +bool Lumix::isConnected(){ + return connectionStatus == CAMERA_CONNECTED; +} + +bool Lumix::isConnecting(){ + return connectionStatus == CAMERA_CONNECTING; +} + +bool Lumix::isDisabled(){ + return connectionStatus == CAMERA_DISABLED; +} + +void Lumix::update(){ + if((millis()-lastKeepAlive) > LUMIX_KEEPALIVE_INTERVAL && !isDisabled()){ + Serial.println("Sending Lumix Keep Alive"); + + if(!isConnected()){ + sendCommand("?mode=accctrl&type=req_acc&value=0&value2=LumixStick"); + } + + sendCommand("?mode=camcmd&value=recmode"); + lastKeepAlive = millis(); + } +} + +int Lumix::getConnectionStatus(){ + return connectionStatus; +} + +String Lumix::getDeviceName(){ + return deviceName; +} + +String Lumix::getBaseUrl(){ + return "http://" + ip + "/cam.cgi"; +} + +String Lumix::getCaptureUrl(){ + return "http://" + ip + "/cam.cgi?mode=camcmd&value=capture"; +} + +//this assumed what you are passing is the bit after cam.cgi ex. "?mode=camcmd&value=recmode" +void Lumix::sendCommand(String paramString){ + String commandUrl = "http://" + ip + "/cam.cgi" + paramString; + Serial.println("Sending request: " + commandUrl); + HTTPClient http; + http.setConnectTimeout(100); + http.begin(commandUrl.c_str()); + + int httpResponseCode = http.GET(); + + if(httpResponseCode == 200){ + String payload = http.getString(); + if(payload.indexOf("ok") >= 0){ + if(paramString.indexOf("req_acc") >= 0 && isConnecting()){ + updateDeviceName(getSplitStringIndex(payload,',',1)); + updateConnectionStatus(CAMERA_CONNECTED); + } + }else{ + if(isConnected()){ + updateConnectionStatus(CAMERA_CONNECTING); + } + } + } + else { + //Some other error either way we didn't get an "ok" back from the camera + if(isConnected()){ + updateConnectionStatus(CAMERA_CONNECTING); + } + } +} + +// https://stackoverflow.com/questions/9072320/split-string-into-string-array +String Lumix::getSplitStringIndex(String data, char separator, int index) +{ + int found = 0; + int strIndex[] = {0, -1}; + int maxIndex = data.length()-1; + + for(int i=0; i<=maxIndex && found<=index; i++){ + if(data.charAt(i)==separator || i==maxIndex){ + found++; + strIndex[0] = strIndex[1]+1; + strIndex[1] = (i == maxIndex) ? i+1 : i; + } + } + + return found>index ? data.substring(strIndex[0], strIndex[1]) : ""; +} diff --git a/Lumix.h b/Lumix.h new file mode 100644 index 0000000..8415a5c --- /dev/null +++ b/Lumix.h @@ -0,0 +1,40 @@ +#define CAMERA_DISABLED 0 +#define CAMERA_CONNECTING 1 +#define CAMERA_CONNECTED 2 + +#define LUMIX_KEEPALIVE_INTERVAL 2000 + +#include +#include + +class Lumix { + private: + String ip; + int connectionStatus; + String deviceName; + unsigned long lastKeepAlive; + + public: + Lumix(String ip); + + void updateDeviceName(String deviceName); + void updateConnectionStatus(int connectionStatus); + void updateIp(String ip); + + bool isConnected(); + bool isConnecting(); + bool isDisabled(); + + void update(); + + int getConnectionStatus(); + String getDeviceName(); + + String getBaseUrl(); + String getCaptureUrl(); + + void sendCommand(String paramString); + + + String getSplitStringIndex(String data, char separator, int index); +}; diff --git a/LumixStick.ino b/LumixStick.ino new file mode 100644 index 0000000..963dc23 --- /dev/null +++ b/LumixStick.ino @@ -0,0 +1,122 @@ +#include +#include + +#include + +#include +#include +#include "Lumix.h" + +// Put your wifi SSID and password here +const char* ssid = "ssid"; +const char* password = "password"; + +WebServer server(80); + +Lumix camera = Lumix("192.168.54.1"); + +const char index_html[] PROGMEM = R"rawliteral( + + LumixStick + + +
+ Camera IP: + +
+)rawliteral"; + + +void handleRoot() { + server.send(200, "text/html", index_html); +} + +void handleSave(){ + if(server.arg("ip") == ""){ + server.send(200,"text/plain", "No IP Param....no update made"); + }else{ + String response = "Camera IP Updated to " + server.arg("ip"); + camera.updateIp(server.arg("ip")); + server.send(200, "text/plain", response); + } +} + +void handleNotFound() { + String message = "File Not Found\n\n"; + message += "URI: "; + message += server.uri(); + message += "\nMethod: "; + message += (server.method() == HTTP_GET) ? "GET" : "POST"; + message += "\nArguments: "; + message += server.args(); + message += "\n"; + for (uint8_t i = 0; i < server.args(); i++) { + message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; + } + server.send(404, "text/plain", message); +} + +// the setup routine runs once when M5StickC starts up +void setup(){ + M5.begin(); + M5.Lcd.fillScreen(BLACK); + Serial.begin(115200); + + WiFi.begin(ssid,password); + Serial.print("WiFi Connecting"); + // Wait for connection + while (WiFi.status() != WL_CONNECTED) { + delay(100); + Serial.print("."); + } + Serial.println(); + Serial.println("WiFi Connected - " + WiFi.localIP().toString()); + + server.on("/", handleRoot); + server.on("/save", handleSave); + server.onNotFound(handleNotFound); + + server.begin(); + +} + +void updateCameraStatusIndicator(){ + + int circleRadius = 7; + int padding = 4; + + switch (camera.getConnectionStatus()) { + case CAMERA_DISABLED: + M5.Lcd.fillCircle(M5.Lcd.width() - circleRadius - padding, circleRadius + padding, circleRadius, TFT_RED); + break; + case CAMERA_CONNECTING: + M5.Lcd.fillCircle(M5.Lcd.width() - circleRadius - padding, circleRadius + padding, circleRadius, TFT_YELLOW); + break; + case CAMERA_CONNECTED: + M5.Lcd.fillCircle(M5.Lcd.width() - circleRadius - padding, circleRadius + padding, circleRadius, TFT_GREEN); + break; + default: + // statements + break; + } +} + +void loop() { + M5.update(); + server.handleClient(); + camera.update(); + updateCameraStatusIndicator(); + + if(M5.BtnB.wasPressed()){ + if(!camera.isDisabled()){ + camera.updateConnectionStatus(CAMERA_DISABLED); + }else{ + camera.updateConnectionStatus(CAMERA_CONNECTING); + } + } + + if(M5.BtnA.wasPressed() && camera.isConnected()){ + Serial.println("Sending Lumix Capture Command"); + camera.sendCommand("?mode=camcmd&value=capture"); + } +}