瀏覽代碼

Connect to wifi and of course not tested

Emil 3 年之前
父節點
當前提交
21962db0c2
共有 5 個文件被更改,包括 80 次插入17 次删除
  1. 4 2
      platformio.ini
  2. 2 2
      src/LightCluster.cpp
  3. 3 3
      src/LightCluster.h
  4. 69 9
      src/main.cpp
  5. 2 1
      src/main.h

+ 4 - 2
platformio.ini

@@ -12,5 +12,7 @@
 platform = espressif32
 board = esp32doit-devkit-v1
 framework = arduino
-lib_deps = 
-	adafruit/Adafruit NeoPixel@^1.10.4
+lib_deps =
+	me-no-dev/AsyncTCP@^1.1.1
+	me-no-dev/ESP Async WebServer
+	adafruit/Adafruit NeoPixel@^1.10.4

+ 2 - 2
src/LightCluster.cpp

@@ -47,7 +47,7 @@ void LightCluster::changeAnimation(int newAnimationNumber) {
     runAnimation();
 }
 
-LightCluster::LightCluster(light *incomingLights, int size, int animation) {
+LightCluster::LightCluster(struct light *incomingLights, int size, int animation) {
 //    for (int i = 0; i < sizeof(lights); i++) {
 //        light data;
 //        data.mapped = lights[i];
@@ -57,7 +57,7 @@ LightCluster::LightCluster(light *incomingLights, int size, int animation) {
     numLights = size;
     numLeds = size;
     lights = incomingLights;
-
+    lastRun = 0;
 
     runSetup();
     runAnimation();

+ 3 - 3
src/LightCluster.h

@@ -13,7 +13,7 @@ struct light {
 
 class LightCluster {
 private:
-    long lastRun;
+    unsigned long lastRun;
 
     void runSetup();
     bool shouldRun();
@@ -25,9 +25,9 @@ public:
     void runAnimation();
     void changeAnimation(int newAnimationNumber);
 
-    LightCluster(light *incomingLights, int size, int animation);
+    LightCluster(struct light *incomingLights, int size, int animation);
 
-    light * lights;
+    struct light *lights;
 };
 
 

+ 69 - 9
src/main.cpp

@@ -1,6 +1,18 @@
 #include "main.h"
 #include "LightCluster.h"
 
+#include <WiFi.h>
+#include <../.pio/libdeps/esp32doit-devkit-v1/AsyncTCP/src/AsyncTCP.h>
+#include "../.pio/libdeps/esp32doit-devkit-v1/ESP Async WebServer/src/ESPAsyncWebServer.h"
+#include "FS.h"
+#include "SD.h"
+#include "SPI.h"
+
+const char* ssid = "REPLACE_WITH_YOUR_SSID";
+const char* password = "REPLACE_WITH_YOUR_PASSWORD";
+AsyncWebServer server(80);
+
+
 Adafruit_NeoPixel leds(NUM_LEDS, LED_PIN, NEO_WRGB + NEO_KHZ800);
 
 //LightCluster clusters[] = {};
@@ -8,9 +20,64 @@ light lampsInCluster1[7] = {{0,0},{1,0},{2,0},{3,0},{4,0},{5,0},{6,0}};
 
 LightCluster myCluster(lampsInCluster1, 7, 0);
 
+void connectWiFi() {
+    WiFi.mode(WIFI_STA);
+    WiFi.begin(ssid, password);
+    Serial.print("Connecting to WiFi ..");
+    while (WiFi.status() != WL_CONNECTED) {
+        Serial.print('.');
+        delay(1000);
+    }
+    Serial.println(WiFi.localIP());
+}
+
+void connectSDCard(){
+    if(!SD.begin()){
+        Serial.println("Card Mount Failed");
+        return;
+    }
+    uint8_t cardType = SD.cardType();
+
+    if(cardType == CARD_NONE){
+        Serial.println("No SD card attached");
+        return;
+    }
+
+    Serial.print("SD Card Type: ");
+    if(cardType == CARD_MMC){
+        Serial.println("MMC");
+    } else if(cardType == CARD_SD){
+        Serial.println("SDSC");
+    } else if(cardType == CARD_SDHC){
+        Serial.println("SDHC");
+    } else {
+        Serial.println("UNKNOWN");
+    }
+    uint64_t cardSize = SD.cardSize() / (1024 * 1024);
+    Serial.printf("SD Card Size: %lluMB\n", cardSize);
+}
+
+void setupServer() {
+    server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
+        request->send(SD, "/index.html", "text/html");
+    });
+
+    server.serveStatic("/", SD, "/");
+
+    server.on("/api/create_cluster", HTTP_POST, [](AsyncWebServerRequest *request) {
+        String lightString = request->getHeader("lights")->toString();
+    });
+}
+
 void setup() {
     // write your initialization code here
     Serial.begin(115000);
+
+    connectSDCard();
+    connectWiFi();
+    setupServer();
+    server.begin();
+
     delay(1000);
     leds.begin();
     leds.clear();
@@ -56,14 +123,7 @@ void loop() {
         leds.show();
         delay(1000);
     }*/
-//    Serial.println("In loop");
-//    delay(1000);
-//    Serial.println("In loop2");
     myCluster.runAnimation();
-    //if (hasRun) {
-      //  Serial.println("Running show");
-      //  leds.show();
-   // }
-   leds.show();
-   delay(1);
+    leds.show();
+    delay(1);
 }

+ 2 - 1
src/main.h

@@ -1,8 +1,9 @@
 //
 // Created by emilr on 2022-04-13.
 //
-#include "../.pio/libdeps/esp32doit-devkit-v1/Adafruit NeoPixel/Adafruit_NeoPixel.h"
 #include <Arduino.h>
+#include "../.pio/libdeps/esp32doit-devkit-v1/Adafruit NeoPixel/Adafruit_NeoPixel.h"
+
 #define LED_PIN    4
 #define NUM_LEDS 10