forked from ornea/slowcooker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlowCook.ino
274 lines (219 loc) · 8.39 KB
/
SlowCook.ino
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
ToDo
Update tick marks based on time since last Reset
Finish the Farenheit Option so it makes sense
Check to make sure HEATER is off when probe disconnected i.e. when reading is a NaN
RC522 MODULE Uno/Nano MEGA
IRQ N/A N/A BR
NSS D10 D53 WH/BR
SCK D13 D52 GR
MOSI D11 D51 WH/GR
MISO D12 D50 OR
GND GND GND WH/OR
RST D9 D5 BL
3.3V 3.3V 3.3V WH/BL
WEMOS
IRQ N/A WH/BL (Blue)
NSS (SDA) D8 WH/GR (Black)
SCK D5 GR (Yellow)
MOSI D7 WH/OR (Red)
MISO D6 BLUE (Green)
GND GND OR (Orange)
RST D0 WH/BR (Brown)
3.3V VCC BR (Grey)
H0 Blue { 53, 5},
H1 Pink { 49, 6},
H2 Silver { 48, 7 },
H3 Red {47, 8}
Test ok with Mega
Tested OK with 6m of cat5
//Normal run
//Vcc 4.85v
//D4 3.36V
//D3 3.36v
//D2 84mv
//D1 84mV
//RX 3.36V
//TX 3.36V
//Res 3.36V
//A0 60mV
//D0 84mV
//D5 84mV
//D6 84mV
//D7 85mV
//D8 85mV
//3.3v 3.36V
//Pins good for input D0,D1,D2,D5,D6,D7
//D0 GPIO16 dont care
//D1 GPIO05 Dont Care
//D2 GPIO04 Dont Care
//D3 GPIO00 can not be low during a reset, but must be low during a reflash
//D3 GPIO00 can be high during a reset but must be low during a reflash
//D3 GPIO00 can be used with wemos relay shield
//D3 pull up
//D4 GPIO02 can not be low during a reset
//D4 GPIO02 can be high during a reset and reflash
//D4 GPIO02 cant be used with wemos relay shield. Will Not reset
//D4 pull up
//D5 GPIO14 Dont Care
//D5 GPIO14 SPI Clock
//D6 GPIO12 Dont Care
//D6 GPIO12 SPI MISO
//D7 GPIO13 (Serial.swap() -> RX) Dont Care
//D7 GPIO13 SPI MOSI
//D8 GPIO15 (Serial.swap() -> TX) can not be high during a reset or a reflash
//D8 GPIO15 (Serial.swap() -> TX) can be low during a reset and reflash
//D8 pull down
//TX GPIO01
//RX GPIO03
*/
//#define USE_MAX6675
#define USE_DHT
//#define USE_ADC
#define SerialSpeed 115200
#define INTERVAL_1_DAY 60 * 60 * 24 *1000 //86400000///24 // 1day => 24*60*60*1000
#define INTERVAL_1_HOUR 60 *60 * 1000
#define INTERVAL_1_MIN 60 * 1000
#define INTERVAL_1_SEC 1000
#define CELSIUS "DEG_C"
#define FARENHEIT "DEG_F"
#define INVERTED "Active Low"
#define NONINVERTED "Active High"
#define MODE_AUTO "Free Run"
#define FORCED_HIGH "Forced High"
#define FORCED_LOW "Forced Low"
#define HEATER_OFF "heater_off"
#define HEATER_ON "heater_on"
#define HEATER D0
#define HEATER_HIGH digitalWrite(HEATER, HIGH)
#define HEATER_LOW digitalWrite(HEATER, LOW)
#define ISHEATERHIGH digitalRead(HEATER)
#define degf(C) ((C*9/5)+32)//(0°C × 9/5) + 32 = 32°F
#define BUTTON_PREAMPLE "*"
#define STRING_PREAMBLE "@"
#define PID_DATA_PREAMBLE "&"
#define TRUE true
#define FALSE false
const char __SKETCH__[] = __FILE__; //Used to get the current path
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>//Used to send heartbeats
#include <WiFiUdp.h>
#include <ArduinoOTA.h> //Update Flash Over The Air
#include <EEPROM.h> //Saves various settings
const byte io_pins [9] = {D0, D1, D2, D3, D4, D5, D6, D7, D8};
//const byte io_pins[9] = {16, 5, 4, 0, 2, 14, 12, 13, 15};
int ticks = 0; //Keeps track of how many times the PID loop has been executed.
//The following info is included in the Build Info page
const String strSummary = "<br><br>Summary<br>PID Slow Cooker <br>controller<br>By Justin Richards";
//byte data[MAX_LEN];
unsigned long next_heartbeat_Millis = 0; //Time to send a http request to webserver for remote logging if needed
unsigned long previous_PID_Millis = 0; //Keeps track of timming for PID loop execution
unsigned long previous_ON_Millis = 0; //PWM implemented in software
unsigned long previous_OFF_Millis = 0;
unsigned long previousyieldMillis = 0; //If probe is disconnected, ensures that utility functions Wifi etc are run periodically in the background
unsigned long smMillis = 0;
//PID variables
float error = 0;
float pTerm = 0;
float iTerm = 0;
float dTerm = 0;
float sensorValue = 0;
float currentHeat = 0;
float currentHumidity = 0;
int CV; //Control Variable
enum sm //state machine enumerated states
{
standby, stage1vectoring, stage1sustain, stage2vectoring, stage2sustain, cyclecomplete
};
int sm = 0;
enum PID //used as index into PID array of floats
{
sp1, sp2, sp1td, sp2td, kp, ki, kd, mvmax, mvmin, cal, tc
};
enum modes
{
free_run, forced_high, forced_low, invalid
};
//int heating_stage = stage1;
typedef struct APP_CONFIG //AppConfig
{
char E_sign[16] = "AABbCcDdEe6b"; //This is a signature that is used to determine
//if EEPROM is to be updated with the hardcoded
//values or continue using previously saved values
//must be at the start of the struct
#ifdef real_credentials
#error
#endif
#include "creds.h" //contains SSID/KEY pairs
//#ifdef credentials
// #error
//#endif
//Default Configs as EP End Point
char EpHostname[15] = "hummingbird"; //End Point Hostname
char EpControlHostname[15] = "hummingbird"; //If we need to send UDP messages to another controller. Not used here
//the requirement to statically seems to have been mitigated by WiFi.persistent(false); in _init.h
uint8_t EpIP[4] = {172, 16, 10, 220}; //I have issues when a Cisco Router issues the IP address.
uint8_t EpMASK[4] = {255, 255, 0, 0}; //So when in that environment I statically assign
uint8_t EpGW[4] = {172, 16, 0, 1};
uint8_t EpDNS[4] = {172, 16, 0, 1};
//Default Configs as AP Access Point so it can be configured for a different network
char ApSSID[32] = "hummingbird"; //Max 32 Chars
char ApKey[64];
uint8_t ApIP[4] = {192, 168, 12, 1};
uint8_t ApMASK[4] = {255, 255, 255, 0};
uint8_t ApGW[4] = {192, 168, 12, 1};
// sp1, sp2, sp1td, sp2td, kp, ki, kd, mvmax, mvmin, cal, tc (timeconstant)
float PID[11] = {5, 10, 0, 0, 0.5, 0.4, 0.5, 140.0, 0, 0, 5000}; //
uint8_t Mode = 0;
struct FLAGS
{
unsigned char b7: 1;
unsigned char bIsCelsius: 1;
unsigned char bIsOutputInverted : 1; //Reverses the logic
unsigned char xbIsModeAuto : 1; //'1' Normal mode
unsigned char xbIsOutForcedHigh : 1; //'1' forces Output HIGH
unsigned char bUseStaticIP : 1; //When Cisco Router is doing DHCP
unsigned char bIsDHCPEnabled : 1; //Not used
unsigned char bInConfigMode : 1; //Not used
// } Flags = { 0, 1, 0, 1, 0, 1, 0, 0};//Leftmost is b7 use static
} Flags = { 0, 1, 0, 1, 0, 0, 0, 0};//Leftmost is b7 use dhcp
// */// Flag structure
uint16_t PWMfreq = 100;
} ;
APP_CONFIG AppConfig;
ESP8266WebServer server(80); //instantiate server object of type class ESP8266WebServer
//WiFiClient client;
HTTPClient http; //instantiate http object of type class HTTPClient
#include "helpers.h"
#include "mdns_query.h"
#if 0
void sendHeartBeat(void)
{
IPAddress ipaddr;
char buffers [200];
//char ipaddr[50];
ipaddr = WiFi.localIP();//.toString();
//Serial.println(F("Sending HeartBeat ... " );
sprintf(buffers, "%shn=%s&ip=%03d.%03d.%03d.%03d&ticks=%010d&heat=%06.2f&Humidity=%06.2f&pid=%03d&err=%06.2f&iterm=%06.2f&dTerm=%06.2f&output=%1d&ms=%010d",
URL_Heartbeat, AppConfig.EpHostname, ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3], ticks, currentHeat, currentHumidity, CV, error, iTerm, dTerm, getPinMode(HEATER), millis());
// http.begin(URL_Heartbeat + String(AppConfig.EpHostname) + "&ip=" + WiFi.localIP().toString() + buffers);//"&millis=" + millis() + "&joto=" + currentHeat + "&hum=" + currentHumidity);
http.begin(buffers);//"&millis=" + millis() + "&joto=" + currentHeat + "&hum=" + currentHumidity);
Serial.print(buffers);
int httpCode = http.GET();
Serial.print(F(" Http Resp: "));
Serial.println(httpCode);
/* if (httpCode == HTTP_CODE_OK)
{
String payload = http.getString();
Serial.println(payload);
}
*/
//sprintf(buffers,"%s,%03d.%03d.%03d.%03d",AppConfig.EpHostname,ipaddr[0],ipaddr[1],ipaddr[2],ipaddr[3]);
//Serial.println(buffers);
http.end();
}
#endif
#include "pid.h"
#include "webSocketEvent.h"