Advertisement
Guest User

Untitled

a guest
May 10th, 2015
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.19 KB | None | 0 0
  1. #include <EmonLib.h>
  2.  
  3. /*
  4.  EmonTx Shield 4 x CT example
  5.  
  6.  An example sketch for the emontx Arduino shield module for
  7.  CT and AC voltage sample electricity monitoring. Enables real power and Vrms calculations.
  8.  
  9.  Part of the openenergymonitor.org project
  10.  Licence: GNU GPL V3
  11.  
  12.  Authors: Glyn Hudson, Trystan Lea
  13.  Builds upon JeeLabs RF12 library and Arduino
  14.  
  15.  emonTx documentation:  http://openenergymonitor.org/emon/modules/emontxshield/
  16.  emonTx firmware code explination: http://openenergymonitor.org/emon/modules/emontx/firmware
  17.  emonTx calibration instructions: http://openenergymonitor.org/emon/modules/emontx/firmware/calibration
  18.  
  19.  THIS SKETCH REQUIRES:
  20.  
  21.  Libraries in the standard arduino libraries folder:
  22.     - JeeLib        https://github.com/jcw/jeelib
  23.     - EmonLib       https://github.com/openenergymonitor/EmonLib.git
  24.  
  25.  Other files in project directory (should appear in the arduino tabs above)
  26.     - emontx_lib.ino
  27.  
  28. */
  29.  
  30. /*Recommended node ID allocation
  31. ------------------------------------------------------------------------------------------------------------
  32. -ID-    -Node Type-
  33. 0   - Special allocation in JeeLib RFM12 driver - reserved for OOK use
  34. 1-4     - Control nodes
  35. 5-10    - Energy monitoring nodes
  36. 11-14   --Un-assigned --
  37. 15-16   - Base Station & logging nodes
  38. 17-30   - Environmental sensing nodes (temperature humidity etc.)
  39. 31  - Special allocation in JeeLib RFM12 driver - Node31 can communicate with nodes on any network group
  40. -------------------------------------------------------------------------------------------------------------
  41. */
  42.  
  43. #define CALIB_VOLT  296    // org = 0, 268.97
  44. #define CALIB_CURR  59     // org = 60.606
  45. #define CALIB_PF    1.8    // org = 1.7
  46.      
  47.  
  48. #define FILTERSETTLETIME 5000                                           //  Time (ms) to allow the filters to settle before sending data
  49.  
  50. const int CT1 = 1;
  51. const int CT2 = 1;                                                      // Set to 0 to disable
  52. const int CT3 = 1;
  53. const int CT4 = 1;
  54.  
  55.  
  56. #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.
  57. const int nodeID = 10;                                                  // emonTx RFM12B node ID
  58. const int networkGroup = 210;                                           // emonTx RFM12B wireless network group - needs to be same as emonBase and emonGLCD                                                
  59.  
  60. #define RF69_COMPAT 1 // set to 1 to use RFM69CW
  61. #include <JeeLib.h>   // make sure V12 (latest) is used if using RFM69CW
  62. #include "EmonLib.h"
  63. EnergyMonitor ct1,ct2,ct3,ct4;                                              // Create  instances for each CT channel
  64.  
  65. typedef struct { int power1, power2, power3, power4, Vrms;} PayloadTX;      // create structure - a neat way of packaging data for RF comms
  66. PayloadTX emontx;                                                      
  67.  
  68. const int LEDpin = 9;                                                   // On-board emonTx LED
  69.  
  70. boolean settled = false;
  71.  
  72. void send_rf_data()
  73. {
  74.   rf12_sleep(RF12_WAKEUP);
  75.   // if ready to send + exit loop if it gets stuck as it seems too
  76.   int i = 0;
  77.   while (!rf12_canSend() && i<10)
  78.   {
  79.     rf12_recvDone();
  80.     i++;
  81.   }
  82.   rf12_sendStart(0, &emontx, sizeof emontx);
  83.   // set the sync mode to 2 if the fuses are still the Arduino default
  84.   // mode 3 (full powerdown) can only be used with 258 CK startup fuses
  85.   //rf12_sendWait(2);
  86.   //rf12_sleep(RF12_SLEEP);
  87. }
  88.  
  89. void setup()
  90. {
  91.   Serial.begin(9600);
  92.   Serial.println("emonTX Shield CT123 example");
  93.   Serial.println("OpenEnergyMonitor.org");
  94.   Serial.print("Node: ");
  95.   Serial.print(nodeID);
  96.   Serial.print(" Freq: ");
  97.   if (RF_freq == RF12_433MHZ) Serial.print("433Mhz");
  98.   if (RF_freq == RF12_868MHZ) Serial.print("868Mhz");
  99.   if (RF_freq == RF12_915MHZ) Serial.print("915Mhz");
  100.   Serial.print(" Network: ");
  101.   Serial.println(networkGroup);
  102.              
  103.   if (CT1) ct1.current(1, CALIB_CURR);                                     // Setup emonTX CT channel (ADC input, calibration)
  104.   if (CT2) ct2.current(2, CALIB_CURR);                                     // Calibration factor = CT ratio / burden resistance
  105.   if (CT3) ct3.current(3, CALIB_CURR);                                     // emonTx Shield Calibration factor = (100A / 0.05A) / 33 Ohms
  106.   if (CT4) ct4.current(4, CALIB_CURR);
  107.  
  108.   if (CT1) ct1.voltage(0, CALIB_VOLT, CALIB_PF);                                // 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                                        
  109.   if (CT2) ct2.voltage(0, CALIB_VOLT, CALIB_PF);                                // 268.97 for the UK adapter, 260 for the Euro and 130 for the US.
  110.   if (CT3) ct3.voltage(0, CALIB_VOLT, CALIB_PF);
  111.   if (CT4) ct4.voltage(0, CALIB_VOLT, CALIB_PF);
  112.  
  113.   rf12_initialize(nodeID, RF_freq, networkGroup);                          // initialize RFM12B
  114.   rf12_sleep(RF12_SLEEP);                                            
  115.  
  116.   pinMode(LEDpin, OUTPUT);                                              // Setup indicator LED
  117.   digitalWrite(LEDpin, HIGH);
  118.  
  119. }
  120.  
  121. void loop()
  122. {
  123.   if (CT1)
  124.   {
  125.     ct1.calcVI(20,2000);                                                  // Calculate all. No.of crossings, time-out
  126.     emontx.power1 = ct1.realPower;
  127. //    Serial.print(emontx.power1);                                        
  128.   }
  129.  
  130.   if (CT2)
  131.   {
  132.     ct2.calcVI(20,2000);                                                  // Calculate all. No.of crossings, time-out
  133.     emontx.power2 = ct2.realPower;
  134. //  Serial.print(" "); Serial.print(emontx.power2);
  135.   }
  136.  
  137.   emontx.Vrms = ct2.Vrms*100;                                            // AC Mains rms voltage
  138.  
  139.   if (CT3)
  140.   {
  141.     ct3.calcVI(20,2000);                                                  // Calculate all. No.of crossings, time-out
  142.     emontx.power3 = ct3.realPower;
  143. //  Serial.print(" "); Serial.print(emontx.power3);
  144.   }
  145.  
  146.   if (CT4)
  147.   {
  148.     ct4.calcVI(20,2000);                                                  // Calculate all. No.of crossings, time-out
  149.     emontx.power4 = ct4.realPower;
  150. //  Serial.print(" "); Serial.print(emontx.power4);
  151.   }
  152.  
  153. //Serial.print(" : "); Serial.print(ct2.Vrms);
  154. //Serial.print(" "); Serial.print(ct2.Irms);
  155. //Serial.print(" "); Serial.print(ct2.powerFactor);
  156. //Serial.println(); delay(100);
  157.  
  158.   ct2.serialprint();  // real apparent V I pf
  159.  
  160.  
  161.   // because millis() returns to zero after 50 days !
  162.   if (!settled && millis() > FILTERSETTLETIME)
  163.   {
  164.     Serial.println("Settled");
  165.     settled = true;
  166.   }
  167.  
  168.   if (settled)                                                            // send data only after filters have settled
  169.   {
  170.     send_rf_data();                                                       // *SEND RF DATA* - see emontx_lib
  171.     digitalWrite(LEDpin, HIGH); delay(2); digitalWrite(LEDpin, LOW);      // flash LED
  172.     delay(2000);                                                          // delay between readings in ms
  173.   }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement