-
Notifications
You must be signed in to change notification settings - Fork 1
/
WIFI_JOYSTICK.ino
356 lines (313 loc) · 11.5 KB
/
WIFI_JOYSTICK.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*----------------------------------------------------------------------
* ESP8266 Wifi Joystick connect to PLC S7-300 (CPU314C-2DP + CP343-1) by TUENHIDIY.
* Thank to Mr.Davide Nardella for his "settimino" Library.
----------------------------------------------------------------------*/
/*----------------------------------------------------------------------
Created 12 Dec 2016
Modified 10 Mar 2019 for Settimino 2.0.0
by Davide Nardella
----------------------------------------------------------------------*/
#include "Platform.h"
#include "Settimino.h"
#include <Adafruit_NeoPixel.h>
#define RING D4 // NEOPIXEL RING 16
#define UP D5 // JOYSTICK UP POSITION
#define DOWN D6 // JOYSTICK DOWN POSITION
#define RIGHT D7 // JOYSTICK RIGHT POSITION
#define LEFT D8 // JOYSTICK LEFT POSITION
#define _RED Adafruit_NeoPixel::Color(255, 0, 0) // RED
#define _GREEN Adafruit_NeoPixel::Color(0, 255, 0) // GREEN
#define _BLUE Adafruit_NeoPixel::Color(0, 0, 255) // BLUE
#define _WHITE Adafruit_NeoPixel::Color(255, 255, 255) // WHITE
#define _BLACK Adafruit_NeoPixel::Color(0, 0, 0) // BLACK
#define _GOLDEN_ROD Adafruit_NeoPixel::Color(238, 232, 170) // GOLDEN ROD
#define _DARK_MAGENTA Adafruit_NeoPixel::Color(148, 0, 211) // DARK MAGENTA
#define _YELLOW_GREEN Adafruit_NeoPixel::Color(85, 107, 47) // YELLOW GREEN
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, RING, NEO_GRB + NEO_KHZ800);
byte WritetoPLC[64]={0}; // Data Array to be written to PLC
byte ReadfromPLC[64]={0}; // Data Array to be read from PLC
// Uncomment next line to perform small and fast data access
#define DO_IT_SMALL
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0F, 0x08, 0xE1 };
IPAddress Local(192, 168, 0, 70); // Local Address
IPAddress PLC(192, 168, 0, 71); // PLC Address
// Following constants are needed if you are connecting via WIFI
// The ssid is the name of my WIFI network (the password obviously is wrong)
char ssid[] = "FTP-Telecom"; // Your network SSID (name)
char pass[] = "12345689"; // Your network password (if any)
IPAddress Gateway(192, 168, 0, 1);
IPAddress Subnet(255, 255, 255, 0);
int DBNum = 1; // This DB must be present in your PLC
byte Buffer[1024];
S7Client Client;
unsigned long Elapsed; // To calc the execution time
//----------------------------------------------------------------------
// Setup : Init Ethernet and Serial port
//----------------------------------------------------------------------
void setup() {
strip.begin();
strip.setBrightness(50);
strip.show(); // Initialize all pixels to 'off'
pinMode(UP, INPUT);
pinMode(DOWN, INPUT);
pinMode(LEFT, INPUT);
pinMode(RIGHT, INPUT);
pinMode(RING, OUTPUT);
// Open serial communications and wait for port to open:
Serial.begin(115200);
#ifdef S7WIFI
//--------------------------------------------- ESP8266 Initialization
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
WiFi.config(Local, Gateway, Subnet);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
Error_Indicator();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("Local IP address : ");
Serial.println(WiFi.localIP());
Ready_Indicator();
#else
//--------------------------------Wired Ethernet Shield Initialization
// Start the Ethernet Library
EthernetInit(mac, Local);
// Setup Time, someone said me to leave 2000 because some
// rubbish compatible boards are a bit deaf.
delay(2000);
Serial.println("");
Serial.println("Cable connected");
Serial.print("Local IP address : ");
Serial.println(Ethernet.localIP());
#endif
}
//----------------------------------------------------------------------
// Connects to the PLC
//----------------------------------------------------------------------
bool Connect()
{
int Result=Client.ConnectTo(PLC,
0, // Rack (see the doc.)
2); // Slot (see the doc.)
Serial.print("Connecting to "); Serial.println(PLC);
if (Result==0)
{
Serial.print("Connected ! PDU Length = "); Serial.println(Client.GetPDULength());
Ready_Indicator();
}
else
Serial.println("Connection error");
Error_Indicator();
return Result==0;
}
//----------------------------------------------------------------------
// Dumps a buffer (a very rough routine)
//----------------------------------------------------------------------
void Dump(void *Buffer, int Length)
{
int i, cnt=0;
pbyte buf;
if (Buffer!=NULL)
buf = pbyte(Buffer);
else
buf = pbyte(&PDU.DATA[0]);
Serial.print("[ Dumping "); Serial.print(Length);
Serial.println(" bytes ]===========================");
for (i=0; i<Length; i++)
{
cnt++;
if (buf[i]<0x10)
Serial.print("0");
Serial.print(buf[i], HEX);
Serial.print(" ");
if (cnt==16)
{
cnt=0;
Serial.println();
}
}
Serial.println("===============================================");
}
//----------------------------------------------------------------------
// Prints the Error number
//----------------------------------------------------------------------
void CheckError(int ErrNo)
{
Serial.print("Error No. 0x");
Serial.println(ErrNo, HEX);
// Checks if it's a Severe Error => we need to disconnect
if (ErrNo & 0x00FF)
{
Serial.println("SEVERE ERROR, disconnecting.");
Client.Disconnect();
}
}
//----------------------------------------------------------------------
// Profiling routines
//----------------------------------------------------------------------
void MarkTime()
{
Elapsed=millis();
}
//----------------------------------------------------------------------
void ShowTime()
{
// Calcs the time
Elapsed=millis()-Elapsed;
Serial.print("Job time (ms) : ");
Serial.println(Elapsed);
}
//----------------------------------------------------------------------
// Main Loop
//----------------------------------------------------------------------
void loop()
{
int Size, Result;
void *Target;
//----------------------------------------------------------------------
// Read Joystick
//----------------------------------------------------------------------
if (digitalRead(UP) == 1)
{
WritetoPLC[0] = 1; // UP position is stored in WritetoPLC Array and written to PLC at DB1.DBX0.0
}
else if (digitalRead(DOWN) == 1)
{
WritetoPLC[0] = 2; // DOWN position is stored in WritetoPLC Array and written to PLC at DB1.DBX0.1
}
else if (digitalRead(LEFT) == 1)
{
WritetoPLC[0] = 4; // LEFT position is stored in WritetoPLC Array and written to PLC at DB1.DBX0.2
}
else if (digitalRead(RIGHT) == 1)
{
WritetoPLC[0] = 8; // RIGHT position is stored in WritetoPLC Array and written to PLC at DB1.DBX0.3
}
else
{
WritetoPLC[0] = 0; // Joystick ZERO position
}
Serial.print("Write data prepare: "); Serial.println(WritetoPLC[0]);
//delay(1000);
//----------------------------------------------------------------------
#ifdef DO_IT_SMALL
Size=1;
Target = NULL; // Uses the internal Buffer (PDU.DATA[])
#else
Size=1024;
Target = &Buffer; // Uses a larger buffer
#endif
// Connection
while (!Client.Connected)
{
if (!Connect())
delay(500);
Ready_Indicator();
}
// Get the current tick
MarkTime();
// Write commands from WIFI JOYSTICK to "DB1 - Byte 0" of PLC
// And PLC will carry out the commands as follows
// UP - DB1.DBX0.0 - CONTROL RELAY 0 AT PLC OUTPUT Q124.0
// DOWN - DB1.DBX0.1 - CONTROL RELAY 1 AT PLC OUTPUT Q124.1
// RIGHT - DB1.DBX0.2 - CONTROL RELAY 2 AT PLC OUTPUT Q124.2
// LEFT - DB1.DBX0.3 - CONTROL RELAY 3 AT PLC OUTPUT Q124.3
Client.WriteArea(S7AreaDB, // We are requesting DB access
DBNum, // DB Number
0, // Start from byte N.0
Size, // We need "Size" bytes
&WritetoPLC); // Put them into our target (Buffer or PDU)
Serial.print("Reading "); Serial.print(Size); Serial.print(" bytes from DB"); Serial.println(DBNum);
// Read the feedback signals from "DB1 - Byte 1" of PLC
// The feedback signals are Normal Open (NO) aux. contact from RELAYS and they are connected to PLC INPUTS as follows
// FEEDBACK UP POSTION - PLC INPUT I124.0 - DB1.DBX1.0
// FEEDBACK DOWN POSTION - PLC INPUT I124.1 - DB1.DBX1.1
// FEEDBACK RIGHT POSTION - PLC INPUT I124.2 - DB1.DBX1.2
// FEEDBACK LEFT POSTION - PLC INPUT I124.3 - DB1.DBX1.3
Result=Client.ReadArea(S7AreaDB, // We are requesting DB access
DBNum, // DB Number
1, // Start from byte N.1
Size, // We need "Size" bytes
&ReadfromPLC); // Put them into our target (Buffer or PDU)
if (Result==0)
{
ShowTime();
Dump(&ReadfromPLC, Size);
// LED Indicator from feedback signals
// It means when we command from WIFI JOYSTICK to PLC, then after processing PLC will feedback the status of this command by LED.
// FEEDBACK UP POSTION
if (bitRead(ReadfromPLC[0],0))
{
for(int i=0; i<4; i++)
{
strip.setPixelColor(i, _RED);
strip.show();
}
}
// FEEDBACK DOWN POSTION
else if (bitRead(ReadfromPLC[0],1))
{
for(int i=8; i<12; i++)
{
strip.setPixelColor(i, _GREEN);
strip.show();
}
}
// FEEDBACK RIGHT POSTION
else if (bitRead(ReadfromPLC[0],2))
{
for(int i=12; i<strip.numPixels(); i++)
{
strip.setPixelColor(i, _BLUE);
strip.show();
}
}
// FEEDBACK LEFT POSTION
else if (bitRead(ReadfromPLC[0],3))
{
for(int i=4; i<8; i++)
{
strip.setPixelColor(i, _WHITE);
strip.show();
}
}
// JOYSTICK ZERO POSTION
else
{
for(int i=0; i<strip.numPixels(); i++)
{
strip.setPixelColor(i, _BLACK);
strip.show();
}
}
}
else
CheckError(Result);
//delay(100);
}
// WIFI JOYSTICK FAULT INDICATION
void Error_Indicator()
{
for(int i=0; i<strip.numPixels(); i++)
{
strip.setPixelColor(i, _DARK_MAGENTA);
strip.show();
}
}
// WIFI JOYSTICK READY INDICATION
void Ready_Indicator()
{
for(int i=0; i<strip.numPixels(); i++)
{ // For each pixel in strip...
strip.setPixelColor(i, _YELLOW_GREEN);
strip.show();
}
}