Skip to content

Commit

Permalink
powersaving & H-bridge driver
Browse files Browse the repository at this point in the history
* esp_gmtime now using undocumented SDK function (bugfix for day offset)
* save valve state immediately after manual override (failsave)
* reevaluate low battery state immediately when receiving new offset
(enhancement)
* based on SDK 1.5.3 (powersaving)
* event driven WLAN AP connect (powersaving)
* event driven TCP receive and disconnect processing (powersaving)
* report total open count (feature)
* H-bridge valve driver support added (feature)
  • Loading branch information
jnsbyr committed Jun 18, 2016
1 parent fffea0f commit 2bc5dd0
Show file tree
Hide file tree
Showing 12 changed files with 521 additions and 306 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
firmware/Makefile.bak
firmware/.cproject.bak
*.bak
46 changes: 39 additions & 7 deletions firmware/.cproject
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@
<targetPlatform archList="all" id="cdt.managedbuild.target.gnu.platform.mingw.base.165418663" name="Debug Platform" osList="win32" superClass="cdt.managedbuild.target.gnu.platform.mingw.base"/>
<builder arguments="-f ${ProjDirPath}/Makefile" buildPath="${workspace_loc:/esp8266-gardena1251}" command="mingw32-make.exe" id="cdt.managedbuild.builder.gnu.cross.1406115981" keepEnvironmentInBuildfile="false" managedBuildOn="false" name="Gnu Make Builder" superClass="cdt.managedbuild.builder.gnu.cross"/>
<tool errorParsers="org.eclipse.cdt.core.GASErrorParser" id="cdt.managedbuild.tool.gnu.assembler.mingw.base.832188319" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.mingw.base">
<option id="gnu.both.asm.option.include.paths.919473686" name="Include paths (-I)" superClass="gnu.both.asm.option.include.paths" valueType="includePath"/>
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.1211266773" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.archiver.mingw.base.1016088165" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.mingw.base"/>
<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.mingw.base.786216720" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.mingw.base"/>
<tool errorParsers="org.eclipse.cdt.core.GCCErrorParser" id="cdt.managedbuild.tool.gnu.c.compiler.mingw.base.862142565" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.mingw.base">
<option id="gnu.c.compiler.option.include.paths.1724602785" name="Include paths (-I)" superClass="gnu.c.compiler.option.include.paths" valueType="includePath">
<listOptionValue builtIn="false" value="&quot;C:\Espressif\ESP8266_SDK\include&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/esp8266-gardena1251/include}&quot;"/>
</option>
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.47897529" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
</tool>
<tool errorParsers="org.eclipse.cdt.core.GLDErrorParser" id="cdt.managedbuild.tool.gnu.c.linker.mingw.base.988257187" name="MinGW C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.mingw.base">
Expand All @@ -53,12 +58,6 @@
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<project id="esp8266-gardena1251.null.1238276365" name="esp8266-gardena1251"/>
</storageModule>
<storageModule moduleId="scannerConfiguration">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
<scannerConfigBuildInfo instanceId="0.1238549876">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
<storageModule moduleId="refreshScope" versionNumber="2">
<configuration configurationName="Default">
Expand All @@ -69,7 +68,7 @@
<buildTargets>
<target name="all" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildArguments>-f ${ProjDirPath}/Makefile</buildArguments>
<buildTarget>all</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
Expand All @@ -91,6 +90,14 @@
<useDefaultCommand>true</useDefaultCommand>
<runAllBuilders>true</runAllBuilders>
</target>
<target name="readMac" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>mingw32-make.exe</buildCommand>
<buildArguments>-f ${ProjDirPath}/Makefile</buildArguments>
<buildTarget>readMac</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
<runAllBuilders>true</runAllBuilders>
</target>
<target name="readFlashId" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>mingw32-make.exe</buildCommand>
<buildArguments>-f ${ProjDirPath}/Makefile</buildArguments>
Expand All @@ -99,6 +106,22 @@
<useDefaultCommand>true</useDefaultCommand>
<runAllBuilders>true</runAllBuilders>
</target>
<target name="imageInfo" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>mingw32-make.exe</buildCommand>
<buildArguments>-f ${ProjDirPath}/Makefile</buildArguments>
<buildTarget>imageInfo</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
<runAllBuilders>true</runAllBuilders>
</target>
<target name="flash-init-data-8M" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>mingw32-make.exe</buildCommand>
<buildArguments>-f ${ProjDirPath}/Makefile</buildArguments>
<buildTarget>flash-init-data-8M</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
<runAllBuilders>true</runAllBuilders>
</target>
<target name="flash-init-data-4M" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>mingw32-make.exe</buildCommand>
<buildArguments>-f ${ProjDirPath}/Makefile</buildArguments>
Expand All @@ -109,4 +132,13 @@
</target>
</buildTargets>
</storageModule>
<storageModule moduleId="scannerConfiguration">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
<scannerConfigBuildInfo instanceId="0.1238549876;0.1238549876.;cdt.managedbuild.tool.gnu.c.compiler.mingw.base.862142565;cdt.managedbuild.tool.gnu.c.compiler.input.47897529">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="0.1238549876">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
</storageModule>
</cproject>
10 changes: 10 additions & 0 deletions firmware/CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,13 @@ CHANGES

