need help with esp8266 telegram ultrasonic alarm

Hello Guys,
I am new in the Arduino thing so I need some help :slight_smile:

I want to build an alarm system based on a Nodemcu and an ultrasonic sensor and the telegram bot. So I want to get notified if the distance of the ultrasonic sensor changes. That’s it :smiley:

I did some research and I found this two tutorials:
http://deloarts.com/de/scripts/esp8266/telegram/
Ultraschallsensor HC-SR04 Entfernung mit Arduino messenMikrocontroller-Elektronik.de
Well, they are in German, but the code matters.
So now I tried these two codes and yeah they work separately. But while I am trying to smash them into one code, I continuously get error messages concerning the int. So I would be really grateful if you could help me.

CODES 1:

/*
 PretzelBoard-Ultraschall-SN-SR04T 
 Beispiel für PretzelBoard - Arduino Board mit WLAN
 Beschreibung des Boards und Beispiels auf der Webseite:
 (c) https://www.mikrocontroller-elektronik.de/
 Aufgabe:
 Entfernung Messen und auf seriellen Schnittstelle im Sekundentakt in cm ausgeben
 */
 
#include <SoftwareSerial.h>

#define trigger 3 // Arduino Pin an HC-SR04 Trig
#define echo 2    // Arduino Pin an HC-SR04 Echo

SoftwareSerial esp8266(11, 12);

void setup()
{
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);
  digitalWrite(trigger, HIGH); //Signal abschalten
 
  // Open serial communications and wait for port to open:
  Serial.begin(19200);
  // set the data rate for the SoftwareSerial port
  esp8266.begin(19200);
 
  //send first AT Command to see if ESP8266-Module responds
  esp8266.println("AT");

  Serial.write("PretzelBoard Beispiel von https://www.mikrocontroller-elektronik.de\n");
}

void loop() // Arduino Hauptschleife
{
 int entfernung=getEntfernung();
 int mittelwert= getEntfernungGD();
 
 Serial.write("Entfernung:");
 Serial.print(entfernung, DEC) ;
 Serial.write(" , Mittelwert (gewichtet): ");
 Serial.print(mittelwert, DEC) ;
 Serial.write(" cm\n");
 
 delay(1000); //1 Sekunde warten
}


// Entfernung in cm über gewöhnlichen Ultraschallsensor mit Echo und Trigger messen
int getEntfernung(){ 
 
 long entfernung=0;
 long zeit=0;

 digitalWrite(trigger, LOW); 
 delayMicroseconds(3);
 noInterrupts();
 digitalWrite(trigger, HIGH); //Trigger Impuls 10 us
 delayMicroseconds(10);
 digitalWrite(trigger, LOW); 
 zeit = pulseIn(echo, HIGH); // Echo-Zeit messen
 interrupts(); 
 
 zeit = (zeit/2); // Zeit halbieren
 entfernung = zeit / 29.1; // Zeit in Zentimeter umrechnen
 return(entfernung); 
}

//Entfernung Gleitender Durchschnitt (gewichtet)
int getEntfernungGD(){ 

int alt=0;
int mittel;
int entf;
int i;

 delay(10);
 alt=getEntfernung();
 delay(10);
 for (i=0; i<10; i++)
 {
   entf=getEntfernung();
   mittel=(0.8*alt) + (0.2*entf);
   alt=mittel;
   delay(10);
 }
 return (mittel);
}

CODE 2:

// #####################################################################################################################
// ######################################### ESP8266 TELEGRAM MESSENGER ################################################
// ##################################################################################################################### 
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266TelegramBOT.h>

extern "C"
{ 
    #include "user_interface.h" 
}

#define inputPin 0

#define botName "Doorbell"
#define botUserName "something that ends with bot"
#define botToken "your:token"
#define adminID "yourID" // Check my website to figure out how to get it

static char SSID[] = "your network name";
static char password[] = "your password";
static char hostname[] = "Doorbell"; // That's the name you will see in your network.

TelegramBOT bot(botToken, botName, botUserName);

