Parcourir la source

Add files via upload

Ferrazzi il y a 3 ans
Parent
commit
97d9928cd7

+ 10 - 0
FlipperZero-IFTTT_WiFi_Module/APIndex.h

@@ -0,0 +1,10 @@
+  String html ="<!DOCTYPE html> \
+  <html> \
+  <body> \
+  <center><h1>FlipperZero IFTTT configuration</h1></center> \
+  <form> \
+  <button name=\"test1\" button style=\"color:green\" value=\"valore1\" type=\"submit\">Test1</button> \
+  <button name=\"test2\" button style=\"color:green\" value=\"valore2\" type=\"submit\">Test2</button> \
+  </form> \
+  </body> \
+  </html>";

+ 47 - 0
FlipperZero-IFTTT_WiFi_Module/APMode.cpp

@@ -0,0 +1,47 @@
+#include "APMode.h"
+#include "APIndex.h"
+#include "Global.h"
+
+WiFiServer server(80);
+
+const char *ssidAP = "FlipperZero-IFTTT";
+const char *passwordAP = "FlipperZero";
+
+APMode::APMode()
+{
+}
+
+String midString(String str, String start, String finish){
+  int locStart = str.indexOf(start);
+  if (locStart==-1) return "";
+  locStart += start.length();
+  int locFinish = str.indexOf(finish, locStart);
+  if (locFinish==-1) return "";
+  return str.substring(locStart, locFinish);
+}
+
+void APMode::RunSetup(){
+  Serial.println();
+  Serial.println("Configuring access point...");
+  WiFi.softAP(ssidAP, passwordAP);
+  IPAddress myIP = WiFi.softAPIP();
+  Serial.print("AP IP address: ");
+  Serial.println(myIP);
+  server.begin();
+  Serial.println("Server started");
+}
+
+void APMode::APCommand(){
+  WiFiClient client=server.available();
+  if(client){
+    String request = client.readStringUntil('\r');
+      if(request.indexOf("test1") != -1){
+        Serial.println( midString( request, "=", " " ) );
+      }
+      if(request.indexOf("test2") != -1){
+        Serial.println( midString( request, "=", " " ) );
+      }
+    client.print(html);
+    request="";
+  }
+}

+ 17 - 0
FlipperZero-IFTTT_WiFi_Module/APMode.h

@@ -0,0 +1,17 @@
+#ifndef APMode_h
+#define APMode_h
+
+#include <Arduino.h>
+
+#include <ESP8266WiFi.h>
+
+class APMode
+{
+  private:
+    void APindex();
+  public:
+    APMode();
+    void RunSetup();
+    void APCommand();
+};
+#endif

+ 70 - 0
FlipperZero-IFTTT_WiFi_Module/FlipperZero-IFTTT_WiFi_Module.ino

@@ -0,0 +1,70 @@
+#include <Arduino.h>
+
+#include "APMode.h"
+#include "IFTTTMode.h"
+#include "Global.h"
+#include "eepromclass.h"
+
+APMode apmode_obj;
+IFTTTMode iftttmode_obj;
+EEPROMclass eeprom_obj;
+
+#define MODULE_CONTROL_COMMAND_SEND 's'
+#define MODULE_CONTROL_COMMAND_REBOOT 'r'
+#define MODULE_CONTROL_COMMAND_CONFIG_ON 'c'
+#define MODULE_CONTROL_COMMAND_CONFIG_OFF 'd'
+#define MODULE_CONTROL_COMMAND_RESET 'a'
+#define FLIPPERZERO_SERIAL_BAUD 115200
+
+int runMode = 0;
+
+void CheckForFlipperCommands()
+{
+  while(Serial.available() > 0)
+  {    
+    int incommingCommand = Serial.read();
+    if(incommingCommand == MODULE_CONTROL_COMMAND_SEND)
+    {
+      iftttmode_obj.IFTTTCommand();
+    }
+    if(incommingCommand == MODULE_CONTROL_COMMAND_REBOOT)
+    {
+      ESP.restart();
+    }
+    if(incommingCommand == MODULE_CONTROL_COMMAND_CONFIG_ON)
+    {
+      eeprom_obj.writeEEPROMint(0,1);
+      delay(10);
+      ESP.restart();
+    }
+    if(incommingCommand == MODULE_CONTROL_COMMAND_CONFIG_OFF)
+    {
+      eeprom_obj.writeEEPROMint(0,0);
+      delay(10);
+      ESP.restart();
+    }
+    if(incommingCommand == MODULE_CONTROL_COMMAND_RESET)
+    {
+      eeprom_obj.clearEEPROM();
+    }
+  }
+}
+
+void setup() {
+  Serial.begin(FLIPPERZERO_SERIAL_BAUD);
+  eeprom_obj.RunSetup();
+  runMode =  eeprom_obj.readEEPROMint(0);
+  delay(10);
+  if(runMode == 0){
+    iftttmode_obj.RunSetup();
+  }else if(runMode == 1){
+    apmode_obj.RunSetup();
+  }
+}
+
+void loop() {
+  CheckForFlipperCommands();
+  if(runMode == 1){
+    apmode_obj.APCommand();
+  }
+}

+ 3 - 0
FlipperZero-IFTTT_WiFi_Module/Global.cpp

@@ -0,0 +1,3 @@
+#include <Arduino.h>
+
+boolean Mode;

+ 8 - 0
FlipperZero-IFTTT_WiFi_Module/Global.h

@@ -0,0 +1,8 @@
+#ifndef GLOBAL_h
+#define GLOBAL_h
+
+#include <Arduino.h>
+
+extern boolean Mode;
+
+#endif

+ 59 - 0
FlipperZero-IFTTT_WiFi_Module/IFTTTMode.cpp

@@ -0,0 +1,59 @@
+#include "IFTTTMode.h"
+#include "Global.h"
+
+//insert trigger name in "triggername" field and your Webhoook Key ID in "keyID" field
+const char* resource = "https://maker.ifttt.com/trigger/triggername/json/with/key/keyID";
+const char* serverIFTTT = "maker.ifttt.com";
+//insert SSID and Password in field
+const char *ssidIFTTT = "SSID";
+const char *passwordIFTTT = "password";
+
+IFTTTMode::IFTTTMode()
+{
+}
+
+void IFTTTMode::RunSetup(){
+  Serial.print("\nConnecting to: "); 
+  Serial.print(ssidIFTTT);
+  WiFi.begin(ssidIFTTT, passwordIFTTT);  
+
+  while(WiFi.status() != WL_CONNECTED) {
+    delay(250);
+    Serial.print(".");
+  }
+
+  Serial.print("\nIP address: "); 
+  Serial.println(WiFi.localIP());
+}
+
+void IFTTTMode::IFTTTCommand(){
+  Serial.print("Connecting to "); 
+  Serial.print(serverIFTTT);
+  
+  WiFiClient client;
+  if (!client.connect(serverIFTTT, 80)) {
+    Serial.println("connection failed");
+  }
+  
+  Serial.print("Request resource: "); 
+  Serial.println(resource);
+  client.print(String("GET ") + resource + " HTTP/1.1\r\n" +
+                  "Host: " + serverIFTTT + "\r\n" + 
+                  "Connection: close\r\n\r\n");
+                  
+
+  unsigned long timeout = millis();
+  // Read all the lines of the reply from server and print them to Serial
+  while (client.available() == 0) {
+    if (millis() - timeout > 5000){
+      Serial.println(">>> Client Timeout !");
+      client.stop(); return;
+    }
+  } 
+
+  while(client.available()){
+    Serial.write(client.read());
+  }
+  Serial.println("\nclosing connection");
+  client.stop();
+}

+ 16 - 0
FlipperZero-IFTTT_WiFi_Module/IFTTTMode.h

@@ -0,0 +1,16 @@
+#ifndef IFTTTMode_h
+#define IFTTTMode_h
+
+#include <Arduino.h>
+
+#include <ESP8266WiFi.h>
+
+class IFTTTMode
+{
+  private:
+  public:
+    IFTTTMode();
+    void RunSetup();
+    void IFTTTCommand();
+};
+#endif

+ 53 - 0
FlipperZero-IFTTT_WiFi_Module/eepromclass.cpp

@@ -0,0 +1,53 @@
+#include "eepromclass.h"
+#include <Arduino.h>
+
+EEPROMclass::EEPROMclass()
+{
+}
+
+void EEPROMclass::RunSetup(){
+  EEPROM.begin(eeprom_size);
+}
+
+void EEPROMclass::writeEEPROMint(int index, int value){
+  EEPROM.write(index, value);
+  EEPROM.commit();
+}
+
+int EEPROMclass::readEEPROMint(int index){
+    int value = EEPROM.read(index);
+    return value;
+}
+
+int EEPROMclass::writeStringToEEPROM(int addrOffset, const String &strToWrite){
+  byte len = strToWrite.length();
+  EEPROM.write(addrOffset, len);
+  for (int i = 0; i < len; i++)
+  {
+    EEPROM.write(addrOffset + 1 + i, strToWrite[i]);
+  }
+  return addrOffset + 1 + len;
+}
+
+int EEPROMclass::readStringFromEEPROM(int addrOffset, String *strToRead){
+  int newStrLen = EEPROM.read(addrOffset);
+  char data[newStrLen + 1];
+  for (int i = 0; i < newStrLen; i++)
+  {
+    data[i] = EEPROM.read(addrOffset + 1 + i);
+  }
+  data[newStrLen] = '\0'; // the character may appear in a weird way, you should read: 'only one backslash and 0'
+  *strToRead = String(data);
+  return addrOffset + 1 + newStrLen;
+}
+
+void EEPROMclass::clearEEPROM(){
+  for (int i = 0; i < EEPROM.length(); i++) {
+    EEPROM.write(i, 0);
+    Serial.println(i);
+    if(i == EEPROM.length()-1){
+      EEPROM.commit();
+      ESP.reset();
+    }
+  }
+}

+ 21 - 0
FlipperZero-IFTTT_WiFi_Module/eepromclass.h

@@ -0,0 +1,21 @@
+#ifndef EEPROMclass_h
+#define EEPROMclass_h
+#include <Arduino.h>
+
+#include <EEPROM.h>
+
+class EEPROMclass
+{
+  private:
+    int eeprom_size = 1025;
+    bool isempty = false;
+  public:
+    EEPROMclass();
+    void RunSetup();
+    void clearEEPROM();
+    int readEEPROMint(int index);
+    void writeEEPROMint(int index, int value);
+    int writeStringToEEPROM(int addrOffset, const String &strToWrite);
+    int readStringFromEEPROM(int addrOffset, String *strToRead);
+};
+#endif