#include <WiFi.h>
#include <HTTPClient.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <ArduinoJson.h> // For parsing the RSS feed
// Wokwi WiFi credentials
const char* ssid = "Wokwi-GUEST"; // Wokwi's predefined WiFi SSID
const char* password = ""; // Wokwi does not require a password
const int channel = 6; // WiFi channel for Wokwi
// RSS feed URL
// const char* rssFeedUrl = "https://feeds.bbci.co.uk/news/business/rss.xml";
const char* rssFeedUrls[] = {
"https://feeds.bbci.co.uk/news/business/rss.xml",
"https://www.news24.al/feed/",
"https://www.dr.dk/nyheder/service/feeds/senestenyt"
};
const int numFeeds = sizeof(rssFeedUrls) / sizeof(rssFeedUrls[0]);
int currentFeedIndex = 0;
// Screen setup
#define TFT_DC 42
#define TFT_CS 1
#define TFT_MOSI 41
#define TFT_SCLK 40
#define TFT_RST 2
#define TFT_MISO 16
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void setup() {
Serial.begin(9600);
// Initialize screen
SPI.begin(TFT_SCLK, TFT_MISO, TFT_MOSI, TFT_CS);
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2); // Smaller font size for titles
// Connect to WiFi
tft.setCursor(0, 0);
tft.println("Connecting to WiFi...");
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password, channel);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
tft.println("WiFi connected.");
}
void loop() {
// Check WiFi connection
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// http.begin(rssFeedUrl); // Start fetching RSS feed
http.begin(rssFeedUrls[currentFeedIndex]);
int httpCode = http.GET(); // Send GET request
if (httpCode == HTTP_CODE_OK) { // Check for successful response
String payload = http.getString();
Serial.println("RSS feed fetched successfully.");
displayRssTitles(payload);
} else {
Serial.printf("Error fetching RSS feed: %d\n", httpCode);
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.println("Error fetching RSS feed.");
}
currentFeedIndex = (currentFeedIndex + 1) % numFeeds;
http.end(); // End the HTTP request
} else {
Serial.println("WiFi disconnected.");
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.println("WiFi disconnected.");
}
delay(10000); // Refresh every 10 seconds
}
void displayRssTitles(const String& xml) {
tft.fillScreen(ILI9341_BLACK); // Clear the screen
tft.setCursor(0, 0);
int startIndex = 0;
int count = 0; // Limit the number of displayed titles
while ((startIndex = xml.indexOf("<title>", startIndex)) != -1) {
int endIndex = xml.indexOf("</title>", startIndex);
if (endIndex == -1) break;
String title = xml.substring(startIndex + 7, endIndex); // Extract title text
// Remove CDATA tags if present
if (title.startsWith("<![CDATA[")) {
title = title.substring(9); // Remove "<![CDATA["
int cdataEnd = title.indexOf("]]>");
if (cdataEnd != -1) {
title = title.substring(0, cdataEnd); // Remove "]]>"
}
}
if (count > 0) { // Skip the first title (it's usually the feed's title)
// Alternate between white and green colors
if (count % 2 == 0) {
tft.setTextColor(ILI9341_WHITE);
} else {
tft.setTextColor(ILI9341_GREEN);
}
tft.println(title); // Display title on the screen
Serial.println(title); // Print title to Serial Monitor
}
startIndex = endIndex + 8;
count++;
if (count >= 7) break; // Display up to 5 titles
}
if (count == 0) {
tft.setTextColor(ILI9341_WHITE);
tft.println("No titles found.");
Serial.println("No titles found.");
}
}