diff --git a/FabLock/.gitignore b/FabLock/.gitignore new file mode 100644 index 0000000..0e2557a --- /dev/null +++ b/FabLock/.gitignore @@ -0,0 +1,7 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch + +src/config.h diff --git a/FabLock/.vscode/extensions.json b/FabLock/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/FabLock/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/FabLock/.vscode/settings.json b/FabLock/.vscode/settings.json new file mode 100644 index 0000000..2f73344 --- /dev/null +++ b/FabLock/.vscode/settings.json @@ -0,0 +1,12 @@ +{ + "files.associations": { + "optional": "cpp", + "ostream": "cpp", + "system_error": "cpp", + "array": "cpp", + "functional": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp" + } +} \ No newline at end of file diff --git a/FabLock/include/README b/FabLock/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/FabLock/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/FabLock/lib/README b/FabLock/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/FabLock/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/FabLock/platformio.ini b/FabLock/platformio.ini new file mode 100644 index 0000000..46c7255 --- /dev/null +++ b/FabLock/platformio.ini @@ -0,0 +1,18 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp12e] +platform = espressif8266 +board = esp12e +framework = arduino +lib_deps = + knolleary/PubSubClient@^2.8 + adafruit/Adafruit MCP23017 Arduino Library@^2.1.0 +monitor_speed = 115200 diff --git a/FabLock/src/config_example.h b/FabLock/src/config_example.h new file mode 100644 index 0000000..3d215c1 --- /dev/null +++ b/FabLock/src/config_example.h @@ -0,0 +1,7 @@ +#define WLAN_SSID "" +#define WLAN_PASS "" +#define MQTT_BROKER "" +#define FABLOCKID 1 +#define MPC_RELAIS_ADDR 0x02 +#define HAS_FEEDBACK 1 +#define MPC_FEEDBACK_ADDR 0x03 \ No newline at end of file diff --git a/FabLock/src/main.cpp b/FabLock/src/main.cpp new file mode 100644 index 0000000..7cad8e1 --- /dev/null +++ b/FabLock/src/main.cpp @@ -0,0 +1,184 @@ +#include +#include +#include + +#include "config.h" +#define MCP_INPUTS 16 // MCP23017 +#define TRIGGER_TIME 1500 + +WiFiClient espClient; +PubSubClient client(espClient); +Adafruit_MCP23X17 mcp_relais; +Adafruit_MCP23X17 mcp_feedback; + +unsigned long lastMsg = 0; +#define MSG_BUFFER_SIZE (50) +char msg[MSG_BUFFER_SIZE]; + +#define TOPIC_BUFFER_SIZE (50) +char topic_recv[TOPIC_BUFFER_SIZE]; +char topic_send[TOPIC_BUFFER_SIZE]; + +void setup_wifi() +{ + delay(10); + + Serial.println(); + Serial.print("Connecting to "); + Serial.println(WLAN_SSID); + + WiFi.mode(WIFI_STA); + WiFi.begin(WLAN_SSID, WLAN_PASS); + + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.print("."); + } + + randomSeed(micros()); + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + +void callback(char* topic, byte* payload, unsigned int length) +{ + Serial.println("Receive message"); + + int topic_len = (int)strlen(topic); + int trigger_len = (int)strlen("/trigger"); + int set_state_len = (int)strlen("/set_state"); + if(strcmp(topic + topic_len - trigger_len, "/trigger")) + { + char str_id[4] = {0}; + strncpy(str_id, topic + topic_len - trigger_len - 3, 3); + int id = atoi(str_id); + + if ((char)payload[0] == '1') + { + mcp_relais.digitalWrite(id, HIGH); + delay(TRIGGER_TIME); + mcp_relais.digitalWrite(id, LOW); + } + else + { + mcp_relais.digitalWrite(id, LOW); + delay(TRIGGER_TIME); + mcp_relais.digitalWrite(id, HIGH); + } + } + + if(strcmp(topic + topic_len - set_state_len, "/set_state")) + { + char str_id[4] = {0}; + strncpy(str_id, topic + topic_len - trigger_len - 3, 3); + int id = atoi(str_id); + + if ((char)payload[0] == '1') + { + mcp_relais.digitalWrite(id, HIGH); + } + else + { + mcp_relais.digitalWrite(id, LOW); + } + } +} + +void reconnect() +{ + while (!client.connected()) + { + Serial.print("Attempting MQTT connection..."); + String clientId = "FABLOCK-"; + clientId += String(random(0xffff), HEX); + if (client.connect(clientId.c_str())) + { + snprintf (topic_recv, TOPIC_BUFFER_SIZE, "fablock/%03d/+/trigger", FABLOCKID); + client.subscribe(topic_recv); + snprintf (topic_recv, TOPIC_BUFFER_SIZE, "fablock/%03d/+/set_state", FABLOCKID); + client.subscribe(topic_recv); + } + else + { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + delay(5000); + } + } +} + +void setup() +{ + int i = 0; + + pinMode(BUILTIN_LED, OUTPUT); + Serial.begin(115200); + setup_wifi(); + + client.setServer(MQTT_BROKER, 1883); + client.setCallback(callback); + + if (!mcp_relais.begin_I2C(MPC_RELAIS_ADDR)) + { + Serial.println("Error."); + while (1); + } + for(i = 0; i++; i 30000) + { + lastMsg = now; + int value; + + Serial.print("Publish State and Feedback"); + for(int i = 0; i++; i