Components and supplies
DHT22 Digital Temperature & Humidity Sensor Module
Breadboard 100x160
Arduino MKR WiFi 1010
Pack of 50 female-female jumper wires in various colors
Apps and platforms
Arduino IoT Cloud
Project description
Code
Arduino IoT Cloud Thing MKR WiFi 1010 and DHT22
cpp
Temperature and Humidity monitoring using MKR WiFi
1/* 2 Sketch generated by the Arduino IoT Cloud Thing "MKR WiFi 1010 and DHT22" 3 https://create.arduino.cc/cloud/things/e75efe13-eb5e-432a-86d3-0bf1cd34aaac 4 5Arduino IoT Cloud Variables description 6 7 The following variables are automatically generated and updated when changes are made to the Thing 8 9 CloudTemperatureSensor temperature; 10 CloudRelativeHumidity humidity; 11 12 Variables which are marked as READ/WRITE in the Cloud Thing will also have functions 13 which are called when their values are changed from the Dashboard. 14 These functions are generated with the Thing and added at the end of this sketch. 15*/ 16 17#include "thingProperties.h" 18#include <Adafruit_Sensor.h> 19#include <DHT.h> 20#include <DHT_U.h> 21 22#define DHTPIN 7 // Digital pin connected to the DHT sensor 23#define DHTTYPE DHT22 // Write DHT11 or DHT22 According to your Sensor 24 25DHT_Unified dht(DHTPIN, DHTTYPE); 26uint32_t delayMS; 27unsigned long previousMillis = 0; 28const long interval = 20000; //milliseconds total time for 20 Seconds 29 30 31 32void setup() { 33 // Initialize serial and wait for port to open: 34 Serial.begin(9600); 35 // This delay gives the chance to wait for a Serial Monitor without blocking if none is found 36 delay(1500); 37 38 // Defined in thingProperties.h 39 initProperties(); 40 41 // Connect to Arduino IoT Cloud 42 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 43 44 /* 45 The following function allows you to obtain more information 46 related to the state of network and IoT Cloud connection and errors 47 the higher number the more granular information you’ll get. 48 The default is 0 (only errors). 49 Maximum is 4 50 */ 51 setDebugMessageLevel(2); 52 ArduinoCloud.printDebugInfo(); 53 54 dht.begin(); //Init DHT 55 56 Serial.println(F("DHTxx Unified Sensor Example")); 57 // Print temperature sensor details. 58 sensor_t sensor; 59 dht.temperature().getSensor(&sensor); 60 Serial.println(F("------------------------------------")); 61 Serial.println(F("Temperature Sensor")); 62 Serial.print (F("Sensor Type: ")); Serial.println(sensor.name); 63 Serial.print (F("Driver Ver: ")); Serial.println(sensor.version); 64 Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id); 65 Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C")); 66 Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C")); 67 Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C")); 68 Serial.println(F("------------------------------------")); 69 // Print humidity sensor details. 70 dht.humidity().getSensor(&sensor); 71 Serial.println(F("Humidity Sensor")); 72 Serial.print (F("Sensor Type: ")); Serial.println(sensor.name); 73 Serial.print (F("Driver Ver: ")); Serial.println(sensor.version); 74 Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id); 75 Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%")); 76 Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%")); 77 Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%")); 78 Serial.println(F("------------------------------------")); 79 // Set delay between sensor readings based on sensor details. 80 delayMS = sensor.min_delay / 1000; 81 STHAM(); 82 83} 84 85void loop() { 86 ArduinoCloud.update(); 87 // Your code here 88 89 unsigned long currentMillis = millis(); 90 if (currentMillis - previousMillis >= interval) { 91 STHAM(); 92 previousMillis = currentMillis; 93 } 94} 95 96 97void STHAM(){ 98 // Get temperature event and print its value. 99 sensors_event_t event; 100 dht.temperature().getEvent(&event); 101 if (isnan(event.temperature)) { 102 Serial.println(F("Error reading temperature!")); 103 //Assign temperature value 0 to Cloud Variable 104 temperature=0; 105 } 106 else { 107 Serial.print(F("Temperature: ")); 108 Serial.print(event.temperature); 109 Serial.println(F("°C")); 110 //Assign temperature value to Cloud Variable 111 temperature=event.temperature; 112 } 113 114 // Get humidity event and print its value. 115 dht.humidity().getEvent(&event); 116 if (isnan(event.relative_humidity)) { 117 Serial.println(F("Error reading humidity!")); 118 //Assign humidity value 0 to Cloud Variable 119 humidity=0; 120 } 121 else { 122 Serial.print(F("Humidity: ")); 123 Serial.print(event.relative_humidity); 124 Serial.println(F("%")); 125 //Assign humidity value to Cloud Variable 126 humidity=event.relative_humidity; 127 } 128} 129 130/* 131 Since Temperature is READ_WRITE variable, onTemperatureChange() is 132 executed every time a new value is received from IoT Cloud. 133*/ 134void onTemperatureChange() { 135 // Add your code here to act upon Temperature change 136} 137 138/* 139 Since Humidity is READ_WRITE variable, onHumidityChange() is 140 executed every time a new value is received from IoT Cloud. 141*/ 142void onHumidityChange() { 143 // Add your code here to act upon Humidity change 144}
Comments
Only logged in users can leave comments
haziquehizaz
a year ago
why my arduino is offline
juanbautioliv2012
7 months ago
idk
attari
8 Followers
•1 Projects
3
WalterB
9 months ago
Is it possible to use a different board instead of MKR WiFi 1010?