Commit 3328e831 authored by antony.adewunmi-jones's avatar antony.adewunmi-jones

Add new file

parents
//LIBRARIES
#include <math.h>
#include <Wire.h>
#include "rgb_lcd.h"
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include "pass.h"
#include "ThingSpeak.h"
rgb_lcd lcd;
//GET NETWORK DETAILS FROM PASS.H
int a;
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS;
int status = WL_IDLE_STATUS;
unsigned long myChannelNumber = 2409097;
const char * myWriteAPIKey = "5JNCWMVQYEXCIDWV";
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
//TOPICS AND BROKER FOR MQTT
const char broker[] = "test.mosquitto.org";
int port = 1883;
const char AAJtopic[] = "BUTTON ACTIVATED";
const char LIGHTtopic[] = "POSSIBLE MOVEMENT DETECTED";
const char SOUNDtopic[] = "SOUND DETECTED";
//SET INTERVAL FOR MSGS
const long interval = 8000;
unsigned long previousMillis = 0;
int count = 0;
//CONSTANTS
const int colorR = 255;
const int colorG = 255;
const int colorB = 255;
const int buttonPin = 2; // the number of the pushbutton pin
const int buzzerPin = 3; // the number of the buzzer pin
// VARIABLES
int buttonState = 0; // variable for reading the pushbutton status
int prevLightLevel = 0; // prev light level
//SETUP
void setup() {
Serial.begin(9600);
while (!Serial);
//BUTTON INIT
pinMode(buttonPin, INPUT);
//BUZZER INIT
pinMode(buzzerPin, OUTPUT);
//LCD INIT
lcd.begin(16, 2);
lcd.setRGB(colorR, colorG, colorB);
lcd.println("WELCOME");
//CONNECT TO WIFI
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
//FAILED?
Serial.print(".");
delay(5000);
}
Serial.println("You're connected to the network");
Serial.println();
//CONNECT TO THINGSPEAK
ThingSpeak.begin(wifiClient);
//CONNECT TO BROKER
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
//FAILED?
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();
delay(1000);
}
void loop() {
lcd.setCursor(0,0);
lcd.print("WELCOME");
//POLL BROKER TO STAY CONNECTED
mqttClient.poll();
//VALUES TO SEND
int Rvalue = 777;
int Svalue = 1;
int Lvalue = 2;
//CHECK FOR LIGHT LEVEL CHANGE
if (updateLightLevel()){
Serial.print("Sending message to topic: ");
Serial.println(LIGHTtopic);
Serial.println(Lvalue);
// send message, the Print interface can be used to set the message contents
mqttClient.beginMessage(LIGHTtopic);
mqttClient.print(Lvalue);
mqttClient.endMessage();
}
//CHECK FOR SOUND LEVEL AND SEND SOUNDTOPIC
if (updateSoundLevel()){
Serial.print("Sending message to topic: ");
Serial.println(SOUNDtopic);
Serial.println(Svalue);
//SEND MESSAGE
mqttClient.beginMessage(SOUNDtopic);
mqttClient.print(Svalue);
mqttClient.endMessage();
}
//IF BUTTON IS PRESSED
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
//SEND BUTTON PRESS NOTIF
Serial.print("Sending message to topic: ");
Serial.println(AAJtopic);
Serial.println(Rvalue);
//SEND MESSAGE
mqttClient.beginMessage(AAJtopic);
mqttClient.print(Rvalue);
mqttClient.endMessage();
//MSG FOR BUTTON PRESSER
lcd.clear();
lcd.print("DING DONG");
digitalWrite(3, HIGH);
delay(1000);
digitalWrite(3, LOW);
delay(1000);
lcd.setCursor(0,0);
lcd.clear();
lcd.print("WELCOME");
}
}
//RETURN TRUE IF LIGHT LEVEL HAS HAD SIGNIFICANT CHANGE
bool updateLightLevel() {
int lightLevel = analogRead(A1);
int lightLevelChange = prevLightLevel - lightLevel;
prevLightLevel = lightLevel;
//TAKE MEASUREMENTS OF SENSORS AND SEND TO THINGSPEAK
ThingSpeak.setField(1,lightLevel);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (lightLevelChange >50 || lightLevelChange < -50){
return true;
}
else{
return false;
}
}
//RETURN TRUE IF SOUND IS LOUD
bool updateSoundLevel() {
int soundLevel = analogRead(A3);
//TAKE MEASUREMENTS OF SENSORS AND SEND TO THINGSPEAK
ThingSpeak.setField(2, soundLevel);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (soundLevel > 750){
return true;
}
else{
return false;
}
}
\ 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