0.9.0.7 - 12.09.2015
documentation added

0.9.1.0 - 06.05.2016
esp_gmtime now using undocumented SDK function (bugfix for day offset)
save valve state immediately after manual override (failsave)
reevaluate low battery state immediately when receiving new offset (enhancement)
based on SDK 1.5.3 (powersaving)
event driven WLAN AP connect (powersaving)
event driven TCP receive and disconnect processing (powersaving)
report total open count (feature)
H-bridge valve driver support added (feature)
15 changes: 12 additions & 3 deletions firmware/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
#

# project name
TARGET = sleeper-0.9.0.7
TARGET = sleeper-0.9.1.0

# source subdirectories
MODULES = user driver

# libraries, mainly provided by the SDK
LIBS = c gcc hal phy pp net80211 lwip wpa json main
LIBS = c gcc hal phy pp net80211 lwip wpa crypto main json

# ESP8266 flash parameters (dependent on ESP8266 hardware)
FLASHPARMS = --flash_freq 40m --flash_mode qio --flash_size 4m
#FLASHPARMS = --flash_freq 40m --flash_mode qio --flash_size 4m
FLASHPARMS = --flash_freq 40m --flash_mode dio --flash_size 8m

# flash tool parameters (dependent on PC UART hardware)
ESPPORT = COM4
Expand Down Expand Up @@ -140,6 +141,14 @@ flash-init-data-4M:
$(ESPTOOL) --port $(ESPPORT) --baud $(ESPBAUD) write_flash $(FLASHPARMS) \
0x7C000 $(SDK_BINDIR)/esp_init_data_default.bin

flash-init-data-8M:
$(ESPTOOL) --port $(ESPPORT) --baud $(ESPBAUD) write_flash $(FLASHPARMS) \
0xFC000 $(SDK_BINDIR)/esp_init_data_default.bin

flash-default-params-8M:
$(ESPTOOL) --port $(ESPPORT) --baud $(ESPBAUD) write_flash $(FLASHPARMS) \
0xFE000 $(SDK_BINDIR)/blank.bin

clean:
rm -f $(APP_AR)
rm -f $(TARGET_OUT)
Expand Down
26 changes: 12 additions & 14 deletions firmware/include/time.h → firmware/include/esp_time.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*****************************************************************************
*
* Copyright (c) 2015 jnsbyr
* Copyright (c) 2015-2016 jnsbyr
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,28 +16,26 @@
*
*****************************************************************************
*
* The author of this file does not claim any copyright or authorship of the
* algorithms and mathematics used in this file. They are either trivial or
* based on well documented public knowledge. The credits go to:
*
* Gregorian calendar (1582 AD)
* Zeller's congruence algorithm (1882 AD)
*
*****************************************************************************
*
* project: ESP8266 library enhancements
*
* file: time.h
* file: esp_time.h
*
* created: 13.04.2015
*
*****************************************************************************/

