safwan092

ESP32 + GAS + LDR + PIR + LED + Arduino IOT Cloud

May 17th, 2022 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. /*
  2. Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  3. https://create.arduino.cc/cloud/things/af1ef035-f477-44a9-b162-2af28cb1d771
  4.  
  5. Arduino IoT Cloud Variables description
  6.  
  7. The following variables are automatically generated and updated when changes are made to the Thing
  8.  
  9. int gasValue;
  10. int ldrValue;
  11. int pirValue;
  12. bool led;
  13.  
  14. Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  15. which are called when their values are changed from the Dashboard.
  16. These functions are generated with the Thing and added at the end of this sketch.
  17. */
  18.  
  19. #include "thingProperties.h"
  20.  
  21. #define pirPin 39
  22. #define ldrPin 35
  23. #define gasPin 34
  24. #define ledPin 33
  25. #define buzzerPin 32
  26.  
  27. void setup() {
  28. // Initialize serial and wait for port to open:
  29. Serial.begin(9600);
  30. // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  31. delay(1500);
  32. pinMode(pirPin, INPUT);
  33. pinMode(ldrPin, INPUT);
  34. pinMode(gasPin, INPUT);
  35. pinMode(ledPin, OUTPUT);
  36. pinMode(buzzerPin, OUTPUT);
  37. digitalWrite(ledPin, LOW);
  38. digitalWrite(buzzerPin, LOW);
  39. // Defined in thingProperties.h
  40. initProperties();
  41.  
  42. // Connect to Arduino IoT Cloud
  43. ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  44.  
  45. /*
  46. The following function allows you to obtain more information
  47. related to the state of network and IoT Cloud connection and errors
  48. the higher number the more granular information you’ll get.
  49. The default is 0 (only errors).
  50. Maximum is 4
  51. */
  52. setDebugMessageLevel(2);
  53. ArduinoCloud.printDebugInfo();
  54. }
  55.  
  56. void loop() {
  57. ArduinoCloud.update();
  58. // Your code here
  59. gasValue = analogRead(gasPin);
  60. ldrValue = analogRead(ldrPin);
  61. pirValue = digitalRead(pirPin);
  62. if (pirValue == 1 || gasValue>3000) {
  63. digitalWrite(buzzerPin, HIGH);
  64. delay(1000);
  65. digitalWrite(buzzerPin, LOW);
  66. }
  67. else {
  68. digitalWrite(buzzerPin, LOW);
  69. }
  70.  
  71. //delay(1000);
  72. }
  73.  
  74.  
  75.  
  76.  
  77. /*
  78. Since Led is READ_WRITE variable, onLedChange() is
  79. executed every time a new value is received from IoT Cloud.
  80. */
  81. void onLedChange() {
  82. // Add your code here to act upon Led change
  83. digitalWrite(ledPin, led);
  84. }
  85.  
Add Comment
Please, Sign In to add comment