Advertisement
LeventeDaradici

ESP32-2432s028 How do we extract data from an XML file located on a remote server.

Apr 9th, 2023 (edited)
1,917
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.26 KB | Source Code | 0 1
  1. //   Code by:
  2. //   Levente Daradici
  3. //   Youtube video with this code on microcontroller
  4. //   https://youtu.be/mdoX3ZSBYtc
  5. //   09-04-2023
  6. //   A short description and discussions in Romanian: http://satelit-info.com/phpBB3/viewtopic.php?f=172&t=3322&start=10#p25865
  7. //
  8. #include <TFT_eSPI.h>
  9. #include <SPI.h>
  10. #include <WiFi.h>
  11. #include <WiFiClient.h>
  12. #include "NTPClient.h"
  13.  
  14. WiFiClient client;
  15. const char* ssid = "Your WIFI network SSID";
  16. const char* password = "Your WIFI password";
  17.  
  18. const char* host = "192.168.XXX.XXX";   //YOUR SET TOP BOX IP
  19. const char* path = "/web/getcurrent";
  20.  
  21. const long utcOffsetInSeconds = 10800;
  22.  
  23. String NumeCanalVechi, NumeCanalNou, EPGacumVECHI, EPGacumNOU, EPGacumVECHI2, EPGacumNOU2, AfisareEPG, AfisareEPG2, AfisareDESC, AfisareDESC2,  AfisareEPGtotal,AfisareDescriereEPG,AfisareDescriereEPG2  = " ";
  24. int Conexiune, Pozitie, EPGacum, DescriereACUM, DescriereACUMepg = 0;
  25.  
  26. String NumeCanalCurent ="Nume Canal Curent";
  27. String NumeEmisiuneCurenta ="Nume Emisiune Curenta";
  28. String EpgEmisiuneCurenta ="EPG Emisiune Curenta";
  29. String OraExacta ="22:26:35";
  30. String NumeProvider ="Digi Tv / RCS & RDS";
  31. String RezolutieCanal ="1920x1080";
  32.  
  33. TFT_eSPI tft = TFT_eSPI();
  34. WiFiUDP ntpUDP;
  35. NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
  36.  
  37.      void setup(void)
  38.           {
  39.             Serial.begin(115200);
  40.             delay(500);
  41.             tft.init();
  42.             tft.setRotation(3);
  43.             tft.fillScreen(TFT_BLACK);
  44.             EPGacum = 1;
  45.             DescriereACUM = 1;
  46.             DescriereACUMepg = 1;
  47.             WiFi.begin(ssid, password);
  48.             Serial.print("Connecting to WiFi");
  49.             while (WiFi.status() != WL_CONNECTED)
  50.                 {
  51.                    Serial.print(".");
  52.                    delay(500);
  53.                 }
  54.             Serial.println();      
  55.             Serial.println("Connected to WiFi");
  56.             delay(2000);  
  57.             Conexiune == 1;
  58.             timeClient.begin();
  59.           }
  60.  
  61.      void loop()
  62.           {  
  63.             timeClient.update();
  64.             tft.setTextSize(2);
  65.             tft.setTextColor(TFT_CYAN, TFT_BLACK);
  66.             OraExacta = timeClient.getFormattedTime();
  67.             Serial.println(OraExacta);
  68.             tft.drawString(OraExacta, 205, 2, 2);
  69.              
  70.             WiFiClient client;
  71.             if (!client.connect(host, 80))
  72.                 {
  73.                    Serial.println("NOT CONNECTED TO");
  74.                    Serial.println("ENIGMA2 RECEIVER!");
  75.                    Conexiune = 0;
  76.                    return;
  77.                 }  else
  78.                    if (Conexiune == 0)
  79.                       {
  80.                          Serial.println("CONNECTED TO");
  81.                          Serial.println("ENIGMA2 RECEIVER");
  82.                          delay(2000);
  83.                          Conexiune = 1;
  84.                          NumeCanalVechi = " ";
  85.                          EPGacumVECHI = " ";
  86.                          EPGacum = 1;
  87.                       }
  88.             Serial.println("");
  89.             String url = String(path);
  90.  
  91.             client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  92.                    "Host: " + host + "\r\n" +
  93.                    "Connection: close\r\n\r\n");
  94.  
  95.             while(!client.available())
  96.                 {
  97.                   delay(500);
  98.                 }
  99.  
  100.             String line;
  101.             while (client.available())
  102.               {
  103.                  line = client.readStringUntil('\n');
  104.                  if (line.indexOf("e2servicename") >= 0)
  105.                     {
  106.                        int startIndex = line.indexOf(">") + 1;
  107.                        int endIndex = line.indexOf("<", startIndex);
  108.                        String e2servicename = line.substring(startIndex, endIndex);
  109.                        NumeCanalNou = e2servicename ;
  110.                        if (NumeCanalVechi != NumeCanalNou)
  111.                           {
  112.                                tft.setTextSize(3);
  113.                                tft.setTextColor(TFT_RED, TFT_BLACK);
  114.                                tft.drawString(NumeCanalNou + "                       ", 0, 32, 2);
  115.                                tft.setTextSize(2);
  116.                                tft.setTextColor(TFT_BLUE, TFT_BLACK);
  117.                                tft.drawString("ACUM", 150, 80, 2);
  118.                                tft.drawString("EPG", 3, 144, 2);
  119.                                NumeCanalVechi = NumeCanalNou;
  120.                           }
  121.                           Serial.println("Canal: " + e2servicename);
  122.                     } else if (line.indexOf("e2providername") >= 0)
  123.                           {
  124.                                int startIndex = line.indexOf(">") + 1;
  125.                                int endIndex = line.indexOf("<", startIndex);
  126.                                String e2providername = line.substring(startIndex, endIndex);
  127.                                NumeProvider = e2providername;
  128.                                tft.setTextSize(1);
  129.                                tft.setTextColor(TFT_YELLOW, TFT_BLACK);                                                      
  130.                                tft.drawString(NumeProvider + + "                 ", 230, 80, 2);
  131.                                Serial.println("Provider: " + e2providername);
  132.                           }
  133.                       else if (line.indexOf("e2servicevideosize") >= 0)
  134.                          {
  135.                               int startIndex = line.indexOf(">") + 1;
  136.                               int endIndex = line.indexOf("<", startIndex);
  137.                               String e2servicevideosize = line.substring(startIndex, endIndex);
  138.                               tft.setTextSize(1);
  139.                               tft.setTextColor(TFT_YELLOW, TFT_BLACK);                              
  140.                               tft.drawString(e2servicevideosize + "          ", 230, 96, 2);
  141.                               Serial.println("Rezolutie: " + e2servicevideosize);
  142.                          }
  143.                       else if (line.indexOf("e2eventname") >= 0)
  144.                          {
  145.                               int startIndex = line.indexOf(">") + 1;
  146.                               int endIndex = line.indexOf("<", startIndex);
  147.                               String e2eventname = line.substring(startIndex, endIndex);
  148.                               if (EPGacum == 1)
  149.                                  {      
  150.                                         AfisareEPG = e2eventname;
  151.                                         tft.setTextSize(2);
  152.                                         tft.setTextColor(TFT_GREEN, TFT_BLACK);
  153.                                         tft.drawString(AfisareEPG + "                               ", 0, 112, 2);
  154.                                  }
  155.                               if (EPGacum == 2) AfisareEPG2 = e2eventname;
  156.                               EPGacum = EPGacum + 1;
  157.                               if (EPGacum > 2) EPGacum = 1;      
  158.                          }
  159.                       else if (line.indexOf("e2eventdescription") >= 0)
  160.                          {
  161.                               int startIndex = line.indexOf(">") + 1;
  162.                               int endIndex = line.indexOf("<", startIndex);
  163.                               String e2eventdescription = line.substring(startIndex, endIndex);
  164.                               if (DescriereACUM > 4) DescriereACUM = 1;
  165.                               if (DescriereACUM == 1) AfisareDESC = e2eventdescription;
  166.                               if (DescriereACUM == 2) AfisareDescriereEPG = e2eventdescription;
  167.                               if (DescriereACUM == 3) AfisareDESC2 = e2eventdescription;
  168.                               if (DescriereACUM == 4) AfisareDescriereEPG2 = e2eventdescription;    
  169.                               tft.setTextSize(1);
  170.                               tft.setTextColor(TFT_YELLOW, TFT_BLACK);
  171.                               tft.drawString(AfisareDESC.substring(0, 40) + "                                                                     ", 55, 144, 2);
  172.                               tft.drawString(AfisareDESC.substring(41,80) + "                                                                     ", 55, 160, 2);
  173.                               tft.setTextColor(TFT_WHITE, TFT_BLACK);
  174.                               tft.drawString(AfisareDescriereEPG.substring(0, 48) + "                                                                     ", 0, 176, 2);
  175.                               tft.drawString(AfisareDescriereEPG.substring(48, 98) + "                                                                     ", 0, 192, 2);  
  176.                               tft.drawString(AfisareDescriereEPG.substring(98, 148) + "                                                                     ", 0, 208, 2);          
  177.                               tft.drawString(AfisareDescriereEPG.substring(148, 198) + "                                                                     ", 0, 224, 2);                          
  178.                               DescriereACUM = DescriereACUM + 1;
  179.                          }
  180.              }
  181.              Serial.println(AfisareEPGtotal);
  182.              client.stop();
  183.              Serial.println("");
  184.              delay(100);
  185.           }
  186.  
  187.  
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement