From 1c361820c7671260c7457dc996b3ddd2392b898e Mon Sep 17 00:00:00 2001 From: Boyan Date: Sat, 9 Sep 2023 19:35:41 +0200 Subject: [PATCH] Remade the ESP32 code. Fixed some bugs in the website. --- src/arduino/text_display/text_display.ino | 116 ++++++++++++++++++++++ src/place/place/urls.py | 1 + src/place/website/config.py | 1 + src/place/website/views.py | 49 +++++---- 4 files changed, 147 insertions(+), 20 deletions(-) create mode 100644 src/arduino/text_display/text_display.ino create mode 100644 src/place/website/config.py diff --git a/src/arduino/text_display/text_display.ino b/src/arduino/text_display/text_display.ino new file mode 100644 index 0000000..a482eea --- /dev/null +++ b/src/arduino/text_display/text_display.ino @@ -0,0 +1,116 @@ + +// Example sketch which shows how to display some patterns +// on a 64x32 LED matrix +// + +#include +#include +#include + + +#define PANEL_RES_X 64 // Number of pixels wide of each INDIVIDUAL panel module. +#define PANEL_RES_Y 32 // Number of pixels tall of each INDIVIDUAL panel module. +#define PANEL_CHAIN 1 // Total number of panels chained one to another + +//MatrixPanel_I2S_DMA dma_display; +MatrixPanel_I2S_DMA *dma_display = nullptr; + +void putPixel(int16_t x, int16_t y, uint16_t color) { + dma_display->drawPixel(x, y, color); +} + +// Replace with your network credentials +const char* ssid = "ssid"; +const char* password = "password"; + +void initWiFi() { + 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()); +} + +String serverName = "https://changethis.com/board/json/"; + + + +void setup() { + initWiFi(); + // Module configuration + HUB75_I2S_CFG mxconfig( + PANEL_RES_X, // module width + PANEL_RES_Y, // module height + PANEL_CHAIN // Chain length + ); + + + // Display Setup + dma_display = new MatrixPanel_I2S_DMA(mxconfig); + dma_display->begin(); + dma_display->setBrightness8(90); //0-255 + dma_display->clearScreen(); + + // fill the screen with 'black' + dma_display->fillScreen(dma_display->color444(0, 0, 0)); + +} + + +// the following variables are unsigned longs because the time, measured in +// milliseconds, will quickly become a bigger number than can be stored in an int. +unsigned long lastTime = 0; +// Timer set to 10 minutes (600000) +//unsigned long timerDelay = 600000; +// Set timer to 5 seconds (5000) +unsigned long timerDelay = 5000; + +void loop() { + + if ((millis() - lastTime) > timerDelay) { + //Check WiFi connection status + if(WiFi.status()== WL_CONNECTED){ + HTTPClient http; + + String serverPath = serverName; + + // Your Domain name with URL path or IP address with path + http.begin(serverPath.c_str()); + + // If you need Node-RED/server authentication, insert user and password below + //http.setAuthorization("REPLACE_WITH_SERVER_USERNAME", "REPLACE_WITH_SERVER_PASSWORD"); + + // Send HTTP GET request + int httpResponseCode = http.GET(); + + if (httpResponseCode>0) { + Serial.print("HTTP Response code: "); + Serial.println(httpResponseCode); + String payload = http.getString(); + Serial.println(payload); + } + else { + Serial.print("Error code: "); + Serial.println(httpResponseCode); + } + // Free resources + http.end(); + } + else { + Serial.println("WiFi Disconnected, trying to reconnect"); + WiFi.reconnect(); + } + lastTime = millis(); + } +} + // animate by going through the colour wheel for the first two lines + drawText(wheelval); + wheelval +=1; + + delay(20); + + +} diff --git a/src/place/place/urls.py b/src/place/place/urls.py index 57dada2..f15a09c 100644 --- a/src/place/place/urls.py +++ b/src/place/place/urls.py @@ -21,5 +21,6 @@ from website import views urlpatterns = [ path('admin/', admin.site.urls), path('board/', views.current_config), + path('board/json/', views.current_board), path('board///', views.update_board), ] diff --git a/src/place/website/config.py b/src/place/website/config.py new file mode 100644 index 0000000..3514e70 --- /dev/null +++ b/src/place/website/config.py @@ -0,0 +1 @@ +URL = "http://127.0.0.1:8000/board/" diff --git a/src/place/website/views.py b/src/place/website/views.py index 4cba090..afee429 100644 --- a/src/place/website/views.py +++ b/src/place/website/views.py @@ -5,6 +5,8 @@ from django.http import HttpResponse from django.core.exceptions import ObjectDoesNotExist from colour import Color from pytz import UTC +from .config import URL + # Change these for your board BOARD_X = 64 BOARD_Y = 32 @@ -17,11 +19,7 @@ BOARD_Y = 32 # Internal validation def validateBoard(x:int, y:int, board:dict, ip) -> bool: - try: - ip = RestrictedIP.objects.get(ip=ip) - return "RESTRICTED" - except ObjectDoesNotExist: - pass + key_counter = 0 # Not using len for optimization purposes for row in board: @@ -48,12 +46,12 @@ def restrict_or_unrestrict(ip): # if banned for more than 1 minute, unban if timedelta.seconds > 60: ip.delete() - return "ALLOWED" + return "SUCESS" return "STILL RESTRICTED" except ObjectDoesNotExist: instance = RestrictedIP(ip=ip, time_added=datetime.now()) instance.save() - return "RESTRICTION" + return "SUCCESS" # Internal board class class localBoard: @@ -83,7 +81,7 @@ class localBoard: instance.save() self.board = Board.objects.latest("update_time") return "SUCCESS" - + def HTML(self) -> str: board = self.getBoard() style = """ @@ -100,30 +98,38 @@ class localBoard: """ - selected_pixel = """ + selected_pixel = f""" """ - - html = f"Ballin{style}{selected_pixel}" + reload_page = """ + + """ + html = f"Ballin{reload_page}{style}{selected_pixel}
" for row in board: html += "" x = 0 @@ -132,7 +138,7 @@ class localBoard: x += 1 html += "" html += "

<------ pick ur color

" - return html + return html # Publicly visible functions def update_board(request, x, y, color): @@ -144,7 +150,7 @@ def update_board(request, x, y, color): board[str(y)][int(x)] = color ip = restrict_or_unrestrict(request.META["REMOTE_ADDR"]) - if ip == "STILL RESTRICTED": + if ip != "SUCCESS": return HttpResponse("RESTRICTED") res = localBoard().setBoard(board, request.META["REMOTE_ADDR"]) return HttpResponse(f"board: {str(res)}, ip: {ip}") @@ -153,3 +159,6 @@ def current_config(request): html = localBoard().HTML() return HttpResponse(html) +def json_board(request): + board = localBoard().getBoard() + return HttpResponse(board)