How to do OTA via http, using ESP32 as AP instead of as STA, and using ESP-IDF? - http

I need to write a program, using ESP-IDF, which is able to update the firmware of an ESP32-S3-based device wirelessly, without having on-field access to Internet.
I googled and found the example project about OTA:
OTA: https://github.com/espressif/esp-idf/tr ... system/ota
But this project seems to use the ESP32 as an HTTP client, connecting to a server running on a computer, and both the ESP32 and the computer being connected to the same local network created by a router acting as AP. For the firmware update on-field, I won't have access to Internet or a local network via a router, hence I will need to set up a direct connection between the ESP32 and my computer. I thought that I could do this by using the ESP32 as AP and then connect with my PC to the ESP32-created network.
For using ESP32 as AP, I found the following example project:
WiFI as AP: https://github.com/espressif/esp-idf/tr ... ted/softAP
and for using ESP32 as HTTP server I found the following example project:
HTTP server: https://github.com/espressif/esp-idf/tr ... ver/simple
My idea was to try to combine code from the above 3 example projects in order to use the ESP32 as AP instead of as STA, run an HTTP server on ESP32, use a computer as HTTP client to connect to the ESP32 server, and from the computer browser to upload the new firmware file to the ESP32 server, and then copy the new firmware from a buffer to the ota flash partition.
As I don't have experience with OTA at all and less experience with ESP32, I wanted to first ask if the above idea is possible at all or there exists a simpler way to accomplish the OTA. If there is no simpler method, how can I use the OTA code with ESP32 being an HTTP server instead of HTTP client (as the OTA example seems to be coupled to an HTTP client) ?
P.S. Using the ESP32 as HTTP client seems not applicable to me because I will need to know in advance a certain permanent IP address for the computer acting as an HTTP server which I think I cannot have, but I am not sure if I am thinking correctly ?

Related

ESP32 + Mongoose server + MQTT broker and client

I have a system already working, which consist in a Mongoose web server running in a ESP32. It is also capable of publishing messagges trough MQTT, using Mongoose's mqtt libraries, when connected trugh an external MQTT broker. The idea is that there could be many of those systems, let's say 5 units, connected to the same LAN whith an extra hardware running a mqtt broker.
The broker is the one who manage some features of the individual units.
The thing is, what if the broker goes down? Or, if the broker is not in the LAN but is connected through internet, what if the internet connection goes down?
If that happens, the idea is that one of those units became a mqtt broker, so it can manage its own features and also other unit's.
In that way, I would have one unit working as a broker (only with basic functionalities) but also as a client.
I would like to know if anyone of you have already worked with a similar setup, or what do you think about it.
Any other idea?
I know there are Mongoose libraries to run a MQTT broker in the device, that wouldn't be an issue... the thing is... Isn't it better to setup an HTTP communication between the units, since they are all already web servers?

Can I use HTTPS and SSE client libraries on Arduino PORTENTA Machine Control board?

I am looking for simple board for IoT professional projects. I bumped into the Arduino PORTENTA Machine Control board a while back, but I cannot get some things sorted out:
What libraries can I use on this board (mainly SSE and HTTPS libraries)?
Does it have enough computational power to do HTTPS requests (not HTTP but HTTPS)?
What am I trying to achieve:
I am trying to build a project around Google’s Firestore / Firebase.
I need to communicate to it via REST API.
Google enables only HTTPS communication with their databases – and
normal Arduino boards can’t do HTTPS requests because they do not
have enough power.
So, I need either SSE client + HTTPS requests library to run on the
PORTENTA board OR Firebase Realtimedatabase library (I know the
library is built on HTTPS requests – but if it’s not compatible I can
recreate it with SSE client and HTTPS requests).
Other info:
By SSE I mean Server Send Events for Realtime communication and
updates to the board :)
I have no problem programming it in native Arduino language or in Micropython.
The answer below states that I can use those libraries if the board is supported - but I don't know how to know if the board is supported :/
I have never heard that Arduino lacks the power for HTTPS requests and Arduino have documentation on how to make HTTPS requests for many of their official boards: https://arduinogetstarted.com/tutorials/arduino-https-request
If you are using an unsupported board you will have to route the traffic to another host first. Either another Arduino or an ESP to act as the main host/client that it can communicate to access external resources such as Firebase.

How to send OTA update to ESP32 through MQTT

I know there is an ArduinoOTA library for sending OTA update to Arduino framework based board (ESP32). To best of my knowledge, the device needs to have static IP address to get the OTA update using this library.
However, If a client device use dynamic IP - like sometime WiFi or cellular network internet, is there any solution, preferably using MQTT message, to send OTA to the client device.
Your best bet will be to use Mongoose OS Implementation on ESP32. The tutorial is here: https://mongoose-os.com/docs/mongoose-os/userguide/ota.md
MQTT is not suitable, but doable. As MQTT just sends messages, it can't send whole files. Though you may be able to send the whole contents of your OTA file and then save it on the other end as the OTA file with proper extension and do the OTA from that point on.
Thank You.
Naveen PS.

Is there a way to update the firmware through internet that works for esp32?

We can update the firmware wirelessly using the OTA updates. But for updating the firmware, the esp32 device needs to be on a local network.
Is there any way or source to update the firmware of the device which is not on the local network?
There is also a way to update ESP32 firmware over the internet without any additional update server (with public and static IP address) and without port forwarding. It is based on Husarnet p2p VPN service.
The instruction is here: https://docs.husarnet.com/blog/internet-ota-esp32
Assuming the ESP32 OTA works very similar to ESP8266 which I have used. This can be accomplished in at least two ways.
This link: http://esp8266.github.io/Arduino/versions/2.1.0/doc/ota_updates/ota_updates.html
gives details on using the browser to deploy the update to the device. You would need to forward your port 80 traffic from your public IP to your internal device IP address. Then you can access your ESP through the browser and your public IP address. The better option is to publish your updated firmware to an externally available HTTP server and change your ESP OTA codde to check that external server for updates. This method is detailed in the "HTTP Server" section of the above document. This is just general guidance to the general question you asked. I believe the solutions above would work, but I have not tried them myself yet.
Yes, absolutely. OTA has no requirement of the device being on a local network. You just need to host your firmware .bin file on a web server, then have the ESP32 connect to the server and send a GET request. Espressif's example project for OTA is pretty good, but I'm assuming you're using the ESP32 Arduino libraries and not the ESP-IDF.
I'm guessing you're using this. I'd take a look at the AWS OTA example which I believe is one of the standard ESP32 Arduino examples. It's under Examples, Examples for SparkFun ESP32 Thing, Update, AWS_S3_OTA_Update. It looks like it allows you to OTA from an Amazon Web Services hosted server, which should do exactly what you want.
You can make an app that fetches the update file from the internet or elsewhere then transfer the file via BLE to the ESP32. In this case, the ESP32 will need to be connected to the phone via BLE only (no WIFI needed).
Here is the example Arduino code for the ESP32 and the Android app code

Forwarding data from server to router to Arduino

Basically I have an Arduino UNO that I have connected via Ethernet to my router. I have logged the IP with my server and I would like to push data to my arduino. I guess the flow of data would be to send data (not sure the best way? Should I just open up a port and stream the data?) to the router that then forwards it to my Arduino at a local address assigned by my router.
How can I tell the router to forward this information on without logging in and changing anything with the router. The point of this exercise is to do everything with code and not do any other activities like logging into the router and setting things up. I want to do this as I have my Arduino recording temperature and I want to push data to state if it should switch sending the data from degrees or Fahrenheit or visa versa.
I would use a socket server with a publish/subscribe pattern. Probably several out there but I know XSockets.NET best.
A few hours ago I actually connected my Arduino to XSockets and communicated between webpages and my arduino both ways.
I will put the code on github when I am done, but you can look at a video showing the concept here: The Stuff For You Starts At 3:20
You dont need to open any ports as long as your arduino and server is on the same local network. Since IP adresses maps all devices on a network including your arduino.
The reason you want to open ports on your router is to make people outside your local network access a server on your network. Reasons why port forwarding exists is that when someone sends a packet of data to your public IP (which is your router) it doesent know which local IP to send the packet to. It then discards it, unless you port forward a port to a local IP, which means your router sends all packets with that port to that local IP adress.
You also need to make your own server program, for example a java program to act as a server.
I made something similar to what you are looking for, but i use a wifi shield instead. I've had some problems with maintaining connections with it, but so far it seems that it's a problem with the wifi libary.
You still might learn something from it though:
Maintaining communication between Arduino and Java program
If you want to know more about how the java server works in general, here is a great tutorial which explains a lot of the basics:
http://www.thenewboston.org/watch.php?cat=25&number=38

Resources