Skip to content

Commit

Permalink
add automation support; add IFTTT support; add WiFi signal strength o…
Browse files Browse the repository at this point in the history
…n homepage
  • Loading branch information
rayshobby committed Jan 3, 2017
1 parent 85eaa5a commit baaed55
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 13 deletions.
185 changes: 185 additions & 0 deletions OpenGarage/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#====================================================================================
# makeESPArduino
#
# A makefile for ESP8286 Arduino projects.
# Edit the contents of this file to suit your project
# or just include it and override the applicable macros.
#
# License: GPL 2.1
# General and full license information is available at:
# https://github.com/plerup/makeEspArduino
#
# Copyright (c) 2016 Peter Lerup. All rights reserved.
#
#====================================================================================

#====================================================================================
# User editable area
#====================================================================================

#=== Project specific definitions: sketch and list of needed libraries
SKETCH ?= ./examples/mainArduino/mainArduino.ino
LIBS ?= $(ESP_LIBS)/Wire \
$(ESP_LIBS)/ESP8266WiFi \
$(ESP_LIBS)/ESP8266mDNS \
$(ESP_LIBS)/ESP8266WebServer \
$(ESP_LIBS)/ESP8266HTTPClient \
$(ESP_LIBS)/Blynk \
.

# Esp8266 Arduino git location
ESP_ROOT ?= $(HOME)/esp8266/
# Output directory
BUILD_ROOT ?= /tmp/$(MAIN_NAME)

# Board definitions
FLASH_SIZE ?= 4M
FLASH_MODE ?= dio
FLASH_SPEED ?= 40
FLASH_LAYOUT ?= eagle.flash.4m.ld

# Upload parameters
UPLOAD_SPEED ?= 230400
UPLOAD_PORT ?= /dev/tty.wchusbserial1420
UPLOAD_VERB ?= -v
UPLOAD_RESET ?= ck

#====================================================================================
# The area below should normally not need to be edited
#====================================================================================

MKESPARD_VERSION = 1.0.0

START_TIME := $(shell perl -e "print time();")
# Main output definitions
MAIN_NAME = $(basename $(notdir $(SKETCH)))
MAIN_EXE = $(BUILD_ROOT)/$(MAIN_NAME).bin
MAIN_ELF = $(OBJ_DIR)/$(MAIN_NAME).elf
SRC_GIT_VERSION = $(call git_description,$(dir $(SKETCH)))

# esp8266 arduino directories
ESP_GIT_VERSION = $(call git_description,$(ESP_ROOT))
ESP_LIBS = $(ESP_ROOT)/libraries
TOOLS_ROOT = $(ESP_ROOT)/tools
TOOLS_BIN = $(TOOLS_ROOT)/xtensa-lx106-elf/bin
SDK_ROOT = $(ESP_ROOT)/tools/sdk

# Directory for intermedite build files
OBJ_DIR = $(BUILD_ROOT)/obj
OBJ_EXT = .o
DEP_EXT = .d

# Compiler definitions
CC = $(TOOLS_BIN)/xtensa-lx106-elf-gcc
CPP = $(TOOLS_BIN)/xtensa-lx106-elf-g++
LD = $(CC)
AR = $(TOOLS_BIN)/xtensa-lx106-elf-ar
ESP_TOOL = $(TOOLS_ROOT)/esptool/esptool
OTA_TOOL = $(TOOLS_ROOT)/espota.py

INCLUDE_DIRS += $(SDK_ROOT)/include $(SDK_ROOT)/lwip/include $(CORE_DIR) $(ESP_ROOT)/variants/generic $(OBJ_DIR)
C_DEFINES = -D__ets__ -DICACHE_FLASH -U__STRICT_ANSI__ -DF_CPU=80000000L -DARDUINO=10605 -DARDUINO_ESP8266_ESP01 -DARDUINO_ARCH_ESP8266 -DESP8266
C_INCLUDES = $(foreach dir,$(INCLUDE_DIRS) $(USER_DIRS),-I$(dir))
C_FLAGS ?= -c -Os -g -Wpointer-arith -Wno-implicit-function-declaration -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals -falign-functions=4 -MMD -std=gnu99 -ffunction-sections -fdata-sections
CPP_FLAGS ?= -c -Os -g -mlongcalls -mtext-section-literals -fno-exceptions -fno-rtti -falign-functions=4 -std=c++11 -MMD -ffunction-sections -fdata-sections
S_FLAGS ?= -c -g -x assembler-with-cpp -MMD
LD_FLAGS ?= -g -w -Os -nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static -L$(SDK_ROOT)/lib -L$(SDK_ROOT)/ld -T$(FLASH_LAYOUT) -Wl,--gc-sections -Wl,-wrap,system_restart_local -Wl,-wrap,register_chipv6_phy
LD_STD_LIBS ?= -lm -lgcc -lhal -lphy -lnet80211 -llwip -lwpa -lmain -lpp -lsmartconfig -lwps -lcrypto -laxtls

# Core source files
CORE_DIR = $(ESP_ROOT)/cores/esp8266
CORE_SRC = $(shell find $(CORE_DIR) -name "*.S" -o -name "*.c" -o -name "*.cpp")
CORE_OBJ = $(patsubst %,$(OBJ_DIR)/%$(OBJ_EXT),$(notdir $(CORE_SRC)))
CORE_LIB = $(OBJ_DIR)/core.ar

# User defined compilation units
USER_SRC = $(SKETCH) $(shell find $(LIBS) -name "*.S" -o -name "*.c" -o -name "*.cpp")
# Object file suffix seems to be significant for the linker...
USER_OBJ = $(subst .ino,.cpp,$(patsubst %,$(OBJ_DIR)/%$(OBJ_EXT),$(notdir $(USER_SRC))))
USER_DIRS = $(sort $(dir $(USER_SRC)))

VPATH += $(shell find $(CORE_DIR) -type d) $(USER_DIRS)

# Automatically generated build information data
# Makes the build date and git descriptions at the actual build
# event available as string constants in the program
BUILD_INFO_H = $(OBJ_DIR)/buildinfo.h
BUILD_INFO_CPP = $(OBJ_DIR)/buildinfo.cpp
BUILD_INFO_OBJ = $(BUILD_INFO_CPP)$(OBJ_EXT)

$(BUILD_INFO_H): | $(OBJ_DIR)
echo "typedef struct { const char *date, *time, *src_version, *env_version;} _tBuildInfo; extern _tBuildInfo _BuildInfo;" >$@

# Utility functions
git_description = $(shell git -C $(1) describe --tags --always --dirty 2>/dev/null)
time_string = $(shell perl -e 'use POSIX qw(strftime); print strftime($(1), localtime());')
MEM_USAGE = \
'while (<>) { \
$$r += $$1 if /^\.(?:data|rodata|bss)\s+(\d+)/;\
$$f += $$1 if /^\.(?:irom0\.text|text|data|rodata)\s+(\d+)/;\
}\
print "\nMemory usage\n";\
print sprintf(" %-6s %6d bytes\n" x 2 ."\n", "Ram:", $$r, "Flash:", $$f);'

# Build rules
$(OBJ_DIR)/%.cpp$(OBJ_EXT): %.cpp $(BUILD_INFO_H)
echo $(<F)
$(CPP) $(C_DEFINES) $(C_INCLUDES) $(CPP_FLAGS) $< -o $@

$(OBJ_DIR)/%.cpp$(OBJ_EXT): %.ino $(BUILD_INFO_H)
echo $(<F)
$(CPP) -x c++ -include $(CORE_DIR)/Arduino.h $(C_DEFINES) $(C_INCLUDES) $(CPP_FLAGS) $< -o $@

$(OBJ_DIR)/%.c$(OBJ_EXT): %.c
echo $(<F)
$(CC) $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) $< -o $@

$(OBJ_DIR)/%.S$(OBJ_EXT): %.S
echo $(<F)
$(CC) $(C_DEFINES) $(C_INCLUDES) $(S_FLAGS) $< -o $@

$(CORE_LIB): $(CORE_OBJ)
echo Creating core archive
rm -f $@
$(AR) cru $@ $^

BUILD_DATE = $(call time_string,"%Y-%m-%d")
BUILD_TIME = $(call time_string,"%H:%M:%S")

$(MAIN_EXE): $(CORE_LIB) $(USER_OBJ)
echo Linking $(MAIN_EXE)
echo " Versions: $(SRC_GIT_VERSION), $(ESP_GIT_VERSION)"
echo '#include <buildinfo.h>' >$(BUILD_INFO_CPP)
echo '_tBuildInfo _BuildInfo = {"$(BUILD_DATE)","$(BUILD_TIME)","$(SRC_GIT_VERSION)","$(ESP_GIT_VERSION)"};' >>$(BUILD_INFO_CPP)
$(CPP) $(C_DEFINES) $(C_INCLUDES) $(CPP_FLAGS) $(BUILD_INFO_CPP) -o $(BUILD_INFO_OBJ)
$(LD) $(LD_FLAGS) -Wl,--start-group $^ $(BUILD_INFO_OBJ) $(LD_STD_LIBS) -Wl,--end-group -L$(OBJ_DIR) -o $(MAIN_ELF)
$(ESP_TOOL) -eo $(ESP_ROOT)/bootloaders/eboot/eboot.elf -bo $@ -bm $(FLASH_MODE) -bf $(FLASH_SPEED) -bz $(FLASH_SIZE) -bs .text -bp 4096 -ec -eo $(MAIN_ELF) -bs .irom0.text -bs .text -bs .data -bs .rodata -bc -ec
$(TOOLS_BIN)/xtensa-lx106-elf-size -A $(MAIN_ELF) | perl -e $(MEM_USAGE)
perl -e 'print "Build complete. Elapsed time: ", time()-$(START_TIME), " seconds\n\n"'

upload: all
$(ESP_TOOL) $(UPLOAD_VERB) -cd $(UPLOAD_RESET) -cb $(UPLOAD_SPEED) -cp $(UPLOAD_PORT) -ca 0x00000 -cf $(MAIN_EXE)

ota: all
$(OTA_TOOL) -i $(ESP_ADDR) -p $(ESP_PORT) -a $(ESP_PWD) -f $(MAIN_EXE)

clean:
echo Removing all intermediate build files...
rm -f $(OBJ_DIR)/*

$(OBJ_DIR):
mkdir -p $(OBJ_DIR)

.PHONY: all
all: $(OBJ_DIR) $(BUILD_INFO_H) $(MAIN_EXE)


# Include all available dependencies
-include $(wildcard $(OBJ_DIR)/*$(DEP_EXT))

.DEFAULT_GOAL = all

ifndef VERBOSE
# Set silent mode as default
MAKEFLAGS += --silent
endif
10 changes: 8 additions & 2 deletions OpenGarage/OpenGarage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ OptionStruct OpenGarage::options[] = {
{"htp", 80, 65535, ""},
{"cdt", 1000, 5000, ""},
{"mod", OG_MOD_AP, 255, ""},
{"ati", 30, 720, ""},
{"ato", OG_AUTO_NONE,255, ""},
{"ssid", 0, 0, ""}, // string options have 0 max value
{"pass", 0, 0, ""},
{"auth", 0, 0, ""},
{"dkey", 0, 0, DEFAULT_DKEY},
{"name", 0, 0, DEFAULT_NAME}
{"name", 0, 0, DEFAULT_NAME},
{"iftt", 0, 0, ""}
};

void OpenGarage::begin() {
Expand Down Expand Up @@ -171,12 +174,15 @@ ulong OpenGarage::read_distance_once() {
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// also clamp the result to maximum value 32767
// wait till echo pin's rising edge
while(digitalRead(PIN_ECHO)==LOW);
unsigned long start_time = micros();
// wait till echo pin's falling edge
while(digitalRead(PIN_ECHO)==HIGH);
return micros() - start_time;
ulong lapse = micros() - start_time;
if (lapse>32767L) lapse = 32767L;
return lapse;
}

uint OpenGarage::read_distance() {
Expand Down
9 changes: 8 additions & 1 deletion OpenGarage/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#define _DEFINES_H

/** Firmware version, hardware version, and maximal values */
#define OG_FWV 104 // Firmware version: 104 means 1.0.4
#define OG_FWV 105 // Firmware version: 105 means 1.0.5

/** GPIO pins */
#define PIN_RELAY 15
Expand Down Expand Up @@ -58,6 +58,10 @@
#define OG_MOD_AP 0xA9
#define OG_MOD_STA 0x2A

#define OG_AUTO_NONE 0x00
#define OG_AUTO_NOTIFY 0x01
#define OG_AUTO_CLOSE 0x02

#define OG_STATE_INITIAL 0
#define OG_STATE_CONNECTING 1
#define OG_STATE_CONNECTED 2
Expand Down Expand Up @@ -92,11 +96,14 @@ typedef enum {
OPTION_HTP, // http port
OPTION_CDT, // click delay time
OPTION_MOD, // mode
OPTION_ATI, // automation interval (in minutes)
OPTION_ATO, // automation options
OPTION_SSID, // wifi ssid
OPTION_PASS, // wifi password
OPTION_AUTH, // authentication token
OPTION_DKEY, // device key
OPTION_NAME, // device name
OPTION_IFTT, // IFTTT token
NUM_OPTIONS // number of options
} OG_OPTION_enum;

Expand Down
1 change: 1 addition & 0 deletions OpenGarage/espconnect.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>
#include <WiFiUdp.h>
#include <time.h>
#include "defines.h"
Expand Down
Binary file modified OpenGarage/html/html2esp
Binary file not shown.
4 changes: 3 additions & 1 deletion OpenGarage/html/sta_home.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<table cellpadding='4'>
<tr><td><b>Distance:</b></td><td><label id='lbl_dist'>-</label></td></tr>
<tr><td><b>Door State:</b></td><td><label id='lbl_status'>-</label></td></tr>
<tr><td><b>Read Count:</b></td><td><label id='lbl_beat'>-</label></td></tr>
<tr><td><b>Read Count:</b></td><td><label id='lbl_beat'>-</label></td></tr>
<tr><td><b>WiFi Signal:</b></td><td><label id='lbl_rssi'>-</label></td></tr>
<tr><td><b>Device Key:</b></td><td><input type='password' size=16 maxlength=32 name='dkey' id='dkey'></td></tr>
<tr><td colspan=2><label id='msg'></label></td></tr>
</table>
Expand Down Expand Up @@ -71,6 +72,7 @@
$('#lbl_dist').text(''+jd.dist+' (cm)');
$('#lbl_status').text(jd.door?'OPEN':'closed').css('color', jd.door?'red':'black');
$('#lbl_beat').text(jd.rcnt);
$('#lbl_rssi').text(jd.rssi+' dBm ('+(jd.rssi<-75?'weak':'ok')+')');
$('#head_name').text(jd.name);
});
}
Expand Down
15 changes: 15 additions & 0 deletions OpenGarage/html/sta_options.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@
</select></td></tr>
<tr><td><b>Device Name:</b></td><td><input type='text' size=20 maxlength=32 id='name' data-mini='true' value='-'></td></tr>
<tr><td><b>HTTP Port:</b></td><td><input type='text' size=5 maxlength=5 id='htp' value=0 data-mini='true'></td></tr>
<tr><td><b>IFTTT Token:</b></td><td><input type='text' size=24 maxlength=64 id='iftt' data-mini='true' value='-'></td></tr>
<tr><td><b>Automation:</b></td><td>If door is open for more than</td></tr>
<tr><td colspan=2>
<table><tr><td><input type='text' size=3 maxlength=3 id='ati' value=30 data-mini='true'></td><td>minutes:</td><td><input type='checkbox' id='ato0' data-mini='true'><label for='ato0'>Notify me</label></td><td><input type='checkbox' id='ato1' data-mini='true'><label for='ato1'>Auto-close</label></td></tr></table>
</td></tr>
<tr><td><b>Device Key:</b></td><td><input type='password' size=24 maxlength=32 id='dkey' data-mini='true'></td></tr>

