Nie zapomnijcie dodać biblioteki:
https://github.com/ricki-z/SDS011
uzupełnić dane wifi, GUID i AUTH, oraz dane do logowania do serwera.
Jak ktoś ma taki czujnik to może sprawdzić czy to działa. Generalnie szkic bez problemu wgrał się na nodeMCU(8266), uruchomił, zalogował do serwera itd.
Szkic:
Code: Select all
#include <SuplaDevice.h>
#include <SDS011.h> // https://github.com/ricki-z/SDS011
#include <Timers.h> // https://github.com/nettigo/Timers
#include <supla/sensor/DHT.h>
#include <supla/sensor/DS18B20.h>
#include <supla/network/esp_wifi.h>
#include <supla/sensor/general_purpose_measurement.h>
Supla::ESPWifi wifi("xx", "xx");
#define dsPin 14 //D5
#define dhtPin 5 //D1
#define windPin 12 //D6
#define rainPin 2 //D4
#define rxPin 13 //D7 SDS011
#define txPin 15 //D8 SDS011
float p10, p25;
int error;
bool startOdczyt = 0;
bool odczyt = 0;
SDS011 my_sds;
Timer minut5;
Timer sekund30;
Timer minuta;
Timer sekund1;
Timer sekund6;
int rai=0;
bool r=0;
float mmM2=0;
volatile int half_revolutions_time = 2; //Utworzenie zmiennej połowa pełnego obrotu (half revolutions)
volatile int rpm = 0;
unsigned long static last_event = 0;
unsigned long static last_event1 = 0;
int sample = 0;
float moment_kmh;
float temp_kmh;
float correct_moment_kmh;
float average_kmh;
Supla::Sensor::GeneralPurposeMeasurement *moment_wind = nullptr;
Supla::Sensor::GeneralPurposeMeasurement *average_wind = nullptr;
Supla::Sensor::GeneralPurposeMeasurement *rain = nullptr;
Supla::Sensor::GeneralPurposeMeasurement *P2 = nullptr;
Supla::Sensor::GeneralPurposeMeasurement *P10 = nullptr;
void ICACHE_RAM_ATTR rn(){// wektor przerwania deszczomierza
unsigned long static last_event1 = 0;
if (millis() - last_event1 < 50) { //debouncing
return;
}
rai++;
last_event1 = millis();
}
void ICACHE_RAM_ATTR rpm_fan() { //funkcja rpm_fan
if (millis() - last_event < 5) { //debouncing
return;
}
half_revolutions_time = (millis() - last_event);
last_event = millis();
}
void temporaryRPM(){
noInterrupts();
if((last_event + 2000) < millis()){
rpm=0;
moment_kmh = 0;
correct_moment_kmh = 0;
}
else{
rpm = (30000 / half_revolutions_time) ;
moment_kmh = 6.28 * 0.075 * rpm/60.0 * 3.6;// pi x promień czujnika x rpm/60s x ms->km/h
if(moment_kmh < 10){
correct_moment_kmh = moment_kmh * 2.8;
}
else if((moment_kmh >= 10) && (moment_kmh < 25)){
correct_moment_kmh = moment_kmh * 2.7;
}
else if(moment_kmh >= 25){
correct_moment_kmh = moment_kmh * 2.8;
}
}
if(sekund6.available()){
temp_kmh = temp_kmh + correct_moment_kmh;
sample++;
sekund6.restart();
}
if(sample==10){
average_kmh = temp_kmh / 10;
temp_kmh = 0;
sample=0;
sekund6.restart();
}
interrupts() ; //Przywróć przerwania
}
void sds(){
if((minut5.available()) && (startOdczyt ==0)){
sekund30.restart();
startOdczyt = 1;
odczyt = 1;
my_sds.wakeup();
}
if((sekund30.available()) && (odczyt == 1)){
error = my_sds.read(&p25, &p10);
if (!error) {
P2->setValue(p25);
P10->setValue(p10);
Serial.println("P2.5: " + String(p25));
Serial.println("P10: " + String(p10));
minut5.restart();
startOdczyt = 0;
odczyt = 0;
my_sds.sleep();
}
}
}
void setup() {
// Replace the falowing GUID with value that you can retrieve from https://www.supla.org/arduino/get-guid
char GUID[SUPLA_GUID_SIZE] = {xx};
// Replace the following AUTHKEY with value that you can retrieve from: https://www.supla.org/arduino/get-authkey
char AUTHKEY[SUPLA_AUTHKEY_SIZE] = {xx};
my_sds.begin(rxPin, txPin); //RX, TX
Serial.begin(9600);
new Supla::Sensor::DS18B20(dsPin);
new Supla::Sensor::DHT(dhtPin, DHT22);
moment_wind = new Supla::Sensor::GeneralPurposeMeasurement();
average_wind = new Supla::Sensor::GeneralPurposeMeasurement();
rain = new Supla::Sensor::GeneralPurposeMeasurement();
P2 = new Supla::Sensor::GeneralPurposeMeasurement();
P10 = new Supla::Sensor::GeneralPurposeMeasurement();
pinMode(windPin,INPUT_PULLUP);
pinMode(rainPin,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(rainPin), rn, RISING);
attachInterrupt(digitalPinToInterrupt(windPin), rpm_fan, FALLING);
minut5.begin(299999);
sekund30.begin(30001);
minuta.begin(59999);
sekund6.begin(6000);
sekund1.begin(1000);
last_event = millis();
last_event1 = millis();
SuplaDevice.begin(GUID, // Global Unique Identifier
"svrxx.supla.org", // SUPLA server address
"xxx@wp.pl", // Email address used to login to Supla Cloud
AUTHKEY); // Authorization key
}
void loop() {
SuplaDevice.iterate();
temporaryRPM();
sendSupla();
sds();
}
void sendSupla(){
if(sekund1.available()){
moment_wind->setValue(correct_moment_kmh);
sekund1.restart();
}
if(minuta.available()){
mmM2=rai*0.15; // dwa przerwnia na jedną kolebkę, więc 0.15
rain->setValue(mmM2);
average_wind->setValue(average_kmh);
rai=0;
minuta.restart();
}
}