void setup() 
{
	// Setup the pins
	pinMode(inputPin, INPUT_PULLUP);

	// Start serial com
	Serial.begin(115200);
	Serial.print("\n  Connecting to " + String(SSID) + " ");

	// Set the hostname (otherwise the controller will be named esp8266) and init the esp for WiFi use
	wifi_station_set_hostname(hostname);
	WiFi.begin(SSID, password);

	// Wait until it is connected to the network
	while (WiFi.status() != WL_CONNECTED) 
	{
	    delay(500);
	    Serial.print(".");
	}

	Serial.println("\n\nWiFi connected.");  
	Serial.print("  IP address: " + WiFi.localIP().toString() + "\n");
	Serial.print("  Host name:  " + String(hostname) + "\n");
	Serial.print("- - - - - - - - - - - - - - - - - - -\n\n");
	delay(3000);

	// Start the bot and send a message to you, so you know the ESP has booted.
	bot.begin();
	bot.sendMessage(adminID, "ESP8266 online.", "");
}

void loop() 
{
	// Doorbell button has been pushed
	if (!digitalRead(inputPin))
	{
		bot.sendMessage(adminID, "Ding Dong", "");
		delay(10000); 	// Halt for 10 sec, if someone pushes the button more often you'll get
						// many messages.
						// You could also use a timer, would, in fact, look more pretty.
	}
	// React on an incomming message:
	/* 	
		GENERAL
			bot.message[0][0] ... number of received messages
			bot.message[0][1] ... code of last message
		MESSAGE
			bot.message[i][0] ... update ID
			bot.message[i][1] ... user ID
			bot.message[i][2] ... first name
			bot.message[i][3] ... last name
			bot.message[i][4] ... user ID
			bot.message[i][5] ... message
	*/
	bot.getUpdates(bot.message[0][1]);
	for (int i = 1; i < bot.message[0][0].toInt() + 1; i++)
	{
		// Only the admin is allowed to talk to the bot
		if (bot.message[i][4] == adminID)
		{
			// extract the message
			bot.message[i][5] = bot.message[i][5].substring(1, bot.message[i][5].length());
			if (bot.message[i][5] == "info")
			{
				bot.sendMessage(bot.message[i][4], "Hello.", "");
				bot.sendMessage(bot.message[i][4], "I am your doorbell.", "");
			}
			else if ((bot.message[i][5] == "get"))
			{
				unsigned long runtime = millis() / 1000;
				bot.sendMessage(bot.message[i][4], "Runtime: " + String(runtime) + "sec.", "");
			}
			else 
			{
				bot.sendMessage(bot.message[i][4], "Command unknown.", "");
			}
		}
		// If any other person as the admin sends something to your bot, let them know they are not allowed to do so.
		else 
		{
			String senderID = bot.message[i][4];
			bot.sendMessage(bot.message[i][4], "Access denied. Your ID: " + senderID, "");
		}
	}
	bot.message[0][0] = "";
	delay(1000);
}

I continuously get error messages concerning the int.

. But you didn’t think it worthwhile to share.

thanks for your quick response. Sorry for my mistake here is the edited code and the error.

// #####################################################################################################################
// ######################################### ESP8266 TELEGRAM MESSENGER ################################################
// ##################################################################################################################### 
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266TelegramBOT.h>




extern "C"
{ 
    #include "user_interface.h" 
}

#define trigger 4 // Arduino Pin an HC-SR04 Trig
#define echo 5    // Arduino Pin an HC-SR04 Echo


#define botName "Alarm"
#define botUserName "***"
#define botToken "****"
#define adminID "****" // Check my website to figure out how to get it

static char SSID[] = "****";
static char password[] = "****";
static char hostname[] = "alarm"; // That's the name you will see in your network.

TelegramBOT bot(botToken, botName, botUserName);

void setup() 
{
  // Setup the pins
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);
  digitalWrite(trigger, HIGH); //Signal abschalten

  // Start serial com
  Serial.begin(115200);
  Serial.print("\n  Connecting to " + String(SSID) + " ");

  // Set the hostname (otherwise the controller will be named esp8266) and init the esp for WiFi use
  wifi_station_set_hostname(hostname);
  WiFi.begin(SSID, password);

  // Wait until it is connected to the network
  while (WiFi.status() != WL_CONNECTED) 
  {
      delay(500);
      Serial.print(".");
  }

  Serial.println("\n\nWiFi connected.");  
  Serial.print("  IP address: " + WiFi.localIP().toString() + "\n");
  Serial.print("  Host name:  " + String(hostname) + "\n");
  Serial.print("- - - - - - - - - - - - - - - - - - -\n\n");
  delay(3000);

  // Start the bot and send a message to you, so you know the ESP has booted.
  bot.begin();
  bot.sendMessage(adminID, "ESP8266 online.", "");
}

void loop() 
{  

 int entfernung=getEntfernung();
 int mittelwert= getEntfernungGD();
 int getEntfernung(){ 
 
 long entfernung=0;
 long zeit=0;

 digitalWrite(trigger, LOW); 
 delayMicroseconds(3);
 noInterrupts();
 digitalWrite(trigger, HIGH); //Trigger Impuls 10 us
 delayMicroseconds(10);
 digitalWrite(trigger, LOW); 
 zeit = pulseIn(echo, HIGH); // Echo-Zeit messen
 interrupts(); 
 
 zeit = (zeit/2); // Zeit halbieren
 entfernung = zeit / 29.1; // Zeit in Zentimeter umrechnen
 return(entfernung); 
}

//Entfernung Gleitender Durchschnitt (gewichtet)
int getEntfernungGD(){ 

int alt=0;
int mittel;
int entf;
int i;

 delay(10);
 alt=getEntfernung();
 delay(10);
 for (i=0; i<10; i++)
 {
   entf=getEntfernung();
   mittel=(0.8*alt) + (0.2*entf);
   alt=mittel;
   delay(10);
 }
 return (mittel);
}
 
  // Doorbell button has been pushed
  if (entfernung > 0 )
  {
    bot.sendMessage(adminID, "Ding Dong", "");
    delay(10000);   // Halt for 10 sec, if someone pushes the button more often you'll get
            // many messages.
            // You could also use a timer, would, in fact, look more pretty.
  }
  // React on an incomming message:
  /*  
    GENERAL
      bot.message[0][0] ... number of received messages
      bot.message[0][1] ... code of last message
    MESSAGE
      bot.message[i][0] ... update ID
      bot.message[i][1] ... user ID
      bot.message[i][2] ... first name
      bot.message[i][3] ... last name
      bot.message[i][4] ... user ID
      bot.message[i][5] ... message
  */
  bot.getUpdates(bot.message[0][1]);
  for (int i = 1; i < bot.message[0][0].toInt() + 1; i++)
  {
    // Only the admin is allowed to talk to the bot
    if (bot.message[i][4] == adminID)
    {
      // extract the message
      bot.message[i][5] = bot.message[i][5].substring(1, bot.message[i][5].length());
      if (bot.message[i][5] == "info")
      {
        bot.sendMessage(bot.message[i][4], "Hello.", "");
        bot.sendMessage(bot.message[i][4], "I am your doorbell.", "");
      }
      else if ((bot.message[i][5] == "get"))
      {
        unsigned long runtime = millis() / 1000;
        bot.sendMessage(bot.message[i][4], "Runtime: " + String(runtime) + "sec.", "");
      }
      else 
      {
        bot.sendMessage(bot.message[i][4], "Command unknown.", "");
      }
    }
    // If any other person as the admin sends something to your bot, let them know they are not allowed to do so.
    else 
    {
      String senderID = bot.message[i][4];
      bot.sendMessage(bot.message[i][4], "Access denied. Your ID: " + senderID, "");
    }
  }
  bot.message[0][0] = "";
  delay(1000);
}

and error

Arduino: 1.8.5 (Windows 10), Board: "NodeMCU 0.9 (ESP-12 Module), 80 MHz, 4M (1M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

E:\Users\Documents\Arduino\esp-telegram-master\esp-telegram\esp-telegram.ino: In function 'void loop()':

esp-telegram:67: error: 'getEntfernung' was not declared in this scope

  int entfernung=getEntfernung();

                               ^

esp-telegram:68: error: 'getEntfernungGD' was not declared in this scope

  int mittelwert= getEntfernungGD();

                                  ^

esp-telegram:69: error: a function-definition is not allowed here before '{' token

  int getEntfernung(){ 

                     ^

esp-telegram:162: error: expected '}' at end of input

 }

 ^

exit status 1
'getEntfernung' was not declared in this scope
int entfernung=getEntfernung();

It appears as if you are calling a function called getEntfernung() and there is no function by that name defined in your sketch. Did you mean this function:

int getEntfernungGD(){

Which is spelled a little different?

BTW: After that function definition you have a bunch of illegal code that isn't inside any function. Use the control-T in the IDE to format the code and that will help you to see how you messed up the { and }.

I am not sure if I mean this function int getEntfernungGD()

More or less I just copy pasted the code and it worked before so what should I exactly do.

No, that code did not work before. That has always been an error to call a function that isn't in your code.

I notice that there is a function by that name in the first code you posted.

xmp: so what should I exactly do.

Sounds like you need to learn a little C++ or hire someone to write your code for you. I tried to look through it but it is poorly formatted and all the names are in a language I don't understand.

xmp: I am not sure if I mean this function

int getEntfernungGD()

More or less I just copy pasted the code and it worked before so what should I exactly do.

Can you explain what you mean by 'it worked before' ?

I tried it again and now I have another error message

// #####################################################################################################################
// ######################################### ESP8266 TELEGRAM MESSENGER ################################################
// #####################################################################################################################
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266TelegramBOT.h>




extern "C"
{
#include "user_interface.h"
}

#define trigger 4 // Arduino Pin an HC-SR04 Trig
#define echo 5    // Arduino Pin an HC-SR04 Echo


#define botName "****"
#define botUserName "****"
#define botToken "*****"
#define adminID "***" // Check my website to figure out how to get it

static char SSID[] = "***";
static char password [] = "****";
static char hostname[] = "***"; // That's the name you will see in your network.

TelegramBOT bot(botToken, botName, botUserName);

void setup()
{
  // Setup the pins
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);
  digitalWrite(trigger, HIGH); //Signal abschalten

  // Start serial com
  Serial.begin(115200);
  Serial.print("\n  Connecting to " + String(SSID) + " ");

  // Set the hostname (otherwise the controller will be named esp8266) and init the esp for WiFi use
  wifi_station_set_hostname(hostname);
  WiFi.begin(SSID, password);

  // Wait until it is connected to the network
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  Serial.println("\n\nWiFi connected.");
  Serial.print("  IP address: " + WiFi.localIP().toString() + "\n");
  Serial.print("  Host name:  " + String(hostname) + "\n");
  Serial.print("- - - - - - - - - - - - - - - - - - -\n\n");
  delay(3000);

  // Start the bot and send a message to you, so you know the ESP has booted.
  bot.begin();
  bot.sendMessage(adminID, "ESP8266 online.", "");
}

void loop()
{
 int entfernung=getEntfernung();
 int mittelwert= getEntfernungGD();
 
 Serial.write("Entfernung:");
 Serial.print(entfernung, DEC) ;
 Serial.write(" , Mittelwert (gewichtet): ");
 Serial.print(mittelwert, DEC) ;
 Serial.write(" cm\n");
 
 delay(1000); //1 Sekunde warten
}


// Entfernung in cm über gewöhnlichen Ultraschallsensor mit Echo und Trigger messen
int getEntfernung(){ 
 
 long entfernung=0;
 long zeit=0;

 digitalWrite(trigger, LOW); 
 delayMicroseconds(3);
 noInterrupts();
 digitalWrite(trigger, HIGH); //Trigger Impuls 10 us
 delayMicroseconds(10);
 digitalWrite(trigger, LOW); 
 zeit = pulseIn(echo, HIGH); // Echo-Zeit messen
 interrupts(); 
 
 zeit = (zeit/2); // Zeit halbieren
 entfernung = zeit / 29.1; // Zeit in Zentimeter umrechnen
 return(entfernung); 
}

