Advertisement
PrintService3D

LocalServerRead-WriteToNextion

Feb 10th, 2024 (edited)
1,128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.47 KB | Source Code | 0 0
  1. #include <WiFi.h>
  2. #include <HTTPClient.h>
  3. #include <ArduinoJson.h>
  4. #include "EasyNextionLibrary.h"  // Einbinden der Easy Nextion Library
  5.  
  6. const char* ssid = "xxxxxxx";
  7. const char* password = "xxxxxxxx";
  8. String serverUrl = "xxxxxxxx";
  9.  
  10.  
  11. EasyNex myNex(Serial1);
  12.  
  13. void extractCpuPackage(String payload) {
  14.   StaticJsonDocument<2048> doc;
  15.   DeserializationError error = deserializeJson(doc, payload);
  16.  
  17.  
  18.   JsonObject CPUWatt = doc["Children"][0]["Children"][0]["Children"][3]["Children"][0];  //cpuWatt
  19.   JsonObject GPUWatt = doc["Children"][0]["Children"][2]["Children"][5]["Children"][0];  //gpuWatt
  20.  
  21.   JsonObject CPUPackageC = doc["Children"][0]["Children"][0]["Children"][1]["Children"][0];  //cpuTemp
  22.   JsonObject gTempC = doc["Children"][0]["Children"][2]["Children"][1]["Children"][0];       //gpuTemp
  23.  
  24.   JsonObject CPUTotal = doc["Children"][0]["Children"][0]["Children"][2]["Children"][0];     //cpuLast
  25.   JsonObject gpuCorePerc = doc["Children"][0]["Children"][2]["Children"][2]["Children"][0];  //gpuLast
  26.  
  27.  
  28.   String cWatt = String(CPUWatt["Value"]);  //String cpuWatt
  29.   String gWatt = String(GPUWatt["Value"]);  //String gpuWatt
  30.  
  31.   String cTemp = String(CPUPackageC["Value"]);  //String cpuTemp
  32.   String gTemp = String(gTempC["Value"]);       //String gpuTemp
  33.  
  34.   String cpuPerc = String(CPUTotal["Value"]);     //String cpuLast
  35.   String gpuPerc = String(gpuCorePerc["Value"]);  //String gpuLast
  36.  
  37.  
  38.   cWatt = cWatt.substring(0, cWatt.length() - 2);  //entferne letzte Ziffer
  39.   gWatt = gWatt.substring(0, gWatt.length() - 2);  //entferne letzte Ziffer
  40.  
  41.   cTemp = cTemp.substring(0, cTemp.length() - 3);  //entferne letzte Ziffer
  42.   gTemp = gTemp.substring(0, gTemp.length() - 3);  //entferne letzte Ziffer
  43.  
  44.   cpuPerc = cpuPerc.substring(0, cpuPerc.length() - 2);  //entferne letzte Ziffer
  45.   gpuPerc = gpuPerc.substring(0, gpuPerc.length() - 2);  //entferne letzte Ziffer
  46.  
  47.  
  48.   // Umwandlung der String-Werte in const char*, um sie an das Nextion-Display zu senden
  49.   const char* cWattChar = cWatt.c_str();  //String in const char* -cpuWatt
  50.   const char* gWattChar = gWatt.c_str();  //String in const char* -gpuWatt
  51.  
  52.   const char* cTempChar = cTemp.c_str();  //String in const char* -cpuTemp
  53.   const char* gTempChar = gTemp.c_str();  //String in const char* -gpuTemp
  54.  
  55.   const char* cpuPercChar = cpuPerc.c_str();  //String in const char* -cpuLast
  56.   const char* gpuPercChar = gpuPerc.c_str();  //String in const char* -gpuLast
  57.  
  58.   // Aktualisierung der Werte auf dem Nextion-Display
  59.   myNex.writeStr("cpuWatt.txt", cWattChar); // schreibe zu Nextion
  60.   myNex.writeStr("gpuWatt.txt", gWattChar); // schreibe zu Nextion
  61.  
  62.   myNex.writeStr("cpuU.txt", cpuPercChar);  // schreibe zu Nextion
  63.   myNex.writeStr("t3.txt", gpuPercChar);    // schreibe zu Nextion
  64.  
  65.  
  66.  
  67.   uint32_t cWattGauge = map(cWatt.toInt(), 0, 80, 0, 180);   //skalieren von cpuWatt
  68.   uint32_t gWattGauge = map(gWatt.toInt(), 0, 160, 0, 180);  //skalieren von gpuWatt
  69.  
  70.   uint32_t cWattwave = map(cWatt.toInt(), 0, 80, 0, 80);  //skalieren von cpuWatt Waveform
  71.   uint32_t gWattwave = map(gWatt.toInt(), 0, 80, 0, 80);  //skalieren von gpuWatt
  72.  
  73.   uint32_t cpuPercGauge = map(cpuPerc.toInt(), 0, 100, 0, 180);  //skalieren von cpuLast
  74.   uint32_t gpuPercGauge = map(gpuPerc.toInt(), 0, 100, 0, 180);  //skalieren von gpuLast
  75.  
  76.   uint32_t cpuPerwave = map(cpuPerc.toInt(), 0, 100, 0, 100);  //skalieren von cpuLast
  77.   uint32_t gpuPerwave = map(gpuPerc.toInt(), 0, 100, 0, 100);  //skalieren von gpuLast
  78.  
  79.   uint32_t cTempCGauge = map(cTemp.toInt(), 0, 100, 0, 100);  //skalieren von cpuTemp
  80.   uint32_t gTempCGauge = map(gTemp.toInt(), 0, 100, 0, 100);  //skalieren von gpuTemp
  81.  
  82.   uint32_t cTempCwave = map(cTemp.toInt(), 0, 100, 0, 100);  //skalieren von cpuTemp
  83.   uint32_t gTempCwave = map(gTemp.toInt(), 0, 100, 0, 100);  //skalieren von gpuTemp
  84.  
  85.  
  86.   myNex.writeNum("cpuW.val", cWattGauge); // schreibe zu Nextion
  87.   myNex.writeNum("gpuW.val", gWattGauge); // schreibe zu Nextion
  88.  
  89.   myNex.writeNum("cpuUsage.val", cpuPercGauge); // schreibe zu Nextion
  90.   myNex.writeNum("gpuUsage.val", gpuPercGauge); // schreibe zu Nextion
  91.  
  92.   myNex.writeNum("cpuT.val", cTempCGauge); // schreibe zu Nextion
  93.   myNex.writeNum("gpuT.val", gTempCGauge); // schreibe zu Nextion
  94.  
  95.   //Cpu Watt Max 95  W
  96.   //Gpu Watt Max 180 W
  97.   /*
  98.   // Serielle Ausgabe
  99.   Serial.print("Cpu Watt: ");
  100.   Serial.print(cWattChar);
  101.   Serial.print("  ,  ");
  102.  
  103.   Serial.print("Gpu Watt: ");
  104.   Serial.print(gWattChar);
  105.   Serial.print("  ,  ");
  106.  
  107.   Serial.print("Cpu Temp: ");
  108.   Serial.print(cTempChar);
  109.   Serial.print("  ,  ");
  110.  
  111.   Serial.print("Gpu Temp: ");
  112.   Serial.print(gTempChar);
  113.   Serial.print("  ,  ");
  114.  
  115.   Serial.print("Cpu Last: ");
  116.   Serial.print(cpuPercChar);
  117.   Serial.print("  ,  ");
  118.  
  119.   Serial.print("Gpu Last: ");
  120.   Serial.print(gpuPercChar);
  121.   Serial.println("");
  122. */
  123.  
  124.  
  125.  
  126.  
  127.   // Waveform CPU Watt
  128.   //myNex.writeNum("cpuWattt.val=", cWattwave);
  129.  
  130.   /*
  131.   Serial2.print("cpuWattt.val=");
  132.   Serial2.print(cWattwave);
  133.   Serial2.write(0xff);
  134.   Serial2.write(0xff);
  135.   Serial2.write(0xff);
  136.  
  137.   //Waveform GPU Watt
  138.   Serial2.print("gpuWattt.val=");
  139.   Serial2.print(gWattwave);
  140.   Serial2.write(0xff);
  141.   Serial2.write(0xff);
  142.   Serial2.write(0xff);
  143.  
  144.   //Waveform CPU %
  145.   Serial2.print("cpuPercc.val=");
  146.   Serial2.print(cpuPerwave);
  147.   Serial2.write(0xff);
  148.   Serial2.write(0xff);
  149.   Serial2.write(0xff);
  150.  
  151.   //Waveform GPU %
  152.   Serial2.print("gpuPercc.val=");
  153.   Serial2.print(gpuPerwave);
  154.   Serial2.write(0xff);
  155.   Serial2.write(0xff);
  156.   Serial2.write(0xff);
  157. */
  158.   // Waveform alle
  159. }
  160.  
  161. void setup() {
  162.   Serial.begin(9600);
  163.   myNex.begin(9600);
  164.  
  165.   WiFi.begin(ssid, password);
  166.   while (WiFi.status() != WL_CONNECTED) {
  167.     delay(1000);
  168.     //Serial.print(".");
  169.   }
  170. }
  171.  
  172. void loop() {
  173.   if (Serial1.available()) {
  174.     // Lesen Sie die verfügbaren Daten von Serial1
  175.     int receivedChar = Serial1.read();
  176.     Serial.print(receivedChar);
  177.   }
  178.  
  179.   HTTPClient http;
  180.   http.begin(serverUrl);
  181.   int httpResponseCode = http.GET();
  182.  
  183.   if (httpResponseCode > 0) {
  184.     String payload = http.getString();
  185.     extractCpuPackage(payload);
  186.   } else {
  187.     //Serial.println("HTTP Request failed");
  188.   }
  189.  
  190.   http.end();
  191.  
  192.   delay(50);
  193. }
  194.  
  195.  
  196. /*
  197. // Nextion RX SeitenID
  198. HomeScreen Cpu: .....1010131 255255255....
  199. Seite 1 X : .....101141 255255255.......
  200. Seite 2 X : .....101151 255255255.......
  201. Seite 3 X : .....101161 255255255.......
  202. Seite 4 X : .....101171 255255255.......
  203. Seite 5 X : .....101181 255255255.......
  204. Seite 6 X : .....101191 255255255.......
  205. */
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement