Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <DHT.h>
- //DHT Sensor
- #define DHTPIN 2
- #define DHTTYPE DHT22
- DHT dht(DHTPIN, DHTTYPE);
- //Wifi module
- #define SSID "WifiNaam" //Vul hier de naam van je thuisnetwerk in, tussen aanhalingstekens
- #define password "WifiWachtwoord" //Vul hier het wachtwoord in, op dezelfde manier
- //SoftwareSerial espSerial(10,11); //RX, TX
- #define IP "184.106.153.149"
- String msg = "GET /update?key=[updatekey]"; // vul, zonder rechte haken, je ThingSpeak post-API key in
- // Constants
- const String quote = "\"";
- //Variabelen
- int fijnstofpin = 7;
- unsigned long duration;
- unsigned long starttime;
- unsigned long sampletime_ms = 2000;
- unsigned long lowpulseoccupancy = 0;
- float ratio = 0;
- float concentration = 0;
- float humidityVal;
- float tempValC;
- float tempValF;
- float heatIndexC;
- float heatIndexF;
- boolean wifi = false;
- //Verbinden met het aangegeven wifi netwerk
- boolean connectWiFi(){
- Serial.println("AT+CWMODE=1\r\n");
- delay(2000);
- String cmd="AT+CWJAP=\"";
- cmd+=SSID;
- cmd+="\",\"";
- cmd+=password;
- cmd+="\"";
- cmd+="\r\n";
- Serial.println(cmd);
- delay(5000);
- if(Serial.find("OK")){
- wifi = true;
- return true;
- Serial.println("Connected successfully");
- }else{
- wifi = true;
- return false;
- Serial.println("Not connected to wifi!");
- }
- }
- void setup() {
- Serial.begin(115200);
- delay(500);
- Serial.println("AT");
- //Initializeer de dht meter
- dht.begin();
- delay(500);
- //verbind met de wifi
- connectWiFi();
- }
- //De main functie van het programma.
- void loop() {
- //Check of hij verbonden is met de wifi ja/nee, zo nee, probeer opnieuw
- while (wifi) {
- Serial.println("Could not connect, retrying...");
- connectWiFi();
- }
- //Lees DHT-Meters.
- humidityVal = dht.readHumidity(); //vraag luchtvochtigheid aan meter
- tempValC = dht.readTemperature();
- tempValF = dht.readTemperature(true);
- //Lees Fijnstofmeter
- duration = pulseIn(fijnstofpin, LOW);
- lowpulseoccupancy = lowpulseoccupancy+duration;
- if ((millis()-starttime) >= sampletime_ms) //if the sample time = = 30s
- {
- ratio = lowpulseoccupancy/(sampletime_ms*10.0);
- concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62;
- lowpulseoccupancy = 0;
- starttime = millis();
- }
- //Check ff of alle waarden 'goed' zijn uitgelezen, oftewel, ze mogen niet 'null' zijn
- if (isnan(humidityVal) || isnan(tempValC) || isnan(tempValF)) {
- Serial.print("Humidity: ");
- Serial.println(humidityVal);
- Serial.println("Uitlezen DHT sensor mislukt!");
- }
- if (isnan(concentration)) {
- Serial.println("Uitlezen fijnstofmeter mislukt!");
- }
- // Bereken gevoelstemp in C
- heatIndexC = dht.computeHeatIndex(tempValC, humidityVal, false);
- // Bereken gevoelstemp in F
- heatIndexF = dht.computeHeatIndex(tempValF, humidityVal);
- //Uploaden die zooi
- update_Data_To_Server();
- //Init Thingspeak
- /*
- //Printeeeeen
- Serial.print("Luchtvochtigheid: ");
- Serial.print(humidityVal);
- Serial.print(" %\t");
- Serial.print("Temperatuur: ");
- Serial.print(tempValC);
- Serial.print(" *C ");
- Serial.print(tempValF);
- Serial.print(" *F\t");
- Serial.print("Gevoelstemperatuur: ");
- Serial.print(heatIndexC);
- Serial.print(" *C ");
- Serial.print(heatIndexF);
- Serial.println(" *F");
- Serial.print("Fijnstofconcentratie = ");
- Serial.print(concentration);
- Serial.println(" pcs/0.01cf");
- Serial.println("\n");
- */
- delay(2000);
- }
- void update_Data_To_Server()
- {
- //the protocol type (TCP/UDP),the IP address (or domain if you have DNS access)
- //and the port number using the CIPSTART command:
- String cmd = "AT+CIPSTART=" + quote + "TCP" + quote + ",";
- cmd += quote + IP + quote;
- cmd += ",80";
- //Hier print het programma dus AT+CIPSTART="TCP","[IP_ADRES]",80
- Serial.println(cmd);
- //Als hij een error als reactie vindt, probeer opnieuw
- if(Serial.find("Error"))
- {
- return;
- }
- //Stuur lengte van de data
- Serial.print("AT+CIPSEND=");
- Serial.println(cmd.length());
- //Als de database het goedkeurt, krijg je ">" terug
- if(Serial.find(">"))
- {
- //Serial monitor prints the Get command with the API key and fields speacified
- Serial.print(cmd);
- }
- cmd = msg ;
- cmd += "&field1="; //Maakt field1 aan waarin we humidity zetten
- cmd += humidityVal;
- cmd += "&field2="; //Field2 hetzelfde voor gevoelstemperatuur
- cmd += heatIndexC;
- cmd += "&field3="; //Field3 laat de concentratie fijnstof zien
- cmd += concentration;
- Serial.println(cmd);
- //sluit TCP connectie
- Serial.println("AT+CIPCLOSE");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement