Advertisement
Guest User

Charge Controller

a guest
Dec 19th, 2016
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.40 KB | None | 0 0
  1. #include <ModbusMaster.h>
  2. #include <ESP8266WiFi.h>
  3.  
  4. const char* ssid     = "xxxxxxxxxx"; //your wifi SSID/name
  5. const char* password = "xxxxxxxx"; //your wifi password
  6. const char* api_key = "xxxxxxxx"; //api from thingspeak
  7. const char* host = "api.thingspeak.com";
  8. const int debug = 1; //change to 0 when you are finished debugging
  9.  
  10. // instantiate ModbusMaster object
  11. ModbusMaster node;
  12.  
  13.  
  14. // put something here if you want it to occur before or after transmission to the serial interface...i.e. delay etc. I have these blank and it works fine
  15. void preTransmission()
  16. {
  17.  
  18. }
  19.  
  20. void postTransmission()
  21. {
  22.  
  23. }
  24.  
  25. void setup()
  26. {
  27.  
  28.  
  29.   // Modbus communication runs at 115200 baud --> you can make this slower if you want, but works fine either way. I would suggest unplugging the RS485 from the ESP when you are uploading the code. Plug it back in as soon as the code successfully uploads
  30.   Serial.begin(115200);
  31.  
  32.   // Modbus slave ID 1  --> this is the default for the 2210, so shouldn't need to change
  33.   node.begin(1, Serial);
  34.   // Callbacks allow us to configure the RS485 transceiver correctly
  35.   node.preTransmission(preTransmission);
  36.   node.postTransmission(postTransmission);
  37.  
  38.  
  39.  
  40.   WiFi.mode(WIFI_STA);
  41.   WiFi.disconnect();
  42.   delay(100);
  43.   WiFi.begin(ssid, password);
  44.   if (debug == 1){
  45.     Serial.print("Connecting to WiFi.");
  46.   }
  47.   while (WiFi.status() != WL_CONNECTED) {
  48.     delay(500);
  49.     if (debug == 1){
  50.       Serial.print(".");
  51.     }
  52.   }
  53.  
  54.   if (debug == 1){
  55.     Serial.println("");
  56.     Serial.println("WiFi connected");  
  57.     Serial.println("IP address: ");
  58.     Serial.println(WiFi.localIP());
  59.     delay(5000);
  60.   }
  61.  
  62. }
  63.  
  64. bool rs485DataReceived = true;
  65. int numLoops = 10000;
  66. void loop()
  67. {
  68.  
  69.   uint8_t result,time1, time2, time3, date1, date2, date3, dateDay, dateMonth, dateYear, timeHour, timeMinute, timeSecond;
  70.   // uint16_t data[6];
  71.   char buf[10];
  72.   String dtString;
  73.   float bvoltage, ctemp, btemp, bremaining, lpower, lcurrent, pvvoltage, pvcurrent, pvpower;
  74.  
  75.   if (debug == 1){
  76.     Serial.print("Beginning Loop ");
  77.     Serial.println(numLoops);
  78.   }
  79.  
  80.   //Get Date and Time, and update Controller Data and Time
  81.  
  82.   if (numLoops == 10000){ // Get date and time every 10000 loops
  83.  
  84.       dtString = getTime();
  85.       if (debug == 1){
  86.         Serial.println(dtString);
  87.       }
  88.       dateDay = atoi(dtString.substring(5,7).c_str());
  89.       dateYear = atoi(dtString.substring(14,16).c_str());
  90.       timeHour = atoi(dtString.substring(17,19).c_str());
  91.       timeMinute = atoi(dtString.substring(20,22).c_str());
  92.       timeSecond = atoi(dtString.substring(23,25).c_str());
  93.       if (dtString.substring(8,11)=="Jan"){
  94.           dateMonth=1;
  95.       }
  96.       if (dtString.substring(8,11)=="Feb"){
  97.           dateMonth=2;
  98.       }
  99.       if (dtString.substring(8,11)=="Mar"){
  100.           dateMonth=3;
  101.       }
  102.       if (dtString.substring(8,11)=="Apr"){
  103.           dateMonth=4;
  104.       }
  105.       if (dtString.substring(8,11)=="May"){
  106.           dateMonth=5;
  107.       }
  108.       if (dtString.substring(8,11)=="Jun"){
  109.           dateMonth=6;
  110.       }
  111.       if (dtString.substring(8,11)=="Jul"){
  112.           dateMonth=7;
  113.       }
  114.       if (dtString.substring(8,11)=="Aug"){
  115.           dateMonth=8;
  116.       }
  117.       if (dtString.substring(8,11)=="Sep"){
  118.           dateMonth=9;
  119.       }
  120.       if (dtString.substring(8,11)=="Oct"){
  121.           dateMonth=10;
  122.       }
  123.       if (dtString.substring(8,11)=="Nov"){
  124.           dateMonth=11;
  125.       }
  126.       if (dtString.substring(8,11)=="Dec"){
  127.           dateMonth=12;
  128.       }
  129.  
  130.       if (debug == 1){
  131.         Serial.print("Date: ");
  132.         Serial.print(dateDay);
  133.         Serial.print("/");
  134.         Serial.print(dateMonth);
  135.         Serial.print("/");
  136.         Serial.print(dateYear);
  137.         Serial.print("     ");
  138.         Serial.print(timeHour);
  139.         Serial.print(":");
  140.         Serial.print(timeMinute);
  141.         Serial.print(":");
  142.         Serial.println(timeSecond);
  143.         Serial.println(" ");
  144.         Serial.println(timeSecond<<8|timeMinute);
  145.         Serial.println(lowByte(timeSecond<<8|timeMinute));
  146.  
  147.         delay(2000);
  148.       }
  149.       node.setTransmitBuffer(0,(timeMinute<<8)|timeSecond);
  150.       node.setTransmitBuffer(1,(dateDay<<8)|timeHour);
  151.       node.setTransmitBuffer(2,(dateYear<<8)|dateMonth);
  152.       result = node.writeMultipleRegisters(0x9013,3);
  153.  
  154.       numLoops = 0;
  155.  
  156.   }   //End of get date and time
  157.  
  158.   delay(500);
  159.  
  160.   if (debug == 1){
  161.     result = node.readHoldingRegisters(0x9013,3);
  162.     if (result == node.ku8MBSuccess)
  163.     {
  164.       if (debug == 1){
  165.         Serial.println("------------------------------------------------------------------");
  166.         Serial.print("Time is: ");
  167.       }
  168.       time1 = lowByte(node.getResponseBuffer(0x00));
  169.       time2 = highByte(node.getResponseBuffer(0x00));
  170.       time3 = lowByte(node.getResponseBuffer(0x01));
  171.  
  172.       sprintf(buf, "%02d:%02d:%02d",time3,time2,time1);
  173.       Serial.println(buf);
  174.  
  175.       Serial.println(time2<<8|time1);
  176.       Serial.println(node.getResponseBuffer(0x00));
  177.  
  178.       Serial.println((time2<<8|time1)==(node.getResponseBuffer(0x00)));
  179.       date1 = (node.getResponseBuffer(0x01) >>8);
  180.       Serial.println(time3);
  181.       Serial.print("Date is: ");
  182.       Serial.println(date1);;
  183.       date2 = node.getResponseBuffer(0x02) & 0xff;
  184.       date3 = (node.getResponseBuffer(0x02) >>8);
  185.       Serial.println(date2);
  186.       Serial.println(date3);
  187.       Serial.println("------------------------------------------------------------------");
  188.     }else{
  189.       rs485DataReceived = false;
  190.     }
  191.     delay(1000);  
  192.   }  
  193.  
  194.  
  195.   // Read 20 registers starting at 0x3100)
  196.   result = node.readInputRegisters(0x3100, 20);
  197.   if (result == node.ku8MBSuccess)
  198.   {
  199.     if (debug == 1){
  200.       Serial.println("------------------------------------------------------------------");
  201.       Serial.print("Controller Temperature: ");
  202.     }
  203.     ctemp = node.getResponseBuffer(0x11)/100.0f;
  204.     if (debug == 1){
  205.       Serial.println(ctemp);
  206.       Serial.print("Battery Voltage: ");
  207.     }
  208.     bvoltage = node.getResponseBuffer(0x04)/100.0f;
  209.     if (debug == 1){
  210.       Serial.println(bvoltage);
  211.       Serial.print("Load Power: ");
  212.     }
  213.     lpower = ((long)node.getResponseBuffer(0x0F)<<16|node.getResponseBuffer(0x0E))/100.0f;
  214.     if (debug == 1){
  215.        Serial.println(lpower);    
  216.        Serial.print("Load Current: ");
  217.     }
  218.     lcurrent = (long)node.getResponseBuffer(0x0D)/100.0f;
  219.     if (debug == 1){
  220.       Serial.println(lcurrent);
  221.       Serial.print("PV Voltage: ");
  222.     }
  223.     pvvoltage = (long)node.getResponseBuffer(0x00)/100.0f;
  224.     if (debug == 1){
  225.       Serial.println(pvvoltage);
  226.       Serial.print("PV Current: ");
  227.     }
  228.     pvcurrent = (long)node.getResponseBuffer(0x01)/100.0f;
  229.     if (debug == 1){
  230.       Serial.println(pvcurrent);
  231.       Serial.print("PV Power: ");
  232.     }
  233.     pvpower = ((long)node.getResponseBuffer(0x03)<<16|node.getResponseBuffer(0x02))/100.0f;
  234.     if (debug == 1){
  235.       Serial.println(pvpower);
  236.       Serial.println("------------------------------------------------------------------");
  237.       delay(500);
  238.     }
  239.   }else{
  240.     rs485DataReceived = false;
  241.   }
  242.  
  243.  
  244.   delay(500);
  245.  
  246.   result = node.readInputRegisters(0x311A, 2);
  247.   if (result == node.ku8MBSuccess)
  248.   {
  249.     if (debug == 1){
  250.       Serial.println("------------------------------------------------------------------");
  251.       Serial.print("Battery Remaining %: ");
  252.     }
  253.     bremaining = node.getResponseBuffer(0x00)/1.0f;
  254.     if (debug == 1){
  255.       Serial.println(bremaining);
  256.       Serial.print("Battery Temperature: ");
  257.     }
  258.     btemp = node.getResponseBuffer(0x01)/100.0f;
  259.     if (debug == 1){
  260.       Serial.println(btemp);
  261.       Serial.println("------------------------------------------------------------------");
  262.       delay(500);
  263.     }
  264.   }else{
  265.     rs485DataReceived = false;
  266.   }
  267.  
  268.  
  269.  
  270.   delay(500);
  271.  
  272.   if (rs485DataReceived){
  273.       if (debug == 1){
  274.         Serial.print("connecting to ");
  275.         Serial.println(host);
  276.       }
  277.  
  278.       // Use WiFiClient class to create TCP connections
  279.       WiFiClient client;
  280.       const int httpPort = 80;
  281.       if (!client.connect(host, httpPort)) {
  282.         if (debug == 1){
  283.           Serial.println("connection failed");
  284.         }
  285.         return;
  286.       }
  287.  
  288.       // field 1 - 8 is setup in thingspeak
  289.  
  290.       String url = "/update";
  291.         url += "?api_key=";
  292.         url += api_key;
  293.         url += "&field1=";
  294.         url += bvoltage;
  295.         url += "&field2=";
  296.         url += bremaining;
  297.         url += "&field3=";
  298.         url += lcurrent;
  299.         url += "&field4=";
  300.         url += lpower;
  301.         url += "&field5=";
  302.         url += pvvoltage;
  303.         url += "&field6=";
  304.         url += pvcurrent;
  305.         url += "&field7=";
  306.         url += pvpower;  
  307.         url += "&field8=";
  308.         url += ctemp;
  309.  
  310.         if (debug == 1){
  311.           Serial.print("Requesting URL: ");
  312.           Serial.println(url);
  313.         }
  314.  
  315.         // This will send the request to the server
  316.         client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  317.                      "Host: " + host + "\r\n" +
  318.                      "Connection: close\r\n\r\n");
  319.         unsigned long timeout = millis();
  320.         while (client.available() == 0) {
  321.           if (millis() - timeout > 5000) {
  322.             if (debug == 1){
  323.                Serial.println(">>> Client Timeout !");
  324.             }
  325.             client.stop();
  326.             return;
  327.           }
  328.         }
  329.  
  330.   }
  331.   numLoops++;
  332.  
  333.   delay(20000);
  334.  
  335.   if (WiFi.status() != WL_CONNECTED)
  336.   {
  337.     if (debug == 1){
  338.       Serial.print("Reconnecting WiFi...");
  339.     }
  340.     WiFi.begin(ssid, password);
  341.     while (WiFi.status() != WL_CONNECTED)
  342.     {
  343.       if (debug == 1){
  344.         Serial.print(".");
  345.       }
  346.       delay(500);
  347.     }
  348.   }
  349.  
  350. } //end Loop
  351.  
  352. String getTime() {
  353.   WiFiClient client;
  354.   int numRetries = 0;
  355.   while ((!!!client.connect("google.com", 80)) & (numRetries<10)) {
  356.     Serial.println("connection failed, retrying...");
  357.     if (WiFi.status() != WL_CONNECTED)
  358.     {
  359.       if (debug == 1){
  360.         Serial.print("Reconnecting WiFi...");
  361.       }
  362.       WiFi.begin(ssid, password);
  363.       while (WiFi.status() != WL_CONNECTED)
  364.       {
  365.         if (debug == 1){
  366.           Serial.print(".");
  367.         }
  368.         delay(500);
  369.       }
  370.     }
  371.  
  372.     numRetries++;
  373.   }
  374.  
  375.   if (numRetries==10){
  376.     numRetries=0;
  377.     while ((!!!client.connect("time.is", 80)) & (numRetries<10)) {
  378.       Serial.println("connection failed, retrying...");
  379.       if (WiFi.status() != WL_CONNECTED)
  380.       {
  381.         if (debug == 1){
  382.           Serial.print("Reconnecting WiFi...");
  383.         }
  384.         WiFi.begin(ssid, password);
  385.         while (WiFi.status() != WL_CONNECTED)
  386.         {
  387.           if (debug == 1){
  388.             Serial.print(".");
  389.           }
  390.           delay(500);
  391.         }
  392.       }
  393.  
  394.       numRetries++;
  395.     }
  396.   }
  397.  
  398.   client.print("HEAD / HTTP/1.1\r\n\r\n");
  399.  
  400.   while(!!!client.available()) {
  401.      yield();
  402.   }
  403.  
  404.   while(client.available()){
  405.     if (client.read() == '\n') {    
  406.       if (client.read() == 'D') {    
  407.         if (client.read() == 'a') {    
  408.           if (client.read() == 't') {    
  409.             if (client.read() == 'e') {    
  410.               if (client.read() == ':') {    
  411.                 client.read();
  412.                 String theDate = client.readStringUntil('\r');
  413.                 client.stop();
  414.                 return theDate;
  415.               }
  416.             }
  417.           }
  418.         }
  419.       }
  420.     }
  421.   }
  422. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement