43 lines
972 B
C
43 lines
972 B
C
|
|
||
|
#ifndef _WIFI_FUNCTIONS_H_
|
||
|
#define _WIFI_FUNCTIONS_H_
|
||
|
|
||
|
#include <WiFi.h>
|
||
|
#include <MQTT.h>
|
||
|
|
||
|
#include "wifi_settings.h"
|
||
|
|
||
|
|
||
|
WiFiClient net;
|
||
|
MQTTClient client;
|
||
|
|
||
|
void connect() {
|
||
|
Serial.print("checking wifi...");
|
||
|
while (WiFi.status() != WL_CONNECTED) {
|
||
|
Serial.print(".");
|
||
|
delay(1000);
|
||
|
}
|
||
|
|
||
|
Serial.print("\nconnecting...");
|
||
|
while (!client.connect(client_id, "public", "public")) {
|
||
|
Serial.print(".");
|
||
|
delay(1000);
|
||
|
}
|
||
|
|
||
|
Serial.println("\nconnected!");
|
||
|
|
||
|
client.subscribe("/hello");
|
||
|
// client.unsubscribe("/hello");
|
||
|
}
|
||
|
|
||
|
|
||
|
void messageReceived(String &topic, String &payload) {
|
||
|
Serial.println("incoming: " + topic + " - " + payload);
|
||
|
|
||
|
// Note: Do not use the client in the callback to publish, subscribe or
|
||
|
// unsubscribe as it may cause deadlocks when other things arrive while
|
||
|
// sending and receiving acknowledgments. Instead, change a global variable,
|
||
|
// or push to a queue and handle it in the loop after calling `client.loop()`.
|
||
|
}
|
||
|
|
||
|
#endif
|