Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add webserver #98

Merged
merged 12 commits into from
Dec 3, 2023
Merged

Add webserver #98

merged 12 commits into from
Dec 3, 2023

Conversation

lenvm
Copy link
Collaborator

@lenvm lenvm commented Nov 16, 2023

What

This pull request adds webserver functionality.
The devboard creates a WiFi Access Point named Battery Emulator that can be connected to using the password "123456789". The IP address of the board as Access Point is printed via Serial. The IP address allows you to connect to the webserver via a webbrowser.
The devboard simultaneously connects to an existing network using the ssid and password settings provided in webserver.cpp. If the connection is successful, the IP address obtained is printed via Serial. This IP address you can connect to via a webbrowser, while you are connected to your local network.

The webserver currently displays:

  • LED status
  • WiFi SSID it is trying to connect to
  • WiFi status: connected or not connected
  • IP address obtained from the WiFi it has connected to

The webserver also allows for updating over the air. Please see the README.md file for details.

How

The webserver is implemented by using the existing ESP32 Wifi.h library.
The over-the-air capability is implemented by adding 3 new libraries:

  • me-no-dev/AsyncTCP
  • me-no-dev/ESPAsyncWebServer
  • ayushsharma82/ElegantOTA

The specific commit of each library is added to the README.md file of the respective library. Adding the libraries as a git submodule would be an alternative, however some specific changes to make the libraries work in the directory that they are stored in are needed, therefore this approach of storing the commit used in the README.md file is taken, for traceability.

Why

It is useful to be able to know the state of the devboard without connecting to it using Serial. Now you are able to check the state of the battery emulation from your couch 😎

This PR opens the possibility to add more webserver functionalities, such as:

  • displaying more variables, such as state of health, power, etc.
  • configuring which battery is used and which inverter protocol shall be emulated
  • configuring which WiFi network shall be connected to
  • displaying current CAN state
  • displaying detailed diagnosis messages for the battery and the inverter, helping users to debug potential issues
  • etc.

@rjsc
Copy link
Collaborator

rjsc commented Nov 16, 2023

You must store the strings in the program memory explicitly, or else, every string gets loaded to RAM at boot. The RAM is getting filled up with strings plus the many Modbus registers from LUNA protocol.
Check https://arduino-esp8266.readthedocs.io/en/latest/PROGMEM.html

I would also recommend using a asynchronous webserver implentacion such as https://github.com/me-no-dev/ESPAsyncWebServer

@lenvm
Copy link
Collaborator Author

lenvm commented Nov 19, 2023

Thanks @rjsc!

I’ll have a go at implementing the webserver as an asynchronous webserver that saves the HTML and CSS files in SPIFFS, as described in this tutorial.

@dalathegreat
Copy link
Owner

@lenvm The Luna2000 registers are not confirmed to need 50k space. For now we can reduce this line in LUNA2000-MODBUS.h and Software.ino

#define MB_RTU_NUM_VALUES 30000

This will make it compile.

Comment on lines 35 to 36
const char* ssidAP = "Battery Emulator"; // maximum of 63 characters;
const char* passwordAP = "123456789"; // minimum of 8 characters; set to NULL if you want the access point to be open
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably move these to USER_SETTINGS.h

As well as a boolean flag to enable/disable the web server.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I fully agree with you on both points:

  • The ssid, password, ssidAP and passwordAP should be user settings.
  • There should be a #define to determine whether WiFi and the webserver should be active.

I had not implemented these points yet, as I first wanted to figure out how to resolve the issue with the code not compiling for LUNA2000_MODBUS.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matvey83, I have implemented the two points above.

if (current_line.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to pull the code that generates the response HTML into a separate file, as I imagine it will grow to be quite big eventually.

And also just would be nice to have logic for this separated from the rest of the web server logic.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree with you on this as well. The folder structure as suggested by the ESP32 Web Server using SPIFFS tutorial uses a data folder and index.html and ccs.style files. This seems a good file structure to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matvey83, due to SPIFFS currently not being supported by Arduino 2.0, I have decided not to proceed an implementation using SPIFFS at this point in time. Therefore there are is also no separate HTML file at this point in time. We can implement this as a future improvement of the code.

@lenvm
Copy link
Collaborator Author

lenvm commented Nov 21, 2023

@lenvm The Luna2000 registers are not confirmed to need 50k space. For now we can reduce this line in LUNA2000-MODBUS.h and Software.ino

#define MB_RTU_NUM_VALUES 30000

This will make it compile.

Thanks @dalathegreat, that is great to know! In order to also address the other comments in this PR, I am of the opinion that the best way forward is to try to implement the webserver as an asynchronous webserver, with the HTML and CSS files stored in SPIFFS. I'll give this a go and update this PR accordingly, before modifying the DRAM space defined for Luna2000.

@lenvm lenvm force-pushed the feature/webserver branch from af0327c to 4961358 Compare November 26, 2023 13:59
@lenvm
Copy link
Collaborator Author

lenvm commented Nov 26, 2023

@dalathegreat I have implemented the code as asynchronous webserver, but I have not implemented the code using SPIFFS, as this is currently not supported in Arduino 2.0.

I have therefore followed your suggestion and changed the value for the Luna2000 registers from 50k to 30k.

@lenvm lenvm marked this pull request as ready for review November 26, 2023 14:05
@dalathegreat
Copy link
Owner

@lenvm Should we mention that an external antenna should be fitted to the board?
bild
Maybe we could open a Wiki page for the Webserver side of this

@StarkJohan
Copy link
Collaborator

It's not necessary to connect an external antenna as the board includes a ceramic antenna which is connected by default (the red component marked C3). If an external antenna is to be used, the zero ohm resistor needs to be moved 90 degrees to the alternate position.

@lenvm lenvm requested a review from dalathegreat November 30, 2023 08:32
@lenvm
Copy link
Collaborator Author

lenvm commented Dec 2, 2023

@dalathegreat, can you please review this pull request? In my opinion it is ready to be merged.

Copy link
Owner

@dalathegreat dalathegreat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tested this out locally, and it seems like a great feature! Approving this 👍

@lenvm lenvm force-pushed the feature/webserver branch from 4961358 to e37b8f7 Compare December 3, 2023 19:10
@lenvm lenvm merged commit 85db9c3 into dalathegreat:main Dec 3, 2023
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants