Skip to content

Commit 3a3680b

Browse files
committed
Merge branch 'develop'
2 parents b18a0eb + d1ac8b9 commit 3a3680b

File tree

702 files changed

+55763
-6129
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

702 files changed

+55763
-6129
lines changed

app/Brewpi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@
4242
#include <AppConfig.h>
4343
#include "AppConfigDefault.h"
4444

45-
#define VERSION_STRING "0.5.4"
45+
#define VERSION_STRING "0.5.5"

app/cbox/env.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export USE_PRINTF_FLOAT=n
2-
export SPARK_CLOUD=y
32
export WARNINGS_AS_ERRORS=n

app/controller/Brewpi.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,15 @@ void setup()
8383

8484
#if BREWPI_SIMULATE
8585
simulator.step();
86-
// initialize the filters with the assigned initial temp value
87-
//tempControl.beerSensor->init();
88-
//tempControl.fridgeSensor->init();
8986
#endif
9087
settingsManager.loadSettings();
9188

9289
control.update();
9390

94-
if(WiFi.hasCredentials()){
95-
Particle.connect();
96-
}
91+
System.disable(SYSTEM_FLAG_RESET_NETWORK_ON_CLOUD_ERRORS);
92+
WiFi.connect(WIFI_CONNECT_SKIP_LISTEN);
93+
Particle.connect();
94+
9795

9896
logDebug("init complete");
9997
}

