Devices & Components
20W Adjustable DC-DC Buck Converter with Digital Display
Arduino Nano 33 IoT
12VDC Fan
Generic Power Strip
Relay breakout module
Hardware & Tools
Soldering iron (generic)
Software & Tools
Arduino IoT Cloud
Arduino IoT Cloud Remote
Project description
Code
Room temperature control
cpp
Code for IoT room temperature control device
1/* 2 Sketch written by Jacob Hylén for Arduino_Genuino Project hub room temperature control project 3 4 Arduino IoT Cloud Variables description 5 6 The following variables are automatically generated and updated when changes are made to the Thing 7 8 float temperature; 9 bool fan; 10 bool heating; 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 19#include "DHT.h" 20 21#define DHTPIN 8 // Digital pin connected to the DHT sensor 22#define DHTTYPE DHT11 //Defining type of DHT sensor, DHT22 is another option 23 24DHT dht(DHTPIN, DHTTYPE); 25 26void setup() { 27 // Initialize serial and wait for port to open: 28 Serial.begin(9600); 29 // This delay gives the chance to wait for a Serial Monitor without blocking if none is found 30 delay(1500); 31 32 pinMode(6, OUTPUT); 33 pinMode(7, OUTPUT); 34 dht.begin(); 35 36 // Defined in thingProperties.h 37 initProperties(); 38 39 // Connect to Arduino IoT Cloud 40 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 41 42 /* 43 The following function allows you to obtain more information 44 related to the state of network and IoT Cloud connection and errors 45 the higher number the more granular information you’ll get. 46 The default is 0 (only errors). 47 Maximum is 4 48 */ 49 setDebugMessageLevel(2); 50 ArduinoCloud.printDebugInfo(); 51} 52 53void loop() { 54 ArduinoCloud.update(); 55 // Your code here 56 57 //Read the room temperature and print temperature to the Serial monitor in Celsius 58 temperature = dht.readTemperature(); 59 Serial.println(temperature); 60 61 //Turn the fan on if room temperature is too low and the radiator is on 62 if(temperature < 22 && heating == true){ 63 fan = true; 64 onFanChange(); 65 } else { 66 fan = false; 67 onFanChange(); 68 } 69 70 71} 72 73 74/* 75 Since Heating is READ_WRITE variable, onHeatingChange() is 76 executed every time a new value is received from IoT Cloud. 77*/ 78void onHeatingChange() { 79 // Add your code here to act upon Heating change 80 if(heating == true){ 81 digitalWrite(6, HIGH); 82 } else { 83 digitalWrite(6, LOW); 84 } 85} 86 87/* 88 Since Fan is READ_WRITE variable, onFanChange() is 89 executed every time a new value is received from IoT Cloud. 90*/ 91void onFanChange() { 92 // Add your code here to act upon Fan change 93 if(fan == true){ 94 digitalWrite(7, HIGH); 95 } else { 96 digitalWrite(7, LOW); 97 } 98}
Comments
Only logged in users can leave comments