Newer
Older
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
/*
ArduinoMqttClient - WiFi Advanced Callback
This example connects to a MQTT broker and subscribes to a single topic,
it also publishes a message to another topic every 10 seconds.
When a message is received it prints the message to the serial monitor,
it uses the callback functionality of the library.
It also demonstrates how to set the will message, get/set QoS,
duplicate and retain values of messages.
The circuit:
- Arduino MKR 1000, MKR 1010 or Uno WiFi Rev.2 board
This example code is in the public domain.
*/
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h> // for MKR1000 change to: #include <WiFi101.h>
#include <avr/wdt.h>
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
//char ssid[] = "PocketCube-ECCB"; // your network SSID (name)
//char pass[] = "1MAD00DH"; // your network password (use for WPA, or use as key for WEP)
char ssid[] = "WebCube4-M3T5"; // your network SSID (name)
char pass[] = "LJ68YQ46"; // your network password (use for WPA, or use as key for WEP)
// To connect with SSL/TLS:
// 1) Change WiFiClient to WiFiSSLClient.
// 2) Change port value from 1883 to 8883.
// 3) Change broker value to a server with a known SSL/TLS root certificate
// flashed in the WiFi module.
const char dom_subdom_service[] = "test/aswf/gpio";
const char clientID[] = "aindout";
const char mqttUser[] = "pissir";
const char mqttPassword[] = "pissir2020";
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
//const char broker[] = "193.206.52.98";
//const char broker[] = "192.168.20.67";
const char broker[] = "test.mosquitto.org";
int port = 1883;
const char willTopic[] = "arduino/will";
const char requestTopic[] = "to/all";
const char inTopic[] = "to/";
const char outTopic[] = "from/";
const char statusTopic[] = "gm/station/status";
const char description0[] ="{\"test/aswf/gpio\":[\n";
const char description1[] = "{\"name\":\"AN0\",\"type\":\"analogin\"},\n{\"name\":\"AN1\",\"type\":\"analogin\"},\n";
const char description2[] = "{\"name\":\"AN2\",\"type\":\"analogin\"},\n{\"name\":\"AN3\",\"type\":\"analogin\"},\n";
const char description3[] = "{\"name\":\"AN4\",\"type\":\"analogin\"},\n{\"name\":\"AN5\",\"type\":\"analogin\"},\n";
const char description4[] = "{\"name\":\"OUT2\",\"type\":\"booleanout\"},\n{\"name\":\"OUT3\",\"type\":\"booleanout\"},\n";
const char description5[] = "{\"name\":\"OUT4\",\"type\":\"booleanout\"},\n{\"name\":\"OUT5\",\"type\":\"booleanout\"}\n";
const char descriptionend[] = "]}";
const long interval = 10000;
unsigned long previousMillis = 0;
// define sensors on the 6 Analog inputs
int sensor[6] = {A0, A1, A2, A3, A4, A5};
uint32_t x=0;
int count = 0;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(5000);
}
Serial.println("You're connected to the network");
//Serial.println();
// You can provide a unique client ID, if not set the library uses Arduin-millis()
// Each client must have a unique client ID
// mqttClient.setId("clientId");
// You can provide a username and password for authentication
// mqttClient.setUsernamePassword("username", "password");
// set a will message, used by the broker when the connection dies unexpectantly
// you must know the size of the message before hand, and it must be set before connecting
String willPayload = "oh no!";
bool willRetain = true;
int willQos = 1;
mqttClient.beginWill(willTopic, willPayload.length(), willRetain, willQos);
mqttClient.print(willPayload);
mqttClient.endWill();
mqttClient.setId(clientID);
mqttClient.setUsernamePassword(mqttUser,mqttPassword);
//Serial.print("Attempting to connect to the MQTT broker: ");
//Serial.println(broker);
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
delay(10000);
wdt_enable(WDTO_2S);
while(1);
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
// set the message receive callback
mqttClient.onMessage(onMqttMessage);
//Serial.print("Subscribing to topic: ");
//Serial.println();
// subscribe to a topic
// the second paramter set's the QoS of the subscription,
// the the library supports subscribing at QoS 0, 1, or 2
int subscribeQos = 1;
String receiveTopic = String(inTopic)+String(dom_subdom_service)+String("/+");
// Serial.println(receiveTopic);
mqttClient.subscribe(receiveTopic.c_str(), subscribeQos);
mqttClient.subscribe(requestTopic, subscribeQos);
// topics can be unsubscribed using:
// mqttClient.unsubscribe(inTopic);
//Serial.print("Waiting for messages on topic: ");
//Serial.println(inTopic);
//Serial.println();
wdt_enable(WDTO_2S);
}
void loop() {
int res;
bool firsttime = true;
if(firsttime) {
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
firsttime = false;
}
wdt_reset();
// call poll() regularly to allow the library to receive MQTT messages and
// send MQTT keep alives which avoids being disconnected by the broker
mqttClient.poll();
// avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay
// see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time a message was sent
previousMillis = currentMillis;
String payload;
String source = String(outTopic)+String(dom_subdom_service)+String("/AN");
source += count;
// Serial.println(source);
x = analogRead(sensor[count]);
payload = "{\"event\":\"";
payload += x;
payload += "\"}";
// Serial.println(source);
// Serial.println(payload);
// send message, the Print interface can be used to set the message contents
// in this case we know the size ahead of time, so the message payload can be streamed
bool retained = false;
int qos = 1;
bool dup = false;
mqttClient.beginMessage(source.c_str(), payload.length(), retained, qos, dup);
mqttClient.print(payload);
if(mqttClient.endMessage()==0){
Serial.println("I'm disconnected");
while (1);
};
count++;
if(count>5) count = 0;
}
}
void onMqttMessage(int messageSize) {
// we received a message, print out the topic and contents
int rel;
int val;
int res;
bool retained = false;
int qos = 1;
bool dup = false;
String msg;
String topic = mqttClient.messageTopic();
// Serial.println(topic);
while (mqttClient.available()) {
msg += ((char)mqttClient.read());
}
if(topic=="to/all") {
// Serial.println(mqttClient.messageTopic());
if(msg=="{\"request\":\"description.json\"}"||msg=="{request:description.json}") {
// Serial.println("sending Description");
msg = description0;
msg += description1;
msg += description2;
msg += description3;
msg += description4;
msg += description5;
msg += descriptionend;
String descriptionTopic = String(outTopic)+String(dom_subdom_service)+String("/description");
mqttClient.beginMessage(descriptionTopic.c_str(), msg.length(), retained, qos, dup);
mqttClient.print(msg);
res = mqttClient.endMessage();
// Serial.println(descriptionTopic);
}
}else {
//Serial.println(topic);
rel = topic.substring(topic.indexOf("OUT")+3).toInt();
// Serial.println(msg);
sscanf(msg.c_str(),"{\"cmd\":%d}",&val);
if(val <0 || val >1) sscanf(msg.c_str(),"{cmd:%d}",&val);
if(val>0 && rel<6 && rel>1) digitalWrite(rel,1);
else if(rel<6 && rel >1) digitalWrite(rel,0);
msg = "{\"event\":\"";
msg += val;
msg += "\"}";
topic = String(outTopic)+String(dom_subdom_service)+String("/OUT");
topic += rel;
//Serial.println(topic);
mqttClient.beginMessage(topic.c_str(), msg.length(), retained, qos, dup);
mqttClient.print(msg);
res = mqttClient.endMessage();
}
}