//Entfernung Gleitender Durchschnitt (gewichtet)
int getEntfernungGD(){ 

int alt=0;
int mittel;
int entf;
int i;

 delay(10);
 alt=getEntfernung();
 delay(10);
 for (i=0; i<10; i++)
 {
   entf=getEntfernung();
   mittel=(0.8*alt) + (0.2*entf);
   alt=mittel;
   delay(10);
 }
 return (mittel);
}
  // Doorbell button has been pushed
  if (entfernung > 0 )
  {
    bot.sendMessage(adminID, "Ding Dong", "");
    delay(10000);   // Halt for 10 sec, if someone pushes the button more often you'll get
    // many messages.
    // You could also use a timer, would, in fact, look more pretty.
  }
  // React on an incomming message:
  /*
    GENERAL
      bot.message[0][0] ... number of received messages
      bot.message[0][1] ... code of last message
    MESSAGE
      bot.message[i][0] ... update ID
      bot.message[i][1] ... user ID
      bot.message[i][2] ... first name
      bot.message[i][3] ... last name
      bot.message[i][4] ... user ID
      bot.message[i][5] ... message
  */
  bot.getUpdates(bot.message[0][1]);
  for (int i = 1; i < bot.message[0][0].toInt() + 1; i++)
  {
    // Only the admin is allowed to talk to the bot
    if (bot.message[i][4] == adminID)
    {
      // extract the message
      bot.message[i][5] = bot.message[i][5].substring(1, bot.message[i][5].length());
      if (bot.message[i][5] == "info")
      {
        bot.sendMessage(bot.message[i][4], "Hello.", "");
        bot.sendMessage(bot.message[i][4], "I am your doorbell.", "");
      }
      else if ((bot.message[i][5] == "get"))
      {
        unsigned long runtime = millis() / 1000;
        bot.sendMessage(bot.message[i][4], "Runtime: " + String(runtime) + "sec.", "");
      }
      else
      {
        bot.sendMessage(bot.message[i][4], "Command unknown.", "");
      }
    }
    // If any other person as the admin sends something to your bot, let them know they are not allowed to do so.
    else
    {
      String senderID = bot.message[i][4];
      bot.sendMessage(bot.message[i][4], "Access denied. Your ID: " + senderID, "");
    }
  }
  bot.message[0][0] = "";
  delay(1000);
}

error

Arduino: 1.8.5 (Windows 10), Board: "NodeMCU 0.9 (ESP-12 Module), 80 MHz, 4M (1M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

esp-telegram:120: error: expected unqualified-id before 'if'

   if (entfernung > 0 )

   ^

esp-telegram:140: error: 'bot' does not name a type

   bot.getUpdates(bot.message[0][1]);

   ^

esp-telegram:141: error: expected unqualified-id before 'for'

   for (int i = 1; i < bot.message[0][0].toInt() + 1; i++)

   ^

esp-telegram:141: error: 'i' does not name a type

   for (int i = 1; i < bot.message[0][0].toInt() + 1; i++)

                   ^

esp-telegram:141: error: 'i' does not name a type

   for (int i = 1; i < bot.message[0][0].toInt() + 1; i++)

                                                      ^

esp-telegram:170: error: 'bot' does not name a type

   bot.message[0][0] = "";

   ^

esp-telegram:171: error: expected constructor, destructor, or type conversion before '(' token

   delay(1000);

        ^

esp-telegram:172: error: expected declaration before '}' token

 }

 ^

exit status 1

and with “it worked before” I mean if I run the ultrasonic code seperatly it works.

Yes, that's what I pointed out before. All ode needs to go in a function except for object creation and variable definitions. The code from that if statement on down isn't inside any function.

Isnt it in the void loop?

xmp: Isnt it in the void loop?

Does the compiler think it is in the loop function?

xmp: Isnt it in the void loop?

No, here is the entire loop function:

void loop()
{
 int entfernung=getEntfernung();
 int mittelwert= getEntfernungGD();

 Serial.write("Entfernung:");
 Serial.print(entfernung, DEC) ;
 Serial.write(" , Mittelwert (gewichtet): ");
 Serial.print(mittelwert, DEC) ;
 Serial.write(" cm\n");

 delay(1000); //1 Sekunde warten
}

It starts at the opening brace and ends at the closing brace.

FORMAT YOUR CODE!!!!!!! And you will be able to see stuff like this. When you see real code written by real coders and all the blocks are all lined up nice and neat... yeah, we don't do that just to make it pretty. It helps you see where all your blocks begin and end.

