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

U8g2RssReader.ino (8321B)

      1 /*******************************************************************************
      2  * U8g2 RSS Reader
      3  * This is a simple RSS Reader sample with UTF-8 support
      4  *
      5  *  * Raspberry Pi Pico W dependent libraries:
      6  * HttpClient: https://github.com/moononournation/HttpClient.git
      7  *
      8  * Setup steps:
      9  * 1. Fill your own SSID_NAME, SSID_PASSWORD, RSS_HOST, RSS_PORT, RSS_PATH
     10  * 2. Change your LCD parameters in Arduino_GFX setting
     11  ******************************************************************************/
     12 
     13 /* WiFi settings */
     14 #define SSID_NAME "YourAP"
     15 #define SSID_PASSWORD "PleaseInputYourPasswordHere"
     16 
     17 #define RSS_HOST "rss.weather.gov.hk"
     18 #define RSS_PORT 80
     19 #define RSS_PATH "/rss/LocalWeatherForecast_uc.xml"
     20 
     21 /*******************************************************************************
     22  * Start of Arduino_GFX setting
     23  *
     24  * Arduino_GFX try to find the settings depends on selected board in Arduino IDE
     25  * Or you can define the display dev kit not in the board list
     26  * Defalult pin list for non display dev kit:
     27  * Arduino Nano, Micro and more: CS:  9, DC:  8, RST:  7, BL:  6, SCK: 13, MOSI: 11, MISO: 12
     28  * ESP32 various dev board     : CS:  5, DC: 27, RST: 33, BL: 22, SCK: 18, MOSI: 23, MISO: nil
     29  * ESP32-C3 various dev board  : CS:  7, DC:  2, RST:  1, BL:  3, SCK:  4, MOSI:  6, MISO: nil
     30  * ESP32-S2 various dev board  : CS: 34, DC: 38, RST: 33, BL: 21, SCK: 36, MOSI: 35, MISO: nil
     31  * ESP32-S3 various dev board  : CS: 40, DC: 41, RST: 42, BL: 48, SCK: 36, MOSI: 35, MISO: nil
     32  * ESP8266 various dev board   : CS: 15, DC:  4, RST:  2, BL:  5, SCK: 14, MOSI: 13, MISO: 12
     33  * Raspberry Pi Pico dev board : CS: 17, DC: 27, RST: 26, BL: 28, SCK: 18, MOSI: 19, MISO: 16
     34  * RTL8720 BW16 old patch core : CS: 18, DC: 17, RST:  2, BL: 23, SCK: 19, MOSI: 21, MISO: 20
     35  * RTL8720_BW16 Official core  : CS:  9, DC:  8, RST:  6, BL:  3, SCK: 10, MOSI: 12, MISO: 11
     36  * RTL8722 dev board           : CS: 18, DC: 17, RST: 22, BL: 23, SCK: 13, MOSI: 11, MISO: 12
     37  * RTL8722_mini dev board      : CS: 12, DC: 14, RST: 15, BL: 13, SCK: 11, MOSI:  9, MISO: 10
     38  * Seeeduino XIAO dev board    : CS:  3, DC:  2, RST:  1, BL:  0, SCK:  8, MOSI: 10, MISO:  9
     39  * Teensy 4.1 dev board        : CS: 39, DC: 41, RST: 40, BL: 22, SCK: 13, MOSI: 11, MISO: 12
     40  ******************************************************************************/
     41 #include <U8g2lib.h>
     42 #include <Arduino_GFX_Library.h>
     43 
     44 #define GFX_BL DF_GFX_BL // default backlight pin, you may replace DF_GFX_BL to actual backlight pin
     45 
     46 /* More dev device declaration: https://github.com/moononournation/Arduino_GFX/wiki/Dev-Device-Declaration */
     47 #if defined(DISPLAY_DEV_KIT)
     48 Arduino_GFX *gfx = create_default_Arduino_GFX();
     49 #else /* !defined(DISPLAY_DEV_KIT) */
     50 
     51 /* More data bus class: https://github.com/moononournation/Arduino_GFX/wiki/Data-Bus-Class */
     52 Arduino_DataBus *bus = create_default_Arduino_DataBus();
     53 
     54 /* More display class: https://github.com/moononournation/Arduino_GFX/wiki/Display-Class */
     55 Arduino_GFX *gfx = new Arduino_ILI9341(bus, DF_GFX_RST, 0 /* rotation */, false /* IPS */);
     56 
     57 #endif /* !defined(DISPLAY_DEV_KIT) */
     58 /*******************************************************************************
     59  * End of Arduino_GFX setting
     60  ******************************************************************************/
     61 
     62 #if defined(ESP32)
     63 #include <WiFi.h>
     64 #include <HTTPClient.h>
     65 WiFiClient client;
     66 HTTPClient http;
     67 #elif defined(ESP8266)
     68 #include <ESP8266WiFi.h>
     69 #include <ESP8266HTTPClient.h>
     70 WiFiClient client;
     71 HTTPClient http;
     72 #elif defined(ARDUINO_RASPBERRY_PI_PICO_W)
     73 #include <WiFi.h>
     74 #include <WiFiClient.h>
     75 #include <HttpClient.h>
     76 WiFiClient client;
     77 HttpClient http(client);
     78 #elif defined(RTL8722DM)
     79 #include <WiFi.h>
     80 #include <WiFiClient.h>
     81 #include <HttpClient.h>
     82 WiFiClient client;
     83 HttpClient http(client);
     84 #endif
     85 
     86 void setup(void)
     87 {
     88   Serial.begin(115200);
     89   // while (!Serial);
     90   // Serial.setDebugOutput(true);
     91   Serial.println("U8g2 RSS Reader");
     92 
     93 #ifdef GFX_EXTRA_PRE_INIT
     94   GFX_EXTRA_PRE_INIT();
     95 #endif
     96 
     97   Serial.println("Init display");
     98   gfx->begin();
     99   gfx->fillScreen(BLACK);
    100   gfx->setUTF8Print(true); // enable UTF8 support for the Arduino print() function
    101 
    102 #ifdef GFX_BL
    103   pinMode(GFX_BL, OUTPUT);
    104   digitalWrite(GFX_BL, HIGH);
    105 #endif
    106 
    107   Serial.println("Init WiFi");
    108   gfx->println("Init WiFi");
    109 #if defined(ESP32) || defined(ESP8266)
    110   WiFi.mode(WIFI_STA);
    111   WiFi.begin(SSID_NAME, SSID_PASSWORD);
    112 #elif defined(ARDUINO_RASPBERRY_PI_PICO_W)
    113   WiFi.mode(WIFI_STA);
    114   WiFi.begin(SSID_NAME, SSID_PASSWORD);
    115 #elif defined(RTL8722DM)
    116   WiFi.begin((char *)SSID_NAME, (char *)SSID_PASSWORD);
    117 #endif
    118   while (WiFi.status() != WL_CONNECTED)
    119   {
    120     delay(500);
    121     Serial.print(".");
    122     gfx->print(".");
    123   }
    124   Serial.println(" CONNECTED");
    125   gfx->println(" CONNECTED");
    126 
    127   /* U8g2 font list: https://github.com/olikraus/u8g2/wiki/fntlistall */
    128   /* U8g2 Unifont list: https://github.com/olikraus/u8g2/wiki/fntgrpunifont */
    129   gfx->setFont(u8g2_font_unifont_t_chinese4);
    130   gfx->setTextColor(WHITE);
    131 }
    132 
    133 void loop()
    134 {
    135   if (WiFi.status() == WL_CONNECTED)
    136   {
    137     Serial.printf("[HTTP] begin...\n");
    138 #if defined(ESP32) || defined(ESP8266)
    139     http.begin(client, RSS_HOST, RSS_PORT, RSS_PATH);
    140     int httpCode = http.GET();
    141 #elif defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(RTL8722DM)
    142     http.get(RSS_HOST, RSS_PORT, RSS_PATH);
    143     int httpCode = http.responseStatusCode();
    144     http.skipResponseHeaders();
    145 #endif
    146 
    147     Serial.printf("[HTTP] GET... code: %d\n", httpCode);
    148     gfx->printf("[HTTP] GET... code: %d\n", httpCode);
    149     // HTTP header has been send and Server response header has been handled
    150     if (httpCode <= 0)
    151     {
    152 #if defined(ESP32) || defined(ESP8266)
    153       Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
    154 #endif
    155     }
    156     else
    157     {
    158       if (httpCode != 200)
    159       {
    160         Serial.printf("[HTTP] Not OK!\n");
    161         gfx->printf("[HTTP] Not OK!\n");
    162         delay(5000);
    163       }
    164       else
    165       {
    166 // get lenght of document(is - 1 when Server sends no Content - Length header)
    167 #if defined(ESP32) || defined(ESP8266)
    168         int len = http.getSize();
    169 #elif defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(RTL8722DM)
    170         int len = http.contentLength();
    171 #endif
    172         Serial.printf("[HTTP] size: %d\n", len);
    173         gfx->printf("[HTTP] size: %d\n", len);
    174 
    175         if (len <= 0)
    176         {
    177           Serial.printf("[HTTP] Unknow content size: %d\n", len);
    178           gfx->printf("[HTTP] Unknow content size: %d\n", len);
    179         }
    180         else
    181         {
    182           // get XML string
    183           String xml = http.readString();
    184           // update hour
    185           int key_idx = xml.indexOf("<item>");
    186           key_idx = xml.indexOf("<title>", key_idx + 6);
    187           int val_start_idx = key_idx + 7;
    188           int val_end_idx = xml.indexOf('<', val_start_idx);
    189           int update_hour = xml.substring(val_start_idx + 35, val_start_idx + 37).toInt();
    190           String title = xml.substring(val_start_idx, val_end_idx);
    191           Serial.println(title);
    192 
    193           gfx->fillScreen(BLACK);
    194           gfx->setCursor(0, 16);
    195 
    196           // gfx->setTextSize(2);
    197           gfx->setTextColor(GREEN);
    198           gfx->println(title);
    199           gfx->println();
    200 
    201           // description
    202           key_idx = xml.indexOf("<description><![CDATA[", val_end_idx);
    203           val_start_idx = key_idx + 22;
    204           val_end_idx = xml.indexOf("]]></description>", val_start_idx);
    205           String description = xml.substring(val_start_idx, val_end_idx);
    206           description.trim();
    207           Serial.println(description);
    208 
    209           // gfx->setTextSize(1);
    210           gfx->setTextColor(WHITE);
    211           val_start_idx = 0;
    212           while (val_start_idx < description.length())
    213           {
    214             val_end_idx = description.indexOf("<br/>", val_start_idx);
    215             if (val_end_idx < 0)
    216             {
    217               val_end_idx = description.length();
    218             }
    219             String paragraph = description.substring(val_start_idx, val_end_idx);
    220             paragraph.trim();
    221             gfx->println(paragraph);
    222             val_start_idx = val_end_idx + 5;
    223           }
    224         }
    225       }
    226 #if defined(ESP32) || defined(ESP8266)
    227       http.end();
    228 #elif defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(RTL8722DM)
    229       http.stop();
    230 #endif
    231 
    232       delay(60 * 60 * 1000);
    233     }
    234   }
    235 
    236   delay(1000);
    237 }