acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

WiFiPhotoFrame.ino (9767B)

      1 /*******************************************************************************
      2  * WiFi Photo Frame
      3  * This is a simple IoT photo frame sample
      4  * Please find more details at instructables:
      5  * https://www.instructables.com/id/Face-Aware-OSD-Photo-Frame/
      6  *
      7  * Dependent libraries:
      8  * JPEGDEC: https://github.com/bitbank2/JPEGDEC.git
      9  *
     10  * Raspberry Pi Pico W dependent libraries:
     11  * HttpClient: https://github.com/moononournation/HttpClient.git
     12  *
     13  * Setup steps:
     14  * 1. Fill your own SSID_NAME, SSID_PASSWORD, HTTP_HOST, HTTP_PORT and HTTP_PATH_TEMPLATE
     15  * 2. Change your LCD parameters in Arduino_GFX setting
     16  ******************************************************************************/
     17 
     18 /* WiFi settings */
     19 const char *SSID_NAME = "YourAP";
     20 const char *SSID_PASSWORD = "PleaseInputYourPasswordHere";
     21 
     22 const char *HTTP_HOST = "192.168.12.34";                /* Your HTTP photo server host name */
     23 const uint16_t HTTP_PORT = 5000;                        /* Your HTTP photo server port */
     24 const char *HTTP_PATH_TEMPLATE = "/OSDPhoto?w=%d&h=%d"; /* Your HTTP photo server URL path template */
     25 
     26 const uint16_t HTTP_TIMEOUT = 30000; // in ms, wait a while for server processing
     27 
     28 /*******************************************************************************
     29  * Start of Arduino_GFX setting
     30  *
     31  * Arduino_GFX try to find the settings depends on selected board in Arduino IDE
     32  * Or you can define the display dev kit not in the board list
     33  * Defalult pin list for non display dev kit:
     34  * Arduino Nano, Micro and more: CS:  9, DC:  8, RST:  7, BL:  6, SCK: 13, MOSI: 11, MISO: 12
     35  * ESP32 various dev board     : CS:  5, DC: 27, RST: 33, BL: 22, SCK: 18, MOSI: 23, MISO: nil
     36  * ESP32-C3 various dev board  : CS:  7, DC:  2, RST:  1, BL:  3, SCK:  4, MOSI:  6, MISO: nil
     37  * ESP32-S2 various dev board  : CS: 34, DC: 38, RST: 33, BL: 21, SCK: 36, MOSI: 35, MISO: nil
     38  * ESP32-S3 various dev board  : CS: 40, DC: 41, RST: 42, BL: 48, SCK: 36, MOSI: 35, MISO: nil
     39  * ESP8266 various dev board   : CS: 15, DC:  4, RST:  2, BL:  5, SCK: 14, MOSI: 13, MISO: 12
     40  * Raspberry Pi Pico dev board : CS: 17, DC: 27, RST: 26, BL: 28, SCK: 18, MOSI: 19, MISO: 16
     41  * RTL8720 BW16 old patch core : CS: 18, DC: 17, RST:  2, BL: 23, SCK: 19, MOSI: 21, MISO: 20
     42  * RTL8720_BW16 Official core  : CS:  9, DC:  8, RST:  6, BL:  3, SCK: 10, MOSI: 12, MISO: 11
     43  * RTL8722 dev board           : CS: 18, DC: 17, RST: 22, BL: 23, SCK: 13, MOSI: 11, MISO: 12
     44  * RTL8722_mini dev board      : CS: 12, DC: 14, RST: 15, BL: 13, SCK: 11, MOSI:  9, MISO: 10
     45  * Seeeduino XIAO dev board    : CS:  3, DC:  2, RST:  1, BL:  0, SCK:  8, MOSI: 10, MISO:  9
     46  * Teensy 4.1 dev board        : CS: 39, DC: 41, RST: 40, BL: 22, SCK: 13, MOSI: 11, MISO: 12
     47  ******************************************************************************/
     48 #include <Arduino_GFX_Library.h>
     49 
     50 #define GFX_BL DF_GFX_BL // default backlight pin, you may replace DF_GFX_BL to actual backlight pin
     51 
     52 /* More dev device declaration: https://github.com/moononournation/Arduino_GFX/wiki/Dev-Device-Declaration */
     53 #if defined(DISPLAY_DEV_KIT)
     54 Arduino_GFX *gfx = create_default_Arduino_GFX();
     55 #else /* !defined(DISPLAY_DEV_KIT) */
     56 
     57 /* More data bus class: https://github.com/moononournation/Arduino_GFX/wiki/Data-Bus-Class */
     58 Arduino_DataBus *bus = create_default_Arduino_DataBus();
     59 
     60 /* More display class: https://github.com/moononournation/Arduino_GFX/wiki/Display-Class */
     61 Arduino_GFX *gfx = new Arduino_ILI9341(bus, DF_GFX_RST, 0 /* rotation */, false /* IPS */);
     62 
     63 #endif /* !defined(DISPLAY_DEV_KIT) */
     64 /*******************************************************************************
     65  * End of Arduino_GFX setting
     66  ******************************************************************************/
     67 
     68 #if defined(ESP32)
     69 #include <esp_task_wdt.h>
     70 #include <WiFi.h>
     71 #include <HTTPClient.h>
     72 WiFiClient client;
     73 HTTPClient http;
     74 #elif defined(ESP8266)
     75 #include <ESP8266WiFi.h>
     76 #include <ESP8266HTTPClient.h>
     77 WiFiClient client;
     78 HTTPClient http;
     79 #elif defined(ARDUINO_RASPBERRY_PI_PICO_W)
     80 #include <WiFi.h>
     81 #include <WiFiClient.h>
     82 #include <HttpClient.h>
     83 WiFiClient client;
     84 HttpClient http(client);
     85 #elif defined(RTL8722DM)
     86 #include <WiFi.h>
     87 #include <WiFiClient.h>
     88 #include <HttpClient.h>
     89 WiFiClient client;
     90 HttpClient http(client);
     91 #endif
     92 
     93 #include "JpegFunc.h"
     94 
     95 // pixel drawing callback
     96 static int jpegDrawCallback(JPEGDRAW *pDraw)
     97 {
     98   // Serial.printf("Draw pos = %d,%d. size = %d x %d\n", pDraw->x, pDraw->y, pDraw->iWidth, pDraw->iHeight);
     99   gfx->draw16bitRGBBitmap(pDraw->x, pDraw->y, pDraw->pPixels, pDraw->iWidth, pDraw->iHeight);
    100   return 1;
    101 }
    102 
    103 static unsigned long next_show_millis = 0;
    104 
    105 char http_path[1024];
    106 
    107 void setup()
    108 {
    109   Serial.begin(115200);
    110   // Serial.setDebugOutput(true);
    111   // while(!Serial);
    112   Serial.println("WiFi Photo Frame");
    113 
    114 #ifdef GFX_EXTRA_PRE_INIT
    115   GFX_EXTRA_PRE_INIT();
    116 #endif
    117 
    118   Serial.println("Init display");
    119   gfx->begin();
    120   gfx->fillScreen(BLACK);
    121 
    122 #ifdef GFX_BL
    123   pinMode(GFX_BL, OUTPUT);
    124   digitalWrite(GFX_BL, HIGH);
    125 #endif
    126 
    127   Serial.println("Init WiFi");
    128   gfx->println("Init WiFi");
    129 #if defined(ESP32) || defined(ESP8266)
    130   WiFi.mode(WIFI_STA);
    131   WiFi.begin(SSID_NAME, SSID_PASSWORD);
    132 #elif defined(ARDUINO_RASPBERRY_PI_PICO_W)
    133   WiFi.mode(WIFI_STA);
    134   WiFi.begin(SSID_NAME, SSID_PASSWORD);
    135 #elif defined(RTL8722DM)
    136   WiFi.begin((char *)SSID_NAME, (char *)SSID_PASSWORD);
    137 #endif
    138   while (WiFi.status() != WL_CONNECTED)
    139   {
    140     delay(500);
    141     Serial.print(".");
    142     gfx->print(".");
    143   }
    144   Serial.println(" CONNECTED");
    145   gfx->println(" CONNECTED");
    146 
    147   Serial.println("Init HTTP path");
    148   gfx->println("Init HTTP path");
    149   // setup http_path query value with LCD dimension
    150   sprintf(http_path, HTTP_PATH_TEMPLATE, gfx->width(), gfx->height());
    151   Serial.println(http_path);
    152   gfx->println(http_path);
    153 
    154 #if defined(ESP32)
    155   Serial.println("Init ESP32 WDT");
    156   gfx->println("Init ESP32 WDT");
    157   // set WDT timeout a little bit longer than HTTP timeout
    158   esp_task_wdt_init((HTTP_TIMEOUT / 1000) + 1, true);
    159   enableLoopWDT();
    160 #endif
    161 }
    162 
    163 void loop()
    164 {
    165   if (millis() < next_show_millis)
    166   {
    167     // Serial.println("Wait for next minute...");
    168     delay(1000);
    169   }
    170   else
    171   {
    172     if (WiFi.status() == WL_CONNECTED)
    173     {
    174       int jpeg_result = 0;
    175 
    176       Serial.printf("[HTTP] begin...\n");
    177       http.setTimeout(HTTP_TIMEOUT);
    178 #if defined(ESP32) || defined(ESP8266)
    179       http.begin(client, HTTP_HOST, HTTP_PORT, http_path);
    180       int httpCode = http.GET();
    181 #elif defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(RTL8722DM)
    182       http.get(HTTP_HOST, HTTP_PORT, http_path);
    183       int httpCode = http.responseStatusCode();
    184       http.skipResponseHeaders();
    185 #endif
    186 
    187       Serial.printf("[HTTP] GET... code: %d\n", httpCode);
    188       gfx->printf("[HTTP] GET... code: %d\n", httpCode);
    189       // HTTP header has been send and Server response header has been handled
    190       if (httpCode <= 0)
    191       {
    192 #if defined(ESP32) || defined(ESP8266)
    193         Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
    194 #endif
    195       }
    196       else
    197       {
    198         if (httpCode != 200)
    199         {
    200           Serial.printf("[HTTP] Not OK!\n");
    201           gfx->printf("[HTTP] Not OK!\n");
    202           delay(5000);
    203         }
    204         else
    205         {
    206 // get lenght of document(is - 1 when Server sends no Content - Length header)
    207 #if defined(ESP32) || defined(ESP8266)
    208           int len = http.getSize();
    209 #elif defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(RTL8722DM)
    210           int len = http.contentLength();
    211 #endif
    212           Serial.printf("[HTTP] size: %d\n", len);
    213           gfx->printf("[HTTP] size: %d\n", len);
    214 
    215           if (len <= 0)
    216           {
    217             Serial.printf("[HTTP] Unknow content size: %d\n", len);
    218             gfx->printf("[HTTP] Unknow content size: %d\n", len);
    219           }
    220           else
    221           {
    222             unsigned long start = millis();
    223 
    224             uint8_t *buf = (uint8_t *)malloc(len);
    225             if (buf)
    226             {
    227 #if defined(ESP32) || defined(ESP8266)
    228               static WiFiClient *http_stream = http.getStreamPtr();
    229               jpeg_result = jpegOpenHttpStreamWithBuffer(http_stream, buf, len, jpegDrawCallback);
    230 #elif defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(RTL8722DM)
    231               jpeg_result = jpegOpenHttpStreamWithBuffer(&client, buf, len, jpegDrawCallback);
    232 #endif
    233               if (jpeg_result)
    234               {
    235                 jpeg_result = jpegDraw(false /* useBigEndian */,
    236                                        0 /* x */, 0 /* y */, gfx->width() /* widthLimit */, gfx->height() /* heightLimit */);
    237               }
    238               free(buf);
    239             }
    240             else
    241             {
    242 // get tcp stream
    243 #if defined(ESP32) || defined(ESP8266)
    244               static WiFiClient *http_stream = http.getStreamPtr();
    245               jpeg_result = jpegOpenHttpStream(http_stream, len, jpegDrawCallback);
    246 #elif defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(RTL8722DM)
    247               jpeg_result = jpegOpenHttpStream(&client, len, jpegDrawCallback);
    248 #endif
    249               if (jpeg_result)
    250               {
    251                 jpeg_result = jpegDraw(false /* useBigEndian */,
    252                                        0 /* x */, 0 /* y */, gfx->width() /* widthLimit */, gfx->height() /* heightLimit */);
    253               }
    254             }
    255             Serial.printf("Time used: %lu\n", millis() - start);
    256           }
    257         }
    258       }
    259 #if defined(ESP32) || defined(ESP8266)
    260       http.end();
    261 #elif defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(RTL8722DM)
    262       http.stop();
    263 #endif
    264 
    265       if (jpeg_result)
    266       {
    267         next_show_millis = ((millis() / 60000L) + 1) * 60000L; // next minute
    268       }
    269     }
    270   }
    271 
    272 #if defined(ESP32)
    273   // notify WDT still working
    274   feedLoopWDT();
    275 #elif defined(ESP8266)
    276   yield();
    277 #elif defined(ARDUINO_RASPBERRY_PI_PICO_W)
    278   yield();
    279 #endif
    280 }