app/controller/DeviceManager.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ Interface * DeviceManager::createDevice(DeviceConfig & config,
141141
case DEVICE_HARDWARE_ONEWIRE_TEMP :
142142
{
143143
auto sensor = new OneWireTempSensor(oneWireBus(config.hw.pinNr), config.hw.address, config.hw.settings.sensor.calibration);
144-
sensor->init();
145144
return sensor;
146145
}
147146
break;

app/controller/PiLink.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@
4444
#endif
4545

4646
#if BREWPI_USE_WIFI
47+
4748
class NetworkSerialMuxer : public Stream
4849
{
4950
private:
5051
TCPServer tcpServer = TCPServer(6666);
5152
TCPClient tcpClient;
5253
Stream * currentStream = &Serial;
54+
bool tcpServerRunning = false;
5355

5456
public:
5557
void print(char c) {
@@ -72,14 +74,23 @@ class NetworkSerialMuxer : public Stream
7274
return currentStream->read();
7375
}
7476

77+
void stopTcp(){
78+
tcpServer.stop();
79+
tcpClient.stop();
80+
tcpServerRunning = false;
81+
}
82+
83+
void startTcp(){
84+
tcpServer.begin();
85+
tcpServerRunning = true;
86+
}
87+
7588
/**
7689
* Check both Serial and WiFi to see if they are connected.
7790
* When Serial is connected it has preference over WiFi.
7891
* Set the current stream to where the data is available and return the number of bytes available
7992
*/
8093
int available() {
81-
static bool tcpServerRunning = false;
82-
8394
int available = 0;
8495

8596
if (Serial.isConnected()) {
@@ -89,37 +100,40 @@ class NetworkSerialMuxer : public Stream
89100
currentStream = &Serial;
90101
}
91102
else{
92-
if (WiFi.ready() && WiFi.localIP()[0] != 0){ // workaround for bug where WiFi.ready() returns true with IP 0.0.0.0
93-
if(!tcpServerRunning) {
94-
tcpServer.begin();
95-
tcpServerRunning = true;
96-
}
103+
if(!WiFi.ready() || WiFi.RSSI() >= 0){
104+
// WiFi is in error state, stop TCP server
105+
if(tcpServerRunning){
106+
stopTcp();
107+
}
108+
}
109+
else{
110+
if(!tcpServerRunning){
111+
startTcp();
112+
}
113+
}
97114

115+
if(tcpServerRunning){
98116
// if a new client appears, drop the old one
99117
TCPClient newClient = tcpServer.available();
100118
if(newClient) {
101119
tcpClient.stop();
102120
tcpClient = newClient;
103121
}
104-
if (tcpClient.connected()) {
122+
if(tcpClient.status()){
105123
available = tcpClient.available();
106124
if(available > 0) {
107125
currentStream = &tcpClient;
108126
}
109127
}
110128
}
111-
else {
112-
tcpServer.stop();
113-
tcpClient.stop();
114-
tcpServerRunning = false;
115-
}
116129
}
117130

118131
return available;
119132
}
120133

121134
void begin(unsigned long rate) {
122135
Serial.begin(rate);
136+
Serial.blockOnOverrun(false);
123137
// WiFi is handled in available()
124138
}
125139

app/controller/env.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export USE_PRINTF_FLOAT=n
2-
export SPARK_CLOUD=n
32
export WARNINGS_AS_ERRORS=n

app/controller/mixins/ControllerMixins.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ void OneWireTempSensorMixin::serializeImpl(JSON::Adapter & adapter)
112112

113113
char addressBuf[17];
114114

115-
printBytes(obj -> sensorAddress, 8, addressBuf); // print to hex string
115+
printBytes(obj -> settings.sensorAddress, 8, addressBuf); // print to hex string
116116
std::string address(addressBuf); // convert to std string
117117
JSON_E(adapter, address);
118-
JSON_OT(adapter, calibrationOffset);
118+
JSON_OT(adapter, settings.calibrationOffset);
119119
}
120120

121121
void TempSensorDisconnectedMixin::serializeImpl(JSON::Adapter & adapter)

app/wifi-test/application.cpp

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
#include "application.h"
2+
#include <inttypes.h>
3+
4+
SYSTEM_MODE(MANUAL);
5+
SYSTEM_THREAD(ENABLED);
6+
SerialLogHandler traceLog(LOG_LEVEL_TRACE);
7+
8+
static TCPServer tcpServer = TCPServer(6666);
9+
static TCPClient tcpClient;
10+
bool tcpServerRunning;
11+
12+
// Toggle LED pin to see that application loop is not blocked.
13+
// You could use D7 when testing with a Photon.
14+
// I'm using another pin here, because D7 is one of the SWD pins used by the debugger
15+
const int LED_PIN = P1S0;
16+
17+
18+
void stopTcp(){
19+
tcpServer.stop();
20+
tcpClient.stop();
21+
tcpServerRunning = false;
22+
Serial.print("TCP server stopped\n");
23+
}
24+
25+
void startTcp(){
26+
tcpServer.begin();
27+
tcpServerRunning = true;
28+
Serial.print("TCP server started\n");
29+
}
30+
31+
32+
void handle_network_events(system_event_t event, int param){
33+
switch(param){
34+
case network_status_powering_on:
35+
break;
36+
case network_status_on:
37+
break;
38+
case network_status_powering_off:
39+
break;
40+
case network_status_off:
41+
break;
42+
case network_status_connecting:
43+
break;
44+
case network_status_connected:
45+
break;
46+
default:
47+
break;
48+
}
49+
Serial.printf("network event %d\n", param);
50+
}
51+
52+
void handle_cloud_events(system_event_t event, int param){
53+
switch(param){
54+
case cloud_status_connecting:
55+
break;
56+
case cloud_status_connected:
57+
break;
58+
case cloud_status_disconnecting:
59+
break;
60+
case cloud_status_disconnected:
61+
break;
62+
default:
63+
break;
64+
}
65+
Serial.printf("cloud event %d\n", param);
66+
}
67+
68+
// return time that has passed since timeStamp, take overflow into account
69+
system_tick_t timeSince(system_tick_t previousTime) {
70+
system_tick_t currentTime = millis();
71+
if(currentTime>=previousTime){
72+
return currentTime - previousTime;
73+
}
74+
else{
75+
// overflow has occurred
76+
return (currentTime + 1440000) - (previousTime +1440000); // add a day to both for calculation
77+
}
78+
}
79+
80+
81+
void setup() {
82+
System.disable(SYSTEM_FLAG_RESET_NETWORK_ON_CLOUD_ERRORS);
83+
Serial.begin(115200);
84+
pinMode(LED_PIN, OUTPUT);
85+
86+
87+
WiFi.connect(WIFI_CONNECT_SKIP_LISTEN);
88+
Particle.connect();
89+
90+
System.on(network_status, handle_network_events);
91+
System.on(cloud_status, handle_cloud_events);
92+
}
93+
94+
void loop() {
95+
static system_tick_t last_update = millis();
96+
static system_tick_t lastLedToggle = millis();
97+
98+
if(!WiFi.ready()){
99+
// WiFi is in error state, stop TCP server
100+
if(tcpServerRunning){
101+
stopTcp();
102+
}
103+
}
104+
else{
105+
if(!tcpServerRunning){
106+
startTcp();
107+
}
108+
}
109+
110+
if(tcpServerRunning){
111+
if(tcpClient.status()){
112+
bool noErrors = true;
113+
while (noErrors && tcpClient.available() > 0) {
114+
int received = tcpClient.read();
115+
switch(received){
116+
case ' ':
117+
case '\n':
118+
case '\r':
119+
break;
120+
case 't':
121+
{
122+
size_t result = tcpClient.write("toc"); // send toc back over tcp
123+
Serial.printf("hw->py: toc (%d bytes sent) \n", result); // confirm toc sent over tcp
124+
}
125+
break;
126+
default:
127+
if(received < 0){
128+
Serial.printf("Receive error: %d\n", received); // confirm toc sent over tcp
129+
noErrors = false;
130+
}
131+
else{
132+
Serial.printf("py->hw: %c\n", received); // confirm character received from tcp
133+
}
134+
break;
135+
}
136+
}
137+
}
138+
// listen for a new client, drop the old one if a new client arrives
139+
TCPClient newClient = tcpServer.available();
140+
if(newClient) {
141+
Serial.print("New TCP client\n");
142+
tcpClient.write("New TCP client arrived, dropping you."); // stop old client
143+
tcpClient.stop(); // stop old client
144+
tcpClient = newClient;
145+
}
146+
}
147+
148+
// print status on serial every second
149+
if(timeSince(last_update) >= 1000UL ) {
150+
last_update = millis();
151+
bool wifiReady = WiFi.ready();
152+
IPAddress ip = WiFi.localIP();
153+
int signal = WiFi.RSSI();
154+
int clientConnected = tcpClient.connected();
155+
Serial.printf(
156+
"WiFi.ready(): %d\t\t"
157+
"IP: %d.%d.%d.%d\t\t"
158+
"RSSI: %d\t\t"
159+
"TCP client connected: %d\t\t"
160+
"millis(): %" PRIu32 "\n",
161+
wifiReady,
162+
ip[0],ip[1],ip[2],ip[3],
163+
signal,
164+
clientConnected,
165+
last_update);
166+
}
167+
168+
if(timeSince(lastLedToggle) >= 200UL ) {
169+
lastLedToggle = millis();
170+
static bool ledOn = true;
171+
ledOn = !ledOn;
172+
digitalWrite(LED_PIN, ledOn);
173+
}
174+
Particle.process();
175+
}

app/wifi-test/build.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
INCLUDE_DIRS += $(SOURCE_PATH)/platform/wiring
2+
INCLUDE_DIRS += $(SOURCE_PATH)/platform/spark
3+
4+
CSRC += $(call target_files,app/wifi-test,*.c)
5+
CPPSRC += $(call target_files,app/wifi-test,*.cpp)

0 commit comments

Comments
 (0)