forked from RoanBrand/MQTT-Siemens-S7-300
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMQTT_Example.SCL
177 lines (141 loc) · 5.56 KB
/
MQTT_Example.SCL
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
(* Example Application Function Block calling MQTT *)
FUNCTION_BLOCK FB70
TITLE = 'MQTT Example Application'
VERSION : '1.0'
AUTHOR : Brand
VAR_INPUT
con : BOOL; // Rise to Connect
discon : BOOL; // Rise to Disconnect
pub : BOOL; // Rise to Publish
sub : BOOL; // Rise to Subscribe
unsub : BOOL; // Rise to Unsubscribe
END_VAR
VAR_TEMP
i : INT;
END_VAR
VAR
fcon, fpub, fsub,fdiscon,funsub : BOOL; // flags used for rising trigger detection
receivedData : ARRAY[0..1024] OF BYTE;
willData : ARRAY[0..128] OF BYTE;
willDataLength : INT := 0;
pubAckReceived : BOOL :=0;
myPublishMessageID : INT := 0;
publishPayloadData : ARRAY[0..100] OF BYTE;
publishPayloadLength : INT := 0;
END_VAR
BEGIN
(* USER INPUT CONTROLS *)
// Manual connect. Make con high
IF con AND NOT fcon THEN
mqttCONNECT( clientID := 'MYRANDOMCLIENTID',
withUsername:=false,
withPassword:=false,
cleanSession:=true,
withWill:=true,
willQos:=0,
willRetain:=0,
willTopic:='Connection Lost',
willPayload:=willData[0],
willPayloadLength:=0,
username:='a',
password:='b'
);
END_IF;
fcon := con;
// Make discon high to disconnect
IF discon AND NOT fdiscon THEN
mqttDISCONNECT();
END_IF;
fdiscon := discon;
// Make pub high to publish
IF pub AND NOT fpub THEN
myPublishMessageID := mqttPUBLISH( topic := 'SIEMENSTOPIC',
messageid := 0, //message id = 0 autogenerate it, > 0 manually set
payload := publishPayloadData,
payloadLength := publishPayloadLength,
qos := mqttGlobals.MQTTQOS1,
retain := false,
duplicate := false
);
END_IF;
fpub := pub;
// Make sub high to subscribe
IF sub AND NOT fsub THEN
mqttSUBSCRIBE( topic := 'SIEMENSTOPIC',
topicQos := mqttGlobals.MQTTQOS0
);
END_IF;
fsub := sub;
// Make sub high to unsubscribe
IF funsub AND NOT unsub THEN
mqttUNSUBSCRIBE(topic := 'SIEMENSTOPIC');
END_IF;
funsub := unsub;
(* OTHER *)
// Automatic reconnect example if needed
IF mqttData.ethTCPConnected AND (mqttData._State < 0) THEN
//connect(id := 'CPU313C');
mqttCONNECT( clientID := 'CPU313C',
withUsername:=false,
withPassword:=false,
cleanSession:=true,
withWill:=true,
willQos:=0,
willRetain:=0,
willTopic:='Connection Lost',
willPayload:=willData[0],
willPayloadLength:=0,
username:='a',
password:='b'
);
END_IF;
(* RECEIVING *)
IF MQTT_DB.callback THEN
// Received a package from a subscription
IF MQTT_DB.callbackPacketType = mqttGlobals.MQTTPUBLISH THEN
MQTT_DB.callback := false;
IF MQTT_DB.pubTopic = 'SIEMENSTOPIC' THEN
IF MQTT_DB.payloadSize >= 1024 THEN
FOR i:=0 TO 1023 DO
receivedData[i] := MQTT_DB.payload[i];
END_FOR;
ELSE
FOR i:=0 TO MQTT_DB.payloadSize-1 DO
receivedData[i] := MQTT_DB.payload[i];
END_FOR;
END_IF;
END_IF;
END_IF;
// Received a packet from a handshake response
// Example for Qos1 (Single stage handshake with PUBACK response) or Qo2 (Dual stage handshake with PUBCOMP response)
IF (MQTT_DB.callbackPacketType = mqttGlobals.MQTTPUBACK) OR (MQTT_DB.callbackPacketType = mqttGlobals.MQTTPUBCOMP) THEN
MQTT_DB.callback := false;
pubAckReceived := true;
END_IF;
END_IF; //MQTT_DB.callback
END_FUNCTION_BLOCK
(* OB1 Cyclic execution by PLC *)
ORGANIZATION_BLOCK OB1
VAR_TEMP
// Reserved
info : ARRAY[0..19] OF BYTE;
// Temporary Variables
ptrPayloadTemp : ANY;
structPtrPayloadTemp AT ptrPayloadTemp : UDT_STRUCTANYPTR;
END_VAR
(* IMPORTANT: Uncomment this for PN ethernet
MQTT.DB71( connectionID := 1 // IN: INT, only used for external ethernet (CP)
,ipBlock1 := 192 // IN: INT, IP address parameter only with internal ethernet (PN)
,ipBlock2 := 168 // IN: INT, IP address parameter only with PN internal ethernet (PN)
,ipBlock3 := 1 // IN: INT, IP address parameter only with PN internal ethernet (PN)
,ipBlock4 := 1 // IN: INT, IP address parameter only with PN internal ethernet (PN)
,ipPort := 1883 // IN: INT, IP port parameter only with PN internal ethernet (PN)
);
*)
(* IMPORTANT: Uncomment this for CP ethernet
MQTT.MQTT_DB( connectionID := 1 // IN: INT, only used for external ethernet (CP)
,cpLADDR := W#16#100 // IN: WORD, only used for external ethhernet (CP)
);
*)
FB70.DB70(); // Example Application
END_ORGANIZATION_BLOCK