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

How to convert ESP8266 code to ESP32

  • 4 min

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

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

However, in turn, the ESP32 has significantly dropped in price. Furthermore, recently, Espressif has mentioned its intention to obsolete the ESP8266 and replace it with the new ESP32-S3.

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

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

Some might be wondering about the opposite case, converting code from ESP32 to ESP8266. In general, it’s not possible except for very simple programs, since the ESP32 has much more power and features than the ESP8266.

WiFi Libraries

The library names change between ESP8266 and ESP32. In the ESP8266 they had the prefix ‘ESP8266’, while in the ESP32 they have no suffix.

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

Actually, this change in the ESP32 is an improvement. Or, put another way, it’s a mistake that the ESP8266 library developers made, and they fixed it when creating the ESP32 ones. 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 preprocessor directive like this one, with the appropriate library names.

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

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

#endif
Copied!

GPIOs

There’s not much to say here. The GPIOs between the ESP8266 and the ESP32 are completely different. If you got a pin right, it’s pure coincidence. You’ll have to look at which pins you want to use on the ESP32 and change it in your code.

Luckily for you, the ESP32 has many more pins available than the ESP8266. So, if it worked on the ESP8266, you have more than enough 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 ESP8266 code uses a PWM output, the best thing you can do is look for one of the (many) libraries that define the “analogWrite” function for the ESP32, and you will hardly have to modify the code.

Other Libraries

And the rest of the libraries in the world? For reading sensors, performing actions, for bringing you coffee in bed? Well, logically, it will depend on the library (there are hundreds, so I can’t tell you 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 (timers, registers) and how “portable” its programmers made it.

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

Blog Examples

And all the examples we have on the blog? And the series of posts about the ESP8266?? Well, similarly, for most of them you just need to keep in mind what we’ve said about library names.

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

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

github-full

Version for ESP8266: https://github.com/luisllamasbinaburo/ESP8266-Examples

Version for ESP32: https://github.com/luisllamasbinaburo/ESP32-Examples