Advertisement
Guest User

Untitled

a guest
Mar 20th, 2015
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 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 = 0; // Set to 0 to disable
  45. const int CT3 = 0;
  46. const int CT4 = 0;
  47.  
  48.  
  49. #define RF_freq RF12_433MHZ // 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. #define RF69_COMPAT 1 // set to 1 to use RFM69CW
  54. #include <JeeLib.h> // make sure V12 (latest) is used if using RFM69CW
  55. #include "EmonLib.h"
  56. EnergyMonitor ct1,ct2,ct3, ct4; // Create instances for each CT channel
  57.  
  58. typedef struct { int power1, power2, power3, power4;} PayloadTX; // create structure - a neat way of packaging data for RF comms
  59. PayloadTX emontx;
  60.  
  61. const int LEDpin = 9; // On-board emonTx LED
  62.  
  63. boolean settled = false;
  64.  
  65. void send_rf_data()
  66. {
  67. rf12_sleep(RF12_WAKEUP);
  68. // if ready to send + exit loop if it gets stuck as it seems too
  69. int i = 0;
  70. while (!rf12_canSend() && i<10)
  71. {
  72. rf12_recvDone();
  73. i++;
  74. }
  75. rf12_sendStart(0, &emontx, sizeof emontx);
  76. // set the sync mode to 2 if the fuses are still the Arduino default
  77. // mode 3 (full powerdown) can only be used with 258 CK startup fuses
  78. //rf12_sendWait(2);
  79. //rf12_sleep(RF12_SLEEP);
  80. }
  81.  
  82. void setup()
  83. {
  84. Serial.begin(9600);
  85. Serial.println("emonTX Shield CT123 example");
  86. Serial.println("OpenEnergyMonitor.org");
  87. Serial.print("Node: ");
  88. Serial.print(nodeID);
  89. Serial.print(" Freq: ");
  90. if (RF_freq == RF12_433MHZ) Serial.print("433Mhz");
  91. if (RF_freq == RF12_868MHZ) Serial.print("868Mhz");
  92. if (RF_freq == RF12_915MHZ) Serial.print("915Mhz");
  93. Serial.print(" Network: ");
  94. Serial.println(networkGroup);
  95.  
  96. if (CT1) ct1.current(1, 60.606); // Setup emonTX CT channel (channel, calibration)
  97. if (CT2) ct2.current(2, 60.606); // Calibration factor = CT ratio / burden resistance
  98. if (CT3) ct3.current(3, 60.606);
  99. if (CT4) ct4.current(4, 60.606);
  100.  
  101. // emonTx Shield Calibration = (100A / 0.05A) / 33 Ohms
  102.  
  103. rf12_initialize(nodeID, RF_freq, networkGroup); // initialize RFM12B
  104. rf12_sleep(RF12_SLEEP);
  105.  
  106. pinMode(LEDpin, OUTPUT); // Setup indicator LED
  107. digitalWrite(LEDpin, HIGH);
  108.  
  109. }
  110.  
  111. void loop()
  112. {
  113. if (CT1) {
  114. emontx.power1 = ct1.calcIrms(1480) * 240.0; //ct.calcIrms(number of wavelengths sample)*AC RMS voltage
  115. Serial.print(emontx.power1);
  116. }
  117.  
  118. if (CT2) {
  119. emontx.power2 = ct2.calcIrms(1480) * 240.0;
  120. Serial.print(" "); Serial.print(emontx.power2);
  121. }
  122.  
  123. if (CT3) {
  124. emontx.power3 = ct3.calcIrms(1480) * 240.0;
  125. Serial.print(" "); Serial.print(emontx.power3);
  126. }
  127.  
  128. if (CT4) {
  129. emontx.power4 = ct4.calcIrms(1480) * 240.0;
  130. Serial.print(" "); Serial.print(emontx.power4);
  131. }
  132.  
  133.  
  134. Serial.println(); delay(100);
  135.  
  136. // because millis() returns to zero after 50 days !
  137. if (!settled && millis() > FILTERSETTLETIME) settled = true;
  138.  
  139. if (settled) // send data only after filters have settled
  140. {
  141. send_rf_data(); // *SEND RF DATA* - see emontx_lib
  142. digitalWrite(LEDpin, HIGH); delay(2); digitalWrite(LEDpin, LOW); // flash LED
  143. delay(2000); // delay between readings in ms
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement