Guest User

Untitled

a guest
Jun 5th, 2017
1,066
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* eslint-disable require-path-exists/exists */
  2. 'use strict';
  3. var express = require('express')
  4. var app = express()
  5. var request = require('request');
  6. const SerialPort = require('serialport');
  7. var fs = require('fs');
  8.  
  9.  
  10. var schedule = require('node-schedule');
  11.  
  12. // this is to send at 23:10 command to the pip going to utility to charge battery POP
  13. var j = schedule.scheduleJob('10 23 * * *', function(){
  14. console.log('nos vamos a corriente');
  15. port.write([0X50,0X4F,0X50,0X30,0X30,0XC2,0X48,0X0D]);
  16. });
  17. // this is to send at 13:10 command to the pip going to battery mode SBU
  18. var jsa = schedule.scheduleJob('11 13 * * *', function(){
  19. console.log('nos vamos a SBU');
  20. port.write([0X50,0X4F,0X50,0X30,0X32,0XE2,0X0B,0X0D]);
  21. });
  22.  
  23.  
  24. // this method can force change to utility via web http://localhost:3001/acorriente
  25. app.get('/acorriente', function (req, res) {
  26. res.send('nos vamos a corriente')
  27. console.log('nos vamos a corriente');
  28. port.write([0X50,0X4F,0X50,0X30,0X30,0XC2,0X48,0X0D]);
  29. })
  30. // this method can force change to battery mode via web http://localhost:3001/acorriente
  31. app.get('/abateria', function (req, res) {
  32. console.log('nos vamos a SBU');
  33. res.send('nos vamos a SBU')
  34. port.write([0X50,0X4F,0X50,0X30,0X32,0XE2,0X0B,0X0D]);
  35. })
  36. /*
  37. #Commands with CRC cheats
  38. QPGS = '\x51\x50\x47\x53\x30\x3f\xda\x0d'
  39. QPIGS = '\x51\x50\x49\x47\x53\xB7\xA9\x0d'
  40. QMCHGCR ='\x51\x4D\x43\x48\x47\x43\x52\xD8\x55\x0D' #?
  41. QMUCHGCR='\x51\x4D\x55\x43\x48\x47\x43\x52\x26\x34\x0D' #?
  42. QPIWS = '\x51\x50\x49\x57\x53\xB4\xDA\x0D' #valid?
  43. POP02 = '\x50\x4F\x50\x30\x32\xE2\x0B\x0D' # set to SBU
  44. POP00 = '\x50\x4F\x50\x30\x30\xC2\x48\x0D' #Set to UTILITY
  45. "QMOD\x49\xC1";
  46. "QID\xD6\xEA";
  47. "QVFW\x62\x99";
  48. "QVFW2\xC3\xF5";
  49. "QPIRI\xF8\x54"; -->51 50 49 52 49
  50. QPIRI = '\x51\x50\x49\x52\x49\xF8\x54\x0D'
  51. "QFLAG\x98\x74";
  52. */
  53.  
  54. var the_interval = 7 * 1000;
  55. var the_interval2 = 10 * 1000;
  56.  
  57. // every 7 secons send the comand QPIGS to the PIP
  58. setInterval(function() {
  59. port.write([0X51,0X50,0X49,0X47,0X53,0XB7,0XA9,0X0D]);
  60. }, the_interval);
  61.  
  62. // every 10 seconds send the command QPIRI to the pip
  63. setInterval(function() {
  64. //QPIRI
  65. port.write([0X51,0X50,0X49,0X52,0X49,0XF8,0X54,0X0D]);
  66. }, the_interval2);
  67.  
  68.  
  69. const port = new SerialPort('/dev/ttyAMA0', {
  70. baudRate: 2400,
  71. parser: SerialPort.parsers.readline('\r')
  72. });
  73.  
  74. port.on('open', () =>
  75. {
  76. console.log('Port open');
  77. port.write([0X51,0X50,0X49,0X47,0X53,0XB7,0XA9,0X0D]);
  78. // when the port is open, send QPIGS
  79. }
  80. );
  81.  
  82. port.on('data', function (data) {
  83. console.log('Data: ' + data+" "+data.length);
  84. if (data.length==109 && data.substring(0,1)=="(") {
  85. data=data.substring(1,data.length-2);
  86. var arr = data.split(" ");
  87. var cuerpo="solar "+preparacadena(arr);
  88. request.post({
  89. headers: {'content-type' : 'application/x-www-form-urlencoded'},
  90. url: 'http://localhost:8086/write?db=logger',
  91. body: cuerpo
  92. }, function(error, response, body){
  93. });
  94. }
  95. if (data.length==101 && data.substring(0,1)=="(") {
  96. data=data.substring(1,data.length-2);
  97. var arr = data.split(" ");
  98. var cuerpo="solar2 "+preparacadenaqpiri(arr);
  99. request.post({
  100. headers: {'content-type' : 'application/x-www-form-urlencoded'},
  101. url: 'http://localhost:8086/write?db=logger',
  102. body: cuerpo
  103. }, function(error, response, body){
  104. });
  105. }
  106. });
  107.  
  108. //Data: (230.0 21.7 230.0 50.0 21.7 5000 4000 48.0 46.0 42.0 56.4 54.0 2 30 060 0 2 0 9 01 0 0 54.0 0 1 000�T 101
  109. function preparacadenaqpiri(arr)
  110. {
  111. var grid_rating_voltage=arr[0];
  112. var grid_rating_current=arr[1];
  113. var ac_output_rating_voltage=arr[2];
  114. var ac_output_rating_frecuency=arr[3];
  115. var ac_output_rating_current=arr[4];
  116. var ac_output_rating_apparent_power=arr[5];
  117. var ac_output_rating_active_power=arr[6];
  118. var battery_rating_voltage=arr[7];
  119. var battery_recharge_voltage=arr[8];
  120. var battery_under_voltage=arr[9];
  121. var battery_bulk_voltage=arr[10];
  122. var battery_float_voltage=arr[11];
  123. var battery_type=arr[12];
  124. var current_max_ac_charging=arr[13];
  125. var current_max_charging_current=arr[14];
  126. var input_voltage_range=arr[15];
  127. var output_source_priority=arr[16];
  128. var charger_source_priority=arr[17];
  129. var parallel_max_num=arr[18];
  130. var machine_type=arr[19];
  131. var topology=arr[20];
  132. var output_mode=arr[21];
  133. var battery_re_discharge_voltage=arr[22];
  134. var pv_condition=arr[23];
  135. var pv_power_balance=arr[24];
  136.  
  137. var cadena="";
  138. cadena =cadena +"battery_rating_voltage="+battery_rating_voltage;
  139. cadena =cadena +",battery_recharge_voltage="+battery_recharge_voltage;
  140. cadena =cadena +",battery_under_voltage="+battery_under_voltage;
  141. cadena =cadena +",battery_float_voltage="+battery_float_voltage;
  142. cadena =cadena +",battery_bulk_voltage="+battery_bulk_voltage;
  143. cadena =cadena +",current_max_charging_current="+current_max_charging_current;
  144. cadena =cadena +",output_source_priority="+output_source_priority;
  145. cadena =cadena +",charger_source_priority="+charger_source_priority;
  146. cadena =cadena +",temperatura="+temperatura;
  147. cadena =cadena +",temperatura2="+temperatura2;
  148. return cadena;
  149. }
  150.  
  151.  
  152. //ejemplo Data: (234.7 49.9 000.0 00.0 0000 0000 000 450 55.70 000 100 0049 0000 000.0 00.00 00000 00000101 00 00 00000 100�
  153. function preparacadena(arr)
  154. {
  155. var grid_voltage=arr[0];
  156. var grid_frecuency=arr[1];
  157. var ac_output_voltage=arr[2];
  158. var ac_output_frecuency=arr[3];
  159. var ac_output_power_va=arr[4];
  160. var ac_output_active_power=arr[5];
  161. var output_load_percent=arr[6];
  162. var bus_voltage=arr[7];
  163. var battery_voltage=arr[8];
  164. var battery_charging_current=arr[9];
  165. var battery_capacity=arr[10];
  166. var inverter_heat_sink_temperature=arr[11];
  167. var pv_input_current_for_battery=arr[12];
  168. var pv_input_voltage_1=arr[13];
  169. var battery_voltage_from_scc=arr[14];
  170. var battery_discharge_current=arr[15];
  171. var device_status=arr[16];
  172.  
  173. var cadena="";
  174. cadena =cadena +"grid_voltage="+grid_voltage;
  175. cadena =cadena +",ac_output_voltage="+ac_output_voltage;
  176. cadena =cadena +",ac_output_power_va="+ac_output_power_va;
  177. cadena =cadena +",ac_output_active_power="+ac_output_active_power;
  178. cadena =cadena +",output_load_percent="+output_load_percent;
  179. cadena =cadena +",bus_voltage="+bus_voltage;
  180. cadena =cadena +",battery_voltage="+battery_voltage;
  181. cadena =cadena +",battery_charging_current="+battery_charging_current;
  182. cadena =cadena +",battery_capacity="+battery_capacity;
  183. cadena =cadena +",inverter_heat_sink_temperature="+inverter_heat_sink_temperature;
  184. cadena =cadena +",pv_input_current_for_battery="+pv_input_current_for_battery;
  185. cadena =cadena +",pv_input_voltage_1="+pv_input_voltage_1;
  186. cadena =cadena +",battery_voltage_from_scc="+battery_voltage_from_scc;
  187. cadena =cadena +",battery_discharge_current="+battery_discharge_current;
  188. cadena =cadena +",device_status="+device_status;
  189. if (device_status.length==8) {
  190. cadena =cadena +",charging_scc="+device_status.substring(5,6);
  191. cadena =cadena +",charging_ac="+device_status.substring(6,7);
  192. cadena =cadena +",charging_scc_acc="+device_status.substring(7,8);
  193. }
  194. return cadena;
  195. }
  196. app.listen(3001, function () {
  197. console.log('Example app listening on port 3001!')
  198. })
  199.  
  200.  
  201.  
  202. }
RAW Paste Data