Advertisement
Guest User

Arduino Code

a guest
Jan 22nd, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1.  
  2. #include <DHT.h>
  3.  
  4. //DHT Sensor
  5. #define DHTPIN 2
  6. #define DHTTYPE DHT22
  7. DHT dht(DHTPIN, DHTTYPE);
  8.  
  9. //Wifi module
  10. #define SSID "WifiNaam" //Vul hier de naam van je thuisnetwerk in, tussen aanhalingstekens
  11. #define password "WifiWachtwoord" //Vul hier het wachtwoord in, op dezelfde manier
  12. //SoftwareSerial espSerial(10,11); //RX, TX
  13. #define IP "184.106.153.149"
  14. String msg = "GET /update?key=[updatekey]"; // vul, zonder rechte haken, je ThingSpeak post-API key in
  15.  
  16. // Constants
  17. const String quote = "\"";
  18.  
  19. //Variabelen
  20. int fijnstofpin = 7;
  21. unsigned long duration;
  22. unsigned long starttime;
  23. unsigned long sampletime_ms = 2000;
  24. unsigned long lowpulseoccupancy = 0;
  25. float ratio = 0;
  26. float concentration = 0;
  27.  
  28. float humidityVal;
  29. float tempValC;
  30. float tempValF;
  31. float heatIndexC;
  32. float heatIndexF;
  33. boolean wifi = false;
  34.  
  35.  
  36. //Verbinden met het aangegeven wifi netwerk
  37. boolean connectWiFi(){
  38. Serial.println("AT+CWMODE=1\r\n");
  39. delay(2000);
  40. String cmd="AT+CWJAP=\"";
  41. cmd+=SSID;
  42. cmd+="\",\"";
  43. cmd+=password;
  44. cmd+="\"";
  45. cmd+="\r\n";
  46. Serial.println(cmd);
  47. delay(5000);
  48. if(Serial.find("OK")){
  49. wifi = true;
  50. return true;
  51. Serial.println("Connected successfully");
  52. }else{
  53. wifi = true;
  54. return false;
  55. Serial.println("Not connected to wifi!");
  56. }
  57. }
  58.  
  59.  
  60.  
  61. void setup() {
  62. Serial.begin(115200);
  63. delay(500);
  64. Serial.println("AT");
  65.  
  66. //Initializeer de dht meter
  67. dht.begin();
  68. delay(500);
  69. //verbind met de wifi
  70. connectWiFi();
  71. }
  72.  
  73. //De main functie van het programma.
  74. void loop() {
  75. //Check of hij verbonden is met de wifi ja/nee, zo nee, probeer opnieuw
  76. while (wifi) {
  77. Serial.println("Could not connect, retrying...");
  78. connectWiFi();
  79. }
  80. //Lees DHT-Meters.
  81. humidityVal = dht.readHumidity(); //vraag luchtvochtigheid aan meter
  82. tempValC = dht.readTemperature();
  83. tempValF = dht.readTemperature(true);
  84.  
  85.  
  86. //Lees Fijnstofmeter
  87. duration = pulseIn(fijnstofpin, LOW);
  88. lowpulseoccupancy = lowpulseoccupancy+duration;
  89. if ((millis()-starttime) >= sampletime_ms) //if the sample time = = 30s
  90. {
  91. ratio = lowpulseoccupancy/(sampletime_ms*10.0);
  92. concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62;
  93. lowpulseoccupancy = 0;
  94. starttime = millis();
  95. }
  96.  
  97. //Check ff of alle waarden 'goed' zijn uitgelezen, oftewel, ze mogen niet 'null' zijn
  98. if (isnan(humidityVal) || isnan(tempValC) || isnan(tempValF)) {
  99. Serial.print("Humidity: ");
  100. Serial.println(humidityVal);
  101. Serial.println("Uitlezen DHT sensor mislukt!");
  102. }
  103. if (isnan(concentration)) {
  104. Serial.println("Uitlezen fijnstofmeter mislukt!");
  105. }
  106.  
  107. // Bereken gevoelstemp in C
  108. heatIndexC = dht.computeHeatIndex(tempValC, humidityVal, false);
  109.  
  110. // Bereken gevoelstemp in F
  111. heatIndexF = dht.computeHeatIndex(tempValF, humidityVal);
  112.  
  113. //Uploaden die zooi
  114. update_Data_To_Server();
  115. //Init Thingspeak
  116.  
  117. /*
  118. //Printeeeeen
  119. Serial.print("Luchtvochtigheid: ");
  120. Serial.print(humidityVal);
  121. Serial.print(" %\t");
  122.  
  123. Serial.print("Temperatuur: ");
  124. Serial.print(tempValC);
  125. Serial.print(" *C ");
  126. Serial.print(tempValF);
  127. Serial.print(" *F\t");
  128.  
  129. Serial.print("Gevoelstemperatuur: ");
  130. Serial.print(heatIndexC);
  131. Serial.print(" *C ");
  132. Serial.print(heatIndexF);
  133. Serial.println(" *F");
  134.  
  135. Serial.print("Fijnstofconcentratie = ");
  136. Serial.print(concentration);
  137. Serial.println(" pcs/0.01cf");
  138. Serial.println("\n");
  139. */
  140.  
  141. delay(2000);
  142. }
  143.  
  144. void update_Data_To_Server()
  145.  
  146. {
  147.  
  148. //the protocol type (TCP/UDP),the IP address (or domain if you have DNS access)
  149. //and the port number using the CIPSTART command:
  150. String cmd = "AT+CIPSTART=" + quote + "TCP" + quote + ",";
  151. cmd += quote + IP + quote;
  152. cmd += ",80";
  153. //Hier print het programma dus AT+CIPSTART="TCP","[IP_ADRES]",80
  154. Serial.println(cmd);
  155. //Als hij een error als reactie vindt, probeer opnieuw
  156. if(Serial.find("Error"))
  157. {
  158. return;
  159. }
  160. //Stuur lengte van de data
  161. Serial.print("AT+CIPSEND=");
  162. Serial.println(cmd.length());
  163. //Als de database het goedkeurt, krijg je ">" terug
  164. if(Serial.find(">"))
  165. {
  166. //Serial monitor prints the Get command with the API key and fields speacified
  167. Serial.print(cmd);
  168. }
  169.  
  170. cmd = msg ;
  171. cmd += "&field1="; //Maakt field1 aan waarin we humidity zetten
  172. cmd += humidityVal;
  173. cmd += "&field2="; //Field2 hetzelfde voor gevoelstemperatuur
  174. cmd += heatIndexC;
  175. cmd += "&field3="; //Field3 laat de concentratie fijnstof zien
  176. cmd += concentration;
  177.  
  178. Serial.println(cmd);
  179.  
  180. //sluit TCP connectie
  181. Serial.println("AT+CIPCLOSE");
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement