forked from ornea/slowcooker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webSocketEvent.h
168 lines (155 loc) · 4.45 KB
/
webSocketEvent.h
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
#include <WebSocketsServer.h>
WebSocketsServer webSocket(81);
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length)
{
Serial.println();
Serial.print(F("clientid="));
Serial.println(num);
Serial.print (F("Type="));
Serial.println (type);
Serial.print(F("length="));
Serial.print(length);
switch (type)
{
case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\n", num);
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
Serial.println();
// send message to client
webSocket.sendTXT(num, "Connected");
sendPIDData();
sendStatus();
}
break;
case WStype_TEXT:
Serial.printf("[%u] get Text: %s\n", num, payload);
Serial.println();
String spayload = (char*)payload;
Serial.println(spayload);
if (payload[0] == BUTTON_PREAMPLE[0]) //we have a button
{
String id = spayload.substring(1, spayload.indexOf(',')) ;
Serial.print("id :");
Serial.print(id);
if (id == "idMode")
{
AppConfig.Mode++;
if (AppConfig.Mode == invalid) AppConfig.Mode = free_run;
}
switch (AppConfig.Mode) //Switch on statemachine variable
{
case free_run: //Normal State
{
webSocket.broadcastTXT(MODE_AUTO);
}
break;
case forced_high: //Normal State
{
webSocket.broadcastTXT(FORCED_HIGH);
webSocket.broadcastTXT("heater_on");
HEATER_HIGH;
}
break;
case forced_low: //Normal State
{
webSocket.broadcastTXT(FORCED_LOW);
webSocket.broadcastTXT("heater_off");
HEATER_LOW;
}
break;
case invalid: //Normal State
{
//Should never get here
AppConfig.Mode = free_run;
}
break;
}
if (id == "idActive")
{
AppConfig.Flags.bIsOutputInverted = !AppConfig.Flags.bIsOutputInverted; //toggle inversion
if (AppConfig.Flags.bIsOutputInverted)
{
webSocket.broadcastTXT(INVERTED);
}
else
{
webSocket.broadcastTXT(NONINVERTED);
}
}
if (id == "idReset")
{
sm = standby;
//sendPIDData(); //i want to do this but the loop still needs to update the variable
//heating_stage = stage1;
}
if (id == "idUnits")
{
AppConfig.Flags.bIsCelsius = !AppConfig.Flags.bIsCelsius;
}
if (id == "idSave")
{
SaveAppConfig();
}
}
if (payload[0] == STRING_PREAMBLE[0])//STRING_PREAMBLE_SQ )//'@')//STRING_PREAMBLE') //we have a string
{
float f = (spayload.substring(spayload.indexOf(',') + 1, spayload.length())).toFloat();
String arrfieldsinput[] = {"sp1", "sp2", "sp1td", "sp2td", "kp", "ki", "kd", "mvmax", "mvmin", "cal"};
//const byte io_pins [9] = {D0, D1, D2, D3, D4, D5, D6, D7, D8};//
String id = spayload.substring(1, spayload.indexOf(',')) ;
if (id == "sp1")
{
AppConfig.PID[sp1] = f;
}
if (id == "sp2")
{
AppConfig.PID[sp2] = f;
}
if (id == "sp1td")
{
AppConfig.PID[sp1td] = f;
}
if (id == "sp2td")
{
AppConfig.PID[sp2td] = f;
}
if (id == "kp")
{
AppConfig.PID[kp] = f;
}
if (id == "ki")
{
AppConfig.PID[ki] = f;
}
if (id == "kd")
{
AppConfig.PID[kd] = f;
}
if (id == "mvmax")
{
AppConfig.PID[mvmax] = f;
}
if (id == "mvmin")
{
AppConfig.PID[mvmin] = f;
}
if (id == "cal")
{
AppConfig.PID[cal] = f;
}
if (id == "tc")
{
AppConfig.PID[tc] = f * 1000; //in ms
}
if (id == "es")
{
//call all stop
}
}
break;
}
}