mirror of
https://github.com/oopuuu/zTC1.git
synced 2026-03-23 20:49:55 +08:00
Compare commits
159 Commits
v2.1.6_1
...
dev_file_o
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eaf8f5bf60 | ||
|
|
5eecfefc74 | ||
|
|
0c675c6f03 | ||
|
|
982e82c2bb | ||
|
|
eece61e380 | ||
|
|
36861fadff | ||
|
|
f3b7477550 | ||
|
|
dfa8b1c475 | ||
|
|
c7d2f8d7d0 | ||
|
|
3f5078294f | ||
|
|
d7e2243a86 | ||
|
|
4e1b376b6a | ||
|
|
393af288ec | ||
|
|
e9f1448a69 | ||
|
|
004eaf28dd | ||
|
|
0e361111a5 | ||
|
|
026e058934 | ||
|
|
5623b632c7 | ||
|
|
34e1cabc53 | ||
|
|
e6d430c37c | ||
|
|
7c77be5455 | ||
|
|
6598250af1 | ||
|
|
60977f0327 | ||
|
|
f1ba19554e | ||
|
|
2b1577998c | ||
|
|
a5c9211fb6 | ||
|
|
e735afc6c5 | ||
|
|
f8d2b25d1e | ||
|
|
a342d4552a | ||
|
|
759a5ec5cc | ||
|
|
0ba50e9a52 | ||
|
|
f8fba89793 | ||
|
|
30a353c035 | ||
|
|
b470b0f8a0 | ||
|
|
76e8612007 | ||
|
|
6cf213a1b9 | ||
|
|
c214f0de30 | ||
|
|
bf7c8dd7fe | ||
|
|
b2bd368b2d | ||
|
|
56c9923336 | ||
|
|
2d34767cde | ||
|
|
2258158a0d | ||
|
|
f7b6b8ae0a | ||
|
|
b38f9f91d8 | ||
|
|
84cf4bfc06 | ||
|
|
542e5daf8a | ||
|
|
ceb208813a | ||
|
|
fe4d037390 | ||
|
|
8e083fe175 | ||
|
|
9ac9e5e2f1 | ||
|
|
36d8330ee3 | ||
|
|
a51c7a8eb2 | ||
|
|
6ef875879f | ||
|
|
2b2a77b5f5 | ||
|
|
787e57439e | ||
|
|
fd157172e3 | ||
|
|
989fd0a1ed | ||
|
|
a76afc6ccb | ||
|
|
23fcd56109 | ||
|
|
a6bb74f906 | ||
|
|
fd5d190096 | ||
|
|
997eda3ef2 | ||
|
|
e4e0ca969e | ||
|
|
4ea9df172b | ||
|
|
d0a71fe216 | ||
|
|
c8dd3d513d | ||
|
|
dd5b282c85 | ||
|
|
8acef18406 | ||
|
|
13df06e546 | ||
|
|
9df4b8358b | ||
|
|
bdfed87f1e | ||
|
|
7ea87ae4c9 | ||
|
|
a87d894c4d | ||
|
|
d7e695e37e | ||
|
|
40bdb50a8d | ||
|
|
caffddfb43 | ||
|
|
11d3d50265 | ||
|
|
66d2863fa4 | ||
|
|
56395f62bc | ||
|
|
0d5ba4a7d3 | ||
|
|
d925c7d805 | ||
|
|
b40bb1a022 | ||
|
|
981c33e3ad | ||
|
|
fc631ac9f5 | ||
|
|
840b0eb374 | ||
|
|
f2f63625cc | ||
|
|
78dbc6209b | ||
|
|
2896243e24 | ||
|
|
9d80970e9e | ||
|
|
7bac9096f5 | ||
|
|
5f81f2ca71 | ||
|
|
57dbe17175 | ||
|
|
a8d5319d52 | ||
|
|
f41a20f8d0 | ||
|
|
52da5983b5 | ||
|
|
758abf123b | ||
|
|
e3de06437b | ||
|
|
7f0e5d15c8 | ||
|
|
7656ae998c | ||
|
|
3aec445e57 | ||
|
|
7048333ebc | ||
|
|
bfae22ddd6 | ||
|
|
520a245ef3 | ||
|
|
1f92b30ea0 | ||
|
|
4a20713693 | ||
|
|
b36fce48f5 | ||
|
|
1b7d86f1f7 | ||
|
|
d7c83fa125 | ||
|
|
44ed1d2604 | ||
|
|
f17ca68404 | ||
|
|
b5cb7b8218 | ||
|
|
b099618b91 | ||
|
|
160f4f5b7f | ||
|
|
168f89cc82 | ||
|
|
713dd1a907 | ||
|
|
59d8809641 | ||
|
|
8537240bbd | ||
|
|
f1e15500e4 | ||
|
|
5a1b82b728 | ||
|
|
f56beae55e | ||
|
|
9caeec8611 | ||
|
|
65d6c77a1e | ||
|
|
8a28029940 | ||
|
|
56f8a2efaa | ||
|
|
3964703152 | ||
|
|
b2b9152441 | ||
|
|
4b000d109c | ||
|
|
6f6c1b700f | ||
|
|
90ba74b401 | ||
|
|
c49fc46e15 | ||
|
|
7113b361e8 | ||
|
|
a3e3639cf9 | ||
|
|
d0ff87c8d0 | ||
|
|
b3a074d4c3 | ||
|
|
a639b58922 | ||
|
|
208a7b3ef9 | ||
|
|
9e26335841 | ||
|
|
ba943fe6ae | ||
|
|
20d3c98bb4 | ||
|
|
c27b89b82d | ||
|
|
3fca5b51aa | ||
|
|
3070e5139f | ||
|
|
daa3415717 | ||
|
|
a10788abeb | ||
|
|
c88fb23dfe | ||
|
|
e7d380bf4c | ||
|
|
997c8695ab | ||
|
|
eb7241e235 | ||
|
|
09a36dc351 | ||
|
|
e6e46bdf39 | ||
|
|
24188e39c9 | ||
|
|
b9e6b02217 | ||
|
|
3350aa5446 | ||
|
|
9f83fbd744 | ||
|
|
51817d6b6a | ||
|
|
ad5f3ecf96 | ||
|
|
4e34a88be6 | ||
|
|
c94e8fcd12 | ||
|
|
4ed9dbd435 |
125
.cproject
Normal file
125
.cproject
Normal file
@@ -0,0 +1,125 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
|
||||
<storageModule moduleId="org.eclipse.cdt.core.settings">
|
||||
<cconfiguration id="0.2123886510">
|
||||
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="0.2123886510" moduleId="org.eclipse.cdt.core.settings" name="Default">
|
||||
<externalSettings/>
|
||||
<extensions>
|
||||
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.VCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
|
||||
</extensions>
|
||||
</storageModule>
|
||||
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
|
||||
<configuration artifactName="${ProjName}" buildProperties="" description="" id="0.2123886510" name="Default" parent="org.eclipse.cdt.build.core.prefbase.cfg">
|
||||
<folderInfo id="0.2123886510." name="/" resourcePath="">
|
||||
<toolChain id="org.eclipse.cdt.build.core.prefbase.toolchain.1020810961" name="No ToolChain" resourceTypeBasedDiscovery="false" superClass="org.eclipse.cdt.build.core.prefbase.toolchain">
|
||||
<targetPlatform id="org.eclipse.cdt.build.core.prefbase.toolchain.1020810961.1245561758" name=""/>
|
||||
<builder buildPath="${ProjDirPath}/" command="${ProjDirPath}/make.exe" id="org.eclipse.cdt.build.core.settings.default.builder.1574076724" keepEnvironmentInBuildfile="false" managedBuildOn="false" name="Gnu Make Builder" superClass="org.eclipse.cdt.build.core.settings.default.builder"/>
|
||||
<tool id="org.eclipse.cdt.build.core.settings.holder.libs.1419411456" name="holder for library settings" superClass="org.eclipse.cdt.build.core.settings.holder.libs"/>
|
||||
<tool id="org.eclipse.cdt.build.core.settings.holder.1269034716" name="Assembly" superClass="org.eclipse.cdt.build.core.settings.holder">
|
||||
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.2077730760" languageId="org.eclipse.cdt.core.assembly" languageName="Assembly" sourceContentType="org.eclipse.cdt.core.asmSource" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
|
||||
</tool>
|
||||
<tool id="org.eclipse.cdt.build.core.settings.holder.416463984" name="GNU C++" superClass="org.eclipse.cdt.build.core.settings.holder">
|
||||
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.1802293645" languageId="org.eclipse.cdt.core.g++" languageName="GNU C++" sourceContentType="org.eclipse.cdt.core.cxxSource,org.eclipse.cdt.core.cxxHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
|
||||
</tool>
|
||||
<tool id="org.eclipse.cdt.build.core.settings.holder.545607982" name="GNU C" superClass="org.eclipse.cdt.build.core.settings.holder">
|
||||
<inputType id="org.eclipse.cdt.build.core.settings.holder.inType.1775218918" languageId="org.eclipse.cdt.core.gcc" languageName="GNU C" sourceContentType="org.eclipse.cdt.core.cSource,org.eclipse.cdt.core.cHeader" superClass="org.eclipse.cdt.build.core.settings.holder.inType"/>
|
||||
</tool>
|
||||
</toolChain>
|
||||
</folderInfo>
|
||||
</configuration>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
|
||||
<storageModule moduleId="ilg.gnuarmeclipse.managedbuild.packs"/>
|
||||
</cconfiguration>
|
||||
</storageModule>
|
||||
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
|
||||
<project id="MiCO-3-0.null.804040427" name="MiCO-3-0"/>
|
||||
</storageModule>
|
||||
<storageModule moduleId="scannerConfiguration">
|
||||
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
|
||||
<scannerConfigBuildInfo instanceId="0.2123886510">
|
||||
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
|
||||
</scannerConfigBuildInfo>
|
||||
<scannerConfigBuildInfo instanceId="0.1826818492">
|
||||
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
|
||||
</scannerConfigBuildInfo>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
|
||||
<storageModule moduleId="refreshScope" versionNumber="2">
|
||||
<configuration configurationName="Default">
|
||||
<resource resourceType="PROJECT" workspacePath="/MiCO-3-0"/>
|
||||
</configuration>
|
||||
</storageModule>
|
||||
<storageModule moduleId="org.eclipse.cdt.internal.ui.text.commentOwnerProjectMappings"/>
|
||||
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets">
|
||||
<buildTargets>
|
||||
<target name="clean" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
|
||||
<buildCommand>make</buildCommand>
|
||||
<buildTarget>clean</buildTarget>
|
||||
<stopOnError>true</stopOnError>
|
||||
<useDefaultCommand>true</useDefaultCommand>
|
||||
<runAllBuilders>true</runAllBuilders>
|
||||
</target>
|
||||
<target name="helloworld@MK3165 total download" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
|
||||
<buildCommand>${ProjDirPath}/make.exe</buildCommand>
|
||||
<buildArguments/>
|
||||
<buildTarget>helloworld@MK3165 total download</buildTarget>
|
||||
<stopOnError>true</stopOnError>
|
||||
<useDefaultCommand>true</useDefaultCommand>
|
||||
<runAllBuilders>true</runAllBuilders>
|
||||
</target>
|
||||
<target name="helloworld@MK3165" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
|
||||
<buildCommand>${ProjDirPath}/make</buildCommand>
|
||||
<buildArguments/>
|
||||
<buildTarget>helloworld@MK3165</buildTarget>
|
||||
<stopOnError>true</stopOnError>
|
||||
<useDefaultCommand>true</useDefaultCommand>
|
||||
<runAllBuilders>true</runAllBuilders>
|
||||
</target>
|
||||
<target name="standard release" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
|
||||
<buildCommand>${ProjDirPath}/make.exe</buildCommand>
|
||||
<buildTarget>standard release</buildTarget>
|
||||
<stopOnError>true</stopOnError>
|
||||
<useDefaultCommand>true</useDefaultCommand>
|
||||
<runAllBuilders>true</runAllBuilders>
|
||||
</target>
|
||||
<target name="helloworld@MK3165 total" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
|
||||
<buildCommand>${ProjDirPath}/make.exe</buildCommand>
|
||||
<buildArguments/>
|
||||
<buildTarget>helloworld@MK3165 total</buildTarget>
|
||||
<stopOnError>true</stopOnError>
|
||||
<useDefaultCommand>true</useDefaultCommand>
|
||||
<runAllBuilders>true</runAllBuilders>
|
||||
</target>
|
||||
<target name="bluetooth.ble_hello_sensor@MK3238" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
|
||||
<buildCommand>${ProjDirPath}/make.exe</buildCommand>
|
||||
<buildArguments/>
|
||||
<buildTarget>bluetooth.ble_hello_sensor@MK3238</buildTarget>
|
||||
<stopOnError>true</stopOnError>
|
||||
<useDefaultCommand>true</useDefaultCommand>
|
||||
<runAllBuilders>true</runAllBuilders>
|
||||
</target>
|
||||
<target name="bluetooth.ble_hello_sensor@MK3238 total" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
|
||||
<buildCommand>${ProjDirPath}/make.exe</buildCommand>
|
||||
<buildArguments/>
|
||||
<buildTarget>bluetooth.ble_hello_sensor@MK3238 total</buildTarget>
|
||||
<stopOnError>true</stopOnError>
|
||||
<useDefaultCommand>true</useDefaultCommand>
|
||||
<runAllBuilders>true</runAllBuilders>
|
||||
</target>
|
||||
<target name="bluetooth.ble_hello_sensor@MK3238 total download" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
|
||||
<buildCommand>${ProjDirPath}/make.exe</buildCommand>
|
||||
<buildArguments/>
|
||||
<buildTarget>bluetooth.ble_hello_sensor@MK3238 total download</buildTarget>
|
||||
<stopOnError>true</stopOnError>
|
||||
<useDefaultCommand>true</useDefaultCommand>
|
||||
<runAllBuilders>true</runAllBuilders>
|
||||
</target>
|
||||
</buildTargets>
|
||||
</storageModule>
|
||||
</cproject>
|
||||
2
.gdbinit
Normal file
2
.gdbinit
Normal file
@@ -0,0 +1,2 @@
|
||||
set remotetimeout 20
|
||||
shell start /B F:\TC1\MiCoder_v1.3_Win32-64\MiCoder/OpenOCD/Win32/openocd_mico.exe -s ./ -f ./mico-os/makefiles/OpenOCD/interface/jlink_swd.cfg -f ./mico-os/makefiles/OpenOCD/mw3xx/mw3xx.cfg -f ./mico-os/makefiles/OpenOCD/mw3xx/mw3xx_gdb_jtag.cfg -l ./build/openocd_log.txt
|
||||
3
.openocd_cfg
Normal file
3
.openocd_cfg
Normal file
@@ -0,0 +1,3 @@
|
||||
source [find F:\TC1\MiCoder_v1.3_Win32-64\MiCoder/OpenOCD/jlink_swd.cfg]
|
||||
source [find F:\TC1\MiCoder_v1.3_Win32-64\MiCoder/OpenOCD/mw3xx.cfg]
|
||||
source [find F:\TC1\MiCoder_v1.3_Win32-64\MiCoder/OpenOCD/mw3xx_gdb_jtag.cfg]
|
||||
27
.project
Normal file
27
.project
Normal file
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>zTC1</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
|
||||
<triggers>cleanfullincremental</triggers>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
|
||||
<triggers>fullincremental</triggers>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.cdt.core.cnature</nature>
|
||||
<nature>org.eclipse.cdt.core.ccnature</nature>
|
||||
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
|
||||
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
18
.vscode/c_cpp_properties.json
vendored
Normal file
18
.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "windows-gcc-x64",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**"
|
||||
],
|
||||
"compilerPath": "gcc",
|
||||
"cStandard": "${default}",
|
||||
"cppStandard": "${default}",
|
||||
"intelliSenseMode": "windows-gcc-x64",
|
||||
"compilerArgs": [
|
||||
""
|
||||
]
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
24
.vscode/launch.json
vendored
Normal file
24
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "C/C++ Runner: Debug Session",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"externalConsole": true,
|
||||
"cwd": "e:/15_GitHub/TC1/zTC1/TC1",
|
||||
"program": "e:/15_GitHub/TC1/zTC1/TC1/build/Debug/outDebug",
|
||||
"MIMode": "gdb",
|
||||
"miDebuggerPath": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
59
.vscode/settings.json
vendored
Normal file
59
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
{
|
||||
"C_Cpp_Runner.cCompilerPath": "gcc",
|
||||
"C_Cpp_Runner.cppCompilerPath": "g++",
|
||||
"C_Cpp_Runner.debuggerPath": "gdb",
|
||||
"C_Cpp_Runner.cStandard": "",
|
||||
"C_Cpp_Runner.cppStandard": "",
|
||||
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
|
||||
"C_Cpp_Runner.useMsvc": false,
|
||||
"C_Cpp_Runner.warnings": [
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-Wpedantic",
|
||||
"-Wshadow",
|
||||
"-Wformat=2",
|
||||
"-Wcast-align",
|
||||
"-Wconversion",
|
||||
"-Wsign-conversion",
|
||||
"-Wnull-dereference"
|
||||
],
|
||||
"C_Cpp_Runner.msvcWarnings": [
|
||||
"/W4",
|
||||
"/permissive-",
|
||||
"/w14242",
|
||||
"/w14287",
|
||||
"/w14296",
|
||||
"/w14311",
|
||||
"/w14826",
|
||||
"/w44062",
|
||||
"/w44242",
|
||||
"/w14905",
|
||||
"/w14906",
|
||||
"/w14263",
|
||||
"/w44265",
|
||||
"/w14928"
|
||||
],
|
||||
"C_Cpp_Runner.enableWarnings": true,
|
||||
"C_Cpp_Runner.warningsAsError": false,
|
||||
"C_Cpp_Runner.compilerArgs": [],
|
||||
"C_Cpp_Runner.linkerArgs": [],
|
||||
"C_Cpp_Runner.includePaths": [],
|
||||
"C_Cpp_Runner.includeSearch": [
|
||||
"*",
|
||||
"**/*"
|
||||
],
|
||||
"C_Cpp_Runner.excludeSearch": [
|
||||
"**/build",
|
||||
"**/build/**",
|
||||
"**/.*",
|
||||
"**/.*/**",
|
||||
"**/.vscode",
|
||||
"**/.vscode/**"
|
||||
],
|
||||
"C_Cpp_Runner.useAddressSanitizer": false,
|
||||
"C_Cpp_Runner.useUndefinedSanitizer": false,
|
||||
"C_Cpp_Runner.useLeakSanitizer": false,
|
||||
"C_Cpp_Runner.showCompilationTime": false,
|
||||
"C_Cpp_Runner.useLinkTimeOptimization": false,
|
||||
"C_Cpp_Runner.msvcSecureNoWarnings": false
|
||||
}
|
||||
22
README.md
22
README.md
@@ -8,10 +8,10 @@
|
||||
|
||||
# 固件web界面
|
||||
|
||||
<img src="doc/2.png"><img src="doc/1.png">
|
||||
<img src="doc/IMG_0863.png"><img src="doc/1.png"><img src="doc/IMG_0887.png">
|
||||
|
||||
# HASS接入效果
|
||||
<img src="doc/3.png">
|
||||
<img src="doc/IMG_0888.png">
|
||||
|
||||
固件启动后, 会开启一个热点 TC1-AP-XXXXXX,连接热点后, 直接用浏览器访问: http://192.168.0.1 即可看到如上web界面.
|
||||
|
||||
@@ -52,17 +52,27 @@ TC1 排插硬件分 A1 A2 两个版本, 本固件仅支持 **A1 版本**. A1 A2
|
||||
|
||||
- [x] HomeAssistant中增加总耗电量传感器,今日耗电量传感器,昨日耗电量传感器,数据来自于插座历史统计
|
||||
- [x] 可以设置mqtt数据上报频率,默认2秒
|
||||
- [x] 可以设置电源 led 是否打开,默认打开(系统自检以及错误指示灯仍会工作)
|
||||
- [x] 后台和ha mqtt增加总开关控制
|
||||
- [x] 后台可以自定义6个插座的名称,名称将会自动同步到ha mqtt
|
||||
- [x] 修复了原代码中电源按钮长按 (现在是5秒)重新配网和(10秒)恢复出厂的逻辑错误
|
||||
- [x] 后台和ha mqtt中增加了童锁开关,开启童锁则上电以后不再响应任何电源按钮的事件,**上电时同时按住按钮的话会开启配网热点**(防止开了童锁,wifi又嗝屁,导致插座功能暴毙的救命逻辑)
|
||||
- [x] 后台可自定义设备名称,此名称将会同至ha mqtt作为mqtt中插座名称的前缀
|
||||
- [x] 后台可自定义按键连击,长按等功能
|
||||
- [x] 定时开关现在支持更多操作项,与按键功能自定义一样
|
||||
|
||||
# 编译固件
|
||||
|
||||
- windows环境,先安装python2.7
|
||||
- 安装[mico-cube 1.0.0](https://files.pythonhosted.org/packages/a8/4e/687a3ef2edabfd9fdd797747d6eeb2ddbe70e65c49fa4062268281a0ded4/mico-cube-1.0.0.tar.gz#sha256=4a930d394344f72c862c203d5d8a832357c3b3dc940c825ca3e528453dd2f17f)
|
||||
- windows环境,先安装python2.7,下载安装的时候添加环境变量到Path,默认安装路径需要添加两个环境变量C:\Python27;C:\Python27\Scripts
|
||||
- 安装[mico-cube 1.0.0](https://files.pythonhosted.org/packages/a8/4e/687a3ef2edabfd9fdd797747d6eeb2ddbe70e65c49fa4062268281a0ded4/mico-cube-1.0.0.tar.gz#sha256=4a930d394344f72c862c203d5d8a832357c3b3dc940c825ca3e528453dd2f17f) python2 -m pip install ./mico-cube-1.0.0.tar.gz
|
||||
- 安装[Micoder](http://firmware.mxchip.com/MiCoder_v1.3_Win32:64.zip)下载完解压出来(不要有中文路径)
|
||||
- 安装完成后添加micoder路径 mico config --global MICODER (micoder所在路径)/MiCoder
|
||||
- 最后切换到此项目根目录执行build.sh即可编译固件
|
||||
|
||||
|
||||
|
||||
# 注意事项
|
||||
- 使用pip安装mico-cube失败,请使用下列命令安装(执行命令时注意路径位置)
|
||||
|
||||
- `python -m pip install mico-cube-1.0.0.tar.gz`
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
###############################################################################
|
||||
###############################################################################
|
||||
|
||||
|
||||
NAME := App_TC1
|
||||
|
||||
@@ -36,6 +36,11 @@
|
||||
#include <http-strings.h>
|
||||
#include "stdlib.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mico.h"
|
||||
#include "httpd_priv.h"
|
||||
#include "app_httpd.h"
|
||||
@@ -52,8 +57,11 @@
|
||||
static bool is_http_init;
|
||||
static bool is_handlers_registered;
|
||||
const struct httpd_wsgi_call g_app_handlers[];
|
||||
char power_info_json[1552] = {0};
|
||||
char power_info_json[2560] = {0};
|
||||
char up_time[16] = "00:00:00";
|
||||
#define CHUNK_SIZE 512 // 每次发送 512 字节,避免 buffer 太大
|
||||
#define OTA_BUFFER_SIZE 512
|
||||
#define MAX_OTA_SIZE 1024*1024
|
||||
|
||||
/*
|
||||
void GetPraFromUrl(char* url, char* pra, char* val)
|
||||
@@ -90,27 +98,30 @@ void GetPraFromUrl(char* url, char* pra, char* val)
|
||||
}
|
||||
*/
|
||||
|
||||
static int HttpGetIndexPage(httpd_request_t *req) {
|
||||
|
||||
static OSStatus send_in_chunks(int sock, const uint8_t *data, int total_len) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
err = httpd_send_all_header(req, HTTP_RES_200, sizeof(web_index_html), HTTP_CONTENT_HTML_ZIP);
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send http index headers."));
|
||||
|
||||
err = httpd_send_body(req->sock, web_index_html, sizeof(web_index_html));
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send http index body."));
|
||||
|
||||
exit:
|
||||
for (int offset = 0; offset < total_len; offset += CHUNK_SIZE) {
|
||||
int chunk_len = (total_len - offset > CHUNK_SIZE) ? CHUNK_SIZE : (total_len - offset);
|
||||
err = httpd_send_body(sock, data + offset, chunk_len);
|
||||
require_noerr_action(err, exit, http_log("ERROR: Send chunk failed at offset %d", offset));
|
||||
}
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpGetDemoPage(httpd_request_t *req) {
|
||||
static int HttpGetIndexPage(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
err = httpd_send_all_header(req, HTTP_RES_200, sizeof(web_index_html), HTTP_CONTENT_HTML_ZIP);
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send http demo headers."));
|
||||
int total_sz = sizeof(web_index_html);
|
||||
|
||||
err = httpd_send_body(req->sock, web_index_html, sizeof(web_index_html));
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send http demo body."));
|
||||
exit:
|
||||
|
||||
err = httpd_send_all_header(req, HTTP_RES_200, total_sz, HTTP_CONTENT_HTML_ZIP);
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send index headers."));
|
||||
|
||||
err = send_in_chunks(req->sock, web_index_html, total_sz);
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send index body."));
|
||||
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -118,14 +129,15 @@ static int HttpGetAssets(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
char *file_name = strstr(req->filename, "/assets/");
|
||||
if (!file_name) { http_log("HttpGetAssets url[%s] err", req->filename);
|
||||
if (!file_name) {
|
||||
http_log("HttpGetAssets url[%s] err", req->filename);
|
||||
return err;
|
||||
}
|
||||
//http_log("HttpGetAssets url[%s] file_name[%s]", req->filename, file_name);
|
||||
|
||||
int total_sz = 0;
|
||||
const unsigned char *file_data = NULL;
|
||||
const char *content_type = HTTP_CONTENT_JS_ZIP;
|
||||
|
||||
if (strcmp(file_name + 8, "js_pack.js") == 0) {
|
||||
total_sz = sizeof(js_pack);
|
||||
file_data = js_pack;
|
||||
@@ -133,34 +145,53 @@ static int HttpGetAssets(httpd_request_t *req) {
|
||||
total_sz = sizeof(css_pack);
|
||||
file_data = css_pack;
|
||||
content_type = HTTP_CONTENT_CSS_ZIP;
|
||||
} else if (strcmp(file_name + 8, "index.html") == 0) {
|
||||
total_sz = sizeof(web_index_html);
|
||||
file_data = web_index_html;
|
||||
content_type = HTTP_CONTENT_HTML_ZIP;
|
||||
}
|
||||
|
||||
if (total_sz == 0 || file_data == NULL) {
|
||||
http_log("File not found: %s", req->filename);
|
||||
return err;
|
||||
}
|
||||
|
||||
if (total_sz == 0) return err;
|
||||
|
||||
err = httpd_send_all_header(req, HTTP_RES_200, total_sz, content_type);
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send http assets headers."));
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send asset headers."));
|
||||
|
||||
err = httpd_send_body(req->sock, file_data, total_sz);
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send http assets body."));
|
||||
err = send_in_chunks(req->sock, file_data, total_sz);
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send asset body."));
|
||||
|
||||
exit:
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpGetTc1Status(httpd_request_t *req) {
|
||||
char *sockets = GetSocketStatus();
|
||||
char *tc1_status = malloc(512);
|
||||
char *short_click_config = GetButtonClickConfig();
|
||||
char *tc1_status = malloc(1500);
|
||||
char *socket_names = malloc(512);
|
||||
sprintf(socket_names, "%s,%s,%s,%s,%s,%s",
|
||||
user_config->socket_names[0],
|
||||
user_config->socket_names[1],
|
||||
user_config->socket_names[2],
|
||||
user_config->socket_names[3],
|
||||
user_config->socket_names[4],
|
||||
user_config->socket_names[5]);
|
||||
sprintf(tc1_status, TC1_STATUS_JSON, sockets, ip_status.mode,
|
||||
sys_config->micoSystemConfig.ssid, sys_config->micoSystemConfig.user_key,
|
||||
user_config->ap_name, user_config->ap_key, MQTT_SERVER, MQTT_SERVER_PORT,
|
||||
MQTT_SERVER_USR, MQTT_SERVER_PWD,
|
||||
VERSION, ip_status.ip, ip_status.mask, ip_status.gateway, user_config->mqtt_report_freq,
|
||||
user_config->power_led_enabled, 0L);
|
||||
user_config->power_led_enabled, 0L, socket_names, childLockEnabled,
|
||||
sys_config->micoSystemConfig.name, short_click_config);
|
||||
|
||||
OSStatus err = kNoErr;
|
||||
send_http(tc1_status, strlen(tc1_status), exit, &err);
|
||||
|
||||
exit:
|
||||
if (socket_names) free(socket_names);
|
||||
if (tc1_status) free(tc1_status);
|
||||
return err;
|
||||
}
|
||||
@@ -183,6 +214,179 @@ static int HttpSetSocketStatus(httpd_request_t *req) {
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpSetSocketName(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
int buf_size = 70;
|
||||
char *buf = malloc(buf_size);
|
||||
|
||||
err = httpd_get_data(req, buf, buf_size);
|
||||
require_noerr(err, exit);
|
||||
int index;
|
||||
char name[64];
|
||||
sscanf(buf, "%d %s", &index, name);
|
||||
strcpy(user_config->socket_names[index], name);
|
||||
mico_system_context_update(sys_config);
|
||||
registerMqttEvents();
|
||||
send_http("OK", 2, exit, &err);
|
||||
|
||||
exit:
|
||||
if (buf) free(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpSetButtonEvent(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
int buf_size = 10;
|
||||
char *buf = malloc(buf_size);
|
||||
|
||||
err = httpd_get_data(req, buf, buf_size);
|
||||
require_noerr(err, exit);
|
||||
int index;
|
||||
int func;
|
||||
int longPress;
|
||||
sscanf(buf, "%d %d %d", &index, &func, &longPress);
|
||||
if (longPress == 1) {
|
||||
set_key_map(user_config->user,index, get_short_func(user_config->user[index]), func == -1 ? NO_FUNCTION : func);
|
||||
} else {
|
||||
set_key_map(user_config->user,index, func == -1 ? NO_FUNCTION : func, get_long_func(user_config->user[index]));
|
||||
}
|
||||
key_log("WARNGIN:set KEY func %d %d %d", index,get_short_func(user_config->user[index]),get_long_func(user_config->user[index]));
|
||||
mico_system_context_update(sys_config);
|
||||
|
||||
send_http("OK", 2, exit, &err);
|
||||
|
||||
exit:
|
||||
if (buf) free(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
#define OTA_BUF_SIZE 5120
|
||||
|
||||
static int HttpSetOTAFile(httpd_request_t *req)
|
||||
{
|
||||
tc1_log("[OTA] hdr_parsed=%d, remaining=%d, body_nbytes=%d, req.chunked=%d",
|
||||
req->hdr_parsed, req->remaining_bytes, req->body_nbytes, req->chunked);
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
int total = 0;
|
||||
int ret = 0;
|
||||
|
||||
// req->chunked = 1;
|
||||
|
||||
int total1 = req->remaining_bytes;
|
||||
char *buffer = malloc(OTA_BUF_SIZE);
|
||||
if (!buffer) return kNoMemoryErr;
|
||||
uint32_t offset = 0;
|
||||
|
||||
mico_logic_partition_t* ota_partition = MicoFlashGetInfo(MICO_PARTITION_OTA_TEMP);
|
||||
MicoFlashErase(MICO_PARTITION_OTA_TEMP, 0x0, ota_partition->partition_length);
|
||||
CRC16_Context crc_context;
|
||||
CRC16_Init(&crc_context);
|
||||
// 尝试读取全部 POST 数据
|
||||
while (1) {
|
||||
ret = httpd_get_data2(req, buffer,OTA_BUF_SIZE);
|
||||
|
||||
// ret = httpd_recv(req->sock, buffer, 128, 0);
|
||||
total += ret;
|
||||
// req->remaining_bytes -= ret;
|
||||
|
||||
if (ret > 0) {
|
||||
CRC16_Update(&crc_context, buffer, ret);
|
||||
err = MicoFlashWrite(MICO_PARTITION_OTA_TEMP, &offset, (uint8_t *)buffer, ret);
|
||||
require_noerr_quiet(err, exit);
|
||||
tc1_log("[OTA] 本次读取 %d 字节,累计 %d 字节", ret, total);
|
||||
}
|
||||
|
||||
if (ret == 0 || req->remaining_bytes <= 0) {
|
||||
// 读取完毕
|
||||
tc1_log("[OTA] 数据读取完成, 总计 %d 字节", total);
|
||||
break;
|
||||
} else if (ret < 0) {
|
||||
tc1_log("[OTA] 数据读取失败, ret=%d", ret);
|
||||
err = kConnectionErr;
|
||||
break;
|
||||
}
|
||||
|
||||
mico_rtos_thread_msleep(100);
|
||||
|
||||
// tc1_log("[OTA] %x", buffer);
|
||||
// tc1_log("[OTA] hdr_parsed=%d, remaining=%d, body_nbytes=%d",
|
||||
// req->hdr_parsed, req->remaining_bytes, req->body_nbytes);
|
||||
}
|
||||
// if (buffer) free(buffer);
|
||||
uint16_t crc16;
|
||||
CRC16_Final(&crc_context, &crc16);
|
||||
|
||||
|
||||
err = mico_ota_switch_to_new_fw(total, crc16);
|
||||
tc1_log("[OTA] mico_ota_switch_to_new_fw err=%d", err);
|
||||
require_noerr(err, exit);
|
||||
|
||||
char resp[128];
|
||||
snprintf(resp, sizeof(resp), "OK, total: %d bytes, req %d %d", total, req->body_nbytes, total1);
|
||||
send_http(resp, strlen(resp), exit, &err);
|
||||
|
||||
mico_system_power_perform(mico_system_context_get(), eState_Software_Reset);
|
||||
exit:
|
||||
if (buffer) free(buffer);
|
||||
return err;
|
||||
|
||||
// ota_file_req = req;
|
||||
|
||||
// OSStatus err = kNoErr;
|
||||
// err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "OtaFileThread", OtaFileThread, 0x1000, 0);
|
||||
// char buf[16] = {0};
|
||||
// sprintf(buf, "%d", sizeof(ota_file_req));
|
||||
// send_http(buf, strlen(buf), exit, &err);
|
||||
|
||||
// exit:
|
||||
// if (buf) free(buf);
|
||||
// return err;
|
||||
}
|
||||
|
||||
static int HttpSetDeviceName(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
int buf_size = 70;
|
||||
char *buf = malloc(buf_size);
|
||||
|
||||
err = httpd_get_data(req, buf, buf_size);
|
||||
require_noerr(err, exit);
|
||||
char name[64];
|
||||
sscanf(buf, "%s", name);
|
||||
strcpy(sys_config->micoSystemConfig.name, name);
|
||||
mico_system_context_update(sys_config);
|
||||
registerMqttEvents();
|
||||
send_http("OK", 2, exit, &err);
|
||||
|
||||
exit:
|
||||
if (buf) free(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpSetChildLock(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
int buf_size = 32;
|
||||
char *buf = malloc(buf_size);
|
||||
|
||||
err = httpd_get_data(req, buf, buf_size);
|
||||
require_noerr(err, exit);
|
||||
int enableLock;
|
||||
sscanf(buf, "%d", &enableLock);
|
||||
user_config->user[0] = enableLock;
|
||||
childLockEnabled = enableLock;
|
||||
mico_system_context_update(sys_config);
|
||||
UserMqttSendChildLockState();
|
||||
send_http("OK", 2, exit, &err);
|
||||
|
||||
exit:
|
||||
if (buf) free(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpGetPowerInfo(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
char buf[16];
|
||||
@@ -204,9 +408,21 @@ static int HttpGetPowerInfo(httpd_request_t *req) {
|
||||
|
||||
char *powers = GetPowerRecord(idx);
|
||||
char *sockets = GetSocketStatus();
|
||||
char *short_click_config = GetButtonClickConfig();
|
||||
char *socket_names = malloc(512);
|
||||
sprintf(socket_names, "%s,%s,%s,%s,%s,%s",
|
||||
user_config->socket_names[0],
|
||||
user_config->socket_names[1],
|
||||
user_config->socket_names[2],
|
||||
user_config->socket_names[3],
|
||||
user_config->socket_names[4],
|
||||
user_config->socket_names[5]);
|
||||
sprintf(power_info_json, POWER_INFO_JSON, sockets, power_record.idx, PW_NUM, p_count, powers,
|
||||
up_time,user_config->power_led_enabled);
|
||||
up_time, user_config->power_led_enabled, RelayOut() ? 1 : 0, socket_names,
|
||||
user_config->p_count_1_day_ago, user_config->p_count_2_days_ago, childLockEnabled,
|
||||
sys_config->micoSystemConfig.name, short_click_config);
|
||||
send_http(power_info_json, strlen(power_info_json), exit, &err);
|
||||
if (socket_names) free(socket_names);
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
@@ -219,19 +435,62 @@ static int HttpGetWifiConfig(httpd_request_t *req) {
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
// 单个十六进制字符转数字(安全)
|
||||
static int hex_char_to_int(char c) {
|
||||
if ('0' <= c && c <= '9') return c - '0';
|
||||
if ('a' <= c && c <= 'f') return c - 'a' + 10;
|
||||
if ('A' <= c && c <= 'F') return c - 'A' + 10;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 健壮版 URL 解码函数
|
||||
void url_decode(const char *src, char *dest, size_t max_len) {
|
||||
size_t i = 0;
|
||||
while (*src && i < max_len - 1) {
|
||||
if (*src == '%') {
|
||||
if (isxdigit((unsigned char)src[1]) && isxdigit((unsigned char)src[2])) {
|
||||
int high = hex_char_to_int(src[1]);
|
||||
int low = hex_char_to_int(src[2]);
|
||||
if (high >= 0 && low >= 0) {
|
||||
dest[i++] = (char)((high << 4) | low);
|
||||
src += 3;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// 非法编码,跳过 %
|
||||
src++;
|
||||
} else if (*src == '+') {
|
||||
dest[i++] = ' ';
|
||||
src++;
|
||||
} else {
|
||||
dest[i++] = *src++;
|
||||
}
|
||||
}
|
||||
dest[i] = '\0';
|
||||
}
|
||||
|
||||
static int HttpSetWifiConfig(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
int buf_size = 97;
|
||||
char *buf = malloc(buf_size);
|
||||
int mode = -1;
|
||||
char *wifi_ssid = malloc(32);
|
||||
char *wifi_key = malloc(32);
|
||||
char *buf = malloc(256);
|
||||
char *ssid_enc = malloc(128);
|
||||
char *key_enc = malloc(128);
|
||||
char *wifi_ssid = malloc(128);
|
||||
char *wifi_key = malloc(128);
|
||||
int mode = -1;
|
||||
|
||||
err = httpd_get_data(req, buf, buf_size);
|
||||
|
||||
|
||||
err = httpd_get_data(req, buf, 256);
|
||||
require_noerr(err, exit);
|
||||
|
||||
sscanf(buf, "%d %s %s", &mode, wifi_ssid, wifi_key);
|
||||
// 假设 httpd_get_data(req, buf, 256);
|
||||
// tc1_log("wifi config %s",buf);
|
||||
sscanf(buf, "%d %s %s", &mode, ssid_enc, key_enc);
|
||||
// tc1_log("wifi config %s %s",ssid_enc,key_enc);
|
||||
url_decode(ssid_enc, wifi_ssid,128);
|
||||
url_decode(key_enc, wifi_key,128);
|
||||
// tc1_log("wifi config decode %s %s",wifi_ssid,wifi_key);
|
||||
if (mode == 1) {
|
||||
WifiConnect(wifi_ssid, wifi_key);
|
||||
} else {
|
||||
@@ -291,7 +550,10 @@ static int HttpSetMqttConfig(httpd_request_t *req) {
|
||||
|
||||
sscanf(buf, "%s %d %s %s", MQTT_SERVER, &MQTT_SERVER_PORT, MQTT_SERVER_USR, MQTT_SERVER_PWD);
|
||||
mico_system_context_update(sys_config);
|
||||
|
||||
if (!(MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1)){
|
||||
err = UserMqttInit();
|
||||
require_noerr(err, exit);
|
||||
}
|
||||
send_http("OK", 2, exit, &err);
|
||||
|
||||
exit:
|
||||
@@ -328,6 +590,7 @@ static int HttpGetMqttReportFreq(httpd_request_t *req) {
|
||||
send_http(freq, strlen(freq), exit, &err);
|
||||
|
||||
exit:
|
||||
if (freq) free(freq);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -350,12 +613,21 @@ static int HttpGetTasks(httpd_request_t *req) {
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpGetButtonEvents(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
char *clicks = GetButtonClickConfig();
|
||||
send_http(clicks, strlen(clicks), exit, &err);
|
||||
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpAddTask(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
//1577369623 4 0
|
||||
char buf[16] = {0};
|
||||
err = httpd_get_data(req, buf, 16);
|
||||
char buf[20] = {0};
|
||||
err = httpd_get_data(req, buf, 20);
|
||||
require_noerr(err, exit);
|
||||
|
||||
pTimedTask task = NewTask();
|
||||
@@ -364,20 +636,19 @@ static int HttpAddTask(httpd_request_t *req) {
|
||||
send_http(mess, strlen(mess), exit, &err);
|
||||
return err;
|
||||
}
|
||||
int re = sscanf(buf, "%ld %d %d %d", &task->prs_time, &task->socket_idx, &task->on,
|
||||
int re = sscanf(buf, "%ld %d %d %d", &task->prs_time, &task->operation, &task->on,
|
||||
&task->weekday);http_log("AddTask buf[%s] re[%d] (%ld %d %d %d)",
|
||||
buf, re, task->prs_time, task->socket_idx, task->on,
|
||||
buf, re, task->prs_time, task->operation, task->on,
|
||||
task->weekday);
|
||||
task->socket_idx--;
|
||||
if (task->prs_time < 1577428136 || task->prs_time > 9577428136
|
||||
|| task->socket_idx < 0 || task->socket_idx > 5
|
||||
|| (task->on != 0 && task->on != 1)) { http_log("AddTask Error!");
|
||||
|| task->operation < 0 || task->operation > 11) { http_log("AddTask Error!");
|
||||
re = 0;
|
||||
}
|
||||
|
||||
char *mess = (re == 4 && AddTask(task)) ? "OK" : "NO";
|
||||
|
||||
send_http(mess, strlen(mess), exit, &err);
|
||||
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
@@ -409,6 +680,7 @@ static int LedStatus(httpd_request_t *req) {
|
||||
send_http(led, strlen(led), exit, &err);
|
||||
|
||||
exit:
|
||||
if (led) free(led);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -427,6 +699,7 @@ static int LedSetEnabled(httpd_request_t *req) {
|
||||
} else {
|
||||
UserLedSet(0);
|
||||
}
|
||||
UserMqttSendLedState();
|
||||
mico_system_context_update(sys_config);
|
||||
|
||||
send_http("OK", 2, exit, &err);
|
||||
@@ -436,6 +709,30 @@ static int LedSetEnabled(httpd_request_t *req) {
|
||||
return err;
|
||||
}
|
||||
|
||||
static int TotalSocketSetEnabled(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
int buf_size = 97;
|
||||
int on;
|
||||
char *buf = malloc(buf_size);
|
||||
|
||||
err = httpd_get_data(req, buf, buf_size);
|
||||
require_noerr(err, exit);
|
||||
|
||||
sscanf(buf, "%d", &on);
|
||||
UserRelaySetAll(on);
|
||||
int i = 0;
|
||||
for (; i < SOCKET_NUM; i++) {
|
||||
UserMqttSendSocketState(i);
|
||||
}
|
||||
UserMqttSendTotalSocketState();
|
||||
send_http("OK", 2, exit, &err);
|
||||
|
||||
exit:
|
||||
if (buf) free(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
static int Otastatus(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
char buf[16] = {0};
|
||||
@@ -461,7 +758,6 @@ static int OtaStart(httpd_request_t *req) {
|
||||
|
||||
const struct httpd_wsgi_call g_app_handlers[] = {
|
||||
{"/", HTTPD_HDR_DEFORT, 0, HttpGetIndexPage, NULL, NULL, NULL},
|
||||
{"/demo", HTTPD_HDR_DEFORT, 0, HttpGetDemoPage, NULL, NULL, NULL},
|
||||
{"/assets", HTTPD_HDR_ADD_SERVER |
|
||||
HTTPD_HDR_ADD_CONN_CLOSE, APP_HTTP_FLAGS_NO_EXACT_MATCH, HttpGetAssets, NULL, NULL, NULL},
|
||||
{"/socket", HTTPD_HDR_DEFORT, 0, NULL, HttpSetSocketStatus, NULL, NULL},
|
||||
@@ -476,6 +772,12 @@ const struct httpd_wsgi_call g_app_handlers[] = {
|
||||
{"/task", HTTPD_HDR_DEFORT, APP_HTTP_FLAGS_NO_EXACT_MATCH, HttpGetTasks, HttpAddTask, NULL, HttpDelTask},
|
||||
{"/ota", HTTPD_HDR_DEFORT, 0, Otastatus, OtaStart, NULL, NULL},
|
||||
{"/led", HTTPD_HDR_DEFORT, 0, LedStatus, LedSetEnabled, NULL, NULL},
|
||||
{"/socketAll", HTTPD_HDR_DEFORT, 0, NULL, TotalSocketSetEnabled, NULL, NULL},
|
||||
{"/socketNames", HTTPD_HDR_DEFORT, 0, NULL, HttpSetSocketName, NULL, NULL},
|
||||
{"/childLock", HTTPD_HDR_DEFORT, 0, NULL, HttpSetChildLock, NULL, NULL},
|
||||
{"/deviceName", HTTPD_HDR_DEFORT, 0, NULL, HttpSetDeviceName, NULL, NULL},
|
||||
{"/buttonEvents", HTTPD_HDR_DEFORT, 0, HttpGetButtonEvents, HttpSetButtonEvent, NULL, NULL},
|
||||
{"/ota/fileUpload", HTTPD_HDR_DEFORT, 0, NULL, HttpSetOTAFile, NULL, NULL},
|
||||
};
|
||||
|
||||
static int g_app_handlers_no = sizeof(g_app_handlers) / sizeof(struct httpd_wsgi_call);
|
||||
|
||||
@@ -64,12 +64,16 @@
|
||||
'gateway':'%s',\
|
||||
'reportFreq':'%d',\
|
||||
'ledEnabled':%d,\
|
||||
'up_time':%ld\
|
||||
'up_time':%ld,\
|
||||
'socketNames':'%s',\
|
||||
'child_lock_enabled':%d,\
|
||||
'deviceName':'%s',\
|
||||
'btnClicks':%s\
|
||||
}"
|
||||
|
||||
#define POWER_INFO_JSON "{'sockets':'%s','idx':%d,'len':%d,'p_count':%ld,'powers':[%s],'up_time':'%s','led_enabled':%d}"
|
||||
#define POWER_INFO_JSON "{'sockets':'%s','idx':%d,'len':%d,'p_count':%ld,'powers':[%s],'up_time':'%s','led_enabled':%d,'total_switch_on':%d,'socketNames':'%s','p_count_1_day_ago':%d,'p_count_2_days_ago':%d,'child_lock_enabled':%d,'deviceName':'%s','btnClicks':%s}"
|
||||
|
||||
int AppHttpdStart(void);
|
||||
extern int AppHttpdStart(void);
|
||||
|
||||
int AppHttpdStop();
|
||||
extern int AppHttpdStop();
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -2,6 +2,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include "mico.h"
|
||||
|
||||
|
||||
#include"http_server/web_log.h"
|
||||
|
||||
@@ -36,6 +38,9 @@ char* GetLogRecord()
|
||||
tmp += strlen(tmp);
|
||||
if (!log_record.logs[i%LOG_NUM]) continue;
|
||||
sprintf(tmp, "%s\n", log_record.logs[i%LOG_NUM]);
|
||||
if(i == log_record.idx){
|
||||
sprintf(tmp, "%s\nFreeMem %d bytes\n",log_record.logs[i%LOG_NUM],MicoGetMemoryInfo()->free_memory);
|
||||
}
|
||||
}
|
||||
return log_record_str;
|
||||
}
|
||||
|
||||
84
TC1/main.c
84
TC1/main.c
@@ -4,18 +4,20 @@
|
||||
#include "time.h"
|
||||
#include "unistd.h"
|
||||
#include "TimeUtils.h"
|
||||
#include "mico_system.h"
|
||||
|
||||
#include "user_gpio.h"
|
||||
#include "user_wifi.h"
|
||||
#include "time_server/user_rtc.h"
|
||||
#include "user_power.h"
|
||||
#include "mqtt_server/user_mqtt_client.h"
|
||||
#include "http_server/app_httpd.h"
|
||||
#include "timed_task/timed_task.h"
|
||||
|
||||
char rtc_init = 0; //sntp校时成功标志位
|
||||
uint32_t total_time = 0;
|
||||
char str_mac[16] = {0};
|
||||
int last_check_day = 0;
|
||||
int childLockEnabled = 0;
|
||||
|
||||
system_config_t *sys_config;
|
||||
user_config_t *user_config;
|
||||
@@ -42,12 +44,23 @@ void appRestoreDefault_callback(void *const user_config_data, uint32_t size) {
|
||||
userConfigDefault->p_count_1_day_ago = 0;
|
||||
userConfigDefault->power_led_enabled = 1;
|
||||
userConfigDefault->version = USER_CONFIG_VERSION;
|
||||
|
||||
int i;
|
||||
for (i = 0; i < SOCKET_NUM; i++) {
|
||||
userConfigDefault->socket_status[i] = 1;
|
||||
set_key_map(userConfigDefault->user,1, SWITCH_ALL_SOCKETS, NO_FUNCTION);
|
||||
for (int i = 2; i < 32; i++) {
|
||||
int longFunc = NO_FUNCTION;
|
||||
//出厂设置,长按5秒开启配网模式,长按10秒恢复出厂设置
|
||||
if (i >=5 && i< 10) {
|
||||
longFunc = CONFIG_WIFI;
|
||||
} else if (i >= 10 && i< 15) {
|
||||
longFunc = RESET_SYSTEM;
|
||||
}
|
||||
set_key_map(userConfigDefault->user,i, NO_FUNCTION, longFunc);
|
||||
}
|
||||
for (i = 0; i < MAX_TASK_NUM; i++) {
|
||||
|
||||
for (int i = 0; i < SOCKET_NUM; i++) {
|
||||
userConfigDefault->socket_status[i] = 1;
|
||||
snprintf(userConfigDefault->socket_names[i], SOCKET_NAME_LENGTH, "插座-%d", i + 1);
|
||||
}
|
||||
for (int i = 0; i < MAX_TASK_NUM; i++) {
|
||||
userConfigDefault->timed_tasks[i].on_use = false;
|
||||
}
|
||||
mico_system_context_update(sys_config);
|
||||
@@ -55,19 +68,38 @@ void appRestoreDefault_callback(void *const user_config_data, uint32_t size) {
|
||||
|
||||
void recordDailyPCount() {
|
||||
// 获取当前时间
|
||||
time_t now;
|
||||
time(&now);
|
||||
struct tm *current_time = localtime(&now);
|
||||
if (current_time->tm_hour != 0 || current_time->tm_min != 0) {
|
||||
return;
|
||||
mico_utc_time_t utc_time;
|
||||
mico_time_get_utc_time(&utc_time);
|
||||
utc_time += 28800;
|
||||
struct tm *current_time = localtime((const time_t *) &utc_time);
|
||||
// 判断上次检查的时间与当前时间的日期是否不同
|
||||
if (last_check_day != 0) {
|
||||
// 如果日期发生变化(即跨天了),则进行记录
|
||||
if (current_time->tm_mday != last_check_day) { tc1_log(
|
||||
"WARNGIN: pcount day changed! now day %d hour %d min %d ,lastCheck day %d",
|
||||
current_time->tm_mday, current_time->tm_hour, current_time->tm_min,
|
||||
last_check_day);
|
||||
|
||||
// tc1_log("WARNGIN: pcount day changed! ");
|
||||
// 记录数据
|
||||
if (user_config->p_count_1_day_ago != 0) {
|
||||
user_config->p_count_2_days_ago = user_config->p_count_1_day_ago;
|
||||
}
|
||||
user_config->p_count_1_day_ago = p_count;
|
||||
|
||||
// 更新系统配置
|
||||
mico_system_context_update(sys_config);
|
||||
|
||||
tc1_log("WARNGIN: p_count record! p_count_1_day_ago:%d p_count_2_days_ago:%d",
|
||||
user_config->p_count_1_day_ago, user_config->p_count_2_days_ago);
|
||||
} else {
|
||||
// tc1_log("WARNGIN: pcount day not changed , waiting for next run! ");
|
||||
}
|
||||
} else { tc1_log("WARNGIN: now day %d hour %d min %d ,lastCheck day %d", current_time->tm_mday,
|
||||
current_time->tm_hour, current_time->tm_min, last_check_day);
|
||||
}
|
||||
if (user_config->p_count_1_day_ago != 0) {
|
||||
user_config->p_count_2_days_ago = user_config->p_count_1_day_ago;
|
||||
}
|
||||
user_config->p_count_1_day_ago = p_count;
|
||||
mico_system_context_update(sys_config);tc1_log(
|
||||
"WARNGIN: p_count record! p_count_1_day_ago:%d p_count_2_days_ago:%d",
|
||||
user_config->p_count_1_day_ago, user_config->p_count_2_days_ago);
|
||||
// 更新上次检查时间
|
||||
last_check_day = current_time->tm_mday;
|
||||
}
|
||||
|
||||
void schedule_p_count_task(mico_thread_arg_t arg) {
|
||||
@@ -105,8 +137,6 @@ int application_start(void) {
|
||||
err = mico_system_init(sys_config);
|
||||
require_noerr(err, exit);
|
||||
|
||||
p_count = user_config->p_count_1_day_ago;
|
||||
|
||||
uint8_t mac[8];
|
||||
mico_wlan_get_mac_address(mac);
|
||||
sprintf(str_mac, "%02X%02X%02X%02X%02X%02X",
|
||||
@@ -116,7 +146,7 @@ int application_start(void) {
|
||||
MicoGpioInitialize((mico_gpio_t) Button, INPUT_PULL_UP);
|
||||
if (!MicoGpioInputGet(Button)) { //开机时按钮状态
|
||||
tc1_log("press ap_init");
|
||||
ApInit(false);
|
||||
ApInit(true);
|
||||
open_ap = true;
|
||||
}
|
||||
|
||||
@@ -127,6 +157,7 @@ int application_start(void) {
|
||||
}
|
||||
MicoSysLed(0);
|
||||
|
||||
childLockEnabled = (int) user_config->user[0];
|
||||
if (user_config->version != USER_CONFIG_VERSION) { tc1_log("WARNGIN: user params restored!");
|
||||
err = mico_system_context_restore(sys_config);
|
||||
require_noerr(err, exit);
|
||||
@@ -136,8 +167,8 @@ int application_start(void) {
|
||||
sprintf(sys_config->micoSystemConfig.name, ZTC1_NAME, str_mac + 8);
|
||||
}
|
||||
|
||||
tc1_log("user:%s", user_config->user);tc1_log("device name:%s",
|
||||
sys_config->micoSystemConfig.name);tc1_log(
|
||||
tc1_log("device name:%s",
|
||||
sys_config->micoSystemConfig.name);tc1_log(
|
||||
"mqtt_ip:%s", user_config->mqtt_ip);tc1_log("mqtt_port:%d",
|
||||
user_config->mqtt_port);tc1_log(
|
||||
"mqtt_user:%s", user_config->mqtt_user);
|
||||
@@ -154,8 +185,6 @@ int application_start(void) {
|
||||
}
|
||||
}
|
||||
KeyInit();
|
||||
err = UserMqttInit();
|
||||
require_noerr(err, exit);
|
||||
err = UserRtcInit();
|
||||
require_noerr(err, exit);
|
||||
PowerInit();
|
||||
@@ -165,12 +194,12 @@ int application_start(void) {
|
||||
|
||||
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "p_count",
|
||||
(mico_thread_function_t) schedule_p_count_task,
|
||||
0x2000, 0);
|
||||
0x800, 0);
|
||||
require_noerr_string(err, exit, "ERROR: Unable to start the p_count thread.");
|
||||
|
||||
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mqtt_power_report",
|
||||
(mico_thread_function_t) reportMqttPowerInfoThread,
|
||||
0x2000, 0);
|
||||
0x800, 0);
|
||||
require_noerr_string(err, exit, "ERROR: Unable to start the mqtt_power_report thread.");
|
||||
|
||||
|
||||
@@ -179,7 +208,6 @@ int application_start(void) {
|
||||
if (user_config->task_top && now >= user_config->task_top->prs_time) {
|
||||
ProcessTask();
|
||||
}
|
||||
|
||||
mico_thread_msleep(1000);
|
||||
}
|
||||
|
||||
|
||||
14
TC1/main.h
14
TC1/main.h
@@ -16,17 +16,17 @@
|
||||
#define wifi_log(M, ...) custom_log("WIFI", M, ##__VA_ARGS__); web_log("WIFI", M, ##__VA_ARGS__);
|
||||
#define power_log(M, ...) custom_log("POWER", M, ##__VA_ARGS__); web_log("POWER", M, ##__VA_ARGS__);
|
||||
|
||||
#define VERSION "v2.1.6"
|
||||
#define VERSION "v2.2.0"
|
||||
|
||||
#define TYPE 1
|
||||
#define TYPE_NAME "zTC1"
|
||||
#define TYPE_NAME "TC1"
|
||||
|
||||
#define ZTC1_NAME "zTC1-%s"
|
||||
#define ZTC1_NAME "TC1-%s"
|
||||
|
||||
#define USER_CONFIG_VERSION 8
|
||||
#define USER_CONFIG_VERSION 9
|
||||
#define SETTING_MQTT_STRING_LENGTH_MAX 32 //必须4字节对齐。
|
||||
|
||||
#define SOCKET_NAME_LENGTH 32
|
||||
#define SOCKET_NAME_LENGTH 64
|
||||
#define SOCKET_NUM 6 //插座数量
|
||||
|
||||
#define Led MICO_GPIO_5
|
||||
@@ -35,6 +35,7 @@
|
||||
|
||||
#define Relay_ON 1
|
||||
#define Relay_OFF 0
|
||||
#define Relay_TOGGLE -1
|
||||
|
||||
#define Relay_0 MICO_GPIO_6
|
||||
#define Relay_1 MICO_GPIO_8
|
||||
@@ -51,6 +52,7 @@ typedef struct
|
||||
{
|
||||
char version;
|
||||
char mqtt_ip[SETTING_MQTT_STRING_LENGTH_MAX];
|
||||
char socket_names[SOCKET_NUM][SOCKET_NAME_LENGTH];
|
||||
int mqtt_port;
|
||||
int mqtt_report_freq;
|
||||
char mqtt_user[SETTING_MQTT_STRING_LENGTH_MAX];
|
||||
@@ -74,5 +76,7 @@ extern char str_mac[16];
|
||||
extern system_config_t* sys_config;
|
||||
extern user_config_t* user_config;
|
||||
extern mico_gpio_t Relay[Relay_NUM];
|
||||
extern int childLockEnabled;
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -27,8 +27,7 @@
|
||||
#include "user_power.h"
|
||||
#include "user_mqtt_client.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
char topic[MAX_MQTT_TOPIC_SIZE];
|
||||
char qos;
|
||||
char retained;
|
||||
@@ -38,17 +37,23 @@ typedef struct
|
||||
} mqtt_recv_msg_t, *p_mqtt_recv_msg_t, mqtt_send_msg_t, *p_mqtt_send_msg_t;
|
||||
|
||||
static void MqttClientThread(mico_thread_arg_t arg);
|
||||
static void MessageArrived(MessageData* md);
|
||||
static OSStatus MqttMsgPublish(Client *c, const char* topic, char qos, char retained, const unsigned char* msg, uint32_t msg_len);
|
||||
|
||||
static void MessageArrived(MessageData *md);
|
||||
|
||||
static OSStatus
|
||||
MqttMsgPublish(Client *c, const char *topic, char qos, char retained, const unsigned char *msg,
|
||||
uint32_t msg_len);
|
||||
|
||||
OSStatus UserRecvHandler(void *arg);
|
||||
void ProcessHaCmd(char* cmd);
|
||||
|
||||
void ProcessHaCmd(char *cmd);
|
||||
|
||||
bool isconnect = false;
|
||||
mico_queue_t mqtt_msg_send_queue = NULL;
|
||||
|
||||
Client c; // mqtt client object
|
||||
Network n; // socket network for mqtt client
|
||||
volatile bool mqtt_thread_should_exit = false;
|
||||
|
||||
static mico_worker_thread_t mqtt_client_worker_thread; /* Worker thread to manage send/recv events */
|
||||
//static mico_timed_event_t mqtt_client_send_event;
|
||||
@@ -58,92 +63,135 @@ char topic_set[MAX_MQTT_TOPIC_SIZE];
|
||||
|
||||
mico_timer_t timer_handle;
|
||||
static char timer_status = 0;
|
||||
void UserMqttTimerFunc(void *arg)
|
||||
{
|
||||
|
||||
void UserMqttTimerFunc(void *arg) {
|
||||
LinkStatusTypeDef LinkStatus;
|
||||
micoWlanGetLinkStatus(&LinkStatus);
|
||||
if (LinkStatus.is_connected != 1)
|
||||
{
|
||||
if (LinkStatus.is_connected != 1) {
|
||||
mico_stop_timer(&timer_handle);
|
||||
return;
|
||||
}
|
||||
if (mico_rtos_is_queue_empty(&mqtt_msg_send_queue))
|
||||
{
|
||||
if (mico_rtos_is_queue_empty(&mqtt_msg_send_queue)) {
|
||||
|
||||
switch (timer_status) {
|
||||
case 0:
|
||||
UserMqttHassAutoLed();
|
||||
UserMqttHassAutoTotalSocket();
|
||||
UserMqttHassAutoChildLock();
|
||||
UserMqttHassAutoRebootButton();
|
||||
break;
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
case 4:
|
||||
case 5:
|
||||
case 6:
|
||||
UserMqttHassAuto(timer_status);
|
||||
break;
|
||||
case 7:
|
||||
UserMqttHassAutoPower();
|
||||
break;
|
||||
default:
|
||||
mico_stop_timer(&timer_handle);
|
||||
break;
|
||||
}
|
||||
timer_status++;
|
||||
switch (timer_status)
|
||||
{
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
case 4:
|
||||
case 5:
|
||||
case 6:
|
||||
UserMqttHassAuto(timer_status);
|
||||
break;
|
||||
case 7:
|
||||
UserMqttHassAutoPower();
|
||||
break;
|
||||
default:
|
||||
mico_stop_timer(&timer_handle);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
OSStatus UserMqttDeInit(void) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
// 1. 请求线程退出
|
||||
mqtt_thread_should_exit = true;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
void clear_mqtt_msg_send_queue(void) {
|
||||
if(mqtt_msg_send_queue == NULL){
|
||||
return;
|
||||
}
|
||||
void *msg = NULL;
|
||||
while (mico_rtos_is_queue_empty(&mqtt_msg_send_queue) == false) {
|
||||
if (mico_rtos_pop_from_queue(&mqtt_msg_send_queue, &msg, 0) == kNoErr) {
|
||||
if (msg) free(msg); // 释放消息内存,避免泄漏
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Application entrance */
|
||||
OSStatus UserMqttInit(void)
|
||||
{
|
||||
OSStatus UserMqttInit(void) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
if(mqtt_msg_send_queue != NULL)
|
||||
return err;
|
||||
sprintf(topic_set, MQTT_CLIENT_SUB_TOPIC1);
|
||||
sprintf(topic_state, MQTT_CLIENT_PUB_TOPIC, str_mac);
|
||||
//TODO size:0x800
|
||||
int mqtt_thread_stack_size = 0x2000;
|
||||
uint32_t mqtt_lib_version = MQTTClientLibVersion();
|
||||
mqtt_log("MQTT client version: [%ld.%ld.%ld]",
|
||||
0xFF & (mqtt_lib_version >> 16), 0xFF & (mqtt_lib_version >> 8), 0xFF & mqtt_lib_version);
|
||||
uint32_t mqtt_lib_version = MQTTClientLibVersion();mqtt_log(
|
||||
"MQTT client version: [%ld.%ld.%ld]",
|
||||
0xFF & (mqtt_lib_version >> 16), 0xFF & (mqtt_lib_version >> 8),
|
||||
0xFF & mqtt_lib_version);
|
||||
|
||||
/* create mqtt msg send queue */
|
||||
err = mico_rtos_init_queue(&mqtt_msg_send_queue, "mqtt_msg_send_queue", sizeof(p_mqtt_send_msg_t),
|
||||
MAX_MQTT_SEND_QUEUE_SIZE);
|
||||
err = mico_rtos_init_queue(&mqtt_msg_send_queue, "mqtt_msg_send_queue",
|
||||
sizeof(p_mqtt_send_msg_t),
|
||||
MAX_MQTT_SEND_QUEUE_SIZE);
|
||||
require_noerr_action(err, exit, mqtt_log("ERROR: create mqtt msg send queue err=%d.", err));
|
||||
|
||||
/* start mqtt client */
|
||||
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mqtt_client",
|
||||
(mico_thread_function_t) MqttClientThread,
|
||||
mqtt_thread_stack_size, 0);
|
||||
(mico_thread_function_t) MqttClientThread,
|
||||
mqtt_thread_stack_size, 0);
|
||||
require_noerr_string(err, exit, "ERROR: Unable to start the mqtt client thread.");
|
||||
|
||||
/* Create a worker thread for user handling MQTT data event */
|
||||
err = mico_rtos_create_worker_thread(&mqtt_client_worker_thread, MICO_APPLICATION_PRIORITY, 0x800, 5);
|
||||
err = mico_rtos_create_worker_thread(&mqtt_client_worker_thread, MICO_APPLICATION_PRIORITY,
|
||||
0x800, 5);
|
||||
require_noerr_string(err, exit, "ERROR: Unable to start the mqtt client worker thread.");
|
||||
|
||||
exit:
|
||||
if (kNoErr != err) mqtt_log("ERROR2, app thread exit err: %d kNoErr[%d]", err, kNoErr);
|
||||
if (kNoErr != err)mqtt_log("ERROR2, app thread exit err: %d kNoErr[%d]", err, kNoErr);
|
||||
return err;
|
||||
}
|
||||
|
||||
static OSStatus UserMqttClientRelease(Client *c, Network *n)
|
||||
{
|
||||
static OSStatus UserMqttClientRelease(Client *c, Network *n) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
if (c->isconnected) MQTTDisconnect(c);
|
||||
if (c == NULL || n == NULL) return kParamErr;
|
||||
|
||||
n->disconnect(n); // close connection
|
||||
if (c->isconnected) {
|
||||
MQTTDisconnect(c);
|
||||
c->isconnected = 0;
|
||||
}
|
||||
|
||||
if (MQTT_SUCCESS != MQTTClientDeinit(c))
|
||||
{
|
||||
if (c->buf) {
|
||||
free(c->buf);
|
||||
c->buf = NULL;
|
||||
}
|
||||
|
||||
if (c->readbuf) {
|
||||
free(c->readbuf);
|
||||
c->readbuf = NULL;
|
||||
}
|
||||
|
||||
if (n->disconnect) {
|
||||
n->disconnect(n);
|
||||
}
|
||||
|
||||
if (MQTT_SUCCESS != MQTTClientDeinit(c)) {
|
||||
mqtt_log("MQTTClientDeinit failed!");
|
||||
err = kDeletedErr;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
// publish msg to mqtt server
|
||||
static OSStatus MqttMsgPublish(Client *c, const char* topic, char qos, char retained,
|
||||
const unsigned char* msg,
|
||||
uint32_t msg_len)
|
||||
{
|
||||
static OSStatus MqttMsgPublish(Client *c, const char *topic, char qos, char retained,
|
||||
const unsigned char *msg,
|
||||
uint32_t msg_len) {
|
||||
OSStatus err = kUnknownErr;
|
||||
int ret = 0;
|
||||
MQTTMessage publishData = MQTTMessage_publishData_initializer;
|
||||
@@ -153,19 +201,16 @@ static OSStatus MqttMsgPublish(Client *c, const char* topic, char qos, char reta
|
||||
// upload data qos0
|
||||
publishData.qos = (enum QoS) qos;
|
||||
publishData.retained = retained;
|
||||
publishData.payload = (void*) msg;
|
||||
publishData.payload = (void *) msg;
|
||||
publishData.payloadlen = msg_len;
|
||||
|
||||
ret = MQTTPublish(c, topic, &publishData);
|
||||
|
||||
if (MQTT_SUCCESS == ret)
|
||||
{
|
||||
if (MQTT_SUCCESS == ret) {
|
||||
err = kNoErr;
|
||||
} else if (MQTT_SOCKET_ERR == ret)
|
||||
{
|
||||
} else if (MQTT_SOCKET_ERR == ret) {
|
||||
err = kConnectionErr;
|
||||
} else
|
||||
{
|
||||
} else {
|
||||
err = kUnknownErr;
|
||||
}
|
||||
|
||||
@@ -173,13 +218,20 @@ static OSStatus MqttMsgPublish(Client *c, const char* topic, char qos, char reta
|
||||
return err;
|
||||
}
|
||||
|
||||
void MqttClientThread(mico_thread_arg_t arg)
|
||||
{
|
||||
void registerMqttEvents(void) {
|
||||
if(timer_status !=0){
|
||||
mico_stop_timer(&timer_handle);
|
||||
}
|
||||
timer_status = 0;
|
||||
mico_start_timer(&timer_handle);
|
||||
}
|
||||
|
||||
void MqttClientThread(mico_thread_arg_t arg) {
|
||||
OSStatus err = kUnknownErr;
|
||||
|
||||
int rc = -1;
|
||||
fd_set readfds;
|
||||
struct timeval t = { 0, MQTT_YIELD_TMIE * 1000 };
|
||||
struct timeval t = {0, MQTT_YIELD_TMIE * 1000};
|
||||
|
||||
ssl_opts ssl_settings;
|
||||
MQTTPacket_connectData connectData = MQTTPacket_connectData_initializer;
|
||||
@@ -195,24 +247,26 @@ void MqttClientThread(mico_thread_arg_t arg)
|
||||
|
||||
/* create msg send queue event fd */
|
||||
msg_send_event_fd = mico_create_event_fd(mqtt_msg_send_queue);
|
||||
require_action(msg_send_event_fd >= 0, exit, mqtt_log("ERROR: create msg send queue event fd failed!!!"));
|
||||
mico_init_timer(&timer_handle, 150, UserMqttTimerFunc, &arg);
|
||||
|
||||
require_action(msg_send_event_fd >= 0, exit,
|
||||
mqtt_log("ERROR: create msg send queue event fd failed!!!"));
|
||||
mqtt_thread_should_exit = false;
|
||||
MQTT_start:
|
||||
|
||||
isconnect = false;
|
||||
/* 1. create network connection */
|
||||
ssl_settings.ssl_enable = false;
|
||||
LinkStatusTypeDef LinkStatus;
|
||||
while (1)
|
||||
{
|
||||
while (!mqtt_thread_should_exit) {
|
||||
isconnect = false;
|
||||
mico_rtos_thread_sleep(3);
|
||||
if (MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1) continue; //鏈厤缃甿qtt鏈嶅姟鍣ㄦ椂涓嶈繛鎺<E7B99B>
|
||||
if (MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1)
|
||||
continue; //鏈厤缃甿qtt鏈嶅姟鍣ㄦ椂涓嶈繛鎺<E7B99B>
|
||||
|
||||
micoWlanGetLinkStatus(&LinkStatus);
|
||||
if (LinkStatus.is_connected != 1)
|
||||
{
|
||||
mqtt_log("ERROR:WIFI not connect, waiting 3s for connecting and then connecting MQTT ");
|
||||
if (LinkStatus.is_connected != 1) { mqtt_log(
|
||||
"ERROR:WIFI not connect, waiting 3s for connecting and then connecting MQTT ");
|
||||
mico_rtos_thread_sleep(3);
|
||||
continue;
|
||||
}
|
||||
@@ -243,28 +297,29 @@ void MqttClientThread(mico_thread_arg_t arg)
|
||||
rc = MQTTConnect(&c, &connectData);
|
||||
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client connect err.");
|
||||
|
||||
mqtt_log("MQTT client connect success!");
|
||||
mqtt_log("MQTT client connect success, result: %d ", rc);
|
||||
|
||||
UserLedSet(user_config->power_led_enabled);
|
||||
UserLedSet(RelayOut() && user_config->power_led_enabled);
|
||||
|
||||
/* 4. mqtt client subscribe */
|
||||
rc = MQTTSubscribe(&c, topic_set, QOS0, MessageArrived);
|
||||
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client subscribe err.");
|
||||
mqtt_log("MQTT client subscribe success! recv_topic=[%s].", topic_set);
|
||||
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client subscribe err.");mqtt_log(
|
||||
"MQTT client subscribe success! recv_topic=[%s].", topic_set);
|
||||
/*4.1 杩炴帴鎴愬姛鍚庡厛鏇存柊鍙戦<E98D99>佷竴娆℃暟鎹<E69A9F>*/
|
||||
isconnect = true;
|
||||
|
||||
int i = 0;
|
||||
for (; i < SOCKET_NUM; i++)
|
||||
{
|
||||
for (; i < SOCKET_NUM; i++) {
|
||||
UserMqttSendSocketState(i);
|
||||
}
|
||||
|
||||
mico_init_timer(&timer_handle, 150, UserMqttTimerFunc, &arg);
|
||||
mico_start_timer(&timer_handle);
|
||||
UserMqttSendLedState();
|
||||
UserMqttSendTotalSocketState();
|
||||
UserMqttSendChildLockState();
|
||||
|
||||
registerMqttEvents();
|
||||
/* 5. client loop for recv msg && keepalive */
|
||||
while (1)
|
||||
{
|
||||
while (!mqtt_thread_should_exit) {
|
||||
isconnect = true;
|
||||
no_mqtt_msg_exchange = true;
|
||||
FD_ZERO(&readfds);
|
||||
@@ -273,39 +328,34 @@ void MqttClientThread(mico_thread_arg_t arg)
|
||||
select(msg_send_event_fd + 1, &readfds, NULL, NULL, &t);
|
||||
|
||||
/* recv msg from server */
|
||||
if (FD_ISSET(c.ipstack->my_socket, &readfds))
|
||||
{
|
||||
if (FD_ISSET(c.ipstack->my_socket, &readfds)) {
|
||||
rc = MQTTYield(&c, (int) MQTT_YIELD_TMIE);
|
||||
require_noerr(rc, MQTT_reconnect);
|
||||
no_mqtt_msg_exchange = false;
|
||||
}
|
||||
|
||||
/* recv msg from user worker thread to be sent to server */
|
||||
if (FD_ISSET(msg_send_event_fd, &readfds))
|
||||
{
|
||||
while (mico_rtos_is_queue_empty(&mqtt_msg_send_queue) == false)
|
||||
{
|
||||
if (FD_ISSET(msg_send_event_fd, &readfds)) {
|
||||
while (mico_rtos_is_queue_empty(&mqtt_msg_send_queue) == false) {
|
||||
// get msg from send queue
|
||||
mico_rtos_pop_from_queue(&mqtt_msg_send_queue, &p_send_msg, 0);
|
||||
require_string(p_send_msg, exit, "Wrong data point");
|
||||
|
||||
// send message to server
|
||||
err = MqttMsgPublish(&c, p_send_msg->topic, p_send_msg->qos, p_send_msg->retained,
|
||||
(const unsigned char*)p_send_msg->data,
|
||||
p_send_msg->datalen);
|
||||
|
||||
(const unsigned char *) p_send_msg->data,
|
||||
p_send_msg->datalen);
|
||||
free(p_send_msg);
|
||||
p_send_msg = NULL;
|
||||
require_noerr_string(err, MQTT_reconnect, "ERROR: MQTT publish data err");
|
||||
|
||||
//mqtt_log("MQTT publish data success! send_topic=[%s], msg=[%ld].", p_send_msg->topic, p_send_msg->datalen);
|
||||
no_mqtt_msg_exchange = false;
|
||||
free(p_send_msg);
|
||||
p_send_msg = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* if no msg exchange, we need to check ping msg to keep alive. */
|
||||
if (no_mqtt_msg_exchange)
|
||||
{
|
||||
if (no_mqtt_msg_exchange) {
|
||||
rc = keepalive(&c);
|
||||
require_noerr_string(rc, MQTT_reconnect, "ERROR: keepalive err");
|
||||
}
|
||||
@@ -313,10 +363,10 @@ void MqttClientThread(mico_thread_arg_t arg)
|
||||
|
||||
MQTT_reconnect:
|
||||
|
||||
mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err);
|
||||
|
||||
timer_status=100;
|
||||
mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err);
|
||||
|
||||
timer_status = 100;
|
||||
clear_mqtt_msg_send_queue();
|
||||
UserMqttClientRelease(&c, &n);
|
||||
isconnect = false;
|
||||
UserLedSet(-1);
|
||||
@@ -325,19 +375,19 @@ void MqttClientThread(mico_thread_arg_t arg)
|
||||
mico_rtos_thread_sleep(5);
|
||||
goto MQTT_start;
|
||||
|
||||
exit:
|
||||
exit:
|
||||
isconnect = false;
|
||||
mqtt_log("EXIT: MQTT client exit with err = %d.", err);
|
||||
UserMqttClientRelease(&c, &n);
|
||||
mico_rtos_delete_thread(NULL);
|
||||
mico_rtos_delete_thread(NULL); // 自删
|
||||
return;
|
||||
}
|
||||
|
||||
// callback, msg received from mqtt server
|
||||
static void MessageArrived(MessageData* md)
|
||||
{
|
||||
static void MessageArrived(MessageData *md) {
|
||||
OSStatus err = kUnknownErr;
|
||||
p_mqtt_recv_msg_t p_recv_msg = NULL;
|
||||
MQTTMessage* message = md->message;
|
||||
MQTTMessage *message = md->message;
|
||||
|
||||
p_recv_msg = (p_mqtt_recv_msg_t) calloc(1, sizeof(mqtt_recv_msg_t));
|
||||
require_action(p_recv_msg, exit, err = kNoMemoryErr);
|
||||
@@ -349,26 +399,25 @@ static void MessageArrived(MessageData* md)
|
||||
memcpy(p_recv_msg->data, message->payload, message->payloadlen);
|
||||
|
||||
mqtt_log("MessageArrived topic[%s] data[%s]", p_recv_msg->topic, p_recv_msg->data);
|
||||
err = mico_rtos_send_asynchronous_event(&mqtt_client_worker_thread, UserRecvHandler, p_recv_msg);
|
||||
err = mico_rtos_send_asynchronous_event(&mqtt_client_worker_thread, UserRecvHandler,
|
||||
p_recv_msg);
|
||||
require_noerr(err, exit);
|
||||
|
||||
exit:
|
||||
if (err != kNoErr)
|
||||
{
|
||||
mqtt_log("ERROR: Recv data err = %d", err);
|
||||
if (err != kNoErr) { mqtt_log("ERROR: Recv data err = %d", err);
|
||||
if (p_recv_msg) free(p_recv_msg);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/* Application process MQTT received data */
|
||||
OSStatus UserRecvHandler(void *arg)
|
||||
{
|
||||
OSStatus UserRecvHandler(void *arg) {
|
||||
OSStatus err = kUnknownErr;
|
||||
p_mqtt_recv_msg_t p_recv_msg = arg;
|
||||
require(p_recv_msg, exit);
|
||||
|
||||
mqtt_log("user get data success! from_topic=[%s], msg=[%ld].", p_recv_msg->topic, p_recv_msg->datalen);
|
||||
mqtt_log("user get data success! from_topic=[%s], msg=[%ld].", p_recv_msg->topic,
|
||||
p_recv_msg->datalen);
|
||||
//UserFunctionCmdReceived(0, p_recv_msg->data);
|
||||
|
||||
ProcessHaCmd(p_recv_msg->data);
|
||||
@@ -379,33 +428,67 @@ OSStatus UserRecvHandler(void *arg)
|
||||
return err;
|
||||
}
|
||||
|
||||
void ProcessHaCmd(char* cmd)
|
||||
{
|
||||
void ProcessHaCmd(char *cmd) {
|
||||
mqtt_log("ProcessHaCmd[%s]", cmd);
|
||||
char mac[20] = { 0 };
|
||||
char mac[20] = {0};
|
||||
|
||||
if (strcmp(cmd, "set socket") == ' ')
|
||||
{
|
||||
if (strcmp(cmd, "set socket") == ' ') {
|
||||
int i, on;
|
||||
sscanf(cmd, "set socket %s %d %d", mac, &i, &on);
|
||||
if (strcmp(mac, str_mac)) return;
|
||||
mqtt_log("set socket[%d] on[%d]", i, on);
|
||||
if (strcmp(mac, str_mac)) return;mqtt_log("set socket[%d] on[%d]", i, on);
|
||||
UserRelaySet(i, on);
|
||||
UserMqttSendSocketState(i);
|
||||
UserMqttSendTotalSocketState();
|
||||
mico_system_context_update(sys_config);
|
||||
} else if (strcmp(cmd, "set led") == ' ') {
|
||||
int on;
|
||||
sscanf(cmd, "set led %s %d", mac, &on);
|
||||
if (strcmp(mac, str_mac)) return;mqtt_log("set led on[%d]", on);
|
||||
user_config->power_led_enabled = on;
|
||||
if (RelayOut() && user_config->power_led_enabled) {
|
||||
UserLedSet(1);
|
||||
} else {
|
||||
UserLedSet(0);
|
||||
}
|
||||
UserMqttSendLedState();
|
||||
mico_system_context_update(sys_config);
|
||||
} else if (strcmp(cmd, "set total_socket") == ' ') {
|
||||
int on;
|
||||
sscanf(cmd, "set total_socket %s %d", mac, &on);
|
||||
if (strcmp(mac, str_mac)) return;mqtt_log("set total_socket on[%d]", on);
|
||||
UserRelaySetAll(on);
|
||||
int i = 0;
|
||||
for (i = 0; i < SOCKET_NUM; i++) {
|
||||
UserRelaySet(i, user_config->socket_status[i]);
|
||||
UserMqttSendSocketState(i);
|
||||
}
|
||||
UserMqttSendTotalSocketState();
|
||||
}else if (strcmp(cmd, "set childLock") == ' ') {
|
||||
int on;
|
||||
sscanf(cmd, "set childLock %s %d", mac, &on);
|
||||
if (strcmp(mac, str_mac)) return;mqtt_log("set childLock on[%d]", on);
|
||||
user_config->user[0] = on;
|
||||
childLockEnabled = on;
|
||||
UserMqttSendChildLockState();
|
||||
mico_system_context_update(sys_config);
|
||||
}else if (strcmp(cmd, "reboot") == ' ') {
|
||||
sscanf(cmd, "reboot %s", mac);
|
||||
if (strcmp(mac, str_mac)) return;
|
||||
MicoSystemReboot(); // 立即重启设备
|
||||
}
|
||||
}
|
||||
|
||||
OSStatus UserMqttSendTopic(char *topic, char *arg, char retained)
|
||||
{
|
||||
OSStatus UserMqttSendTopic(char *topic, char *arg, char retained) {
|
||||
OSStatus err = kUnknownErr;
|
||||
p_mqtt_send_msg_t p_send_msg = NULL;
|
||||
if(mqtt_msg_send_queue == NULL|| !isconnect){
|
||||
return err;
|
||||
}
|
||||
|
||||
// mqtt_log("======App prepare to send ![%d]======", MicoGetMemoryInfo()->free_memory);
|
||||
|
||||
/* Send queue is full, pop the oldest */
|
||||
if (mico_rtos_is_queue_full(&mqtt_msg_send_queue) == true)
|
||||
{
|
||||
if (mico_rtos_is_queue_full(&mqtt_msg_send_queue) == true) {
|
||||
mico_rtos_pop_from_queue(&mqtt_msg_send_queue, &p_send_msg, 0);
|
||||
free(p_send_msg);
|
||||
p_send_msg = NULL;
|
||||
@@ -432,21 +515,64 @@ OSStatus UserMqttSendTopic(char *topic, char *arg, char retained)
|
||||
}
|
||||
|
||||
/* Application collect data and seng them to MQTT send queue */
|
||||
OSStatus UserMqttSend(char *arg)
|
||||
{
|
||||
OSStatus UserMqttSend(char *arg) {
|
||||
return UserMqttSendTopic(topic_state, arg, 0);
|
||||
}
|
||||
|
||||
//鏇存柊ha寮<61>鍏崇姸鎬<E5A7B8>
|
||||
OSStatus UserMqttSendSocketState(char socket_id)
|
||||
{
|
||||
OSStatus UserMqttSendSocketState(char socket_id) {
|
||||
char *send_buf = malloc(64);
|
||||
char *topic_buf = malloc(64);
|
||||
OSStatus oss_status = kUnknownErr;
|
||||
if (send_buf != NULL && topic_buf != NULL)
|
||||
{
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/socket_%d/state", str_mac, (int)socket_id);
|
||||
sprintf(send_buf, "set socket %s %d %d", str_mac, socket_id, (int)user_config->socket_status[(int)socket_id]);
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/socket_%d/state", str_mac, (int) socket_id);
|
||||
sprintf(send_buf, "set socket %s %d %d", str_mac, socket_id,
|
||||
(int) user_config->socket_status[(int) socket_id]);
|
||||
oss_status = UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf) free(send_buf);
|
||||
if (topic_buf) free(topic_buf);
|
||||
|
||||
return oss_status;
|
||||
}
|
||||
|
||||
OSStatus UserMqttSendTotalSocketState(void) {
|
||||
char *send_buf = malloc(64);
|
||||
char *topic_buf = malloc(64);
|
||||
OSStatus oss_status = kUnknownErr;
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/total_socket/state", str_mac);
|
||||
sprintf(send_buf, "set total_socket %s %d", str_mac, RelayOut() ? 1 : 0);
|
||||
oss_status = UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf) free(send_buf);
|
||||
if (topic_buf) free(topic_buf);
|
||||
|
||||
return oss_status;
|
||||
}
|
||||
|
||||
OSStatus UserMqttSendLedState(void) {
|
||||
char *send_buf = malloc(64);
|
||||
char *topic_buf = malloc(64);
|
||||
OSStatus oss_status = kUnknownErr;
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/led/state", str_mac);
|
||||
sprintf(send_buf, "set led %s %d", str_mac, (int) user_config->power_led_enabled);
|
||||
oss_status = UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf) free(send_buf);
|
||||
if (topic_buf) free(topic_buf);
|
||||
|
||||
return oss_status;
|
||||
}
|
||||
|
||||
OSStatus UserMqttSendChildLockState(void) {
|
||||
char *send_buf = malloc(64);
|
||||
char *topic_buf = malloc(64);
|
||||
OSStatus oss_status = kUnknownErr;
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/childLock/state", str_mac);
|
||||
sprintf(send_buf, "set childLock %s %d", str_mac, childLockEnabled);
|
||||
oss_status = UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf) free(send_buf);
|
||||
@@ -456,24 +582,31 @@ OSStatus UserMqttSendSocketState(char socket_id)
|
||||
}
|
||||
|
||||
//hass mqtt鑷姩鍙戠幇鏁版嵁寮<E5B581>鍏冲彂閫<E5BD82>
|
||||
void UserMqttHassAuto(char socket_id)
|
||||
{
|
||||
void UserMqttHassAuto(char socket_id) {
|
||||
socket_id--;
|
||||
char *send_buf = NULL;
|
||||
char *topic_buf = NULL;
|
||||
send_buf = (char *) malloc(300);
|
||||
send_buf = (char *) malloc(600);
|
||||
topic_buf = (char *) malloc(64);
|
||||
if (send_buf != NULL && topic_buf != NULL)
|
||||
{
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/socket_%d/config", str_mac, socket_id);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"TC1_%s_Socket_%d\","
|
||||
"\"uniq_id\":\"%s_s%d\","
|
||||
"\"stat_t\":\"homeassistant/switch/%s/socket_%d/state\","
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_on\":\"set socket %s %d 1\","
|
||||
"\"pl_off\":\"set socket %s %d 0\"}",
|
||||
str_mac+8, socket_id+1, str_mac, socket_id, str_mac, socket_id, str_mac, socket_id, str_mac, socket_id);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"%s\","
|
||||
"\"uniq_id\":\"tc1_%s_s%d\","
|
||||
"\"object_id\":\"tc1_%s_s%d\","
|
||||
"\"stat_t\":\"homeassistant/switch/%s/socket_%d/state\","
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_on\":\"set socket %s %d 1\","
|
||||
"\"pl_off\":\"set socket %s %d 0\","
|
||||
"\"device_class\":\"outlet\","
|
||||
"\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
user_config->socket_names[(int)socket_id], str_mac, socket_id,str_mac, socket_id, str_mac, socket_id,
|
||||
str_mac,
|
||||
socket_id, str_mac, socket_id, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf)
|
||||
@@ -481,85 +614,261 @@ void UserMqttHassAuto(char socket_id)
|
||||
if (topic_buf)
|
||||
free(topic_buf);
|
||||
}
|
||||
//hass mqtt鑷姩鍙戠幇鏁版嵁鍔熺巼鍙戦<E98D99><E688A6>
|
||||
void UserMqttHassAutoPower(void)
|
||||
{
|
||||
|
||||
void UserMqttHassAutoRebootButton(void) {
|
||||
char *send_buf = NULL;
|
||||
char *topic_buf = NULL;
|
||||
send_buf = malloc(512);
|
||||
topic_buf = malloc(128);
|
||||
if (send_buf != NULL && topic_buf != NULL)
|
||||
{
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/power/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"TC1_%s_Power\","
|
||||
"\"uniq_id\":\"%s_p\","
|
||||
"\"state_topic\":\"homeassistant/sensor/%s/power/state\","
|
||||
"\"unit_of_measurement\":\"W\","
|
||||
"\"icon\":\"mdi:gauge\","
|
||||
"\"value_template\":\"{{ value_json.power }}\"}",
|
||||
str_mac+8, str_mac, str_mac);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumption/config", str_mac);
|
||||
send_buf = (char *) malloc(600);
|
||||
topic_buf = (char *) malloc(64);
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
// 重启按钮配置
|
||||
sprintf(topic_buf, "homeassistant/button/%s/reboot/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"TC1_%s_PowerConsumption\","
|
||||
"\"uniq_id\":\"%s_pc\","
|
||||
"\"state_topic\":\"homeassistant/sensor/%s/powerConsumption/state\","
|
||||
"\"unit_of_measurement\":\"kWh\","
|
||||
"\"icon\":\"mdi:fence-electric\","
|
||||
"\"value_template\":\"{{ value_json.powerConsumption }}\"}",
|
||||
str_mac+8, str_mac, str_mac);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionToday/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"TC1_%s_powerConsumptionToday\","
|
||||
"\"uniq_id\":\"%s_pc_today\","
|
||||
"\"state_topic\":\"homeassistant/sensor/%s/powerConsumptionToday/state\","
|
||||
"\"unit_of_measurement\":\"kWh\","
|
||||
"\"icon\":\"mdi:fence-electric\","
|
||||
"\"value_template\":\"{{ value_json.powerConsumptionToday }}\"}",
|
||||
str_mac+8, str_mac, str_mac);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionYesterday/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"TC1_%s_powerConsumptionYesterday\","
|
||||
"\"uniq_id\":\"%s_pc_yesterday\","
|
||||
"\"state_topic\":\"homeassistant/sensor/%s/powerConsumptionYesterday/state\","
|
||||
"\"unit_of_measurement\":\"kWh\","
|
||||
"\"icon\":\"mdi:fence-electric\","
|
||||
"\"value_template\":\"{{ value_json.powerConsumptionYesterday }}\"}",
|
||||
str_mac+8, str_mac, str_mac);
|
||||
"{\"name\":\"重启设备\","
|
||||
"\"uniq_id\":\"tc1_%s_reboot\","
|
||||
"\"object_id\":\"tc1_%s_reboot\","
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_prs\":\"reboot %s\","
|
||||
"\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac,str_mac,str_mac,str_mac, sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf) free(send_buf);
|
||||
if (topic_buf) free(topic_buf);
|
||||
}
|
||||
|
||||
char topic_buf[128] = { 0 };
|
||||
char send_buf[128] = { 0 };
|
||||
void UserMqttHassPower(void)
|
||||
{
|
||||
void UserMqttHassAutoLed(void) {
|
||||
char *send_buf = NULL;
|
||||
char *topic_buf = NULL;
|
||||
send_buf = (char *) malloc(600);
|
||||
topic_buf = (char *) malloc(64);
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/led/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"LED指示灯\","
|
||||
"\"uniq_id\":\"tc1_%s_led\","
|
||||
"\"object_id\":\"tc1_%s_led\","
|
||||
"\"stat_t\":\"homeassistant/switch/%s/led/state\","
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_on\":\"set led %s 1\","
|
||||
"\"pl_off\":\"set led %s 0\","
|
||||
"\"device_class\":\"outlet\","
|
||||
"\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac,str_mac,str_mac, str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf)
|
||||
free(send_buf);
|
||||
if (topic_buf)
|
||||
free(topic_buf);
|
||||
}
|
||||
|
||||
void UserMqttHassAutoChildLock(void) {
|
||||
char *send_buf = NULL;
|
||||
char *topic_buf = NULL;
|
||||
send_buf = (char *) malloc(600);
|
||||
topic_buf = (char *) malloc(64);
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/childLock/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"童锁\","
|
||||
"\"uniq_id\":\"tc1_%s_child_lock\","
|
||||
"\"object_id\":\"tc1_%s_child_lock\","
|
||||
"\"stat_t\":\"homeassistant/switch/%s/childLock/state\","
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_on\":\"set childLock %s 1\","
|
||||
"\"pl_off\":\"set childLock %s 0\","
|
||||
"\"device_class\":\"outlet\","
|
||||
"\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac,str_mac,str_mac, str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf)
|
||||
free(send_buf);
|
||||
if (topic_buf)
|
||||
free(topic_buf);
|
||||
}
|
||||
|
||||
void UserMqttHassAutoTotalSocket(void) {
|
||||
char *send_buf = NULL;
|
||||
char *topic_buf = NULL;
|
||||
send_buf = (char *) malloc(600);
|
||||
topic_buf = (char *) malloc(64);
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/total_socket/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"总开关\","
|
||||
"\"uniq_id\":\"tc1_%s_total_socket\","
|
||||
"\"object_id\":\"tc1_%s_total_socket\","
|
||||
"\"stat_t\":\"homeassistant/switch/%s/total_socket/state\","
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_on\":\"set total_socket %s 1\","
|
||||
"\"pl_off\":\"set total_socket %s 0\","
|
||||
"\"device_class\":\"outlet\","
|
||||
"\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac, str_mac, str_mac, str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf)
|
||||
free(send_buf);
|
||||
if (topic_buf)
|
||||
free(topic_buf);
|
||||
}
|
||||
|
||||
//hass mqtt鑷姩鍙戠幇鏁版嵁鍔熺巼鍙戦<E98D99><E688A6>
|
||||
void UserMqttHassAutoPower(void) {
|
||||
char *send_buf = NULL;
|
||||
char *topic_buf = NULL;
|
||||
send_buf = malloc(600);
|
||||
topic_buf = malloc(128);
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/power/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"功率\","
|
||||
"\"uniq_id\":\"tc1_%s_p\","
|
||||
"\"object_id\":\"tc1_%s_p\","
|
||||
"\"state_topic\":\"homeassistant/sensor/%s/power/state\","
|
||||
"\"unit_of_measurement\":\"W\","
|
||||
"\"icon\":\"mdi:gauge\","
|
||||
"\"value_template\":\"{{ value_json.power }}\",""\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac,str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumption/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"总耗电量\","
|
||||
"\"uniq_id\":\"tc1_%s_pc\","
|
||||
"\"object_id\":\"tc1_%s_pc\","
|
||||
"\"state_topic\":\"homeassistant/sensor/%s/powerConsumption/state\","
|
||||
"\"unit_of_measurement\":\"kWh\","
|
||||
"\"icon\":\"mdi:fence-electric\","
|
||||
"\"value_template\":\"{{ value_json.powerConsumption }}\",""\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac, str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
|
||||
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/startupTime/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"运行时间\","
|
||||
"\"uniq_id\":\"tc1_%s_sut\","
|
||||
"\"object_id\":\"tc1_%s_sut\","
|
||||
"\"state_topic\":\"homeassistant/sensor/%s/startupTime/state\","
|
||||
"\"icon\":\"mdi:clock-time-three-outline\","
|
||||
"\"entity_category\":\"diagnostic\","
|
||||
"\"value_template\":\"{{ value_json.startupTime }}\",""\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac, str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionToday/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"今日耗电量\","
|
||||
"\"uniq_id\":\"tc1_%s_pc_today\","
|
||||
"\"object_id\":\"tc1_%s_pc_today\","
|
||||
"\"state_topic\":\"homeassistant/sensor/%s/powerConsumptionToday/state\","
|
||||
"\"unit_of_measurement\":\"kWh\","
|
||||
"\"icon\":\"mdi:fence-electric\","
|
||||
"\"value_template\":\"{{ value_json.powerConsumptionToday }}\",""\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac,str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionYesterday/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"昨日耗电量\","
|
||||
"\"uniq_id\":\"tc1_%s_pc_yesterday\","
|
||||
"\"object_id\":\"tc1_%s_pc_yesterday\","
|
||||
"\"state_topic\":\"homeassistant/sensor/%s/powerConsumptionYesterday/state\","
|
||||
"\"unit_of_measurement\":\"kWh\","
|
||||
"\"icon\":\"mdi:fence-electric\","
|
||||
"\"value_template\":\"{{ value_json.powerConsumptionYesterday }}\",""\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac,str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf) free(send_buf);
|
||||
if (topic_buf) free(topic_buf);
|
||||
}
|
||||
|
||||
char topic_buf[128] = {0};
|
||||
char send_buf[128] = {0};
|
||||
|
||||
extern void UserMqttHassPower(void) {
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/power/state", str_mac);
|
||||
sprintf(send_buf, "{\"power\":\"%.3f\"}", real_time_power/10);
|
||||
sprintf(send_buf, "{\"power\":\"%.3f\"}", real_time_power / 10);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 0);
|
||||
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumption/state", str_mac);
|
||||
sprintf(send_buf, "{\"powerConsumption\":\"%.3f\"}", (17.1 * p_count) / 1000 / 36000);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 0);
|
||||
|
||||
//计算系统运行时间
|
||||
char up_time[16] = "00:00:00";
|
||||
mico_time_t past_ms = 0;
|
||||
mico_time_get_time(&past_ms);
|
||||
int past = past_ms / 1000;
|
||||
int d = past / 3600 / 24;
|
||||
int h = past / 3600 % 24;
|
||||
int m = past / 60 % 60;
|
||||
int s = past % 60;
|
||||
sprintf(up_time, "%d - %02d:%02d:%02d", d, h, m, s);
|
||||
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/startupTime/state", str_mac);
|
||||
sprintf(send_buf, "{\"startupTime\":\"%s\"}", up_time);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 0);
|
||||
|
||||
// tc1_log("p_count %ld, p_count_1_day_ago %ld ,p_count_2_days_ago %ld, result %ld",p_count,user_config->p_count_1_day_ago,user_config->p_count_2_days_ago,((p_count-user_config->p_count_1_day_ago)<0?0:(p_count-user_config->p_count_1_day_ago)));
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionToday/state", str_mac);
|
||||
sprintf(send_buf, "{\"powerConsumptionToday\":\"%.3f\"}", (17.1 * ((p_count-user_config->p_count_1_day_ago)<0?0:(p_count-user_config->p_count_1_day_ago))) / 1000 / 36000);
|
||||
sprintf(send_buf, "{\"powerConsumptionToday\":\"%.3f\"}", (17.1 * ((p_count -
|
||||
user_config->p_count_1_day_ago) <
|
||||
0 ? 0 : (p_count -
|
||||
user_config->p_count_1_day_ago))) /
|
||||
1000 / 36000);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 0);
|
||||
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionYesterday/state", str_mac);
|
||||
sprintf(send_buf, "{\"powerConsumptionYesterday\":\"%.3f\"}", (17.1 * ((user_config->p_count_1_day_ago-user_config->p_count_2_days_ago)<0?0:(user_config->p_count_1_day_ago-user_config->p_count_2_days_ago))) / 1000 / 36000);
|
||||
sprintf(send_buf, "{\"powerConsumptionYesterday\":\"%.3f\"}", (17.1 *
|
||||
((user_config->p_count_1_day_ago -
|
||||
user_config->p_count_2_days_ago) <
|
||||
0 ? 0 : (
|
||||
user_config->p_count_1_day_ago -
|
||||
user_config->p_count_2_days_ago))) /
|
||||
1000 / 36000);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 0);
|
||||
}
|
||||
|
||||
bool UserMqttIsConnect()
|
||||
{
|
||||
bool UserMqttIsConnect() {
|
||||
return isconnect;
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
#define MQTT_CMD_TIMEOUT 5000 // 5s
|
||||
#define MQTT_YIELD_TMIE 5000 // 5s
|
||||
|
||||
#define MAX_MQTT_TOPIC_SIZE (256)
|
||||
#define MAX_MQTT_DATA_SIZE (1024)
|
||||
#define MAX_MQTT_TOPIC_SIZE (512)
|
||||
#define MAX_MQTT_DATA_SIZE (2048)
|
||||
#define MAX_MQTT_SEND_QUEUE_SIZE (10)
|
||||
|
||||
#define MQTT_SERVER user_config->mqtt_ip
|
||||
@@ -22,6 +22,7 @@
|
||||
#define MQTT_LED_ENABLED user_config->power_led_enabled
|
||||
|
||||
extern OSStatus UserMqttInit(void);
|
||||
extern OSStatus UserMqttDeInit(void);
|
||||
|
||||
extern OSStatus UserMqttSend(char *arg);
|
||||
|
||||
@@ -29,10 +30,26 @@ extern bool UserMqttIsConnect(void);
|
||||
|
||||
extern OSStatus UserMqttSendSocketState(char socket_id);
|
||||
|
||||
extern OSStatus UserMqttSendLedState(void);
|
||||
|
||||
extern OSStatus UserMqttSendChildLockState(void);
|
||||
|
||||
extern OSStatus UserMqttSendTotalSocketState(void);
|
||||
|
||||
extern void UserMqttHassAuto(char socket_id);
|
||||
|
||||
extern void UserMqttHassPower(void);
|
||||
|
||||
extern void UserMqttHassAutoPower(void);
|
||||
|
||||
extern void UserMqttHassAutoLed(void);
|
||||
|
||||
extern void UserMqttHassAutoChildLock(void);
|
||||
|
||||
extern void UserMqttHassAutoTotalSocket(void);
|
||||
|
||||
extern void registerMqttEvents(void);
|
||||
|
||||
extern void UserMqttHassAutoRebootButton(void);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -257,7 +257,7 @@ static void OtaServerThread(mico_thread_arg_t arg)
|
||||
InitMd5(&md5);
|
||||
}
|
||||
|
||||
httpHeader = HTTPHeaderCreateWithCallback(1024, OnReceivedData, NULL, NULL);
|
||||
httpHeader = HTTPHeaderCreateWithCallback(512, OnReceivedData, NULL, NULL);
|
||||
require_action(httpHeader, DELETE, OtaServerProgressSet(OTA_FAIL));
|
||||
|
||||
while (1)
|
||||
|
||||
@@ -67,7 +67,7 @@ OSStatus UserRtcInit(void)
|
||||
/* start rtc client */
|
||||
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "rtc",
|
||||
(mico_thread_function_t) RtcThread,
|
||||
0x1000, 0);
|
||||
0x800, 0);
|
||||
require_noerr_string(err, exit, "ERROR: Unable to start the rtc thread.");
|
||||
|
||||
if (kNoErr != err) rtc_log("ERROR1, app thread exit err: %d kNoErr[%d]", err, kNoErr);
|
||||
|
||||
@@ -6,8 +6,10 @@
|
||||
|
||||
#include"main.h"
|
||||
#include"user_gpio.h"
|
||||
#include "mqtt_server/user_mqtt_client.h"
|
||||
#include"timed_task/timed_task.h"
|
||||
#include"http_server/web_log.h"
|
||||
#include "user_wifi.h"
|
||||
|
||||
int day_sec = 86400;
|
||||
|
||||
@@ -142,9 +144,61 @@ bool DelTask(int time)
|
||||
|
||||
void ProcessTask()
|
||||
{
|
||||
task_log("process task time[%ld] socket_idx[%d] on[%d]",
|
||||
user_config->task_top->prs_time, user_config->task_top->socket_idx, user_config->task_top->on);
|
||||
UserRelaySet(user_config->task_top->socket_idx, user_config->task_top->on);
|
||||
task_log("process task time[%ld] operation[%s] on[%d]",
|
||||
user_config->task_top->prs_time, get_func_name(user_config->task_top->operation), user_config->task_top->on);
|
||||
switch (user_config->task_top->operation) {
|
||||
case SWITCH_ALL_SOCKETS:
|
||||
UserRelaySetAll(user_config->task_top->on);
|
||||
mico_system_context_update(sys_config);
|
||||
for (int i = 0; i < SOCKET_NUM; i++) {
|
||||
UserMqttSendSocketState(i);
|
||||
}
|
||||
UserMqttSendTotalSocketState();
|
||||
break;
|
||||
case SWITCH_SOCKET_1:
|
||||
case SWITCH_SOCKET_2:
|
||||
case SWITCH_SOCKET_3:
|
||||
case SWITCH_SOCKET_4:
|
||||
case SWITCH_SOCKET_5:
|
||||
case SWITCH_SOCKET_6:
|
||||
UserRelaySet(user_config->task_top->operation - 1, user_config->task_top->on);
|
||||
UserMqttSendSocketState(user_config->task_top->operation - 1);
|
||||
UserMqttSendTotalSocketState();
|
||||
mico_system_context_update(sys_config);
|
||||
break;
|
||||
case SWITCH_LED_ENABLE:
|
||||
|
||||
if (RelayOut() && user_config->task_top->on) {
|
||||
UserLedSet(1);
|
||||
} else {
|
||||
UserLedSet(0);
|
||||
}
|
||||
UserMqttSendLedState();
|
||||
mico_system_context_update(sys_config);
|
||||
break;
|
||||
case SWITCH_CHILD_LOCK_ENABLE:
|
||||
user_config->user[0] = user_config->task_top->on;
|
||||
childLockEnabled = user_config->user[0];
|
||||
mico_system_context_update(sys_config);
|
||||
UserMqttSendChildLockState();
|
||||
break;
|
||||
case REBOOT_SYSTEM:
|
||||
MicoSystemReboot();
|
||||
break;
|
||||
case CONFIG_WIFI:
|
||||
|
||||
micoWlanSuspendStation();
|
||||
ApInit(true);
|
||||
break;
|
||||
case RESET_SYSTEM:
|
||||
|
||||
mico_system_context_restore(sys_config);
|
||||
mico_rtos_thread_sleep(1);
|
||||
MicoSystemReboot();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
DelFirstTask();
|
||||
}
|
||||
|
||||
@@ -164,8 +218,8 @@ char* GetTaskStr()
|
||||
tm_info = localtime(&prs_time);
|
||||
strftime(buffer, 26, "%m-%d %H:%M", tm_info);
|
||||
|
||||
sprintf(tmp_str, "{'timestamp':%ld,'prs_time':'%s','socket_idx':%d,'on':%d,'weekday':%d},",
|
||||
tmp_tsk->prs_time, buffer, tmp_tsk->socket_idx+1, tmp_tsk->on, tmp_tsk->weekday);
|
||||
sprintf(tmp_str, "{'timestamp':%ld,'prs_time':'%s','operation':%d,'on':%d,'weekday':%d},",
|
||||
tmp_tsk->prs_time, buffer, tmp_tsk->operation, tmp_tsk->on, tmp_tsk->weekday);
|
||||
tmp_str += strlen(tmp_str);
|
||||
tmp_tsk = tmp_tsk->next;
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ struct TimedTask
|
||||
{
|
||||
bool on_use; //正在使用
|
||||
time_t prs_time; //被执行的格林尼治时间戳
|
||||
int socket_idx; //要控制的插孔
|
||||
int on; //开或者关
|
||||
int operation; //要进行的操作
|
||||
int on; //开或者关,或者其他操作
|
||||
int weekday; //星期重复 0代表不重复 8代表每日重复
|
||||
pTimedTask next; //下一个任务(按之间排序)
|
||||
};
|
||||
|
||||
432
TC1/user_gpio.c
432
TC1/user_gpio.c
@@ -2,13 +2,14 @@
|
||||
|
||||
#include "main.h"
|
||||
#include "user_gpio.h"
|
||||
#include "user_wifi.h"
|
||||
#include "mqtt_server/user_mqtt_client.h"
|
||||
|
||||
mico_gpio_t relay[Relay_NUM] = { Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5 };
|
||||
char socket_status[32] = { 0 };
|
||||
mico_gpio_t relay[Relay_NUM] = {Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5};
|
||||
char socket_status[32] = {0};
|
||||
char btn_click_config[500] = {0};
|
||||
|
||||
void UserLedSet(char x)
|
||||
{
|
||||
void UserLedSet(char x) {
|
||||
if (x == -1)
|
||||
MicoGpioOutputTrigger(Led);
|
||||
else if (x)
|
||||
@@ -17,75 +18,142 @@ void UserLedSet(char x)
|
||||
MicoGpioOutputLow(Led);
|
||||
}
|
||||
|
||||
bool RelayOut(void)
|
||||
{
|
||||
bool RelayOut(void) {
|
||||
int i;
|
||||
for (i = 0; i < SOCKET_NUM; i++)
|
||||
{
|
||||
if (user_config->socket_status[i] != 0)
|
||||
{
|
||||
for (i = 0; i < SOCKET_NUM; i++) {
|
||||
if (user_config->socket_status[i] != 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
char* GetSocketStatus()
|
||||
{
|
||||
char* get_func_name(char func_code) {
|
||||
static char buffer[32];
|
||||
switch (func_code) {
|
||||
case SWITCH_ALL_SOCKETS:
|
||||
return "Toggle All Sockets";
|
||||
case SWITCH_SOCKET_1:
|
||||
case SWITCH_SOCKET_2:
|
||||
case SWITCH_SOCKET_3:
|
||||
case SWITCH_SOCKET_4:
|
||||
case SWITCH_SOCKET_5:
|
||||
case SWITCH_SOCKET_6:
|
||||
sprintf(buffer, "Toggle Socket %d %s", func_code - 1,
|
||||
user_config->socket_names[func_code - 1]);
|
||||
return buffer;
|
||||
case SWITCH_LED_ENABLE:
|
||||
return "Toggle LED";
|
||||
case REBOOT_SYSTEM:
|
||||
return "Reboot";
|
||||
case REBOOT_HTTP:
|
||||
return "REBOOT_HTTP";
|
||||
case CONFIG_WIFI:
|
||||
return "WiFi Config";
|
||||
case RESET_SYSTEM:
|
||||
return "Factory Reset";
|
||||
case SWITCH_CHILD_LOCK_ENABLE:
|
||||
return "Toggle ChildLick";
|
||||
case -1:
|
||||
case NO_FUNCTION:
|
||||
return "Unassigned";
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
/// 针对电源按钮的点击事件
|
||||
/// \param index 判断短按(连击)时,代表连击次数,判断长按时代表长按秒数
|
||||
/// \param short_func 功能码 在user_gpio.h中定义了
|
||||
/// \param long_func 功能码 在user_gpio.h中定义了
|
||||
void set_key_map(char user[],int index, char short_func, char long_func) {
|
||||
user[index] = ((long_func & 0x0F) << 4) | (short_func & 0x0F);
|
||||
}
|
||||
|
||||
char get_short_func(char val) {
|
||||
char func = val & 0x0F;
|
||||
return (func == NO_FUNCTION) ? -1 : func; // -1 表示未配置
|
||||
}
|
||||
|
||||
char get_long_func(char val) {
|
||||
char func = (val >> 4) & 0x0F;
|
||||
return (func == NO_FUNCTION) ? -1 : func; // -1 表示未配置
|
||||
}
|
||||
|
||||
|
||||
char *GetSocketStatus() {
|
||||
sprintf(socket_status, "%d,%d,%d,%d,%d,%d",
|
||||
user_config->socket_status[0],
|
||||
user_config->socket_status[1],
|
||||
user_config->socket_status[2],
|
||||
user_config->socket_status[3],
|
||||
user_config->socket_status[4],
|
||||
user_config->socket_status[5]);
|
||||
user_config->socket_status[0],
|
||||
user_config->socket_status[1],
|
||||
user_config->socket_status[2],
|
||||
user_config->socket_status[3],
|
||||
user_config->socket_status[4],
|
||||
user_config->socket_status[5]);
|
||||
return socket_status;
|
||||
}
|
||||
|
||||
void SetSocketStatus(char* socket_status)
|
||||
{
|
||||
char *GetButtonClickConfig() {
|
||||
char temp[32];
|
||||
int len = 0;
|
||||
int max_len =sizeof(btn_click_config);
|
||||
len += snprintf(btn_click_config + len, max_len - len, "[");
|
||||
|
||||
for (int i = 1; i <= 30; i++) {
|
||||
char short_func = get_short_func(user_config->user[i]);
|
||||
char long_func = get_long_func(user_config->user[i]);
|
||||
// key_log("WARNGIN:KEY func %d %d %d", i,short_func,long_func);
|
||||
|
||||
snprintf(temp, sizeof(temp), "{'%d':[%d,%d]}%s", i, short_func, long_func, (i != 30) ? "," : "");
|
||||
len += snprintf(btn_click_config + len, max_len - len, "%s", temp);
|
||||
|
||||
if (len >= max_len - 1) break;
|
||||
}
|
||||
snprintf(btn_click_config + len, max_len - len, "]");
|
||||
|
||||
return btn_click_config;
|
||||
}
|
||||
|
||||
void SetSocketStatus(char *socket_status) {
|
||||
sscanf(socket_status, "%d,%d,%d,%d,%d,%d,",
|
||||
(int*)&user_config->socket_status[0],
|
||||
(int*)&user_config->socket_status[1],
|
||||
(int*)&user_config->socket_status[2],
|
||||
(int*)&user_config->socket_status[3],
|
||||
(int*)&user_config->socket_status[4],
|
||||
(int*)&user_config->socket_status[5]);
|
||||
(int *) &user_config->socket_status[0],
|
||||
(int *) &user_config->socket_status[1],
|
||||
(int *) &user_config->socket_status[2],
|
||||
(int *) &user_config->socket_status[3],
|
||||
(int *) &user_config->socket_status[4],
|
||||
(int *) &user_config->socket_status[5]);
|
||||
int i = 0;
|
||||
for (i = 0; i < SOCKET_NUM; i++)
|
||||
{
|
||||
for (i = 0; i < SOCKET_NUM; i++) {
|
||||
UserRelaySet(i, user_config->socket_status[i]);
|
||||
UserMqttSendSocketState(i);
|
||||
}
|
||||
UserMqttSendTotalSocketState();
|
||||
mico_system_context_update(sys_config);
|
||||
}
|
||||
|
||||
/*UserRelaySet
|
||||
* 设置继电器开关
|
||||
* i:编号 0-5
|
||||
* on:开关 0:关 1:开
|
||||
* on:开关 0:关 1:开 -1:切换
|
||||
*/
|
||||
void UserRelaySet(unsigned char i, unsigned char on)
|
||||
{
|
||||
void UserRelaySet(unsigned char i, char on) {
|
||||
if (i < 0 || i >= SOCKET_NUM) return;
|
||||
|
||||
if (on == Relay_ON)
|
||||
{
|
||||
if (on == Relay_ON) {
|
||||
MicoGpioOutputHigh(relay[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else if (on == Relay_OFF) {
|
||||
MicoGpioOutputLow(relay[i]);
|
||||
} else if (on == Relay_TOGGLE) {
|
||||
if (user_config->socket_status[i] == Relay_OFF) {
|
||||
MicoGpioOutputHigh(relay[i]);
|
||||
} else {
|
||||
MicoGpioOutputLow(relay[i]);
|
||||
}
|
||||
}
|
||||
user_config->socket_status[i] = on >= 0 ? on : (user_config->socket_status[i] == 0 ? 1 : 0);
|
||||
|
||||
user_config->socket_status[i] = on;
|
||||
|
||||
if (RelayOut()&&user_config->power_led_enabled)
|
||||
{
|
||||
if (RelayOut() && user_config->power_led_enabled) {
|
||||
UserLedSet(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
UserLedSet(0);
|
||||
}
|
||||
}
|
||||
@@ -95,111 +163,229 @@ void UserRelaySet(unsigned char i, unsigned char on)
|
||||
* y: 0:全部关 1:全部开
|
||||
*
|
||||
*/
|
||||
void UserRelaySetAll(char y)
|
||||
{
|
||||
void UserRelaySetAll(char y) {
|
||||
int i;
|
||||
for (i = 0; i < SOCKET_NUM; i++)
|
||||
UserRelaySet(i, y);
|
||||
}
|
||||
|
||||
static void KeyLongPress(void)
|
||||
{
|
||||
// key_log("KeyLongPress");
|
||||
// UserLedSet(1);
|
||||
// UserMqttSend("mqtt test");
|
||||
static void KeyEventHandler(int num, boolean longPress) {
|
||||
key_log("WARNGIN:Power key %s %d %s", !longPress ? "quick clicked" : "longPressed", num,
|
||||
num > 1 ? (longPress ? "seconds" : "times") : (longPress ? "second" : "time"));
|
||||
if (num > 30 || num <= 0)
|
||||
return;
|
||||
int function = !longPress ? get_short_func(user_config->user[num]) : get_long_func(
|
||||
user_config->user[num]);
|
||||
boolean showLog= childLockEnabled==0;
|
||||
switch (function) {
|
||||
case SWITCH_ALL_SOCKETS:
|
||||
if (childLockEnabled)
|
||||
break;
|
||||
if (RelayOut()) {
|
||||
UserRelaySetAll(0);
|
||||
} else {
|
||||
UserRelaySetAll(1);
|
||||
}
|
||||
mico_system_context_update(sys_config);
|
||||
for (int i = 0; i < SOCKET_NUM; i++) {
|
||||
UserMqttSendSocketState(i);
|
||||
}
|
||||
UserMqttSendTotalSocketState();
|
||||
break;
|
||||
case SWITCH_SOCKET_1:
|
||||
case SWITCH_SOCKET_2:
|
||||
case SWITCH_SOCKET_3:
|
||||
case SWITCH_SOCKET_4:
|
||||
case SWITCH_SOCKET_5:
|
||||
case SWITCH_SOCKET_6:
|
||||
if (childLockEnabled)
|
||||
break;
|
||||
UserRelaySet(function - 1, Relay_TOGGLE);
|
||||
UserMqttSendSocketState(function - 1);
|
||||
UserMqttSendTotalSocketState();
|
||||
mico_system_context_update(sys_config);
|
||||
break;
|
||||
case SWITCH_LED_ENABLE:
|
||||
if (childLockEnabled)
|
||||
break;
|
||||
MQTT_LED_ENABLED = MQTT_LED_ENABLED == 0 ? 1 : 0;
|
||||
if (RelayOut() && MQTT_LED_ENABLED) {
|
||||
UserLedSet(1);
|
||||
} else {
|
||||
UserLedSet(0);
|
||||
}
|
||||
UserMqttSendLedState();
|
||||
mico_system_context_update(sys_config);
|
||||
break;
|
||||
case SWITCH_CHILD_LOCK_ENABLE:
|
||||
showLog=true;
|
||||
user_config->user[0] = user_config->user[0] == 0 ? 1 : 0;
|
||||
childLockEnabled = user_config->user[0];
|
||||
mico_system_context_update(sys_config);
|
||||
UserMqttSendChildLockState();
|
||||
break;
|
||||
case REBOOT_SYSTEM:
|
||||
if (childLockEnabled)
|
||||
break;
|
||||
MicoSystemReboot();
|
||||
break;
|
||||
case REBOOT_HTTP:
|
||||
if (childLockEnabled)
|
||||
break;
|
||||
AppHttpdStop();
|
||||
mico_rtos_thread_sleep(1);
|
||||
AppHttpdStart();
|
||||
break;
|
||||
case CONFIG_WIFI:
|
||||
if (childLockEnabled)
|
||||
break;
|
||||
StartLedBlink(3);
|
||||
UserMqttDeInit();
|
||||
micoWlanSuspendStation();
|
||||
ApInit(true);
|
||||
break;
|
||||
case RESET_SYSTEM:
|
||||
if (childLockEnabled)
|
||||
break;
|
||||
StartLedBlink(8);
|
||||
mico_system_context_restore(sys_config);
|
||||
mico_rtos_thread_sleep(1);
|
||||
MicoSystemReboot();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
key_log("WARNGIN:%s",showLog? get_func_name(function):"child lock enabled,ignore key event !");
|
||||
}
|
||||
|
||||
static void KeyLong10sPress(void)
|
||||
{
|
||||
key_log("WARNGIN: user params restored!");
|
||||
appRestoreDefault_callback(user_config, sizeof(user_config_t));
|
||||
sys_config->micoSystemConfig.ssid[0] = 0;
|
||||
mico_system_context_update(mico_system_context_get());
|
||||
}
|
||||
static void KeyShortPress(void)
|
||||
{
|
||||
char i;
|
||||
|
||||
if (RelayOut())
|
||||
{
|
||||
UserRelaySetAll(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
UserRelaySetAll(1);
|
||||
}
|
||||
|
||||
for (i = 0; i < SOCKET_NUM; i++)
|
||||
{
|
||||
UserMqttSendSocketState(i);
|
||||
}
|
||||
}
|
||||
mico_timer_t user_key_timer;
|
||||
// 全局静态变量声明
|
||||
static uint8_t click_count = 0;
|
||||
static mico_timer_t click_end_timer;
|
||||
uint16_t key_time = 0;
|
||||
#define BUTTON_LONG_PRESS_TIME 10 //100ms*10=1s
|
||||
|
||||
static void KeyTimeoutHandler(void* arg)
|
||||
static mico_timer_t led_blink_timer;
|
||||
static bool timer_initialized = false;
|
||||
|
||||
static uint8_t total_blinks = 0;
|
||||
static uint8_t blink_counter = 0;
|
||||
static bool led_state = false;
|
||||
#define BUTTON_LONG_PRESS_TIME 10 //100ms*10=1s
|
||||
// 定时器回调
|
||||
static void _led_blink_timer_handler(void *arg)
|
||||
{
|
||||
if (blink_counter >= total_blinks) {
|
||||
UserLedSet(0); // 闪烁完成,灭灯
|
||||
mico_stop_timer(&led_blink_timer);
|
||||
mico_deinit_timer(&led_blink_timer);
|
||||
timer_initialized = false;
|
||||
return;
|
||||
}
|
||||
|
||||
led_state = !led_state;
|
||||
UserLedSet(led_state ? 1 : 0);
|
||||
blink_counter++;
|
||||
}
|
||||
|
||||
// 安全重入的启动函数
|
||||
void StartLedBlink(uint8_t times)
|
||||
{
|
||||
if (times == 0) return;
|
||||
|
||||
// 如果之前已启动,先停止并清理
|
||||
if (timer_initialized) {
|
||||
mico_stop_timer(&led_blink_timer);
|
||||
mico_deinit_timer(&led_blink_timer);
|
||||
timer_initialized = false;
|
||||
}
|
||||
|
||||
total_blinks = times * 2;
|
||||
blink_counter = 0;
|
||||
led_state = false;
|
||||
|
||||
mico_init_timer(&led_blink_timer, 100, _led_blink_timer_handler, NULL);
|
||||
mico_start_timer(&led_blink_timer);
|
||||
timer_initialized = true;
|
||||
}
|
||||
static void ClickEndTimeoutHandler(void *arg) {
|
||||
if (click_count <= 0) {
|
||||
click_count = 0;
|
||||
return;
|
||||
}
|
||||
KeyEventHandler(click_count,false);
|
||||
click_count = 0;
|
||||
}
|
||||
|
||||
static void KeyTimeoutHandler(void *arg) {
|
||||
static char key_trigger, key_continue;
|
||||
//按键扫描程序
|
||||
static uint8_t key_time = 0;
|
||||
|
||||
char tmp = ~(0xfe | MicoGpioInputGet(Button));
|
||||
key_trigger = tmp & (tmp ^ key_continue);
|
||||
key_continue = tmp;
|
||||
if (key_trigger != 0) key_time = 0; //新按键按下时,重新开始按键计时
|
||||
if (key_continue != 0)
|
||||
{
|
||||
//any button pressed
|
||||
key_time++;
|
||||
if (key_time <= BUTTON_LONG_PRESS_TIME)
|
||||
{
|
||||
key_log("button long pressed:%d",key_time);
|
||||
|
||||
if (key_time == 30)
|
||||
{
|
||||
KeyLongPress();
|
||||
}
|
||||
else if (key_time == 100)
|
||||
{
|
||||
KeyLong10sPress();
|
||||
}
|
||||
else if (key_time == 102)
|
||||
{
|
||||
UserLedSet(1);
|
||||
}
|
||||
else if (key_time == 103)
|
||||
{
|
||||
UserLedSet(0);
|
||||
key_time = 101;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//button released
|
||||
if (key_time < BUTTON_LONG_PRESS_TIME)
|
||||
{ //100ms*10=1s 大于1s为长按
|
||||
key_time = 0;
|
||||
key_log("button short pressed:%d",key_time);
|
||||
KeyShortPress();
|
||||
}
|
||||
else if (key_time > 100)
|
||||
{
|
||||
MicoSystemReboot();
|
||||
if (key_trigger != 0)
|
||||
key_time = 0;
|
||||
|
||||
if (key_continue != 0) {
|
||||
key_time++;
|
||||
// if (key_time > BUTTON_LONG_PRESS_TIME) {
|
||||
// if (key_time == 50) {
|
||||
// KeyLong5sPress();
|
||||
// } else if (key_time > 50 && key_time < 57) {
|
||||
// switch (key_time) {
|
||||
// case 51:
|
||||
// UserLedSet(1);
|
||||
// break;
|
||||
// case 52:
|
||||
// UserLedSet(0);
|
||||
// break;
|
||||
// case 53:
|
||||
// UserLedSet(1);
|
||||
// break;
|
||||
// case 54:
|
||||
// UserLedSet(0);
|
||||
// break;
|
||||
// case 55:
|
||||
// UserLedSet(1);
|
||||
// break;
|
||||
// case 56:
|
||||
// UserLedSet(0);
|
||||
// break;
|
||||
// }
|
||||
// } else if (key_time == 57) {
|
||||
// UserLedSet(RelayOut() && user_config->power_led_enabled);
|
||||
// } else if (key_time == 100) {
|
||||
// KeyLong10sPress();
|
||||
// } else if (key_time == 102) {
|
||||
// UserLedSet(1);
|
||||
// } else if (key_time == 103) {
|
||||
// UserLedSet(0);
|
||||
// key_time = 101;
|
||||
// }
|
||||
// }
|
||||
} else {
|
||||
if (key_time < BUTTON_LONG_PRESS_TIME) {
|
||||
click_count++;
|
||||
|
||||
// 重启 click_end_timer(300ms)
|
||||
mico_rtos_stop_timer(&click_end_timer);
|
||||
mico_rtos_start_timer(&click_end_timer);
|
||||
} else {
|
||||
KeyEventHandler(key_time/10,true);
|
||||
}
|
||||
|
||||
mico_rtos_stop_timer(&user_key_timer);
|
||||
}
|
||||
}
|
||||
|
||||
static void KeyFallingIrqHandler(void* arg)
|
||||
{
|
||||
static void KeyFallingIrqHandler(void *arg) {
|
||||
mico_rtos_start_timer(&user_key_timer);
|
||||
}
|
||||
|
||||
void KeyInit(void)
|
||||
{
|
||||
void KeyInit(void) {
|
||||
MicoGpioInitialize(Button, INPUT_PULL_UP);
|
||||
mico_rtos_init_timer(&user_key_timer, 100, KeyTimeoutHandler, NULL);
|
||||
|
||||
mico_rtos_init_timer(&click_end_timer, 400, ClickEndTimeoutHandler, NULL);
|
||||
MicoGpioEnableIRQ(Button, IRQ_TRIGGER_FALLING_EDGE, KeyFallingIrqHandler, NULL);
|
||||
|
||||
}
|
||||
|
||||
@@ -4,14 +4,35 @@
|
||||
#include "mico.h"
|
||||
#include "micokit_ext.h"
|
||||
|
||||
#define NO_FUNCTION 0x0F
|
||||
#define SWITCH_ALL_SOCKETS 0
|
||||
#define SWITCH_SOCKET_1 1
|
||||
#define SWITCH_SOCKET_2 2
|
||||
#define SWITCH_SOCKET_3 3
|
||||
#define SWITCH_SOCKET_4 4
|
||||
#define SWITCH_SOCKET_5 5
|
||||
#define SWITCH_SOCKET_6 6
|
||||
#define SWITCH_LED_ENABLE 7
|
||||
#define REBOOT_SYSTEM 8
|
||||
#define CONFIG_WIFI 9
|
||||
#define RESET_SYSTEM 10
|
||||
#define SWITCH_CHILD_LOCK_ENABLE 11
|
||||
#define REBOOT_HTTP 12
|
||||
|
||||
extern char socket_status[32];
|
||||
|
||||
void UserLedSet(char x);
|
||||
void KeyInit(void);
|
||||
void UserRelaySet(unsigned char x,unsigned char y);
|
||||
void UserRelaySet(unsigned char x, char y);
|
||||
void UserRelaySetAll(char y);
|
||||
bool RelayOut(void);
|
||||
char* GetSocketStatus();
|
||||
char* GetButtonClickConfig();
|
||||
void SetSocketStatus(char* socket_status);
|
||||
void set_key_map(char user[],int index, char short_func, char long_func);
|
||||
char get_short_func(char val);
|
||||
char get_long_func(char val);
|
||||
char *get_func_name(char func_code);
|
||||
void StartLedBlink(uint8_t times);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -7,26 +7,23 @@
|
||||
#include "user_power.h"
|
||||
|
||||
uint32_t p_count = 0;
|
||||
PowerRecord power_record = { 1,{ 0 } };
|
||||
char power_record_str[1101] = { 0 };
|
||||
PowerRecord power_record = {1, {0}};
|
||||
char power_record_str[1101] = {0};
|
||||
float real_time_power = 0;
|
||||
|
||||
void SetPowerRecord(PowerRecord* pr, uint32_t pw)
|
||||
{
|
||||
pr->powers[(++pr->idx)% PW_NUM] = pw;
|
||||
void SetPowerRecord(PowerRecord *pr, uint32_t pw) {
|
||||
pr->powers[(++pr->idx) % PW_NUM] = pw;
|
||||
}
|
||||
|
||||
char* GetPowerRecord(int idx)
|
||||
{
|
||||
char *GetPowerRecord(int idx) {
|
||||
if (idx > power_record.idx) return "";
|
||||
idx = idx <= power_record.idx - PW_NUM ? 0 : idx;
|
||||
|
||||
int i = idx > 0 ? idx : (power_record.idx - PW_NUM + 1);
|
||||
i = i < 0 ? 0 : i;
|
||||
char* tmp = power_record_str;
|
||||
for (; i <= power_record.idx; i++)
|
||||
{
|
||||
sprintf(tmp, "%lu,", power_record.powers[i%PW_NUM]);
|
||||
char *tmp = power_record_str;
|
||||
for (; i <= power_record.idx; i++) {
|
||||
sprintf(tmp, "%lu,", power_record.powers[i % PW_NUM]);
|
||||
tmp += strlen(tmp);
|
||||
}
|
||||
*(--tmp) = 0;
|
||||
@@ -38,9 +35,11 @@ float n_1s = 0; //在当前这一秒功率中断次数
|
||||
uint64_t past_ns = 0; //系统运行的纳秒数
|
||||
uint64_t irq_old = 0; //上次中断的时间(纳秒)
|
||||
|
||||
static void PowerIrqHandler(void* arg)
|
||||
{
|
||||
static void PowerIrqHandler(void *arg) {
|
||||
//警告! 不能在此函数里调用任何有关moloc()的操作
|
||||
if (p_count == 0) {
|
||||
p_count = user_config->p_count_1_day_ago;
|
||||
}
|
||||
|
||||
p_count++;
|
||||
|
||||
@@ -48,30 +47,27 @@ static void PowerIrqHandler(void* arg)
|
||||
past_ns = mico_nanosecond_clock_value(); //系统运行纳秒数
|
||||
uint64_t spend_ns = past_ns - irq_old;
|
||||
|
||||
if (irq_old % NS + spend_ns <= NS)
|
||||
{
|
||||
if (irq_old % NS + spend_ns <= NS) {
|
||||
n_1s += 1;
|
||||
irq_old = past_ns;
|
||||
return;
|
||||
}
|
||||
|
||||
int n = (spend_ns - past_ns % NS) / NS;
|
||||
n_1s += (float)(NS - irq_old % NS) / spend_ns;
|
||||
n_1s += (float) (NS - irq_old % NS) / spend_ns;
|
||||
real_time_power = 17.1 * n_1s;
|
||||
SetPowerRecord(&power_record, (int)real_time_power);
|
||||
SetPowerRecord(&power_record, (int) real_time_power);
|
||||
|
||||
int i = 0;
|
||||
for (; i < n; i++)
|
||||
{
|
||||
for (; i < n; i++) {
|
||||
real_time_power = 17.1 * NS / spend_ns;
|
||||
SetPowerRecord(&power_record, (int)real_time_power);
|
||||
SetPowerRecord(&power_record, (int) real_time_power);
|
||||
}
|
||||
irq_old = past_ns;
|
||||
n_1s = (float)(past_ns % NS) / spend_ns;
|
||||
n_1s = (float) (past_ns % NS) / spend_ns;
|
||||
}
|
||||
|
||||
void PowerInit(void)
|
||||
{
|
||||
void PowerInit(void) {
|
||||
power_log("PowerInit");
|
||||
MicoGpioInitialize(POWER, INPUT_PULL_UP);
|
||||
MicoGpioEnableIRQ(POWER, IRQ_TRIGGER_FALLING_EDGE, PowerIrqHandler, NULL);
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "mico_socket.h"
|
||||
#include "user_gpio.h"
|
||||
#include "http_server/web_log.h"
|
||||
#include "mqtt_server/user_mqtt_client.h"
|
||||
|
||||
char wifi_status = WIFI_STATE_NOCONNECT;
|
||||
|
||||
@@ -122,6 +123,9 @@ static void WifiLedTimerCallback(void* arg)
|
||||
UserLedSet(-1);
|
||||
break;
|
||||
case WIFI_STATE_CONNECTED:
|
||||
if (!(MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1)){
|
||||
UserMqttInit();
|
||||
}
|
||||
UserLedSet(0);
|
||||
mico_rtos_stop_timer(&wifi_led_timer);
|
||||
if (RelayOut()&&user_config->power_led_enabled)
|
||||
|
||||
38
build.sh
38
build.sh
@@ -1,11 +1,27 @@
|
||||
cd ./TC1/http_server
|
||||
|
||||
python ./test.py > web_data.c
|
||||
|
||||
cd ../..
|
||||
|
||||
mico make clean
|
||||
|
||||
mico make TC1@MK3031@moc total
|
||||
|
||||
cp ./build/TC1\@MK3031\@moc/binary/TC1\@MK3031\@moc.ota.bin ./build/TC1\@MK3031\@moc/binary/ota.bin
|
||||
version_file=".version"
|
||||
if [ ! -f $version_file ]; then echo "v1.0.0" > $version_file; fi
|
||||
|
||||
# 读当前版本并自增
|
||||
ver=$(cat $version_file)
|
||||
major=$(echo $ver | cut -d. -f1 | sed 's/v//')
|
||||
minor=$(echo $ver | cut -d. -f2)
|
||||
patch=$(echo $ver | cut -d. -f3)
|
||||
patch=$((patch + 1))
|
||||
new_ver="v${major}.${minor}.${patch}"
|
||||
echo $new_ver > $version_file
|
||||
|
||||
|
||||
cd ./TC1/http_server
|
||||
|
||||
python ./test.py > web_data.c
|
||||
|
||||
cd ../..
|
||||
|
||||
mico make TC1@MK3031@moc total
|
||||
|
||||
cp ./build/TC1\@MK3031\@moc/binary/TC1\@MK3031\@moc.ota.bin ./build/TC1\@MK3031\@moc/binary/ota.bin
|
||||
|
||||
cp ./build/TC1\@MK3031\@moc/binary/TC1\@MK3031\@moc.ota.bin ./build/TC1\@MK3031\@moc/binary/ota$(date +%Y%m%d%H%M%S).bin
|
||||
|
||||
cp ./build/TC1\@MK3031\@moc/binary/TC1\@MK3031\@moc.all.bin ./build/TC1\@MK3031\@moc/binary/all$(date +%Y%m%d%H%M%S).bin
|
||||
|
||||
|
||||
BIN
doc/IMG_0862.png
Normal file
BIN
doc/IMG_0862.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 396 KiB |
BIN
doc/IMG_0863.png
Normal file
BIN
doc/IMG_0863.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 286 KiB |
BIN
doc/IMG_0887.png
Normal file
BIN
doc/IMG_0887.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 216 KiB |
BIN
doc/IMG_0888.png
Normal file
BIN
doc/IMG_0888.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 265 KiB |
4
mico-os/.gitignore
vendored
Normal file
4
mico-os/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
MiCoder/compiler
|
||||
Projects
|
||||
MiCoder
|
||||
tools_root
|
||||
@@ -39,17 +39,11 @@ OSStatus system_discovery_init( system_context_t * const inContext )
|
||||
init.service_name = "_easylink._tcp.local.";
|
||||
|
||||
/* name#xxxxxx.local. */
|
||||
snprintf( temp_txt, 100, "%s#%c%c%c%c%c%c.local.", inContext->flashContentInRam.micoSystemConfig.name,
|
||||
inContext->micoStatus.mac[9], inContext->micoStatus.mac[10], \
|
||||
inContext->micoStatus.mac[12], inContext->micoStatus.mac[13], \
|
||||
inContext->micoStatus.mac[15], inContext->micoStatus.mac[16] );
|
||||
snprintf( temp_txt, 100, "%s.local.", inContext->flashContentInRam.micoSystemConfig.name);
|
||||
init.host_name = (char*)__strdup(temp_txt);
|
||||
|
||||
/* name#xxxxxx. */
|
||||
snprintf( temp_txt, 100, "%s#%c%c%c%c%c%c", inContext->flashContentInRam.micoSystemConfig.name,
|
||||
inContext->micoStatus.mac[9], inContext->micoStatus.mac[10], \
|
||||
inContext->micoStatus.mac[12], inContext->micoStatus.mac[13], \
|
||||
inContext->micoStatus.mac[15], inContext->micoStatus.mac[16] );
|
||||
snprintf( temp_txt, 100, "%s", inContext->flashContentInRam.micoSystemConfig.name);
|
||||
init.instance_name = (char*)__strdup(temp_txt);
|
||||
|
||||
#ifndef MICO_LOCAL_SERVER_PORT
|
||||
|
||||
@@ -50,7 +50,8 @@ httpd_state_t httpd_state;
|
||||
|
||||
static mico_thread_t httpd_main_thread;
|
||||
|
||||
#define http_server_thread_stack_size 0x2000
|
||||
// 0x8000 不行
|
||||
#define http_server_thread_stack_size 0x6000
|
||||
|
||||
/* Why HTTPD_MAX_MESSAGE + 2?
|
||||
* Handlers are allowed to use HTTPD_MAX_MESSAGE bytes of this buffer.
|
||||
|
||||
@@ -1125,6 +1125,8 @@ int httpd_parse_hdr_tags(httpd_request_t *req, int sock,
|
||||
*/
|
||||
int httpd_get_data(httpd_request_t *req, char *content, int length);
|
||||
|
||||
int httpd_get_data2(httpd_request_t *req, char *content, int length);
|
||||
|
||||
/** @brief Get the incoming JSON data in case of HTTP POST request
|
||||
*
|
||||
* @note This function is an extension to \ref httpd_get_data. Additionally this
|
||||
|
||||
@@ -543,6 +543,52 @@ out:
|
||||
return req->remaining_bytes;
|
||||
}
|
||||
|
||||
int httpd_get_data2(httpd_request_t *req, char *content, int length)
|
||||
{
|
||||
int ret;
|
||||
char *buf;
|
||||
|
||||
// /* Is this condition required? */
|
||||
// if (req->body_nbytes >= HTTPD_MAX_MESSAGE - 2)
|
||||
// return -kInProgressErr;
|
||||
|
||||
if (!req->hdr_parsed) {
|
||||
buf = malloc(HTTPD_MAX_MESSAGE);
|
||||
if (!buf) {
|
||||
system_log("Failed to allocate memory for buffer");
|
||||
return -kInProgressErr;
|
||||
}
|
||||
|
||||
ret = httpd_parse_hdr_tags(req, req->sock, buf,
|
||||
HTTPD_MAX_MESSAGE);
|
||||
|
||||
if (ret != kNoErr) {
|
||||
system_log("Unable to parse header tags");
|
||||
goto out;
|
||||
} else {
|
||||
system_log("Headers parsed successfully\r\n");
|
||||
req->hdr_parsed = 1;
|
||||
}
|
||||
|
||||
free(buf);
|
||||
}
|
||||
|
||||
/* handle here */
|
||||
ret = httpd_recv(req->sock, content,
|
||||
length, 0);
|
||||
if (ret == -1) {
|
||||
system_log("Failed to read POST data");
|
||||
goto out;
|
||||
}
|
||||
/* scratch will now have the JSON data */
|
||||
// content[ret] = '\0';
|
||||
req->remaining_bytes -= ret;
|
||||
system_log("Read %d bytes and remaining %d bytes",
|
||||
ret, req->remaining_bytes);
|
||||
out:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int get_matching_chars(const char *s1, const char *s2)
|
||||
{
|
||||
int match = 0;
|
||||
|
||||
Reference in New Issue
Block a user