Language: EN

como-convertir-codigo-del-esp8266-al-esp32

How to convert ESP8266 code to ESP32

It’s been almost four years since we started talking about the ESP8266 and the ESP32. A lot has happened during this time in the realm of these two interesting SoCs.

During this time, the ESP8266 has enjoyed great popularity, both in the hobbyist sector and in commercial applications, and it has been integrated into a large number of products due to its low price.

However, at the same time, the ESP32 has significantly reduced its price. Additionally, recently, Espressif has commented on its intention to obsolete the ESP8266 and replace it with the new ESP32-S3.

So let’s assume you want to transition from the ESP8266 to its “big brother” the ESP32. You have plenty of reasons since, as we saw in this post, its features are far superior.

But then what happens to all your programs, and your code, and your…! Don’t panic… Normally, it’s very simple to convert a program from ESP8266 to ESP32 if you keep these small tips in mind.

Some may be wondering about the opposite case, converting code from ESP32 to ESP8266. In general, this is not possible except for very simple programs, as the ESP32 has much more power and functionalities than the ESP8266.

WiFi Libraries

The names of the libraries change between ESP8266 and ESP32. In the ESP8266, they had the prefix ‘ESP8266’, while in the ESP32 they lack a suffix.

So, for example, the library ESP8266WiFi.h in the ESP32 is called WiFi.h, the library ESP8266HTTPClient becomes HTTPClient, and so on with the other libraries.

In fact, this change in the ESP32 is an improvement. Or, in other words, it is a mistake that the developers of the ESP8266 libraries made, which they corrected when creating those for the ESP32. The advantage of not having a prefix is that you can compile the same code for another machine without having to change any code.

And if you want to make your program compatible with both devices, you can use a precompilation directive like this, with the appropriate library names.

#if defined(ESP8266)
    #define HARDWARE "ESP8266"
    #include "ESP8266WiFi.h"

#elif defined(ESP32)
    #define HARDWARE "ESP32"
    #include "WiFi.h"

#endif

GPIOs

There isn’t much to say here. The GPIOs between the ESP8266 and the ESP32 are completely different. If you got a pin right, it was pure luck. You will need to look at which pins you want to use from the ESP32 and change it in your code.

Fortunately for you, the ESP32 has many more available pins than the ESP8266. So, if it worked on the ESP8266, you are well covered with pins on the ESP32. You just have to choose which pins to use.

PWM Function

The functions implemented in the ESP32 do not include “analogWrite,” as the ESP8266 does. This is because the ESP32 has several ways to generate a PWM signal.

If your code on the ESP8266 uses a PWM output, the best thing you can do is look for one of the (many) libraries that define the “analogWrite” function on the ESP32, and you will hardly need to modify the code.

Other Libraries

And what about the rest of the libraries in the world? For reading sensors, for performing actions, for bringing you coffee in bed? Well, logically, it will depend on the library (there are hundreds, so I can’t say this in general).

In principle, you have a good chance that it will work directly on the ESP32, but it will depend on the internal functions it uses (timer, registers) and how “portable” the programmers have made it.

You will have to try, and if you’re unlucky and it is not compatible, look for a replacement. But don’t worry, it shouldn’t be hard. Practically all libraries have a ported version for the ESP8266.

Blog Examples

And what about all the examples we have in the blog? What about the series of posts about the ESP8266? Well, similarly, most simply need to take into account what we have discussed about the names of the libraries.

So we have updated all the blog posts that were possible (except for those specific to each processor) to reference both processors. Future posts will follow this criterion.

Additionally, we have created a new repository on Github with the codes adapted to the ESP32, so you have the examples for the ESP8266 and for the ESP32 separately.

github-full

ESP8266 version: https://github.com/luisllamasbinaburo/ESP8266-Examples

ESP32 version: https://github.com/luisllamasbinaburo/ESP32-Examples