Arduino code in ESPhome

Hello,

how can you integrate an arduino library into ESPhome?
I would like to bind an ESP8266 so that it communicates with the FritzBox as a VOIP device.
Currently I have it running as an arduino program, but I would like to use an ESP 8266 for two tasks.
A relay for the bell on / off and at an input detection when someone rings the doorbell (display in HA and internal call with the FritzBox).

This is my current program code that I would like to implement in ESPhome, unfortunately I am a beginner and still have far too little experience.


#include <WiFiUdp.h>
#include <ArduinoSIP.h>

/* ====================================================================

   Copyright (c) 2018 Juergen Liegner  All rights reserved.
   (https://www.mikrocontroller.net/topic/444994)
   
   Copyright (c) 2019 Florian Wernze
   (https://wernze-home.net/wordpress/hobbys/arduino-esp8266-tuerklingel/)
   
   Copyright (c) 2019 Thorsten Godau (dl9sec)
   (Did some optimizations, extensions and beautification of Florian's code
   and integrated the library)
   

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the
      distribution.

   3. Neither the name of the author(s) nor the names of any contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS "AS IS" AND
   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   SUCH DAMAGE.

   ====================================================================*/

#include <ESP8266WiFi.h>
#include <ArduinoSIP.h>

//#define DEBUGLOG

#define LED_ESP12E    2
#define LED_NODEMCU   16

//------------------------------------------------
// Configuration with static IP
//------------------------------------------------

// WiFi parameters
const char* WiFiSSID    = "RXXXXXXXXXXXX";		    // WiFi SSID
const char* WiFiPSK     = "GXXXXXXXXXXXX";	// WiFi WPA2 preshared key

const char *WiFiIP      = "192.168.150.201"; // WiFi IP of the ESP
const char *WiFiGW      = "192.168.150.10";  // WiFi GW .10
const char *WiFiNM      = "255.255.255.0";  // WiFi NM
const char *WiFiDNS     = "192.168.150.10";  // WiFi DNS .10


// Sip parameters
const char *SipIP       = "192.168.50.1"; 	// IP-Adresse der Fritz!Box
const int   SipPORT     = 5060;		          // SIP port der FRITZ!Box
const char *SipUSER     = "Tuerklingel"; 	  // SIP-Call username FRITZ!Box
const char *SipPW  	   = "gl########";    // SIP-Call password FRITZ!Box

// Dial parameters
const char *SipDIAL     = "**711";          	// Nummer die zu wählen ist **9 alle Telefone **711 interne Rufgruppe
const char *SipTEXT   = "Glocke"; 	      // Text der angezeigt wird (Eingang 2)

//------------------------------------------------


char acSipIn[2048];
char acSipOut[2048];

Sip aSip(acSipOut, sizeof(acSipOut));

void setup()
{
  int i = 0;

  IPAddress myIP;
  IPAddress myGW;
  IPAddress myNM;
  IPAddress myDNS;

  ESP.wdtDisable();
  ESP.wdtEnable(WDTO_8S);

  Serial.begin(115200);
  Serial.setDebugOutput(false);
  delay(10);

  pinMode(LED_ESP12E, OUTPUT);
  pinMode(LED_NODEMCU, OUTPUT);

  pinMode(2, INPUT);

  digitalWrite(LED_ESP12E, 1);  // LED off
  digitalWrite(LED_NODEMCU, 1); // LED off

  Serial.printf("\r\n\r\nConnecting to %s\r\n", WiFiSSID);

  WiFi.setAutoConnect (true);
  WiFi.setAutoReconnect (true);
  WiFi.softAPdisconnect (true);

  myIP.fromString(WiFiIP);
  myGW.fromString(WiFiGW);
  myNM.fromString(WiFiNM);
  myDNS.fromString(WiFiDNS);

  WiFi.config(myIP, myGW, myNM, myDNS);

  if ( String(WiFiSSID) != WiFi.SSID() )
  {
    Serial.print("Wifi initializing...\r\n");
    WiFi.begin(WiFiSSID, WiFiPSK);
  }
  
  while ( WiFi.status() != WL_CONNECTED )
  {
    delay(500);
    Serial.print(".");
  }

  WiFi.persistent(true);

  Serial.printf("\r\nWiFi connected to: %s\r\n", WiFi.localIP().toString().c_str());
  digitalWrite(LED_ESP12E, 0);

  aSip.Init(SipIP, SipPORT, WiFiIP, SipPORT, SipUSER, SipPW, 15);

}


void loop(void)
{
  // SIP processing
  aSip.Processing(acSipIn, sizeof(acSipIn));

  // Doorbell handling begin ===========================
  
  if ( digitalRead(2) == LOW )
  {
    aSip.Dial(SipDIAL, SipTEXT);
  }

  // Doorbell handling end =============================

  ESP.wdtFeed();  // Retrigger watchdog

}

Gerald

1 Like

Search the docs here for the word ‘custom’ esphome.io

I integrated an Arduino library in one of my ESPHome projects a while ago to read a sensor. Have a look here at the code:

Look at the include/sht20.h file and the platform: custom sensor.

1 Like

So in the air quality example, the file ‘sht20.h’ is copied in the esphome/config/YourAirQualityName folder? How about the ‘uFire SHT20’ library files then?

These are installed by PlatformIO, because they are defined as libraries in the YAML file:

esphome:
  name: m5stack_air_quality
  platform: ESP32
  board: m5stack-core-esp32
  libraries:
    - "Wire"
    - "uFire SHT20"
    - "ArduinoJson"
  includes:
    - include/sht20.h

Okay, thank you.
What if the library required is somewhere in the Github? (PlatformIO concept is not familiar to me)

You can specify something like:

  libraries:
    - uFire SHT20=https://github.com/u-fire/uFire_SHT20

Have a look at the supported schemes here:

https://docs.platformio.org/en/latest//core/userguide/lib/cmd_install.html

Fantastic! Couldn’t figure this out by going through the docs.

Hi,

How is it possible to get a HEX file with Arduino IDE when you have an ESP8266 ?

I mean I’ve checked everything possible on internet and they all mention that you have to check this box and after you will get a path to the HEX file.

But it’s false, it’s not working with an ESP8266 board…

So I’m very curious to know how you’ve done that :ok_hand:

Sorry this thread is about esphome.

you’re offensive today. Get out of bed on the wrong side? You are asking about the arduino IDE, which has nothing to do with esphome.

And what is the title of the topic ???

So can you master tell us how you do that ???

At one point you are obliged to have a HEX file, and every person who will land here will have the same question as me… congrats to you !

You just blocked an interesting topic :ok_hand:

Such a smart person here…

This thread is not about using hex files. Read the thread.

Maybe if you posted in the right forum for your question, which has absolutely nothing to do with ESPHome, and if rather than insulting the people who try to help you, you would actually take a look at the console output generated by the box you checked, you would notice that the compiled binaries (hex, bin) are referenced multiple times in the console including their full paths…

Of course none of that will just magically make your project work with an ESP, it’s just the built binary. There’s a lot more you need to configure to make the Arduino IDE work with non-AVR chips. All questions to ask on the relevant forums. And try to not insult people - that tends to be, you know, counterproductive…

1 Like

Hi Gerald,
hast du die Integration hinbekommen? Ich steh nämlich gerade vor dem selben Problem.

Nein, leider nicht - habe die Türklingel dann direkt in Arduino realisiert.

English please.

I also look for the same solution, it already takes a lot of hours to convert it.
I don’t want to go back to Arduino project.