<tr><td colspan=2><p id='msg'></p></td></tr>
</table>
<div data-role='controlgroup' data-type='horizontal'>
Expand All @@ -53,6 +59,7 @@
<script>
function clear_msg() {$('#msg').text('');}
function show_msg(s) {$('#msg').text(s).css('color','red'); setTimeout(clear_msg, 2000);}
function eval_cb(n) {return $(n).is(':checked')?1:0;}
$('#cb_key').click(function(e){
$('#nkey').textinput($(this).is(':checked')?'enable':'disable');
$('#ckey').textinput($(this).is(':checked')?'enable':'disable');
Expand All @@ -71,8 +78,13 @@
comm+='&alm='+$('#alm').val();
comm+='&htp='+$('#htp').val();
comm+='&cdt='+$('#cdt').val();
comm+='&ati='+$('#ati').val();
var ato=0;
for(var i=1;i>=0;i--) { ato=(ato<<1)+eval_cb('#ato'+i); }
comm+='&ato='+ato;
comm+='&name='+encodeURIComponent($('#name').val());
comm+='&auth='+encodeURIComponent($('#auth').val());
comm+='&iftt='+encodeURIComponent($('#iftt').val());
if($('#cb_key').is(':checked')) {
if(!$('#nkey').val()) {
if(!confirm('New device key is empty. Are you sure?')) return;
Expand Down Expand Up @@ -101,8 +113,11 @@
$('#riv').val(jd.riv);
$('#htp').val(jd.htp);
$('#cdt').val(jd.cdt);
$('#ati').val(jd.ati);
for(var i=0;i<=1;i++) {if(jd.ato&(1<<i)) $('#ato'+i).attr('checked',true).checkboxradio('refresh');}
$('#name').val(jd.name);
$('#auth').val(jd.auth);
$('#iftt').val(jd.iftt);
});
});
</script>
Expand Down
4 changes: 3 additions & 1 deletion OpenGarage/html_sta_home.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const char html_sta_home[] PROGMEM = R"(<body>
<table cellpadding='4'>
<tr><td><b>Distance:</b></td><td><label id='lbl_dist'>-</label></td></tr>
<tr><td><b>Door State:</b></td><td><label id='lbl_status'>-</label></td></tr>
<tr><td><b>Read Count:</b></td><td><label id='lbl_beat'>-</label></td></tr>
<tr><td><b>Read Count:</b></td><td><label id='lbl_beat'>-</label></td></tr>
<tr><td><b>WiFi Signal:</b></td><td><label id='lbl_rssi'>-</label></td></tr>
<tr><td><b>Device Key:</b></td><td><input type='password' size=16 maxlength=32 name='dkey' id='dkey'></td></tr>
<tr><td colspan=2><label id='msg'></label></td></tr>
</table>
Expand Down Expand Up @@ -64,6 +65,7 @@ function show() {
$('#lbl_dist').text(''+jd.dist+' (cm)');
$('#lbl_status').text(jd.door?'OPEN':'closed').css('color', jd.door?'red':'black');
$('#lbl_beat').text(jd.rcnt);
$('#lbl_rssi').text(jd.rssi+' dBm ('+(jd.rssi<-75?'weak':'ok')+')');
$('#head_name').text(jd.name);
});
}
Expand Down
15 changes: 15 additions & 0 deletions OpenGarage/html_sta_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ const char html_sta_options[] PROGMEM = R"(<body>
</select></td></tr>
<tr><td><b>Device Name:</b></td><td><input type='text' size=20 maxlength=32 id='name' data-mini='true' value='-'></td></tr>
<tr><td><b>HTTP Port:</b></td><td><input type='text' size=5 maxlength=5 id='htp' value=0 data-mini='true'></td></tr>
<tr><td><b>IFTTT Token:</b></td><td><input type='text' size=24 maxlength=64 id='iftt' data-mini='true' value='-'></td></tr>
<tr><td><b>Automation:</b></td><td>If door is open for more than</td></tr>
<tr><td colspan=2>
<table><tr><td><input type='text' size=3 maxlength=3 id='ati' value=30 data-mini='true'></td><td>minutes:</td><td><input type='checkbox' id='ato0' data-mini='true'><label for='ato0'>Notify me</label></td><td><input type='checkbox' id='ato1' data-mini='true'><label for='ato1'>Auto-close</label></td></tr></table>
</td></tr>
<tr><td><b>Device Key:</b></td><td><input type='password' size=24 maxlength=32 id='dkey' data-mini='true'></td></tr>

<tr><td colspan=2><p id='msg'></p></td></tr>
</table>
<div data-role='controlgroup' data-type='horizontal'>
Expand All @@ -46,6 +52,7 @@ const char html_sta_options[] PROGMEM = R"(<body>
<script>
function clear_msg() {$('#msg').text('');}
function show_msg(s) {$('#msg').text(s).css('color','red'); setTimeout(clear_msg, 2000);}
function eval_cb(n) {return $(n).is(':checked')?1:0;}
$('#cb_key').click(function(e){
$('#nkey').textinput($(this).is(':checked')?'enable':'disable');
$('#ckey').textinput($(this).is(':checked')?'enable':'disable');
Expand All @@ -64,8 +71,13 @@ comm+='&riv='+$('#riv').val();
comm+='&alm='+$('#alm').val();
comm+='&htp='+$('#htp').val();
comm+='&cdt='+$('#cdt').val();
comm+='&ati='+$('#ati').val();
var ato=0;
for(var i=1;i>=0;i--) { ato=(ato<<1)+eval_cb('#ato'+i); }
comm+='&ato='+ato;
comm+='&name='+encodeURIComponent($('#name').val());
comm+='&auth='+encodeURIComponent($('#auth').val());
comm+='&iftt='+encodeURIComponent($('#iftt').val());
if($('#cb_key').is(':checked')) {
if(!$('#nkey').val()) {
if(!confirm('New device key is empty. Are you sure?')) return;
Expand Down Expand Up @@ -94,8 +106,11 @@ setTimeout(close, 4000);
$('#riv').val(jd.riv);
$('#htp').val(jd.htp);
$('#cdt').val(jd.cdt);
$('#ati').val(jd.ati);
for(var i=0;i<=1;i++) {if(jd.ato&(1<<i)) $('#ato'+i).attr('checked',true).checkboxradio('refresh');}
$('#name').val(jd.name);
$('#auth').val(jd.auth);
$('#iftt').val(jd.iftt);
});
});
</script>
Expand Down
Loading

0 comments on commit baaed55

Please sign in to comment.