Skip to content

Commit d5d6b7e

Browse files
author
linomp
committed
first tests with beta mqtt client
0 parents  commit d5d6b7e

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
arduino_secrets.h

arduino_secrets _TEMPLATE.h

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

test_WiFiSimpleSender.ino

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
ArduinoMqttClient - WiFi Simple Sender
3+
4+
This example connects to a MQTT broker and publishes a message to
5+
a topic once a second.
6+
7+
The circuit:
8+
- Arduino MKR 1000, MKR 1010 or Uno WiFi Rev2 board
9+
10+
This example code is in the public domain.
11+
*/
12+
13+
#include <ArduinoMqttClient.h>
14+
/*
15+
#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2)
16+
#include <WiFiNINA.h>
17+
#elif defined(ARDUINO_SAMD_MKR1000)
18+
#include <WiFi101.h>
19+
#elif defined(ARDUINO_ARCH_ESP8266)
20+
#include <ESP8266WiFi.h>
21+
#elif defined(ARDUINO_ARCH_ESP32)
22+
#include <WiFi.h>
23+
#endif
24+
*/
25+
#include <WiFiNINA.h>
26+
27+
#include "arduino_secrets.h"
28+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
29+
char ssid[] = SECRET_SSID; // your network SSID (name)
30+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
31+
32+
// To connect with SSL/TLS:
33+
// 1) Change WiFiClient to WiFiSSLClient.
34+
// 2) Change port value from 1883 to 8883.
35+
// 3) Change broker value to a server with a known SSL/TLS root certificate
36+
// flashed in the WiFi module.
37+
38+
WiFiClient wifiClient;
39+
MqttClient mqttClient(wifiClient);
40+
41+
const char broker[] = "apps.xmp.systems";
42+
int port = 1883;
43+
const char topic[] = "iiot-project/test";
44+
45+
const long interval = 1000;
46+
unsigned long previousMillis = 0;
47+
48+
int count = 0;
49+
50+
void setup() {
51+
//Initialize serial and wait for port to open:
52+
Serial.begin(9600);
53+
while (!Serial) {
54+
; // wait for serial port to connect. Needed for native USB port only
55+
}
56+
57+
// attempt to connect to WiFi network:
58+
Serial.print("Attempting to connect to WPA SSID: ");
59+
Serial.println(ssid);
60+
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
61+
// failed, retry
62+
Serial.print(".");
63+
delay(5000);
64+
}
65+
66+
Serial.println("You're connected to the network");
67+
Serial.println();
68+
69+
// You can provide a unique client ID, if not set the library uses Arduino-millis()
70+
// Each client must have a unique client ID
71+
// mqttClient.setId("clientId");
72+
73+
// You can provide a username and password for authentication
74+
// mqttClient.setUsernamePassword("username", "password");
75+
76+
Serial.print("Attempting to connect to the MQTT broker: ");
77+
Serial.println(broker);
78+
79+
if (!mqttClient.connect(broker, port)) {
80+
Serial.print("MQTT connection failed! Error code = ");
81+
Serial.println(mqttClient.connectError());
82+
83+
while (1);
84+
}
85+
86+
Serial.println("You're connected to the MQTT broker!");
87+
Serial.println();
88+
}
89+
90+
void loop() {
91+
// call poll() regularly to allow the library to send MQTT keep alives which
92+
// avoids being disconnected by the broker
93+
mqttClient.poll();
94+
95+
// to avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay
96+
// see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info
97+
unsigned long currentMillis = millis();
98+
99+
if (currentMillis - previousMillis >= interval) {
100+
// save the last time a message was sent
101+
previousMillis = currentMillis;
102+
103+
Serial.print("Sending message to topic: ");
104+
Serial.println(topic);
105+
Serial.print("hello ");
106+
Serial.println(count);
107+
108+
// send message, the Print interface can be used to set the message contents
109+
mqttClient.beginMessage(topic);
110+
mqttClient.print("hello ");
111+
mqttClient.print(count);
112+
mqttClient.endMessage();
113+
114+
Serial.println();
115+
116+
count++;
117+
}
118+
}

0 commit comments

Comments
 (0)