Advertisement
Guest User

Untitled

a guest
Apr 28th, 2023
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.78 KB | None | 0 0
  1. #include <ModbusMaster.h>
  2. #include <ESP8266WiFi.h>
  3. //---------------------------------------------------------------------
  4. const char* ssid     = "************"; // název wifi sítě
  5. const char* password = "************";  // heslo wifi sítě
  6. const char* host = "*****************"; //jmeno serveru
  7. //const int httpPort = 80; //port serveru
  8. char* apiKey ="VYROBA_1";
  9. //---------------------------------------------------------------------
  10.  
  11. unsigned long  data_zmena = 0;      // bude ukládat poslední případ aktualizace data
  12. unsigned long  data_interval = 1000;        //interval stažení
  13. unsigned long  vyroba_zmena = 0;      // bude ukládat poslední případ aktualizace data
  14. unsigned long  vyroba_interval = 1000;        //interval stažení
  15. unsigned long  export_zmena = 0;      // bude ukládat poslední export
  16. unsigned long  export_interval = 1*60*1000;        //interval exportu 1 minuta
  17. unsigned long  cas=0; //cas_mereni double  
  18. unsigned long  cas_export=0; //cas_exportu
  19. double pn=0;
  20. double pp=0;
  21. double pv=0;
  22. double bn=0;
  23. double bp=0;
  24. double bv=0;
  25. double bmax=0;
  26. double bmin=0;
  27. double btemp=0;
  28. double dtemp=0;
  29. double bsoc=0;
  30. double ednes=0;
  31. double emesic=0;
  32. double erok=0;
  33. double etotal=0;
  34. double cassecmin=0;
  35. double cashodden=0;
  36. double casmesrok=0;
  37. double bstatus=0;
  38. double cstatus=0;
  39. double dstatus=0;
  40.  
  41. int kolikaty=0;
  42. //---------------------------------------------------------------------
  43. //piny pro ovladaní modulu 485
  44. #define MAX485_DE      12
  45. #define MAX485_RE_NEG  14
  46.  
  47. //---------------------------------------------------------------------
  48. // ModBus Register Locations
  49. #define LIVE_DATA       0x3100     // start of live-data
  50. #define LIVE_DATA_CNT   16         // 16 regs
  51.  
  52. // just for reference, not used in code
  53. #define PANEL_VOLTS     0x00      
  54. #define PANEL_AMPS      0x01
  55. #define PANEL_POWER_L   0x02
  56. #define PANEL_POWER_H   0x03
  57.  
  58. #define BATT_VOLTS      0x04
  59. #define BATT_AMPS       0x05
  60. #define BATT_POWER_L    0x06
  61. #define BATT_POWER_H    0x07
  62.  
  63. // instantiate ModbusMaster object
  64. ModbusMaster node;
  65. void preTransmission()
  66. {
  67.   digitalWrite(MAX485_RE_NEG, 1);
  68.   digitalWrite(MAX485_DE, 1);
  69. }
  70. void postTransmission()
  71. {
  72.   digitalWrite(MAX485_RE_NEG, 0);
  73.   digitalWrite(MAX485_DE, 0);
  74. }
  75.  
  76. //---------------------------------------------------------------------
  77.  
  78. //---------------------------------------------------------------------
  79. void setup()
  80. {
  81.   WiFi.begin(ssid, password);
  82.   int timeout = millis()+10000;
  83.   while ((WiFi.status() != WL_CONNECTED) && (timeout > millis())) {
  84.     delay(1000);
  85.   }
  86.  
  87.  
  88.  
  89.   pinMode(MAX485_RE_NEG, OUTPUT);
  90.   pinMode(MAX485_DE, OUTPUT);
  91.   // Init in receive mode
  92.   digitalWrite(MAX485_RE_NEG, 0);
  93.   digitalWrite(MAX485_DE, 0);
  94.   // Modbus communication runs at 115200 baud
  95.   Serial.begin(115200);
  96.   // Modbus slave ID 1
  97.   node.begin(1, Serial);
  98.   // Callbacks allow us to configure the RS485 transceiver correctly
  99.   node.preTransmission(preTransmission);
  100.   node.postTransmission(postTransmission);
  101.  
  102. }
  103. bool state = true;
  104.  
  105. void loop()
  106. {
  107.     unsigned long currentMillis = millis();
  108.  
  109.     if(currentMillis - data_zmena > data_interval) {
  110.   uint8_t result;
  111.   uint16_t data[6];
  112.   kolikaty=kolikaty+1;  
  113.   // Read 16 registers starting at 0x3100)
  114. if (kolikaty%6==0) {
  115.  
  116.   result = node.readInputRegisters(0x3100, 16);
  117.   if (result == node.ku8MBSuccess)
  118.   {
  119.     Serial.println(" - - - - - - - - - - - - - - ");
  120.     Serial.println("MAIN DETAILS:");
  121.     Serial.print("Panely Napětí: ");
  122.     pn=node.getResponseBuffer(0x00)/ 100.0f;
  123.     Serial.println(pn);
  124.     pp=node.getResponseBuffer(0x01)/ 100.0f;
  125.     Serial.print("Panely Proud: ");
  126.     Serial.println(pp);
  127.     pv=(node.getResponseBuffer(0x02) | node.getResponseBuffer(0x03) << 16)/ 100.0f;
  128.     Serial.print("Panely Výkon: ");
  129.     Serial.println(pv);
  130.     Serial.print("Baterie Napětí: ");
  131.     bn=node.getResponseBuffer(0x04)/ 100.0f;
  132.     Serial.println(bn);
  133.     bp=node.getResponseBuffer(0x05)/ 100.0f;
  134.     Serial.print("Baterie Proud: ");
  135.     Serial.println(bp);
  136.     bv=(node.getResponseBuffer(0x06) | node.getResponseBuffer(0x07) << 16)/ 100.0f;
  137.     Serial.print("Baterie Výkon: ");
  138.     Serial.println(bv);
  139.   }
  140.   else {
  141.     Serial.print("err: ");
  142.     Serial.print(result);
  143.     Serial.print("--");
  144.     Serial.println(node.ku8MBSuccess);
  145.   }
  146.   Serial.print("A:");
  147.   Serial.println(kolikaty);
  148.  
  149. }
  150. else if (kolikaty%6==1) {
  151.   result = node.readInputRegisters(0x3300, 4);
  152.   if (result == node.ku8MBSuccess)
  153.   {
  154.     Serial.println(" - - - - - - - - - - - - - - ");
  155.     Serial.println("MAIN DETAILS:");
  156.     Serial.print("Baterie max: ");
  157.     bmax=node.getResponseBuffer(0x02)/ 100.0f;
  158.     Serial.println(bmax);
  159.     Serial.print("Baterie min: ");
  160.     bmin=node.getResponseBuffer(0x03)/ 100.0f;
  161.     Serial.println(bmin);
  162.   }
  163.   else {
  164.     Serial.print("err: ");
  165.     Serial.print(result);
  166.     Serial.print("--");
  167.     Serial.println(node.ku8MBSuccess);
  168.   }
  169.   Serial.print("B:");
  170.   Serial.println(kolikaty);
  171. }
  172. else if (kolikaty%6==2) {
  173.   result = node.readInputRegisters(0x330c, 8);
  174.   if (result == node.ku8MBSuccess)
  175.   {
  176.     Serial.println(" - - - - - - - - - - - - - - ");  
  177.     Serial.println("MAIN DETAILS:");
  178.     Serial.print("BEnergie dnes: ");
  179.     ednes=(node.getResponseBuffer(0x00) | node.getResponseBuffer(0x01) << 16)/ 100.0f;
  180.     Serial.println(ednes);
  181.     Serial.print("BEnergie mesic: ");
  182.     emesic=(node.getResponseBuffer(0x02) | node.getResponseBuffer(0x03) << 16)/ 100.0f;
  183.     Serial.println(emesic);
  184.     Serial.print("BEnergie rok: ");
  185.     erok=(node.getResponseBuffer(0x04) | node.getResponseBuffer(0x05) << 16)/ 100.0f;
  186.     Serial.println(erok);
  187.     Serial.print("BEnergie celkem: ");
  188.     etotal=(node.getResponseBuffer(0x06) | node.getResponseBuffer(0x07) << 16)/ 100.0f;
  189.     Serial.println(etotal);
  190.   }
  191.   else {
  192.     Serial.print("err: ");
  193.     Serial.print(result);
  194.     Serial.print("--");
  195.     Serial.println(node.ku8MBSuccess);
  196.   }
  197.   Serial.print("C:");
  198.   Serial.println(kolikaty);
  199. }
  200. else if (kolikaty%6==3) {
  201. //  result = node.readInputRegisters(0x9013, 3);
  202.   result = node.readInputRegisters(0x3200, 3);
  203.   if (result == node.ku8MBSuccess)
  204.   {
  205.     Serial.println(" - - - - - - - - - - - - - - ");
  206.     Serial.println("MAIN DETAILS:");
  207.     Serial.print("Batery status: ");
  208.     bstatus=node.getResponseBuffer(0x00);
  209.     Serial.println(bstatus);
  210.     Serial.print("Charging status: ");
  211.     cstatus=node.getResponseBuffer(0x01);
  212.     Serial.println(cstatus);
  213.     Serial.print("Device status: ");
  214.     dstatus=node.getResponseBuffer(0x02);
  215.     Serial.println(dstatus);
  216.   }
  217.   else {
  218.     Serial.print("err: ");
  219.     Serial.print(result);
  220.     Serial.print("--");
  221.     Serial.println(node.ku8MBSuccess);
  222.   }
  223.   Serial.print("D:");
  224.   Serial.println(kolikaty);
  225. }
  226. else if (kolikaty%6==4) {
  227.   result = node.readInputRegisters(0x3110, 10);
  228.   if (result == node.ku8MBSuccess)
  229.   {
  230.     Serial.println(" - - - - - - - - - - - - - - ");
  231.     Serial.println("MAIN DETAILS:");
  232.     Serial.print("Baterie temp: ");
  233.     btemp=node.getResponseBuffer(0x0)/ 100.0f;
  234.     Serial.println(dtemp);
  235.     Serial.print("Device temp: ");
  236.     dtemp=node.getResponseBuffer(0x1)/ 100.0f;
  237.     Serial.println(btemp);
  238.     Serial.print("Baterie soc: ");
  239.     bsoc=node.getResponseBuffer(0xa)/ 100.0f;
  240.     Serial.println(bsoc);
  241.   }
  242.   else {
  243.     Serial.print("err: ");
  244.     Serial.print(result);
  245.     Serial.print("--");
  246.     Serial.println(node.ku8MBSuccess);
  247.   }
  248.   Serial.print("E:");
  249.   Serial.println(kolikaty);
  250. }
  251. else if (kolikaty%6==5) {
  252.   result = node.readInputRegisters(0x311A, 1);
  253.   if (result == node.ku8MBSuccess)
  254.   {
  255.     Serial.println(" - - - - - - - - - - - - - - ");
  256.     Serial.println("MAIN DETAILS:");
  257.     Serial.print("Baterie soc: ");
  258.     bsoc=node.getResponseBuffer(0xa)/ 100.0f;
  259.     Serial.println(bsoc);
  260.   }
  261.   else {
  262.     Serial.print("err: ");
  263.     Serial.print(result);
  264.     Serial.print("--");
  265.     Serial.println(node.ku8MBSuccess);
  266.   }
  267.   Serial.print("E:");
  268.   Serial.println(kolikaty);
  269. }
  270.  
  271.       data_zmena = currentMillis;
  272.     }
  273.  
  274.     if(currentMillis - export_zmena > export_interval) {
  275.       kolikaty=0;
  276.       cas_export=currentMillis-export_zmena;
  277.       data_zmena = currentMillis;
  278.       export_zmena = currentMillis;
  279.       WiFiClient client;
  280.       if(client.connect(host, httpPort)){
  281.         delay(100);
  282.         String url = "/arduino/fveupdate.php?api_key=";
  283.         url += apiKey;
  284.         url += "&panely_napeti=";
  285.         url += (pn);
  286.         url += "&panely_proud=";
  287.         url += (pp);
  288.         url += "&panely_vykon=";
  289.         url += (pv);
  290.         url += "&baterie_napeti=";
  291.         url += (bn);
  292.         url += "&baterie_proud=";
  293.         url += (bp);
  294.         url += "&baterie_vykon=";
  295.         url += (bv);
  296.         url += "&baterie_max=";
  297.         url += (bmax);
  298.         url += "&baterie_min=";
  299.         url += (bmin);
  300.         url += "&baterie_temp=";
  301.         url += (btemp);
  302.         url += "&baterie_soc=";
  303.         url += (bsoc);
  304.         url += "&device_temp=";
  305.         url += (dtemp);
  306.         url += "&energie_dnes=";
  307.         url += (ednes);
  308.         url += "&energie_mes=";
  309.         url += (emesic);
  310.         url += "&energie_rok=";
  311.         url += (erok);
  312.         url += "&energie_total=";
  313.         url += (etotal);
  314.         url += "&c_status=";
  315.         url += (cstatus);
  316.         url += "&b_status=";
  317.         url += (bstatus);
  318.         url += "&d_status=";
  319.         url += (dstatus);
  320.         url += "&ssid=";
  321.         url += (ssid);
  322.         url += "&cas_export=";
  323.         url += (cas);
  324.         client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  325.                "Host: " + host + "\r\n" +
  326.                "Connection: close\r\n\r\n");
  327.         delay(10);
  328.         while(client.available()){
  329.           String line = client.readStringUntil('\r');
  330.         }  
  331.        }    
  332.  
  333.  
  334.  
  335.  
  336. }
  337. }
  338.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement