-
Notifications
You must be signed in to change notification settings - Fork 0
/
DustCollectorRelay.ino
261 lines (212 loc) · 6.31 KB
/
DustCollectorRelay.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
/* Dust Collector Relay
*
* Used to turn on/off the dust collector via WiFi from remotes at each machine
* This program runs at the dust collector and controls the power relay.
* A separate program runs in each remote. Remotes are located by each machine
* so the collection system can be turned on conveniently.
*
* Written by Doug Kimber
*/
// This program is to be uploaded onto the ESP32 arduino.
// Board type is WEMOS LOLIN32
#include <WiFi.h>
#include "SSD1306.h" // Header file for OLED
// Declare the OLED Display object
SSD1306 display( 0x3c, 5, 4 );
// Arrays to store the strings on each line of the display
// Max is 4 lines of 16 chars (really more like 14 chars)
#define DISP_WIDTH 16
#define DISP_LINES 4
#define LINE_HEIGHT 16 // A line is 16 pixels tall
typedef struct Line
{
int offset; // Vertical offset in pixels of this line
char string[ DISP_WIDTH ];
} LINE;
LINE Lines[ DISP_LINES ];
int lineCount; // Current # of displayed lines
#define AP_SSID "DustCollector"
#define AP_PWD "Breathe"
#define PORT 23
/* Set these to your desired AP credentials. */
const char *APssid = AP_SSID;
const char *APpassword = AP_PWD;
int port = PORT;
// pin assignments
const int RelayPin = 16;
// Buffer for an incoming line from the serial port
// Serial I/O is event (interrupt) driven
char lineBuf[ 256 ];
int lineIdx;
// declare telnet server (do NOT put in setup())
WiFiServer TelnetServer( port );
WiFiClient Telnet;
// Implement a scrolling display - scrolls lines up the screen
void DisplayLine( char* theLine )
{
int i;
// If this is the first line then clear the display
if( lineCount == 0 )
display.clear();
// Decide if we need to scroll lines up
if( lineCount >= DISP_LINES )
{
// Need to scroll up. Delete top line and move all others up
for( i = 0; i < DISP_LINES - 1; i++ )
{
strcpy( Lines[ i ].string, Lines[ i + 1 ].string );
}
strcpy( Lines[ DISP_LINES - 1 ].string, theLine );
// Now display the lines
display.clear();
for( i = 0; i < DISP_LINES; i++ )
{
display.drawString( 0, Lines[ i ].offset, Lines[ i ].string );
}
}
else
{
// Save the characters of the new line
strcpy( Lines[ lineCount ].string, theLine );
display.drawString( 0, Lines[ lineCount ].offset, theLine );
lineCount++;
}
display.display();
}
void doChar( char c )
{
// Is this the end of a line?
if( (c == '\n') || (c == '\r') )
{
// This is the end of an input line
processLine();
lineIdx = 0;
}
else
{
lineBuf[ lineIdx ] = c;
lineIdx++;
lineBuf[ lineIdx ] = '\0'; // Null terminate for safety
}
}
void handleTelnet(){
if( TelnetServer.hasClient() ){
// client is connected
if( !Telnet || !Telnet.connected() ){
if( Telnet ) Telnet.stop(); // client disconnected
Telnet = TelnetServer.available(); // ready for new client
} else {
TelnetServer.available().stop(); // have client, block new conections
DisplayLine( "New Connect" );
Telnet = TelnetServer.available(); // ready for new client
}
}
if( Telnet && Telnet.connected() && Telnet.available()){
// client input processing
while( Telnet.available() )
{
char c = Telnet.read();
c = c & 0x7f; // Strip parity bit
doChar( c ); // Add character to the buffer, handle command if command is complete
}
}
}
void startAP()
{
Serial.println();
Serial.println("Starting AP");
WiFi.mode( WIFI_AP );
IPAddress staticIP( 192, 168, 10, 2 );
IPAddress gateway( 192, 168, 10, 1 );
IPAddress subnet( 255, 255, 255, 0 );
WiFi.softAPConfig( staticIP, gateway, subnet );
//WiFi.softAPConfig( staticIP );
WiFi.softAP( APssid ); // Open network without passing password parameter
//WiFi.softAPConfig( staticIP, gateway, subnet );
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
//Serial.print( "IP address: " );
//Serial.println( staticIP );
//Serial.println( WiFi.localIP() );
display.drawString( 0, Lines[ 1 ].offset, staticIP.toString() );
display.display();
delay(1000); // ap delay
}
void StartDisplay()
{
display.init();
display.flipScreenVertically();
display.setFont( ArialMT_Plain_16 );
}
void setup()
{
int i;
Serial.begin( 115200 );
pinMode( RelayPin, OUTPUT );
delay(1000); // serial delay
//Set up the array that holds line characters so we can scroll
for( i = 0; i < DISP_LINES; i++ )
{
Lines[ i ].offset = i * LINE_HEIGHT;
}
lineCount = 0;
StartDisplay();
display.drawString( 0, 0, "Dust Collector" );
display.display();
startAP(); // startup is async
TelnetServer.begin( );
Serial.print("Starting telnet server on port " + (String)port);
//TelnetServer.setNoDelay(true); // ESP BUG ?
Serial.println();
delay(100);
// Initialize global variables
lineIdx = 0;
}
// Process a single command line
void processLine()
{
int val;
DisplayLine( lineBuf );
// Accept only 2 commands, ignore everything else
// One side of relay is connected to power, the other to RelayPin
// Thus, driving RelayPin low turns on the relay
if( strcmp( lineBuf, "DustFree" ) == 0 )
{
// Turn on the relay
Serial.println("Relay On!");
digitalWrite (RelayPin, LOW);
}
if( strcmp( lineBuf, "Dusty" ) == 0 )
{
// Turn on the relay
Serial.println("Relay Off!");
digitalWrite (RelayPin, HIGH);
}
Telnet.stop();
lineIdx = 0;
}
int loopCount = 0;
void loop()
{
// See if we got any wireless data
handleTelnet();
delay(200); // Give recipient time to process any transmissions
// Restart WiFi and the display periodically
loopCount++;
if( loopCount >= 500 ) // > 10 seconds with 200mS delay above
{
Serial.println( "Restarting Display" );
StartDisplay();
Serial.println( "Restarting WiFi" );
//ESP.restart(); // Can't reboot - need to rpeserve the relay state
Telnet.stop();
WiFi.disconnect();
startAP(); // startup is async
delay( 1000 );
TelnetServer.begin( );
delay( 1000 );
loopCount = 0;
}
delay( 1000 );
}