Commit a73e055c authored by antony.adewunmi-jones's avatar antony.adewunmi-jones

Add new file

parent feeed289
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include "pass.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID
char pass[] = SECRET_PASS; // your network password
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "test.mosquitto.org";
int port = 1883;
const char AAJtopic[] = "BUTTON ACTIVATED";
const char SOUNDtopic[] = "SOUND DETECTED";
const char LIGHTtopic[] = "POSSIBLE MOVEMENT DETECTED";
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
//WIFI
Serial.println("Attempting to connect to SSID: ");
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
int numSsid = WiFi.scanNetworks();
if (numSsid == -1) {
Serial.println("Couldn't get a wifi connection");
while (true);
}
//FAILED
Serial.print(".");
delay(5000);
}
Serial.println("You're connected to the network");
Serial.println();
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());
while (1);
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
//CALLBACK
mqttClient.onMessage(onMqttMessage);
Serial.print("Subscribing to topic: ");
Serial.println(AAJtopic);
Serial.println();
Serial.print("Subscribing to topic: ");
Serial.println(SOUNDtopic);
Serial.println();
Serial.print("Subscribing to topic: ");
Serial.println(LIGHTtopic);
Serial.println();
// TOPIC SUBSCRIPTION
mqttClient.subscribe(AAJtopic);
mqttClient.subscribe(SOUNDtopic);
mqttClient.subscribe(LIGHTtopic);
Serial.println();
}
void loop() {
//POLL BROKER TO STAY CONNECTED
mqttClient.poll();
}
//ON MESSAGE RECIEVED, PRINT IT
void onMqttMessage(int messageSize) {
Serial.print(mqttClient.messageTopic());
Serial.println();
Serial.println();
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment