Advertisement
kevin2458

OpenEnergyMonitor (Arduino Yún/Serial/API)

Nov 3rd, 2016
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.88 KB | None | 0 0
  1. #include <Bridge.h>
  2.  
  3. #include <BridgeClient.h>
  4.  
  5. #include <BridgeServer.h>
  6.  
  7. #include <HttpClient.h>
  8.  
  9. #include <ArduinoJson.h>
  10.  
  11. #include "EmonLib.h"
  12.  
  13. #define PIN_EMON_TX_SHIELD_LED 9
  14.  
  15. #define DELAY_LOOP 0
  16.  
  17. #define SECOND_UNIT 1000
  18.  
  19. #define SEND_READ false
  20.  
  21. #define SEND_READ_SECONDS 15
  22.  
  23. #define URL_SEND_READ "http://192.168.240.154/AddRead.aspx"
  24.  
  25. #define APPARENT_POWER "apparent_power"
  26.  
  27. #define IRMS "irms"
  28.  
  29. #define POWER_FACTOR "power_factor"
  30.  
  31. #define REAL_POWER "real_power"
  32.  
  33. #define VRMS "vrms"
  34.  
  35. unsigned long _ReadTime;
  36.  
  37. unsigned long _RutineTime;
  38.  
  39. unsigned long _SendReadTime;
  40.  
  41. BridgeServer _Server;
  42.  
  43. EnergyMonitor _CT1;
  44.  
  45. void setup() {
  46.   Serial.begin(9600);
  47.  
  48.   delay(5 * SECOND_UNIT);
  49.  
  50.   if (Serial) {
  51.     Serial.println();
  52.  
  53.     Serial.println("Iniciando...");
  54.  
  55.     Serial.println();
  56.   }
  57.  
  58.   pinMode(13, OUTPUT);
  59.  
  60.   digitalWrite(13, LOW);
  61.  
  62.   Bridge.begin();
  63.  
  64.   digitalWrite(13, HIGH);
  65.  
  66.   _Server.listenOnLocalhost();
  67.  
  68.   _Server.begin();
  69.  
  70.   _CT1.voltage(0, 140.8, 1.7);
  71.  
  72.   _CT1.current(1, 60.606);
  73.  
  74.   pinMode(PIN_EMON_TX_SHIELD_LED, OUTPUT);
  75.  
  76.   if (SEND_READ) {
  77.     _SendReadTime = millis() + (10 * SECOND_UNIT);
  78.   }
  79. }
  80.  
  81. void loop() {
  82.   _ReadTime = millis();
  83.  
  84.   digitalWrite(PIN_EMON_TX_SHIELD_LED, HIGH);
  85.  
  86.   _CT1.calcVI(20, 2000);
  87.  
  88.   digitalWrite(PIN_EMON_TX_SHIELD_LED, LOW);
  89.  
  90.   _ReadTime = millis() - _ReadTime;
  91.  
  92.   _RutineTime = millis();
  93.  
  94.   if (Serial) {
  95.     for (int i = 1; i <= 3; i++) {
  96.       Serial.print("==================================================");
  97.     }
  98.  
  99.     Serial.println();
  100.  
  101.     Serial.println();
  102.  
  103.     Serial.print("> Tiempo de lectura: ");
  104.  
  105.     if (_ReadTime >= SECOND_UNIT) {
  106.       Serial.print(_ReadTime / SECOND_UNIT);
  107.  
  108.       Serial.print(" s");
  109.     } else {
  110.       Serial.print(_ReadTime);
  111.  
  112.       Serial.print(" ms");
  113.     }
  114.  
  115.     Serial.println();
  116.  
  117.     Serial.println();
  118.   }
  119.  
  120.   printRead();
  121.  
  122.   requestRead();
  123.  
  124.   if (SEND_READ && millis() >= _SendReadTime) {
  125.     sendRead();
  126.  
  127.     _SendReadTime = millis() + (SEND_READ_SECONDS * SECOND_UNIT);
  128.   }
  129.  
  130.   _RutineTime = millis() - _RutineTime;
  131.  
  132.   const int duration = DELAY_LOOP - (_ReadTime + _RutineTime);
  133.  
  134.   if (duration > 0) {
  135.     delay(duration);
  136.   }
  137. }
  138.  
  139. void printRead() {
  140.   if (Serial) {
  141.     Serial.print("Voltaje: ");
  142.  
  143.     Serial.print(validateValue(_CT1.Vrms));
  144.  
  145.     Serial.print(" V");
  146.  
  147.     Serial.println();
  148.  
  149.     Serial.print("Corriente: ");
  150.  
  151.     Serial.print(validateValue(_CT1.Irms));
  152.  
  153.     Serial.print(" A");
  154.  
  155.     Serial.println();
  156.  
  157.     Serial.print("Potencia real: ");
  158.  
  159.     Serial.print(validateValue(_CT1.realPower));
  160.  
  161.     Serial.print(" W");
  162.  
  163.     Serial.println();
  164.  
  165.     Serial.print("Potencia aparente: ");
  166.  
  167.     Serial.print(validateValue(_CT1.apparentPower));
  168.  
  169.     Serial.print(" W");
  170.  
  171.     Serial.println();
  172.  
  173.     Serial.print("Factor de potencia: ");
  174.  
  175.     Serial.print(validateValue(_CT1.powerFactor));
  176.  
  177.     Serial.println();
  178.  
  179.     Serial.println();
  180.   }
  181. }
  182.  
  183. void sendRead() {
  184.   String URL = URL_SEND_READ;
  185.  
  186.   URL += "?" + paramURL(VRMS, _CT1.Vrms);
  187.  
  188.   URL += "&" + paramURL(IRMS, _CT1.Irms);
  189.  
  190.   URL += "&" + paramURL(REAL_POWER, _CT1.realPower);
  191.  
  192.   URL += "&" + paramURL(APPARENT_POWER, _CT1.apparentPower);
  193.  
  194.   URL += "&" + paramURL(POWER_FACTOR, _CT1.powerFactor);
  195.  
  196.   HttpClient client;
  197.  
  198.   client.get(URL);
  199.  
  200.   if (Serial) {
  201.     Serial.println("> Enviando lectura... " + URL);
  202.  
  203.     Serial.println();
  204.   }
  205.  
  206.   if (client.available()) {
  207.     while (client.available()) {
  208.       const char r = client.read();
  209.  
  210.       if (Serial) {
  211.         if (r == '0') {
  212.           Serial.println("Respuesta: No se guardo la lectura");
  213.         } else if (r == '1') {
  214.           Serial.println("Respuesta: Lectura guardada");
  215.         } else {
  216.           Serial.println("Sin respuesta");
  217.         }
  218.  
  219.         Serial.println();
  220.       }
  221.  
  222.       break;
  223.     }
  224.   } else if (Serial) {
  225.     Serial.println("Servidor no disponible");
  226.  
  227.     Serial.println();
  228.   }
  229. }
  230.  
  231. void requestRead() {
  232.   BridgeClient client = _Server.accept();
  233.  
  234.   if (client) {
  235.     if (Serial) {
  236.       Serial.println("> Lectura solicitada");
  237.     }
  238.  
  239.     StaticJsonBuffer<200> jsonBuffer;
  240.  
  241.     JsonObject& json = jsonBuffer.createObject();
  242.  
  243.     json[VRMS] = validateValue(_CT1.Vrms).toFloat();
  244.  
  245.     json[IRMS] = validateValue(_CT1.Irms).toFloat();
  246.  
  247.     json[REAL_POWER] = validateValue(_CT1.realPower).toFloat();
  248.  
  249.     json[APPARENT_POWER] = validateValue(_CT1.apparentPower).toFloat();
  250.  
  251.     json[POWER_FACTOR] = validateValue(_CT1.powerFactor).toFloat();
  252.  
  253.     json.prettyPrintTo(client);
  254.  
  255.     if (Serial) {
  256.       Serial.println();
  257.  
  258.       json.prettyPrintTo(Serial);
  259.  
  260.       Serial.println();
  261.  
  262.       Serial.println();
  263.     }
  264.  
  265.     client.stop();
  266.   }
  267. }
  268.  
  269. String paramURL(String key, float value) {
  270.   return key + String("=") + validateValue(value);
  271. }
  272.  
  273. String validateValue(float value) {
  274.   return String(abs(value), 2);
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement