-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLOW-VOLTAGE-AC-DRIVES-ACS580-Or-ACS880-ESP32-control-Driver-using-PLC-and-Profinet-Protocol.ino
314 lines (285 loc) · 9.96 KB
/
LOW-VOLTAGE-AC-DRIVES-ACS580-Or-ACS880-ESP32-control-Driver-using-PLC-and-Profinet-Protocol.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
#include "Platform.h"
#include "Settimino.h"
#include <HCSR04.h>
#include <ESP32Servo.h>
#define DO_IT_SMALL
Servo myservo; // create servo object to control a servo
HCSR04 hc(27,14); //initialisation class HCSR04 (trig pin , echo pin)
IPAddress Local(192,168,43,90); // Local Address
IPAddress PLC(192,168,43,52); // PLC Address
char ssid[] = "Dar internen"; // Your network SSID (name)
char pass[] = "00000003"; // Your network password (if any)
IPAddress Gateway(192, 168,43, 1);
IPAddress Subnet(255, 255, 255, 0);
int DBNum = 2; // This DB must be present in your PLC
byte Buffer[1024];
S7Client Client;
unsigned long Elapsed;
int niveau;
float MfrequanceF = 14;
int MfrequanceI = 15;
int servoPin = 15;
int times = 15000 ;
int frequenceval;
int LED_BUILTIN = 2;
const int marche = 26;
const int lampe = 25;
const int S3 = 33;
const int S4 = 32;
const int Ready = 21;
const int defaut = 19;
const int thermistors = 18;
const int inuse = 5;
int frequencepot = 34;
int ADC_Max = 4096;
uint16_t count = 0;
void setup() {
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(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("Local IP address : ");
Serial.println(WiFi.localIP());
#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
myservo.setPeriodHertz(50); // Standard 50hz servo
myservo.attach(servoPin, 500, 2400); // attaches the servo on pin 18 to the servo object
// using SG90 servo min/max of 500us and 2400us
// for MG995 large servo, use 1000us and 2000us,
// which are the defaults, so this line could be
digitalWrite(marche, HIGH); // "myservo.attach(servoPin);"
digitalWrite(lampe, HIGH);
digitalWrite(S3, HIGH);
digitalWrite(32, HIGH);
pinMode(marche, OUTPUT);
pinMode(lampe, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(S4, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(Ready, INPUT_PULLUP);
pinMode(defaut, INPUT_PULLUP);
pinMode(thermistors, INPUT_PULLUP);
pinMode(inuse, INPUT_PULLUP);
}
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());
}
else
Serial.println("Connection error");
return Result==0;
}
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);
}
void loop() {
int ReadyVal = digitalRead(Ready);
int defautVal = digitalRead(defaut);
int thermistorsVal = digitalRead(thermistors);
int inuseVal = digitalRead(inuse);
int frequance ;
niveau = int(hc.dist());
frequenceval = analogRead(frequencepot);
Serial.println(frequenceval);
frequenceval = map(frequenceval, 0, ADC_Max, 0, 50);
int Size, Result;
void *Target;
bool P60 , Rege , D3, D4;
#ifdef DO_IT_SMALL
Size=25;
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);
}
if (Connect())
{
digitalWrite(LED_BUILTIN, HIGH);
}
else {
digitalWrite(LED_BUILTIN, LOW);
}
if (ReadyVal == HIGH) {
S7.SetBitAt(0,0,0);
Client.WriteArea(S7AreaDB, // We are requesting DB access
DBNum, // DB Number
0, // Start from byte N.0
1, // We need "Size" bytes
Target); // Put them into our target (Buffer or PDU)
} else {
S7.SetBitAt(0,0,1);
Client.WriteArea(S7AreaDB, // We are requesting DB access
DBNum, // DB Number
0, // Start from byte N.0
1, // We need "Size" bytes
Target); // Put them into our target (Buffer or PDU)
}
if (thermistorsVal == HIGH) {
S7.SetBitAt(0,0,0);
Client.WriteArea(S7AreaDB, // We are requesting DB access
DBNum, // DB Number
8, // Start from byte N.0
1, // We need "Size" bytes
Target); // Put them into our target (Buffer or PDU)
} else {
digitalWrite(marche, HIGH);
S7.SetBitAt(0,0,1);
Client.WriteArea(S7AreaDB, // We are requesting DB access
DBNum, // DB Number
8, // Start from byte N.0
1, // We need "Size" bytes
Target); // Put them into our target (Buffer or PDU)
S7.SetBitAt(0,0,0);
Client.WriteArea(S7AreaDB, // We are requesting DB access
DBNum, // DB Number
20, // Start from byte N.0
1, // We need "Size" bytes
Target); // Put them into our target (Buffer or PDU)
}
if (defautVal == HIGH) {
S7.SetBitAt(0,0,0);
Client.WriteArea(S7AreaDB, // We are requesting DB access
DBNum, // DB Number
12, // Start from byte N.0
1, // We need "Size" bytes
Target); // Put them into our target (Buffer or PDU)
} else {
S7.SetBitAt(0,0,1);
Client.WriteArea(S7AreaDB, // We are requesting DB access
DBNum, // DB Number
12, // Start from byte N.0
1, // We need "Size" bytes
Target); // Put them into our target (Buffer or PDU)
}
if (inuseVal == HIGH) {
S7.SetBitAt(0,0,0);
Client.WriteArea(S7AreaDB, // We are requesting DB access
DBNum, // DB Number
4, // Start from byte N.0
1, // We need "Size" bytes
Target); // Put them into our target (Buffer or PDU)
} else {
S7.SetBitAt(0,0,1);
Client.WriteArea(S7AreaDB, // We are requesting DB access
DBNum, // DB Number
4, // Start from byte N.0
1, // We need "Size" bytes
Target); // Put them into our target (Buffer or PDU)
}
S7.SetIntAt(0,niveau);
Client.WriteArea(S7AreaDB, // We are requesting DB access
DBNum, // DB Number
18, // Start from byte N.0
2, // We need "Size" bytes
Target); // Put them into our target (Buffer or PDU)
S7.SetIntAt(0,frequenceval);
Client.WriteArea(S7AreaDB, // We are requesting DB access
DBNum, // DB Number
6, // Start from byte N.0
2, // We need "Size" bytes
Target); // Put them into our target (Buffer or PDU)
Serial.print("Reading ");Serial.print(Size);Serial.print(" bytes from DB");Serial.println(DBNum);
// Get the current tick
MarkTime();
Result=Client.ReadArea(S7AreaDB, // We are requesting DB access
DBNum, // DB Number
0, // Start from byte N.0
Size, // We need "Size" bytes
Target); // Put them into our target (Buffer or PDU)
if (Result==0)
{
ShowTime();
Serial.println(PDU.DATA[0]); // Print the first byte received
Serial.println(PDU.DATA[1]); // Print the second… and so on
MfrequanceI=S7.IntegerAt(14);
P60 = S7.BitAt(20,0);
Rege = S7.BitAt(24,0);
D3 = S7.BitAt(16,0);
D4 = S7.BitAt(16,1);
if(P60){
digitalWrite(marche, LOW);
}
else {
digitalWrite(marche, HIGH);
}
if(Rege){
digitalWrite(lampe, LOW);
}
else {
digitalWrite(lampe, HIGH);
}
if(D3){
digitalWrite(S3, LOW);
}
else {
digitalWrite(S3, HIGH);
}
if(D4){
digitalWrite(S4, LOW);;
}
else {
digitalWrite(S4, HIGH);
}
MfrequanceI = map(MfrequanceI, 15, 50, 0, 180);
myservo.write(MfrequanceI);
}
else
CheckError(Result);
delay(500);
}