Commit f883e637 authored by angel.rivera's avatar angel.rivera

Initial commit

parents
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
"version": "0.2.0",
"configurations": [
]
}
#include <WiFiNINA.h>
#include <Buzzer.h>
#include <rgb_lcd.h>
#include <Wire.h>
#include <Seeed_SHT35.h>
#include <DHT.h>
#include "ThingSpeak.h"
// Function prototypes
float readTemperature();
int readLightLevel();
int readSoundLevel();
void checkAlerts(float temperature, int light, int sound);
// Sensor and output pins
#define DHTPIN 2 // DHT sensor pin
#define DHTTYPE DHT11 // DHT 11 type
#define LIGHT_SENSOR_PIN A0
#define SOUND_SENSOR_PIN A1
#define TEMP_SENSOR_PIN A2
#define TEMP_LED_PIN 8 // LED for temperature alert
#define LIGHT_LED_PIN 9 // LED for light alert
#define SOUND_LED_PIN 10 // LED for sound alert
#define BUZZER_PIN 11 // Buzzer pin
// Initialize DHT sensor and RGB LCD display
DHT dht(DHTPIN, DHTTYPE);
rgb_lcd lcd;
WiFiClient client;
// Temperature sensor constants
const int B = 4275; // B value of the thermistor
const int R0 = 100000; // R0 = 100k
const int pinTempSensor = A0; // Temperature sensor pin
char ssid[] = "Pixel_7931"; // Your network SSID (name)
char pass[] = "norbert1"; // Your network password
int keyIndex = 0; // Your network key Index number (needed only for WEP)
unsigned long myChannelNumber = "2409148"; // Replace YOUR_CHANNEL_NUMBER with your ThingSpeak channel number
const char *myWriteAPIKey = "VHG57K7FOIXKMUJ9"; // Replace YOUR_WRITE_API_KEY with your ThingSpeak Write API Key
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv != "1.0.0") {
Serial.println("Please upgrade the firmware");
}
ThingSpeak.begin(client); // Initialize ThingSpeak
// Initialize the DHT sensor
dht.begin();
delay(2000); // Wait for the sensor to stabilize
// Initialize the RGB LCD
lcd.begin(16, 2);
lcd.setRGB(0, 255, 0); // Set initial backlight color to green
// Set sensor and LED pins as input/output
pinMode(TEMP_SENSOR_PIN, INPUT);
pinMode(LIGHT_SENSOR_PIN, INPUT);
pinMode(SOUND_SENSOR_PIN, INPUT);
pinMode(TEMP_LED_PIN, OUTPUT);
pinMode(LIGHT_LED_PIN, OUTPUT);
pinMode(SOUND_LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT); // Set buzzer pin as output
}
void loop() {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
// Read sensor data
float temp = readTemperature();
int lightLevel = readLightLevel();
int soundLevel = readSoundLevel();
// Display data on RGB LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: "); lcd.print(temp);
lcd.setCursor(0, 1);
lcd.print("Light: "); lcd.print(lightLevel);
lcd.setCursor(0, 2);
lcd.print("Sound: "); lcd.print(soundLevel);
// Print data to Serial Monitor
Serial.print("Temperature: "); Serial.println(temp);
Serial.print("Light Level: "); Serial.println(lightLevel);
Serial.print("Sound Level: "); Serial.println(soundLevel);
// Check thresholds and activate LEDs and buzzer
checkAlerts(temp, lightLevel, soundLevel);
// Write to ThingSpeak
int x = ThingSpeak.writeField(myChannelNumber, 1, temp, myWriteAPIKey);
if (x == 200) {
Serial.println("ThingSpeak update successful.");
} else {
Serial.println("Problem updating ThingSpeak. HTTP error code " + String(x));
}
delay(20000); // Delay for 2 seconds
}
float readTemperature() {
int a = analogRead(pinTempSensor);
float R = 1023.0 / a - 1.0;
R = R0 * R;
float temperature = 1.0 / ((log(R / R0) / B) + 1.0 / 298.15) - 273.15; // convert to temperature via datasheet
return temperature;
}
int readLightLevel() {
return analogRead(LIGHT_SENSOR_PIN);
}
int readSoundLevel() {
return analogRead(SOUND_SENSOR_PIN);
}
void checkAlerts(float temperature, int light, int sound) {
const float TEMP_THRESHOLD = 0.0;
const int LIGHT_THRESHOLD = 600;
const int SOUND_THRESHOLD = 400;
const int SOUND_BUZZER_THRESHOLD = 100; // Buzzer threshold for sound level
if (temperature > TEMP_THRESHOLD) {
digitalWrite(TEMP_LED_PIN, HIGH);
lcd.setRGB(255, 0, 0); // Set backlight color to red for high temperature
} else {
digitalWrite(TEMP_LED_PIN, LOW);
lcd.setRGB(255, 0, 0); // Set backlight color to green for normal temperature
}
if (light > LIGHT_THRESHOLD) {
digitalWrite(LIGHT_LED_PIN, HIGH);
} else {
digitalWrite(LIGHT_LED_PIN, LOW);
}
if (sound > SOUND_THRESHOLD) {
digitalWrite(SOUND_LED_PIN, HIGH);
} else {
digitalWrite(SOUND_LED_PIN, LOW);
}
// Activate buzzer if sound level is below the buzzer threshold
if (sound < SOUND_BUZZER_THRESHOLD) {
digitalWrite(BUZZER_PIN, HIGH);
} else {
digitalWrite(BUZZER_PIN, LOW);
}
}
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