Felix
Published

Measurement Of A Cistern Volume With An Ultrasonic Sensor

An IoT device that can help watch the water level of your cistern.

BeginnerWork in progress1,728
Measurement Of A Cistern Volume With An Ultrasonic Sensor

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Arduino Nano Ethernet Shield
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
ThingSpeak API
ThingSpeak API

Story

Read more

Code

Arduino Code

C/C++
Code for the Arduino Nano
#include <stdint.h>
#include <UIPEthernet.h>
#define SENDEINTERVALL 118

const char* server = "api.thingspeak.com";
String apiKey = "XXXXXXXXXXX";

uint8_t senden_erlaubt = 0;       
uint8_t zaehler = SENDEINTERVALL;
uint8_t trigger = 5; 
uint8_t echo = 6;
float abstand = 0.0;
float sendewert = 3000.0;

uint32_t sent = 0;
uint32_t failed = 0;

// **** ETHERNET Einstellungen ****
byte mac[] = { 0x00, 0x78, 0x35, 0xC9, 0xF2, 0x3A  };
EthernetClient client;  

void setup()
{
  Serial.begin(9600);    

  Ethernet.begin(mac);

  pinMode(trigger, OUTPUT);   
  pinMode(echo, INPUT);     
  Serial.println("Ultraschallmessung:");
  Serial.println("Messung laeuft...");
}

void loop()
{ 
  delay(1000);  
  zaehler--;               
  if(zaehler <= 5)             
  {
    ultrasonic();
  }
  if(zaehler <= 0)
  {                             
    sendtoThingSpeak(); 
    zaehler = SENDEINTERVALL;
  }
}

void ultrasonic()
{
  float dauer = 0.0; 
  
  digitalWrite(trigger, LOW); 
  delay(5); 
  digitalWrite(trigger, HIGH);   
  delay(10);
  digitalWrite(trigger, LOW);     
  
  dauer = pulseIn(echo, HIGH);
  
  abstand = (dauer/2) * 0.03432; // in cm
    
  if (abstand >= 500 || abstand <= 0) 
  {
    Serial.println("Kein Messwert"); 
    Serial.println(abstand);
  }
  else 
  {
    senden_erlaubt = 1;      
    sendewert = abstand;  
    Serial.print(abstand); 
    Serial.println(" cm"); 
  }
}

void sendtoThingSpeak()
{ 
  int ConnectionStatus = 0;
      
  if(senden_erlaubt == 1) 
  { 
    client.stop();
    do
    {
      ConnectionStatus = client.connect(server, 80);
      if (ConnectionStatus == 1)
      {  
        Serial.println("LAN Client connected.");
  
        String postStr = apiKey;
        postStr += "&field1=";
        postStr += String(sendewert);
  
        client.print("POST /update HTTP/1.1\n");
        client.print("Host: api.thingspeak.com\n");   
        client.print("Connection: close\n");          
        client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
        client.print("Content-Type: application/x-www-form-urlencoded\n");
        client.print("Content-Length: ");
        client.print(postStr.length());
        client.print("\n\n");
        client.println(postStr);
   
        delay(1000);  
        sent++;
        Serial.println(sent);
        Serial.println(sendewert);
      }
      else
      {
        Serial.println("connection failed: " + ConnectionStatus);     
        failed++;
      }
      client.flush();   
      client.stop();    
    }
    while(ConnectionStatus != 1 && failed < 10);
    senden_erlaubt = 0;
    failed = 0;
  }
  else
  {
    Serial.println("Messfehler: Senden nicht freigegeben");
  }
}

Credits

Felix
1 project • 2 followers
mechatronics-engineer
Contact

Comments

Please log in or sign up to comment.