From d2c128035dc14487d57d5714c4dbb0e6c5ead272 Mon Sep 17 00:00:00 2001 From: Simon Milvert Date: Thu, 26 Nov 2015 11:12:30 +0100 Subject: [PATCH] Added platformio to build_script Have only tested that the build_script works to build. Have not verify the build_script on hardware! --- .gitmodules | 9 + README.md | 10 +- build_project.sh | 163 ++++++++++++++++++ libraries/arduino_github | 1 + libraries/milvert_mysensors | 1 + libraries/mysensors_github | 1 + mysensors_controller/platformio.ini | 7 + .../src/mysensors_controller.ino | 97 +++++++++++ temp_hum_sensor/platformio.ini | 6 + .../temp_hum_sensor-cache.bck | 0 .../temp_hum_sensor-cache.dcm | 0 .../temp_hum_sensor.kicad_pcb | 0 .../{ => schematics}/temp_hum_sensor.pro | 0 .../{ => schematics}/temp_hum_sensor.sch | 0 temp_hum_sensor/src/temp_hum_sensor.ino | 100 +++++++++++ 15 files changed, 394 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 100755 build_project.sh create mode 160000 libraries/arduino_github create mode 160000 libraries/milvert_mysensors create mode 160000 libraries/mysensors_github create mode 100644 mysensors_controller/platformio.ini create mode 100644 mysensors_controller/src/mysensors_controller.ino create mode 100644 temp_hum_sensor/platformio.ini rename temp_hum_sensor/{ => schematics}/temp_hum_sensor-cache.bck (100%) rename temp_hum_sensor/{ => schematics}/temp_hum_sensor-cache.dcm (100%) rename temp_hum_sensor/{ => schematics}/temp_hum_sensor.kicad_pcb (100%) rename temp_hum_sensor/{ => schematics}/temp_hum_sensor.pro (100%) rename temp_hum_sensor/{ => schematics}/temp_hum_sensor.sch (100%) create mode 100644 temp_hum_sensor/src/temp_hum_sensor.ino diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..1c8b61d --- /dev/null +++ b/.gitmodules @@ -0,0 +1,9 @@ +[submodule "libraries/arduino_github"] + path = libraries/arduino_github + url = git@github.com:RobTillaart/Arduino.git +[submodule "libraries/mysensors_github"] + path = libraries/mysensors_github + url = git@github.com:mysensors/Arduino.git +[submodule "libraries/milvert_mysensors"] + path = libraries/milvert_mysensors + url = git@github.com:milvert/Arduino.git diff --git a/README.md b/README.md index 9c53079..ecd6091 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ -# mysensors_nodes +# My personal [MySensors](http://mysensors.org) nodes + +To upload a sketch to a node run build_project.sh. + +## ToDo: +- Each node should have a KiCad-schematic + - Add external dependencies + - Fix includes in temp_hum_sensor +- Better comments \ No newline at end of file diff --git a/build_project.sh b/build_project.sh new file mode 100755 index 0000000..bd02f44 --- /dev/null +++ b/build_project.sh @@ -0,0 +1,163 @@ +#! /bin/bash + +PARENT_DIR="$(pwd)" + +#Add a path to all libraries +MYSENSORS_LIB="libraries/mysensors_github/libraries/MySensors" +DHT_LIB="libraries/mysensors_github/libraries/DHT" +#DHT_LIB="libraries/arduino_github/libraries/DHTlib" + +function CHECK_DEPENDENCIES { + + if ! which platformio >/dev/null; then + echo "Install platfromio!" + echo "http://platformio.org/#!/get-started" + exit + fi + + if [ -n "$DISPLAY" ]; then + if which arduino > /dev/null; then + ARDUINO=$(which arduino) + else + echo "Install arduino" + exit + fi + else + if which arduino-headless > /dev/null; then + ARDUINO=$(which arduino-headless) + else + echo "Install arduino-headless" + echo "Create arduino-headless and copy:" + echo " + #!/bin/bash + Xvfb :1 -nolisten tcp -screen :1 1280x800x24 & + xvfb=\"$!\" + DISPLAY=:1 arduino \$@ + kill -9 \$xvfb + " + echo "and make arduino-headless runnable (/usr/local/bin/arduino-headless)" + exit + fi + fi +} + +### +# List all nodes/gateways that are possible to build +### +function LIST_DIR { + + local i=0 + dirs=( $(find . -maxdepth 1 -type d -printf '%P\n') ) + for dir in "${dirs[@]}"; do + if [ "${dir}" == ".git" ]; then + unset dirs[$i] + elif [ "${dir}" == "libraries" ]; then + unset dirs[$i] + fi + i=$i+1 + done + dirs=("${dirs[@]}" "Quit") + echo "Which project do you want to build? " + select dir in "${dirs[@]}"; do + if [ "${dir}" == "Quit" ]; then + echo "Quit" + break + elif [[ $REPLY -gt 0 && $REPLY < ${#dirs[@]} ]] ; then + echo "${dir} selected" + BUILD_WITH_PLATFORMIO "${dir}" + #BUILD_WITH_ARDUINO "${dir}" + LIST_DIR + else + echo "Select between 0 and ${#dirs[@]}" + fi + done +} + +### +# Use the the arduino cli for building. +# TODO: Add interface for build with differnet boards and 8Mhz/16Mhz +### +BUILD_WITH_ARDUINO() { + LINK_HEADER "${dir}" "arduino" + ${ARDUINO} --upload -v --board arduino:avr:pro --pref build.f_cpu=8000000 --pref build.mcu=atmega328p --pref compiler.warning_level=all --pref sketchbook.path="${PWD}/${dir}" "${PWD}/${dir}/${dir}.ino" + +} + +### +# TODO: Platformio dosen't work with MySensors lib. +### +BUILD_WITH_PLATFORMIO() { + LINK_HEADER "${dir}" "platformio" + (cd "${1}" && platformio run --target clean) + (cd "${1}" && platformio run) + + if [ "$?" -ne "0" ]; then + echo "Compiliing failed " + echo "Exiting" + exit + fi + + read -r -p "Want to upload? [Y/n]" response + response=${response,,} # tolower + if [ "${response}" = "" ]; then + response='yes' + fi + + if [[ $response =~ ^(yes|y| ) ]]; then + echo "Upload" + (cd "${1}" && platformio run --target upload) + else + echo "Not upload" + fi +} + +### +# Checks which includes the sketch wants and creates a symlink to +# the lib +# TODO: Platformio dosen't work with MySensors lib. +### +LINK_HEADER() { + cd "${1}" + echo "Link_header " + + # Scan the ino file for #include + if [ "${2}" = "arduino" ]; then + local header_files=($(grep "include" ./*.ino | sed 's/\(#include <\)\(.*\)\(.h>\)/\2/')) + lib_dir=libraries + else + echo "platformio" + local header_files=($(grep "include" src/*.ino | sed 's/\(#include <\)\(.*\)\(.h>\)/\2/')) + lib_dir=lib + fi + + if [ ! -d "$lib_dir" ]; then + mkdir "$lib_dir" + else + rm -r ${lib_dir}/* + fi + + for file in "${header_files[@]}"; do + case $file in + "MySensor") + # Fix path to mysensors lib in platformio.ini + if [ "${2}" = "platformio" ]; then + sed -i "s:\(build_flags\s*=\)\(.*\):\1 \-I$PARENT_DIR/$MYSENSORS_LIB:" platformio.ini + cat platformio.ini + elif [[ ! -d "$lib_dir/${file}" ]]; then + ln -s "${PARENT_DIR}/${MYSENSORS_LIB}" "$lib_dir/${file}" + fi + ;; + "DHT") + if [[ ! -d "$lib_dir/${file}" ]]; then + ln -s "${PARENT_DIR}/${DHT_LIB}" "$lib_dir/${file}" + fi + ;; + *) + echo "Nothing ${file}" + esac + done + cd "${PARENT_DIR}" +} + +CHECK_DEPENDENCIES +LIST_DIR diff --git a/libraries/arduino_github b/libraries/arduino_github new file mode 160000 index 0000000..34ff8cf --- /dev/null +++ b/libraries/arduino_github @@ -0,0 +1 @@ +Subproject commit 34ff8cfd021fd214d3850a1c37f739c84e3c7815 diff --git a/libraries/milvert_mysensors b/libraries/milvert_mysensors new file mode 160000 index 0000000..59f8876 --- /dev/null +++ b/libraries/milvert_mysensors @@ -0,0 +1 @@ +Subproject commit 59f887687058e5abdfbf80354cb5bf246e45cca7 diff --git a/libraries/mysensors_github b/libraries/mysensors_github new file mode 160000 index 0000000..367dee1 --- /dev/null +++ b/libraries/mysensors_github @@ -0,0 +1 @@ +Subproject commit 367dee18b033f02199576b65b0eb5a28c20e6abd diff --git a/mysensors_controller/platformio.ini b/mysensors_controller/platformio.ini new file mode 100644 index 0000000..8a0d7a7 --- /dev/null +++ b/mysensors_controller/platformio.ini @@ -0,0 +1,7 @@ +[env:pro8MHzatmega328] +platform = atmelavr +framework = arduino +board = pro8MHzatmega328 +build_flags = -I/home/simon/repo/milvert_sensor/libraries/mysensors_github/libraries/MySensors +lib_ignore = MySensors + diff --git a/mysensors_controller/src/mysensors_controller.ino b/mysensors_controller/src/mysensors_controller.ino new file mode 100644 index 0000000..9eee4e8 --- /dev/null +++ b/mysensors_controller/src/mysensors_controller.ino @@ -0,0 +1,97 @@ + /** + * The MySensors Arduino library handles the wireless radio link and protocol + * between your home built sensors/actuators and HA controller of choice. + * The sensors forms a self healing radio network with optional repeaters. Each + * repeater and gateway builds a routing tables in EEPROM which keeps track of the + * network topology allowing messages to be routed to nodes. + * + * Created by Henrik Ekblad + * Copyright (C) 2013-2015 Sensnology AB + * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors + * + * Documentation: http://www.mysensors.org + * Support Forum: http://forum.mysensors.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + ******************************* + * + * DESCRIPTION + * The ArduinoGateway prints data received from sensors on the serial link. + * The gateway accepts input on seral which will be sent out on radio network. + * + * The GW code is designed for Arduino Nano 328p / 16MHz + * + * Wire connections (OPTIONAL): + * - Inclusion button should be connected between digital pin 3 and GND + * - RX/TX/ERR leds need to be connected between +5V (anode) and digital pin 6/5/4 with resistor 270-330R in a series + * + * LEDs (OPTIONAL): + * - To use the feature, uncomment MY_LEDS_BLINKING_FEATURE in MyConfig.h + * - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved + * - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly + * - ERR (red) - fast blink on error during transmission error or recieve crc error + * + */ + +// Enable debug prints to serial monitor +#define MY_DEBUG + + +// Enable and select radio type attached +#define MY_RADIO_NRF24 +//#define MY_RADIO_RFM69 + +// Set LOW transmit power level as default, if you have an amplified NRF-module and +// power your radio separately with a good regulator you can turn up PA level. +#define MY_RF24_PA_LEVEL RF24_PA_LOW + +// Enable serial gateway +#define MY_GATEWAY_SERIAL + +// Flash leds on rx/tx/err +#define MY_LEDS_BLINKING_FEATURE +// Set blinking period +#define MY_DEFAULT_LED_BLINK_PERIOD 300 + +// Inverses the behavior of leds +//#define MY_WITH_LEDS_BLINKING_INVERSE + +// Enable inclusion mode +#define MY_INCLUSION_MODE_FEATURE +// Enable Inclusion mode button on gateway +#define MY_INCLUSION_BUTTON_FEATURE + +// Inverses behavior of inclusion button (if using external pullup) +//#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP + +// Set inclusion mode duration (in seconds) +#define MY_INCLUSION_MODE_DURATION 60 +// Digital pin used for inclusion mode button +#define MY_INCLUSION_MODE_BUTTON_PIN 3 + +#define MY_DEFAULT_ERR_LED_PIN 4 // Error led pin +#define MY_DEFAULT_RX_LED_PIN 6 // Receive led pin +#define MY_DEFAULT_TX_LED_PIN 5 // the PCB, on board LED + +#include +#include + +void setup() { + // Setup locally attached sensors +} + +void presentation() { + // Present locally attached sensors +} + +void loop() { + // Send locally attached sensor data here +} + + + + + diff --git a/temp_hum_sensor/platformio.ini b/temp_hum_sensor/platformio.ini new file mode 100644 index 0000000..c65cb7c --- /dev/null +++ b/temp_hum_sensor/platformio.ini @@ -0,0 +1,6 @@ +[env:pro8MHzatmega328] +platform = atmelavr +framework = arduino +board = pro8MHzatmega328 +build_flags = -I/home/simon/repo/milvert_sensor/libraries/mysensors_github/libraries/MySensors +lib_ignore = MySensors diff --git a/temp_hum_sensor/temp_hum_sensor-cache.bck b/temp_hum_sensor/schematics/temp_hum_sensor-cache.bck similarity index 100% rename from temp_hum_sensor/temp_hum_sensor-cache.bck rename to temp_hum_sensor/schematics/temp_hum_sensor-cache.bck diff --git a/temp_hum_sensor/temp_hum_sensor-cache.dcm b/temp_hum_sensor/schematics/temp_hum_sensor-cache.dcm similarity index 100% rename from temp_hum_sensor/temp_hum_sensor-cache.dcm rename to temp_hum_sensor/schematics/temp_hum_sensor-cache.dcm diff --git a/temp_hum_sensor/temp_hum_sensor.kicad_pcb b/temp_hum_sensor/schematics/temp_hum_sensor.kicad_pcb similarity index 100% rename from temp_hum_sensor/temp_hum_sensor.kicad_pcb rename to temp_hum_sensor/schematics/temp_hum_sensor.kicad_pcb diff --git a/temp_hum_sensor/temp_hum_sensor.pro b/temp_hum_sensor/schematics/temp_hum_sensor.pro similarity index 100% rename from temp_hum_sensor/temp_hum_sensor.pro rename to temp_hum_sensor/schematics/temp_hum_sensor.pro diff --git a/temp_hum_sensor/temp_hum_sensor.sch b/temp_hum_sensor/schematics/temp_hum_sensor.sch similarity index 100% rename from temp_hum_sensor/temp_hum_sensor.sch rename to temp_hum_sensor/schematics/temp_hum_sensor.sch diff --git a/temp_hum_sensor/src/temp_hum_sensor.ino b/temp_hum_sensor/src/temp_hum_sensor.ino new file mode 100644 index 0000000..a90bf59 --- /dev/null +++ b/temp_hum_sensor/src/temp_hum_sensor.ino @@ -0,0 +1,100 @@ +/** + * The MySensors Arduino library handles the wireless radio link and protocol + * between your home built sensors/actuators and HA controller of choice. + * The sensors forms a self healing radio network with optional repeaters. Each + * repeater and gateway builds a routing tables in EEPROM which keeps track of the + * network topology allowing messages to be routed to nodes. + * + * Created by Henrik Ekblad + * Copyright (C) 2013-2015 Sensnology AB + * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors + * + * Documentation: http://www.mysensors.org + * Support Forum: http://forum.mysensors.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + ******************************* + * + * REVISION HISTORY + * Version 1.0 - Henrik EKblad + * + * DESCRIPTION + * This sketch provides an example how to implement a humidity/temperature + * sensor using DHT11/DHT-22 + * http://www.mysensors.org/build/humidity + */ + +// Enable debug prints +#define MY_DEBUG + +// Enable and select radio type attached +#define MY_RADIO_NRF24 +//#define MY_RADIO_RFM69 + +#include +#include +#include + +#define CHILD_ID_HUM 0 +#define CHILD_ID_TEMP 1 +#define HUMIDITY_SENSOR_DIGITAL_PIN 3 +unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) + +DHT dht; +float lastTemp; +float lastHum; +boolean metric = true; +MyMessage msgHum(CHILD_ID_HUM, V_HUM); +MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); + + +void setup() +{ + dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); + + metric = getConfig().isMetric; +} + +void presentation() +{ + // Send the Sketch Version Information to the Gateway + sendSketchInfo("Humidity", "1.0"); + + // Register all sensors to gw (they will be created as child devices) + present(CHILD_ID_HUM, S_HUM); + present(CHILD_ID_TEMP, S_TEMP); +} + + +void loop() +{ + delay(dht.getMinimumSamplingPeriod()); + + float temperature = dht.getTemperature(); + if (isnan(temperature)) { + Serial.println("Failed reading temperature from DHT"); + } else if (temperature != lastTemp) { + lastTemp = temperature; + if (!metric) { + temperature = dht.toFahrenheit(temperature); + } + send(msgTemp.set(temperature, 1)); + Serial.print("T: "); + Serial.println(temperature); + } + + float humidity = dht.getHumidity(); + if (isnan(humidity)) { + Serial.println("Failed reading humidity from DHT"); + } else if (humidity != lastHum) { + lastHum = humidity; + send(msgHum.set(humidity, 1)); + Serial.print("H: "); + Serial.println(humidity); + } + + sleep(SLEEP_TIME); //sleep a bit +}