KiZD

Untitled

Sep 13th, 2013
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. /*
  2. emonTx Shield 4 x CT + Voltage example
  3.  
  4. An example sketch for the emontx Arduino shield module for
  5. CT and AC voltage sample electricity monitoring. Enables real power and Vrms calculations.
  6.  
  7. Part of the openenergymonitor.org project
  8. Licence: GNU GPL V3
  9.  
  10. Authors: Glyn Hudson, Trystan Lea
  11. Builds upon JeeLabs RF12 library and Arduino
  12.  
  13. emonTx documentation: http://openenergymonitor.org/emon/modules/emontxshield/
  14. emonTx firmware code explination: http://openenergymonitor.org/emon/modules/emontx/firmware
  15. emonTx calibration instructions: http://openenergymonitor.org/emon/modules/emontx/firmware/calibration
  16.  
  17. THIS SKETCH REQUIRES:
  18.  
  19. Libraries in the standard arduino libraries folder:
  20. - JeeLib https://github.com/jcw/jeelib
  21. - EmonLib https://github.com/openenergymonitor/EmonLib.git
  22.  
  23. Other files in project directory (should appear in the arduino tabs above)
  24. - emontx_lib.ino
  25.  
  26. */
  27.  
  28. /*Recommended node ID allocation
  29. ------------------------------------------------------------------------------------------------------------
  30. -ID- -Node Type-
  31. 0 - Special allocation in JeeLib RFM12 driver - reserved for OOK use
  32. 1-4 - Control nodes
  33. 5-10 - Energy monitoring nodes
  34. 11-14 --Un-assigned --
  35. 15-16 - Base Station & logging nodes
  36. 17-30 - Environmental sensing nodes (temperature humidity etc.)
  37. 31 - Special allocation in JeeLib RFM12 driver - Node31 can communicate with nodes on any network group
  38. -------------------------------------------------------------------------------------------------------------
  39. */
  40.  
  41. #define FILTERSETTLETIME 5000 // Time (ms) to allow the filters to settle before sending data
  42.  
  43. const int CT1 = 1;
  44. const int CT2 = 1; // Set to 0 to disable
  45. const int CT3 = 1;
  46. const int CT4 = 1;
  47.  
  48.  
  49. #define freq RF12_868MHZ // Frequency of RF12B module can be RF12_433MHZ, RF12_868MHZ or RF12_915MHZ. You should use the one matching the module you have.
  50. const int nodeID = 10; // emonTx RFM12B node ID
  51. const int networkGroup = 210; // emonTx RFM12B wireless network group - needs to be same as emonBase and emonGLCD
  52.  
  53. #include <JeeLib.h> // Download JeeLib: http://github.com/jcw/jeelib
  54. #include "EmonLib.h"
  55. EnergyMonitor ct1,ct2,ct3, ct4; // Create instances for each CT channel
  56.  
  57. typedef struct { int power1, power2, power3, power4, Vrms;} PayloadTX; // create structure - a neat way of packaging data for RF comms
  58. PayloadTX emontx;
  59.  
  60. const int LEDpin = 9; // On-board emonTx LED
  61.  
  62. boolean settled = false;
  63.  
  64. void setup()
  65. {
  66. Serial.begin(9600);
  67. //while (!Serial) {
  68. ; // wait for serial port to connect. Needed for Leonardo only
  69.  
  70. Serial.println("emonTX Shield CT123 Voltage example");
  71. Serial.println("OpenEnergyMonitor.org");
  72. Serial.print("Node: ");
  73. Serial.print(nodeID);
  74. Serial.print(" Freq: ");
  75. if (freq == RF12_433MHZ) Serial.print("433Mhz");
  76. if (freq == RF12_868MHZ) Serial.print("868Mhz");
  77. if (freq == RF12_915MHZ) Serial.print("915Mhz");
  78. Serial.print(" Network: ");
  79. Serial.println(networkGroup);
  80. // }
  81.  
  82. if (CT1) ct1.current(1, 60.606); // Setup emonTX CT channel (ADC input, calibration)
  83. if (CT2) ct2.current(2, 60.606); // Calibration factor = CT ratio / burden resistance
  84. if (CT3) ct3.current(3, 60.606); // emonTx Shield Calibration factor = (100A / 0.05A) / 33 Ohms
  85. if (CT4) ct4.current(4, 60.606);
  86.  
  87. if (CT1) ct1.voltage(0, 228.268, 1.7); // ct.voltageTX(ADC input, calibration, phase_shift) - make sure to select correct calibration for AC-AC adapter http://openenergymonitor.org/emon/modules/emontx/firmware/calibration. Default set for Ideal Power adapter
  88. if (CT2) ct2.voltage(0, 228.268, 1.7);
  89. if (CT3) ct3.voltage(0, 228.268, 1.7);
  90. if (CT4) ct4.voltage(0, 228.268, 1.7);
  91.  
  92. rf12_initialize(nodeID, freq, networkGroup); // initialize RFM12B
  93. rf12_sleep(RF12_SLEEP);
  94.  
  95. pinMode(LEDpin, OUTPUT); // Setup indicator LED
  96. digitalWrite(LEDpin, HIGH);
  97.  
  98.  
  99. }
  100.  
  101. void loop()
  102. {
  103. if (CT1) {
  104. ct1.calcVI(20,2000); // Calculate all. No.of crossings, time-out
  105. emontx.power1 = ct1.realPower;
  106. Serial.print(emontx.power1);
  107. }
  108.  
  109. emontx.Vrms = ct1.Vrms*100; // AC Mains rms voltage
  110.  
  111. if (CT2) {
  112. ct2.calcVI(20,2000); // Calculate all. No.of crossings, time-out
  113. emontx.power2 = ct2.realPower;
  114. Serial.print(" "); Serial.print(emontx.power2);
  115. }
  116.  
  117. if (CT3) {
  118. ct3.calcVI(20,2000); // Calculate all. No.of crossings, time-out
  119. emontx.power3 = ct3.realPower;
  120. Serial.print(" "); Serial.print(emontx.power3);
  121. }
  122.  
  123. if (CT4) {
  124. ct4.calcVI(20,2000); // Calculate all. No.of crossings, time-out
  125. emontx.power4 = ct4.realPower;
  126. Serial.print(" "); Serial.print(emontx.power4);
  127. }
  128.  
  129. Serial.print(" "); Serial.print(ct1.Vrms);
  130.  
  131. Serial.println(); delay(100);
  132.  
  133. // because millis() returns to zero after 50 days !
  134. if (!settled && millis() > FILTERSETTLETIME) settled = true;
  135.  
  136. if (settled) // send data only after filters have settled
  137. {
  138. send_rf_data(); // *SEND RF DATA* - see emontx_lib
  139. digitalWrite(LEDpin, HIGH); delay(2); digitalWrite(LEDpin, LOW); // flash LED
  140. delay(2000); // delay between readings in ms
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment