Commit d0364108 authored by bryan.quispe's avatar bryan.quispe

Initial commit

parents
#include <Wire.h>
#include <SoftwareSerial.h>
#include <WiFiNINA.h>
#include "ThingSpeak.h" // Always include ThingSpeak header file after other header files and custom macros
char ssid[] = "Pixel_7931"; // network SSID (name)
char pass[] = "norbert1"; // network password
int keyIndex = 0; // Your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = "2408902";
const char *myWriteAPIKey = "AGU3FZ7L5ZZFL5VX";
int number = 0;
// Grove Temperature Sensor
int temperaturePin = A0;
// Grove Dust Sensor
int dustPin = 8;
unsigned long dustDuration;
unsigned long dustStartTime;
unsigned long dustSampleTime_ms = 30000; // sample 30s ;
unsigned long dustLowPulseOccupancy = 0;
float dustRatio = 0;
float dustConcentration = 0;
// Grove Buzzer Sensor
int buzzerPin = 2;
void setup() {
Serial.begin(9600);
while (!Serial); // Wait for serial port to connect
// Check for the WiFi module
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
pinMode(temperaturePin, INPUT);
pinMode(dustPin, INPUT);
pinMode(buzzerPin, OUTPUT);
dustStartTime = millis(); // get the current time;
}
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 Temperature
int temperatureValue = analogRead(temperaturePin);
float voltage = temperatureValue * 5.0 / 1023.0;
float temperature = (voltage - 0.5) * 100.0;
// Read Dust Sensor
dustDuration = pulseIn(dustPin, LOW);
dustLowPulseOccupancy = dustLowPulseOccupancy + dustDuration;
// If the sample time is reached (30s)
if ((millis() - dustStartTime) > dustSampleTime_ms) {
dustRatio = dustLowPulseOccupancy / (dustSampleTime_ms * 10.0); // Integer percentage 0=>100
dustConcentration = 1.1 * pow(dustRatio, 3) - 3.8 * pow(dustRatio, 2) + 520 * dustRatio + 0.62; // using spec sheet curve
// Print dust sensor data
Serial.print("Dust: ");
Serial.print(dustLowPulseOccupancy);
Serial.print(",");
Serial.print(dustRatio);
Serial.print(",");
Serial.println(dustConcentration);
// Reset dust sensor values
dustLowPulseOccupancy = 0;
dustStartTime = millis();
}
// Control Buzzer based on temperature
if (temperature > 25.0) {
// If temperature is above 25 degrees Celsius, turn on the buzzer
digitalWrite(buzzerPin, HIGH);
} else {
// Otherwise, turn off the buzzer
digitalWrite(buzzerPin, LOW);
}
// Print temperature data
Serial.print("Temperature: ");
Serial.println(temperature);
// Write to ThingSpeak
int x = ThingSpeak.writeField(myChannelNumber, 1, temperature, myWriteAPIKey);
if (x == 200) {
Serial.println("ThingSpeak update successful.");
} else {
Serial.println("Problem updating ThingSpeak. HTTP error code " + String(x));
}
// Change the value
number++;
if (number > 99) {
number = 0;
}
delay(20000); // Wait 20 seconds to update ThingSpeak channel again
}
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