I ll format it when I am back at my pc. So can I create a new void loop or should i put the if statement in the first loop?

You can't have two functions with the same name if that's what you're getting at.

This would be a lot easier if you'd stop for a minute and do a basic tutorial on C++. You can probably read through one in a day and at least get the basics. Doing it this way, try something, post the error, wait for snarky comment, try something, post the error, wait for snarky comment .... could easily take weeks to get to the finished product if you can even stand the frustration and stick with it that long. You'll get a lot better help when it's not the raw basics you're asking about. Take a day and read through this:

http://www.cplusplus.com/doc/tutorial/

You don't have to become an expert programmer, but at least learn the basic syntax, what the braces mean, how a function is defined, where it's legal to put code, that sort of basic basic stuff. You'll be glad you did.

ok I know i have to learn it so i ll do that. I overlooked the code and it works … kinda

here is the code with no errors:

// #####################################################################################################################
// ######################################### ESP8266 TELEGRAM MESSENGER ################################################
// #####################################################################################################################
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266TelegramBOT.h>




extern "C"
{
#include "user_interface.h"
}

#define trigger 4 // Arduino Pin an HC-SR04 Trig
#define echo 5    // Arduino Pin an HC-SR04 Echo


#define botName "***"
#define botUserName "***"
#define botToken "****"
#define adminID "***" //Check my website to figure out how to get it

static char SSID[] = "***";
static char password[] = "***";
static char hostname[] = "*****"; // That's the name you will see in your network.

TelegramBOT bot(botToken, botName, botUserName);

void setup()
{
  // Setup the pins
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);
  digitalWrite(trigger, HIGH); //Signal abschalten

  // Start serial com
  Serial.begin(115200);
  Serial.print("\n  Connecting to " + String(SSID) + " ");

  // Set the hostname (otherwise the controller will be named esp8266) and init the esp for WiFi use
  wifi_station_set_hostname(hostname);
  WiFi.begin(SSID, password);

  // Wait until it is connected to the network
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  Serial.println("\n\nWiFi connected.");
  Serial.print("  IP address: " + WiFi.localIP().toString() + "\n");
  Serial.print("  Host name:  " + String(hostname) + "\n");
  Serial.print("- - - - - - - - - - - - - - - - - - -\n\n");
  delay(3000);

  // Start the bot and send a message to you, so you know the ESP has booted.
  bot.begin();
  bot.sendMessage(adminID, "ESP8266 online.", "");
}

void loop()

{
  int entfernung = getEntfernung();
  int mittelwert = getEntfernungGD();

  Serial.write("Entfernung:");
  Serial.print(entfernung, DEC) ;
  Serial.write(" , Mittelwert (gewichtet): ");
  Serial.print(mittelwert, DEC) ;
  Serial.write(" cm\n");

  delay(1000); //1 Sekunde warten
  if (entfernung < 130 )
  {
    bot.sendMessage(adminID, "Ding Dong", "");
    // Halt for 10 sec, if someone pushes the button more often you'll get
    // many messages.
    // You could also use a timer, would, in fact, look more pretty.
  }
  bot.getUpdates(bot.message[0][1]);
  for (int i = 1; i < bot.message[0][0].toInt() + 1; i++)
  {
    // Only the admin is allowed to talk to the bot
    if (bot.message[i][4] == adminID)
    {
      // extract the message
      bot.message[i][5] = bot.message[i][5].substring(1, bot.message[i][5].length());
      if (bot.message[i][5] == "info")
      {
        bot.sendMessage(bot.message[i][4], "Hello.", "");
        bot.sendMessage(bot.message[i][4], "I am your doorbell.", "");
      }
      else if ((bot.message[i][5] == "get"))
      {
        unsigned long runtime = millis() / 1000;
        bot.sendMessage(bot.message[i][4], "Runtime: " + String(runtime) + "sec.", "");
      }
      else
      {
        bot.sendMessage(bot.message[i][4], "Command unknown.", "");
      }
    }
    // If any other person as the admin sends something to your bot, let them know they are not allowed to do so.
    else
    {
      String senderID = bot.message[i][4];
      bot.sendMessage(bot.message[i][4], "Access denied. Your ID: " + senderID, "");
    }
  }
  bot.message[0][0] = "";
  delay(1000);
}


// Entfernung in cm über gewöhnlichen Ultraschallsensor mit Echo und Trigger messen
int getEntfernung() {

  long entfernung = 0;
  long zeit = 0;

  digitalWrite(trigger, LOW);
  delayMicroseconds(3);
  noInterrupts();
  digitalWrite(trigger, HIGH); //Trigger Impuls 10 us
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);
  zeit = pulseIn(echo, HIGH); // Echo-Zeit messen
  interrupts();

  zeit = (zeit / 2); // Zeit halbieren
  entfernung = zeit / 29.1; // Zeit in Zentimeter umrechnen
  return (entfernung);
}

//Entfernung Gleitender Durchschnitt (gewichtet)
int getEntfernungGD() {

  int alt = 0;
  int mittel;
  int entf;
  int i;

  delay(10);
  alt = getEntfernung();
  delay(10);
  for (i = 0; i < 10; i++)
  {
    entf = getEntfernung();
    mittel = (0.8 * alt) + (0.2 * entf);
    alt = mittel;
    delay(10);
  }
  return (mittel);
}
// Doorbell button has been pushed
// Halt for 10 sec, if someone pushes the button more often you'll get
// many messages.
// You could also use a timer, would, in fact, look more pretty.

// React on an incomming message:
/*
  GENERAL
    bot.message[0][0] ... number of received messages
    bot.message[0][1] ... code of last message
  MESSAGE
    bot.message[i][0] ... update ID
    bot.message[i][1] ... user ID
    bot.message[i][2] ... first name
    bot.message[i][3] ... last name
    bot.message[i][4] ... user ID
    bot.message[i][5] ... message
*/

I have a small problem. So it works I get a message in telegram if the ultrasonic sensor gets a value below 130. But after 1min the arduino reboots and I dont know why. Here is the output of the serial monitor:

Exception (29):
epc1=0x4020d3c8 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ctx: sys 
sp: 3ffffcd0 end: 3fffffb0 offset: 01a0

>>>stack>>>
3ffffe70:  3fff0ca0 3ffed258 00000000 fffffffb  
3ffffe80:  3fff0b7c 3fff0b7c 00000000 4020e260  
3ffffe90:  3fff0b78 3fff0d74 00000000 fffffffb  
3ffffea0:  3fff0b7c 00000000 3fff0b78 4020d479  
3ffffeb0:  3fff0d5c 3ffe9254 3fff0b78 4021082c  
3ffffec0:  00000000 00000007 3fff1804 fffffffb  
3ffffed0:  3ffe9254 3ffe9254 3fff0b78 4021085a  
3ffffee0:  00000000 3fff0b78 3fff1804 40212d74  
3ffffef0:  32b2a8c0 3ffeaa34 3ffeaa4d 3ffeaa6b  
3fffff00:  00000000 3fff0b78 3fff1804 40212dc7  
3fffff10:  4020e6e8 3fff07cc 3fff0ca4 40100690  
3fffff20:  3ffeedb0 3ffe9904 00000037 3fff07c8  
3fffff30:  4020e6e8 3fff07cc 3ffe91e8 4020e6f6  
3fffff40:  40104d7e 01228f64 0000000f 4020e7b4  
3fffff50:  3ffe91e8 3ffe9904 01228f64 00000000  
3fffff60:  40238748 3ffeed88 3ffeedb0 60000600  
3fffff70:  04fb795e 3ffeedb0 3ffeed88 40238755  
3fffff80:  4023879a 3fffdab0 00000000 3fffdcb0  
3fffff90:  3ffeedd8 3fffdad0 3ffef720 40205627  
3fffffa0:  40000f49 40000f49 3fffdab0 40000f49  
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(1,7)


 ets Jan  8 2013,rst cause:4, boot mode:(1,7)

wdt reset

Any ideas?

xmp: Any ideas?

Any code?

In my last post there is the Code, where I said it works kinda.

Ok I fixed this error by using an older esp8266 library and now it doesnt crash. I have another question ... The code runs pretty slow so between every mesuarment there is a 2 or 3 seconds delay. I dont know why. I commented out every delay in the code but still not faster.