Advertisement
Guest User

Matthijs

a guest
Apr 13th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.81 KB | Software | 0 0
  1. #define DEBUG_ETHERNET_WEBSERVER_PORT Serial
  2. #define UNIQUE_SUBARTNET
  3. #define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
  4.  
  5. #define NB_CHANNEL_PER_LED 3          //Should be 4 if your sending tool is sending RGBW
  6. #define UNIVERSE_SIZE_IN_CHANNEL 510  //here we define a universe of 170 pixels each pixel is composed of 3 channels
  7.  
  8. #include <Preferences.h>
  9. #include "WebServer_ESP32_W5500.h"
  10. #include "I2SClocklessLedDriver.h"
  11. #include "artnetESP32V2.h"
  12.  
  13. WebServer server(80);
  14. Preferences preferences;
  15.  
  16. // Default configuration values
  17. int numledsperstrip = 680;
  18. int numstrips = 3;
  19. int startuniverse = 0;
  20. int pins[3] = { 2, 15, 0 };
  21. String colororder = "GRB";  // Default color order
  22. String nodename = "Artnet Node esp32";
  23. artnetESP32V2 artnet = artnetESP32V2();
  24. I2SClocklessLedDriver driver;
  25.  
  26. void displayfunction(void *param) {
  27.   subArtnet *subartnet = (subArtnet *)param;
  28.   driver.showPixels(NO_WAIT, subartnet->data);
  29. }
  30.  
  31.  
  32. void handleRoot() {
  33.   String content = "<!DOCTYPE html><html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
  34.   content += "<link rel=\"icon\" href=\"data:,\">";  // Empty favicon
  35.   content += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}";
  36.   content += ".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}";
  37.   content += ".button2 { background-color: #555555; }</style></head>";
  38.   content += "<body><h1>" + nodename + " Configuration</h1><form method='get' action='/config'>";
  39.   content += "Number of LEDs per strip: <input type='text' name='numledsperstrip' value='" + String(numledsperstrip) + "'><br>";
  40.   content += "Number of strips: <input type='text' name='numstrips' value='" + String(numstrips) + "'><br>";
  41.   content += "Startuniverse: <input type='text' name='startuniverse' value='" + String(startuniverse) + "'><br>";
  42.   content += "Color order (e.g., RGB, GRB): <input type='text' name='colororder' value='" + colororder + "'><br>";
  43.   content += "Arnet Node name: <input type='text' name='nodename' value='" + nodename + "'><br>";
  44.   content += "<input type='submit' value='Save'></form></body></html>";
  45.   server.send(200, "text/html", content);
  46. }
  47.  
  48. void handleConfig() {
  49.   if (server.args() > 0) {
  50.     for (uint8_t i = 0; i < server.args(); i++) {
  51.       if (server.argName(i) == "numledsperstrip") {
  52.         numledsperstrip = server.arg(i).toInt();
  53.       } else if (server.argName(i) == "numstrips") {
  54.         numstrips = server.arg(i).toInt();
  55.       } else if (server.argName(i) == "startuniverse") {
  56.         startuniverse = server.arg(i).toInt();
  57.       } else if (server.argName(i) == "colororder") {
  58.         colororder = server.arg(i);
  59.       } else if (server.argName(i) == "nodename") {
  60.         nodename = server.arg(i);
  61.       }
  62.     }
  63.  
  64.     // Store updated configuration in Preferences
  65.     preferences.begin("esp32config", false);
  66.     preferences.putInt("numledsperstrip", numledsperstrip);
  67.     preferences.putInt("numstrips", numstrips);
  68.     preferences.putInt("startuniverse", startuniverse);
  69.     preferences.putString("colororder", colororder);
  70.     preferences.putString("nodename", nodename);
  71.     preferences.end();
  72.   }
  73.  
  74.   handleRoot();
  75. }
  76.  
  77.  
  78. void setup() {
  79.   Serial.begin(115200);
  80.  
  81.   // Initialize Preferences
  82.   preferences.begin("esp32config", true);
  83.   numledsperstrip = preferences.getInt("numledsperstrip", numledsperstrip);
  84.   numstrips = preferences.getInt("numstrips", numstrips);
  85.   colororder = preferences.getString("colororder", colororder);
  86.   nodename = preferences.getString("nodename", nodename);
  87.   startuniverse = preferences.getInt("startuniverse", startuniverse);
  88.   preferences.end();
  89.  
  90.   while (!Serial && (millis() < 5000))
  91.     ;
  92.   driver.initled(NULL, pins, numstrips, numledsperstrip);
  93.   driver.setBrightness(20);
  94.  
  95.   // Initialize Ethernet
  96.   ESP32_W5500_onEvent();
  97.   ETH.begin(MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST);
  98.  
  99.   //Output
  100.   //addSubArtnet(Output universe start, #bits 680*1*3 = 2040, universe size = 510, callbackfunction )
  101.   artnet.addSubArtnet(startuniverse, numledsperstrip * numstrips * NB_CHANNEL_PER_LED, UNIVERSE_SIZE_IN_CHANNEL, &displayfunction);
  102.   artnet.setNodeName("Arnet Node esp32");
  103.  
  104.   if (artnet.listen(ETH.localIP(), 6454)) {
  105.     Serial.print("artnet Listening on IP: ");
  106.     Serial.println(ETH.localIP());
  107.   }
  108.  
  109.   // Initialize Web Server
  110.   server.on("/", HTTP_GET, handleRoot);
  111.   server.on("/config", HTTP_GET, handleConfig);
  112.   server.begin();
  113.  
  114.   ESP32_W5500_waitForConnect();
  115.  
  116.   Serial.println("");
  117.   Serial.println("Ethernet connected.");
  118.   Serial.println("IP address: ");
  119.   Serial.println(ETH.localIP());
  120.   Serial.println("HTTP server started");
  121. }
  122.  
  123. void loop() {
  124.   server.handleClient();
  125. }
  126.  
Tags: Arduino
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement