diff --git a/software/.gitignore b/software/.gitignore new file mode 100644 index 0000000..2de98ab --- /dev/null +++ b/software/.gitignore @@ -0,0 +1,6 @@ +.pio +.pioenvs +.piolibdeps +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json diff --git a/software/.vscode/extensions.json b/software/.vscode/extensions.json new file mode 100644 index 0000000..8281e64 --- /dev/null +++ b/software/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} \ No newline at end of file diff --git a/software/.vscode/ipch/38c473d870b159fa/main.ipch b/software/.vscode/ipch/38c473d870b159fa/main.ipch new file mode 100644 index 0000000..ba1b9c9 Binary files /dev/null and b/software/.vscode/ipch/38c473d870b159fa/main.ipch differ diff --git a/software/.vscode/ipch/38c473d870b159fa/mmap_address.bin b/software/.vscode/ipch/38c473d870b159fa/mmap_address.bin new file mode 100644 index 0000000..71307ab Binary files /dev/null and b/software/.vscode/ipch/38c473d870b159fa/mmap_address.bin differ diff --git a/software/.vscode/settings.json b/software/.vscode/settings.json new file mode 100644 index 0000000..016fb3d --- /dev/null +++ b/software/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "terminal.integrated.env.osx": { + "PATH": "/Users/monster/.platformio/penv/bin:/Users/monster/.platformio/penv:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin", + "PLATFORMIO_CALLER": "vscode" + } +} \ No newline at end of file diff --git a/software/lib/readme.txt b/software/lib/readme.txt new file mode 100644 index 0000000..4e20fa2 --- /dev/null +++ b/software/lib/readme.txt @@ -0,0 +1,41 @@ + +This directory is intended for the project specific (private) libraries. +PlatformIO will compile them to static libraries and link to executable file. + +The source code of each library should be placed in separate directory, like +"lib/private_lib/[here are source files]". + +For example, see how can be organized `Foo` and `Bar` libraries: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) http://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- readme.txt --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +Then in `src/main.c` you should use: + +#include +#include + +// rest H/C/CPP code + +PlatformIO will find your libraries automatically, configure preprocessor's +include paths and build them. + +More information about PlatformIO Library Dependency Finder +- http://docs.platformio.org/page/librarymanager/ldf.html diff --git a/software/platformio.ini b/software/platformio.ini new file mode 100644 index 0000000..5ccdd7b --- /dev/null +++ b/software/platformio.ini @@ -0,0 +1,24 @@ +; 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 +; http://docs.platformio.org/page/projectconf.html + +[env:d1_mini] +platform = espressif8266 +board = nodemcuv2 +framework = arduino +lib_deps = + # Using a library name + ArduinoJSON@5.10.0 + Homie@1.5.0, + Bounce2 + +monitor_speed = 115200 +#upload_speed = 921600 + +#build_flags = -Wl,-Tesp8266.flash.4m1m.ld \ No newline at end of file diff --git a/software/src/main.ino b/software/src/main.ino new file mode 100644 index 0000000..483b6cd --- /dev/null +++ b/software/src/main.ino @@ -0,0 +1,111 @@ +#include +#include + +#define NEST_Y1 D5 //# +#define NEST_Y2 D6 //# +#define NEST_G D7 //D3 +#define NEST_S D2 +#define NEST_W1 D1 //# +#define NEST_W2 D0 + + +int lastState_W1 = -1; +int lastState_W2 = -1; +int lastState_Y1 = -1; +int lastState_Y2 = -1; +int lastState_S = -1; +int lastState_G = -1; + +bool state_w1 = false; +bool state_w2 = false; +bool state_y1 = false; +bool state_y2 = false; +bool state_s = false; +bool state_g = false; + +HomieNode nestNode("nest", "node"); + +bool resetHandler(String value) { + //remove config =) + SPIFFS.remove("/homie/config.json"); + + Homie.setNodeProperty(nestNode, "reset", value, false); + return true; +} + +void setupHandler() { + pinMode(NEST_W1, INPUT_PULLUP); + pinMode(NEST_W2, INPUT_PULLUP); + pinMode(NEST_Y1, INPUT_PULLUP); + pinMode(NEST_Y2, INPUT_PULLUP); + pinMode(NEST_S, INPUT_PULLUP); + pinMode(NEST_G, INPUT_PULLUP); + + nestNode.subscribe("reset", resetHandler); +} + +void loopHandler() { +} + +void setup() { + SPIFFS.begin(); + Homie.disableResetTrigger(); + + Homie.setFirmware("nest-firmware", "1.0.0"); + Homie.setSetupFunction(setupHandler); + Homie.setLoopFunction(loopHandler); + Homie.registerNode(nestNode); + Homie.setup(); +} + +void read_pin(int pin, bool &pin_state) { + int state = digitalRead(pin); + // Serial.print(pin); + // Serial.print("="); + // Serial.println(state); + if (state == 0) { + pin_state = true; + } +} + +void send_state(int state, int& lastState, const String name) { + if (state != lastState) { + Serial.print(name); + Serial.print(" state changed from "); + Serial.print(lastState); + Serial.print(" to "); + Serial.println(state); + + Homie.setNodeProperty(nestNode, name, String(state), true); + lastState = state; + } +} + +void measure_pins() { + state_w1 = false; + state_w2 = false; + state_y1 = false; + state_y2 = false; + state_s = false; + state_g = false; + for(int i = 0; i<250; i++) { + read_pin(NEST_W1, state_w1); + read_pin(NEST_W2, state_w2); + read_pin(NEST_Y1, state_y1); + read_pin(NEST_Y2, state_y2); + read_pin(NEST_S, state_s); + read_pin(NEST_G, state_g); + Homie.loop(); + delay(1); + } + send_state(state_w1, lastState_W1, "w1"); + send_state(state_w2, lastState_W2, "w2"); + send_state(state_y1, lastState_Y1, "y1"); + send_state(state_y2, lastState_Y2, "y2"); + send_state(state_s, lastState_S, "s"); + send_state(state_g, lastState_G, "g"); +} + +void loop() { + measure_pins(); +} \ No newline at end of file