Advertisement
KiZD

emonTxShield_CT1234

Sep 14th, 2013
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. /*
  2. EmonTx Shield 4 x CT example
  3.  
  4. An example sketch for the emontx Arduino shield module for
  5. CT only electricity monitoring.
  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 = 15; // 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;} 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. Serial.println("emonTX Shield CT123 example");
  68. Serial.println("OpenEnergyMonitor.org");
  69. Serial.print("Node: ");
  70. Serial.print(nodeID);
  71. Serial.print(" Freq: ");
  72. if (freq == RF12_433MHZ) Serial.print("433Mhz");
  73. if (freq == RF12_868MHZ) Serial.print("868Mhz");
  74. if (freq == RF12_915MHZ) Serial.print("915Mhz");
  75. Serial.print(" Network: ");
  76. Serial.println(networkGroup);
  77.  
  78. if (CT1) ct1.current(1, 60.606); // Setup emonTX CT channel (channel, calibration)
  79. if (CT2) ct2.current(2, 60.606); // Calibration factor = CT ratio / burden resistance
  80. if (CT3) ct3.current(3, 60.606);
  81. if (CT4) ct4.current(4, 60.606);
  82.  
  83. // emonTx Shield Calibration = (100A / 0.05A) / 33 Ohms
  84.  
  85. rf12_initialize(nodeID, freq, networkGroup); // initialize RFM12B
  86. rf12_sleep(RF12_SLEEP);
  87.  
  88. pinMode(LEDpin, OUTPUT); // Setup indicator LED
  89. digitalWrite(LEDpin, HIGH);
  90.  
  91. }
  92.  
  93. void loop()
  94. {
  95. if (CT1) {
  96. emontx.power1 = ct1.calcIrms(1480) * 240.0; //ct.calcIrms(number of wavelengths sample)*AC RMS voltage
  97. Serial.print(emontx.power1);
  98. }
  99.  
  100. if (CT2) {
  101. emontx.power2 = ct2.calcIrms(1480) * 240.0;
  102. Serial.print(" "); Serial.print(emontx.power2);
  103. }
  104.  
  105. if (CT3) {
  106. emontx.power3 = ct3.calcIrms(1480) * 240.0;
  107. Serial.print(" "); Serial.print(emontx.power3);
  108. }
  109.  
  110. if (CT4) {
  111. emontx.power4 = ct4.calcIrms(1480) * 240.0;
  112. Serial.print(" "); Serial.print(emontx.power4);
  113. }
  114.  
  115.  
  116. Serial.println(); delay(100);
  117.  
  118. // because millis() returns to zero after 50 days !
  119. if (!settled && millis() > FILTERSETTLETIME) settled = true;
  120.  
  121. if (settled) // send data only after filters have settled
  122. {
  123. send_rf_data(); // *SEND RF DATA* - see emontx_lib
  124. digitalWrite(LEDpin, HIGH); delay(2); digitalWrite(LEDpin, LOW); // flash LED
  125. delay(2000); // delay between readings in ms
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement