You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.2 KiB
71 lines
1.2 KiB
#include <Arduino.h>
|
|
#include <PubSubClient.h>
|
|
#include <rpcWiFi.h>
|
|
#include <SPI.h>
|
|
|
|
#include "config.h"
|
|
|
|
void connectWiFi()
|
|
{
|
|
while (WiFi.status() != WL_CONNECTED)
|
|
{
|
|
Serial.println("Connecting to WiFi..");
|
|
WiFi.begin(SSID, PASSWORD);
|
|
delay(500);
|
|
}
|
|
|
|
Serial.println("Connected!");
|
|
}
|
|
|
|
WiFiClient wioClient;
|
|
PubSubClient client(wioClient);
|
|
|
|
void reconnectMQTTClient()
|
|
{
|
|
while (!client.connected())
|
|
{
|
|
Serial.print("Attempting MQTT connection...");
|
|
|
|
if (client.connect(CLIENT_NAME.c_str()))
|
|
{
|
|
Serial.println("connected");
|
|
}
|
|
else
|
|
{
|
|
Serial.print("Retying in 5 seconds - failed, rc=");
|
|
Serial.println(client.state());
|
|
|
|
delay(5000);
|
|
}
|
|
}
|
|
}
|
|
|
|
void createMQTTClient()
|
|
{
|
|
client.setServer(BROKER.c_str(), 1883);
|
|
reconnectMQTTClient();
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
Serial.begin(9600);
|
|
|
|
while (!Serial)
|
|
; // Wait for Serial to be ready
|
|
|
|
delay(1000);
|
|
|
|
pinMode(WIO_LIGHT, INPUT);
|
|
pinMode(D0, OUTPUT);
|
|
|
|
connectWiFi();
|
|
createMQTTClient();
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
reconnectMQTTClient();
|
|
client.loop();
|
|
|
|
delay(2000);
|
|
} |