#ifndef __USER_TIME_H__
#define __USER_TIME_H__
#ifndef __USER_ESP_TIME_H__
#define __USER_ESP_TIME_H__

#include <c_types.h>

/*
* constants
*/
#define MINUTES_PER_DAY 1440
#define SECONDS_PER_DAY 86400
#define SECONDS_PER_HOUR 3600

/*
* structure for storing time information
*/
Expand Down Expand Up @@ -70,4 +68,4 @@ void ICACHE_FLASH_ATTR esp_gmtime(uint64* t, struct ets_tm* tms);
*/
const char* ICACHE_FLASH_ATTR esp_strptime(const char *s, const char *format, struct ets_tm* tms);

#endif /* __USER_TIME_H__ */
#endif /* __ESP_TIME_H__ */
69 changes: 40 additions & 29 deletions firmware/include/main.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*****************************************************************************
*
* Copyright (c) 2015 jnsbyr
* Copyright (c) 2015-2016 jnsbyr
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,9 +30,18 @@
#include <c_types.h>
#include <ip_addr.h>

#define SLEEPER_COMMANDTIME 1600 // [ms] 1.6 s, typical time for boot, AP connect and TCP handshake
#define SLEEPER_MIN_DOWNTIME 1000 // [ms] 1.0 s, minimum time between shutdown and restart
#define MIN_BATTERY_VOLTAGE 3270 // [mV] minimum supply voltage before shutting down operation (nominal regulated voltage is 3320 mV)
#define SLEEPER_BOOTTIME 87 // [ms] bootloader runtime after reset
#define SLEEPER_COMMANDTIME 600 // [ms] 0.6 s, typical time runtime (boot, AP connect and TCP handshake)
#define SLEEPER_MIN_DOWNTIME 1000 // [ms] 1.0 s, minimum time between shutdown and restart

#define DEFAULT_DEEP_SLEEP_SCALE 10375 // extend deep sleep duration by 3.75% to compensate for early wakeup by RTC

#define MIN_BATTERY_VOLTAGE 3270 // [mV] minimum supply voltage before shutting down operation (nominal regulated voltage is 3320 mV)

#define MAX_WLAN_TIME 8000 // [ms] timeout
#define MAX_UPLINK_TIME 2000 // [ms] timeout
#define WLAN_TIMER_PERIOD 500 // [ms] interval
#define UPLINK_TIMER_PERIOD 200 // [ms] interval

#define MAX_ACTIVITIES 32

Expand All @@ -59,41 +68,43 @@ typedef struct // 5 Byte
uint16 duration; // seconds
} ActivityT;

