Advertisement
safwan092

Untitled

Jun 4th, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. //#include <ThingSpeak.h>
  2. #include <ESP8266WiFi.h>
  3.  
  4. //////////////////////////////
  5.  
  6. const int trigPin = 12;
  7. const int echoPin = 14;
  8.  
  9. //define sound velocity in cm/uS
  10. #define SOUND_VELOCITY 0.034
  11. #define CM_TO_INCH 0.393701
  12.  
  13. long duration;
  14. float distanceCm;
  15. float distanceInch;
  16. //////////////////////////////
  17. //Wifi config
  18. const char* ssid = "Fatimah"; //Input here your network SSID(name)
  19. const char* pass = "123456789";
  20.  
  21. //ThingSpeak config
  22. //unsigned long myChannelNumber = 2168066; //Input here your channel number
  23. //const char * myWriteAPIKey = "Y86LIUY1H210Q8FK"; // Paste here your ThingSpeak Write API Key
  24.  
  25. const char* host = "maker.ifttt.com";
  26.  
  27.  
  28. const int buzzer = D7;
  29. const int ledPin = D8;
  30.  
  31.  
  32. int safetyDistance = 30;
  33.  
  34. void setup() {
  35.  
  36. WiFi.begin(ssid, pass);
  37. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  38. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  39.  
  40. pinMode(buzzer, OUTPUT);
  41. pinMode(ledPin, OUTPUT);
  42. Serial.begin(9600); // Starts the serial communication
  43.  
  44. while (WiFi.status() != WL_CONNECTED) {
  45. delay(500);
  46. Serial.print(".");
  47. }
  48.  
  49. Serial.println("IP address: ");
  50. Serial.println(WiFi.localIP());
  51. getDistanceAndShow();
  52. }
  53.  
  54. void loop() {
  55. getDistanceAndShow();
  56. WiFiClient client;
  57. const int httpPort = 80;
  58. if (!client.connect(host, httpPort)) {
  59. Serial.println("connection failed");
  60. return;
  61. }
  62. safetyDistance = distanceCm;
  63. if (safetyDistance <= 10) {
  64.  
  65. digitalWrite(buzzer, HIGH);
  66. digitalWrite(ledPin, HIGH);
  67.  
  68. String url = "/trigger/Alert/with/key/krVWdWa8GJn8-Q9yAbqKNirgfQqaYxM68vd3eJQLUuK";
  69.  
  70. Serial.print("Requesting URL: ");
  71. Serial.println(url);
  72.  
  73. client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  74. "Host: " + host + "\r\n" +
  75. "Connection: close\r\n\r\n");
  76. }
  77. else {
  78. digitalWrite(buzzer, LOW);
  79. digitalWrite(ledPin, LOW);
  80. }
  81. /*
  82. int x = ThingSpeak.writeField(myChannelNumber, 1, distance, myWriteAPIKey);
  83.  
  84. if(x == 200){
  85. Serial.println("Channel update successful.");
  86. }
  87. else{
  88. Serial.println("Problem updating channel. HTTP error code " + String(x));
  89. }*/
  90. }
  91.  
  92. void getDistanceAndShow() {
  93. // Clears the trigPin
  94. digitalWrite(trigPin, LOW);
  95. delayMicroseconds(2);
  96. // Sets the trigPin on HIGH state for 10 micro seconds
  97. digitalWrite(trigPin, HIGH);
  98. delayMicroseconds(10);
  99. digitalWrite(trigPin, LOW);
  100.  
  101. // Reads the echoPin, returns the sound wave travel time in microseconds
  102. duration = pulseIn(echoPin, HIGH);
  103.  
  104. // Calculate the distance
  105. distanceCm = duration * SOUND_VELOCITY / 2;
  106.  
  107. // Convert to inches
  108. distanceInch = distanceCm * CM_TO_INCH;
  109.  
  110. // Prints the distance on the Serial Monitor
  111. Serial.print("Distance (cm): ");
  112. Serial.println(distanceCm);
  113. delay(1000);
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement