forked from uzbhutta/XlightSmartController
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSmartController.ino
More file actions
241 lines (209 loc) · 5.96 KB
/
SmartController.ino
File metadata and controls
241 lines (209 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//#define UNIT_TEST_ENABLE //toggle unit testing
/**
* This is the firmware of Xlight SmartController based on Photon/P1 MCU.
* There are two Protocol Domains:
* 1. MySensor Protocol: Radio network and BLE communication
* 2. MQTT Protocol: LAN & WAN
*
* Created by Baoshi Sun <bs.sun@datatellit.com>
* Copyright (C) 2015-2016 DTIT
* Full contributor list:
*
* Documentation:
* Support Forum:
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* REVISION HISTORY
* Version 1.0 - Created by Baoshi Sun <bs.sun@datatellit.com>
*
* DESCRIPTION
* 1. Setup program skeleton
* 2. Define major interfaces
* 3. Define basic data structures
* 4. Define fundamental status machine
* 5. Define primary messages
* 6. Define cloud variables and Functions
*
* ToDo:
* 1. Include MySensor lib
* 2. Include MQTT lib
**/
#ifdef UNIT_TEST_ENABLE
#include "test.ino"
#else
//------------------------------------------------------------------
// Include dependency packages below
//------------------------------------------------------------------
#include "application.h"
#include "xlxConfig.h"
#include "xlSmartController.h"
#include "xlxSerialConsole.h"
#include "SparkIntervalTimer.h"
#include "xlxBLEInterface.h"
//------------------------------------------------------------------
// Program Body Begins Here
//------------------------------------------------------------------
// SINGLETONS:
// theSys: SmartControllerClass
// theConfig: ConfigClass
// theConsole: SerialConsoleClass
// theLog: LoggerClass
// theRadio: RF24ServerClass
// Define hardware IntervalTimer
IntervalTimer sysTimer;
void SysteTimerCB()
{
static UC fastTick = 0; // must be static
static UC slowTick = 0; // must be static
// Change Status Indicator according to system status
// e.g: fast blink, slow blink, breath, etc
// ToDo:
// ToDo: MIC input (tone detection)
// High speed non-block process
if (++fastTick > RTE_TICK_FASTPROCESS) {
fastTick = 0;
theSys.FastProcess();
}
//if( !theConfig.GetDisableWiFi() ) {
// Timeout interuption of Cloud connecting
//#ifndef SYS_SERIAL_DEBUG
//if( WiFi.listening() ) {
/*if (++slowTick > RTE_TICK_SLOWPROCESS) {
slowTick = 0;
//SERIAL_LN("Wi-Fi in listening mode...");
// Get Wi-Fi credential from BLE
//SERIAL_LN("ProcessLocalCommands ...");
//theSys.ProcessLocalCommands();
//SERIAL_LN("ProcessLocalCommands end");
if( WiFi.hasCredentials() ) {
//WiFi.listen(false);
//theSys.ResetSerialPort();
SERIAL_LN("will connect Wi-Fi in system thread");
//theSys.connectWiFi();
}
}*/
// Reset?
//}
//#endif
//}
}
// Set "manual" mode
SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);
//
void setup()
{
WiFi.on();
WiFi.listen(false);
// System Initialization
theSys.Init();
#ifdef SYS_TEST
theSys.InitCloudObj();
Particle.connect();
waitFor(Particle.connected, 5000);
#endif
// Load Configuration
theConfig.LoadConfig();
// Initialize Pins
theSys.InitPins();
// Initialization Radio Interfaces
theSys.InitRadio();
// Open Wi-Fi
if( theConfig.GetDisableWiFi() ) {
WiFi.disconnect();
WiFi.off();
} else {
// Initiaze Cloud Variables & Functions
///It is fine to call this function when the cloud is disconnected - Objects will be registered next time the cloud is connected
theSys.InitCloudObj();
}
// Initialize Serial Console
theConsole.Init();
// Start system timer: callback every n * 0.5ms using hmSec timescale
//Use TIMER6 to retain PWM capabilities on all pins
sysTimer.begin(SysteTimerCB, RTE_DELAY_SYSTIMER, hmSec, TIMER6);
if( !theConfig.GetDisableWiFi() ) {
while(1) {
if( !WiFi.hasCredentials() || !theConfig.GetWiFiStatus() ) {
if( !theSys.connectWiFi() ) {
// get credential from BLE or Serial
SERIAL_LN("will enter listening mode");
WiFi.listen();
break;
}
}
// Connect to Wi-Fi
SERIAL_LN("will connect WiFi");
if( theSys.connectWiFi() ) {
if( theConfig.GetUseCloud() == CLOUD_DISABLE ) {
Particle.disconnect();
} else {
// Connect to the Cloud
if( !theSys.connectCloud() ) {
if( theConfig.GetUseCloud() == CLOUD_MUST_CONNECT ) {
// Must connect to the Cloud
continue;
}
}
}
} else {
if( theConfig.GetUseCloud() == CLOUD_MUST_CONNECT ) {
// Must have network
continue;
}
}
break;
}
// Initialization network Interfaces
theSys.InitNetwork();
// Wait the system started
if( Particle.connected() == true ) {
while( millis() < 2000 ) {
Particle.process();
}
}
}
// Initialize Sensors
theSys.InitSensors();
// System Starts
theSys.Start();
// Setp WD and reset the application if no reponds
ApplicationWatchdog wd(RTE_WATCHDOG_TIMEOUT, System.reset, 256);
}
// Notes: approximate RTE_DELAY_SELFCHECK ms per loop
/// If you need more accurate and faster timer, do it with sysTimer
void loop()
{
static UL lastTick = millis();
static UC tick = 0;
// Process commands
IF_MAINLOOP_TIMER( theSys.ProcessCommands(), "ProcessCommands" );
// Collect data
if( millis() - lastTick >= 1000 ) {
lastTick = millis();
IF_MAINLOOP_TIMER( theSys.CollectData(tick++), "CollectData" );
// Check Max Base RF network enable duration
theSys.CheckRFBaseNetEnableDur();
}
// Act on new Rules in Rules chain
IF_MAINLOOP_TIMER( theSys.ReadNewRules(), "ReadNewRules" );
// ToDo: transfer data
// ToDo: status synchronize
// Process Panel input
IF_MAINLOOP_TIMER( theSys.ProcessPanel(), "ProcessPanel" );
// Self-test & alarm trigger, also insert delay between each loop
IF_MAINLOOP_TIMER( theSys.SelfCheck(RTE_DELAY_SELFCHECK), "SelfCheck" );
//if( !theConfig.GetDisableWiFi() ) {
// Process Could Messages
//if( Particle.connected() == true ) {
IF_MAINLOOP_TIMER( Particle.process(), "ProcessCloud" );
//}
//}
//wd.checkin();
}
#endif