Commit 35c504fc authored by bryan.quispe's avatar bryan.quispe

Replace sketch_jan22b.ino

parent 6a27ac1c
int pin = 8; #include <Wire.h>
unsigned long duration; #include <SoftwareSerial.h>
unsigned long starttime; #include <WiFiNINA.h>
unsigned long sampletime_ms = 30000;//sampe 30s ; #include "ThingSpeak.h" // Always include ThingSpeak header file after other header files and custom macros
unsigned long lowpulseoccupancy = 0;
float ratio = 0; char ssid[] = "Pixel_7931"; // network SSID (name)
float concentration = 0; char pass[] = "norbert1"; // network password
int keyIndex = 0; // Your network key Index number (needed only for WEP)
void setup() WiFiClient client;
{
Serial.begin(9600); unsigned long myChannelNumber = "2408902";
pinMode(pin,INPUT); const char *myWriteAPIKey = "AGU3FZ7L5ZZFL5VX";
starttime = millis();//get the current time;
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() void loop() {
{ // Connect or reconnect to WiFi
duration = pulseIn(pin, LOW); if (WiFi.status() != WL_CONNECTED) {
lowpulseoccupancy = lowpulseoccupancy+duration; Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
if ((millis()-starttime) > sampletime_ms)//if the sampel time == 30s while (WiFi.status() != WL_CONNECTED) {
{ WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100 Serial.print(".");
concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve delay(5000);
Serial.print(lowpulseoccupancy);
Serial.print(",");
Serial.print(ratio);
Serial.print(",");
Serial.println(concentration);
lowpulseoccupancy = 0;
starttime = millis();
} }
} Serial.println("\nConnected.");
\ No newline at end of file }
// 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