-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathw5500_client.ino
48 lines (37 loc) · 1.41 KB
/
w5500_client.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
// Platform compatibility: espressif8266
#include <Arduino.h>
#include <Ethernet.h>
#include <PicoMQTT.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
PicoMQTT::Client mqtt(client, "broker.hivemq.com");
unsigned long last_publish_time = 0;
int greeting_number = 1;
void setup() {
Serial.begin(115200);
Serial.println("Connecting to network...");
Ethernet.init(5); // ss pin
while (!Ethernet.begin(mac)) {
Serial.println("Failed, retrying...");
}
Serial.println(Ethernet.localIP());
// Subscribe to a topic and attach a callback
mqtt.subscribe("picomqtt/#", [](const char * topic, const char * payload) {
// payload might be binary, but PicoMQTT guarantees that it's zero-terminated
Serial.printf("Received message in topic '%s': %s\n", topic, payload);
});
mqtt.begin();
}
void loop() {
mqtt.loop();
// Publish a greeting message every 3 seconds.
if (millis() - last_publish_time >= 3000) {
// We're publishing to a topic, which we're subscribed too.
// The broker should deliver the messages back to us.
String topic = "picomqtt/esp-" + WiFi.macAddress();
String message = "Hello #" + String(greeting_number++);
Serial.printf("Publishing message in topic '%s': %s\n", topic.c_str(), message.c_str());
mqtt.publish(topic, message);
last_publish_time = millis();
}
}