typedef struct // 60 + N*5 Byte
typedef struct // 62 + N*5 Byte
{
uint16 magic; // static
uint8 mode; // config
uint8 valveOpen; // state, bool, valve state
uint8 offMode; // state, mode to set after valve is switched off
uint8 lowBattery; // state, bool, vdd33 voltage is below hard coded limit
uint8 override; // state, bool, manual override of current activity
uint8 overriddenMode; // state, mode to set when override ends
uint8 valveCloseTimeEstimated; // state, bool, valve close time is only estimated
uint8 overrideEndTimeEstimated; // state, bool, override end time is only estimated
uint16 boottime; // config, milliseconds
uint16 defaultDuration; // config, seconds, default duration to open vale (manual mode, override)
uint16 downtimeScale; // config, 10000 = 1.0
sint16 batteryOffset; // config, millivolt
uint32 activityProgramId; // config
uint32 downtime; // config, milliseconds
uint32 lastDowntime; // state, milliseconds, last sleep duration
uint32 totalOpenDuration; // state, seconds, total duration the valve was open since init
uint64 valveOpenTime; // state, milliseconds, time when valve was opened
uint64 valveCloseTime; // state, milliseconds, time when valve must be closed
uint64 lastShutdownTime; // state, milliseconds, time when last os shutdown was initiated
uint64 overrideEndTime; // state, milliseconds, time when override is reset
struct ip_info ipConfig; // state
uint16 magic; // static
uint8 mode; // config
uint8 valveOpen; // state, bool, valve state
uint8 offMode; // state, mode to set after valve is switched off
uint8 lowBattery; // state, bool, vdd33 voltage is below hard coded limit
uint8 override; // state, bool, manual override of current activity
uint8 overriddenMode; // state, mode to set when override ends
uint8 valveCloseTimeEstimated; // state, bool, valve close time is only estimated
uint8 overrideEndTimeEstimated; // state, bool, override end time is only estimated
uint16 totalOpenCount; // state, total number valve was opened since init
uint16 boottime; // config, milliseconds
uint16 defaultDuration; // config, seconds, default duration to open vale (manual mode, override)
uint16 downtimeScale; // config, 10000 = 1.0
sint16 batteryOffset; // config, millivolt
uint32 activityProgramId; // config
uint32 downtime; // config, milliseconds
uint32 lastDowntime; // state, milliseconds, last sleep duration
uint32 totalOpenDuration; // state, seconds, total duration the valve was open since init
uint64 valveOpenTime; // state, milliseconds, time when valve was opened
uint64 valveCloseTime; // state, milliseconds, time when valve must be closed
uint64 lastShutdownTime; // state, milliseconds, time when last os shutdown was initiated
uint64 overrideEndTime; // state, milliseconds, time when override is reset
struct ip_info ipConfig; // state
ActivityT activities[MAX_ACTIVITIES]; // config
} PersistentStateT;

typedef struct
{
PersistentStateT rtcMem; // persistent values
sint16 batteryVoltage; // supply voltage at time of boot
uint8 timeSynchronized; // bool, current is synchronized with server
uint8 timeSynchronized; // bool, current time is synchronized with server
} SleeperStateT;


uint64 ICACHE_FLASH_ATTR getTime();
uint64 getTime();
void comProcessing();

#endif /* __USER_MAIN_H__ */
11 changes: 7 additions & 4 deletions firmware/include/user_config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*****************************************************************************
*
* Copyright (c) 2015 jnsbyr
* Copyright (c) 2015-2016 jnsbyr
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,8 +33,11 @@
#define REMOTE_IP "192.168.0.1" // IP address of control server
#define REMOTE_PORT 3030 // port of control server

#define DEFAULT_DOWNTIME 10000 // milliseconds, 10 s initial deep sleep duration while not configured
#define DEFAULT_MANUAL_DURATION 600 // seconds, 10 min manual override valve open duration while not configured
#define MAX_VALVE_OPEN_DOWNTIME 300000 // milliseconds, 5 min maximum downtime while valve is open
#define DEFAULT_DOWNTIME 10000 // milliseconds, default 10 s initial deep sleep duration while not configured
#define DEFAULT_MANUAL_DURATION 600 // seconds, default 10 min manual override valve open duration while not configured
#define MAX_VALVE_OPEN_DOWNTIME 300000 // milliseconds, default 5 min maximum downtime while valve is open

#define VALVE_DRIVER_TYPE 1 // 1=capacitor, 2=H-bridge
#define VALVE_OPEN_PULSE_DURATION 200000 // microseconds, recommended 250 ms open pulse duration to open valve

#endif
7 changes: 3 additions & 4 deletions firmware/include/valve.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*****************************************************************************
*
* Copyright (c) 2015 jnsbyr
* Copyright (c) 2015-2016 jnsbyr
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -27,11 +27,10 @@
#ifndef __USER_VALVE_H__
#define __USER_VALVE_H__

#include <c_types.h>
#include "main.h"

void ICACHE_FLASH_ATTR valueInitGPIOs();
void ICACHE_FLASH_ATTR valveDriverInit(void);
uint64 ICACHE_FLASH_ATTR valveControl(SleeperStateT* sleeperState, uint8 override, uint8 mode, uint64 startTime);
void ICACHE_FLASH_ATTR valveShutdown();
void ICACHE_FLASH_ATTR valveDriverShutdown(void);

#endif /* __USER_VALVE_H__ */
Loading

0 comments on commit 2bc5dd0

Please sign in to comment.