Zdrojové kódy na vyskúšanie na tomto webovom rozhraní pre ESP32, Arduino, ESP8266, GSM: Github
Webové rozhranie projektu je možné zakúpiť: martinius96@gmail.com

Zapojenie


Arduino HCSR-04
5V Vcc
GND GND
D3 Trigger
D4 Echo

Offline tester


#include <NewPing.h>

#define TRIGGER_PIN  3  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     4  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 450 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}
  

Zdrojový kód - Ethernet W5100


#include <avr\wdt.h>
#include <SPI.h>
#include <Ethernet.h>
#include <NewPing.h>
#define pinTrigger    3
#define pinEcho       4
#define maxVzdialenost 450
NewPing sonar(pinTrigger, pinEcho, maxVzdialenost);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "www.arduino.php5.sk";
IPAddress ip(192, 168, 1, 101);
EthernetClient client;
void setup() {
  Serial.begin(9600);
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }
  wdt_enable(WDTO_8S);
}

void loop() {
  wdt_reset();
  int vzdialenost = sonar.ping_cm();
  delay(50);
  if (vzdialenost > 0) {
    vzdialenost = 0;
    for (int i = 0; i < 5; i++) {
      vzdialenost += sonar.ping_cm();
      delay(50);
    }
    vzdialenost = vzdialenost / 5;
    Serial.print("Vzdialenost medzi senzorom a predmetom je: ");
    Serial.print(vzdialenost);
    Serial.println(" cm.");
    if (client.connect(server, 80)) {
      String hodnota = String(vzdialenost);
      Serial.println("Pripojenie uspesne na webserver, vykonavam request... ");
      client.print("GET /studna/data.php?hodnota=");
      client.print(hodnota);
      client.println(" HTTP/1.1");
      client.println("Host: www.arduino.php5.sk");
      client.println("Connection: close");
      client.println();
      Serial.println("Data uspesne odoslane na web");
      client.stop();
    } else {
      Serial.println("Pripojenie zlyhalo...");
    }
  }
  else {
    Serial.println("Vzdialenost medzi predmetom a senzorom je mimo rozsah.");
    wdt_reset();
    delay(500);
  }
  for (int i = 0; i <= 300; i++) {
    delay(1000);
    wdt_reset();
  }
}