Advertisement
Guest User

Untitled

a guest
Apr 18th, 2013
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.40 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. #define FILTERSETTLETIME 5000                                           //  Time (ms) to allow the filters to settle before sending data
  28.  
  29. const int CT1 = 1;
  30. const int CT2 = 0;                                                      // Set to 0 to disable
  31. const int CT3 = 0;
  32. const int CT4 = 0;
  33.  
  34.  
  35. #define 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.
  36. const int nodeID = 10;                                                  // emonTx RFM12B node ID
  37. const int networkGroup = 210;                                           // emonTx RFM12B wireless network group - needs to be same as emonBase and emonGLCD                                                
  38.  
  39. #include <JeeLib.h>                                                     // Download JeeLib: http://github.com/jcw/jeelib
  40. #include "EmonLib.h"
  41. EnergyMonitor ct1,ct2,ct3, ct4;                                              // Create  instances for each CT channel
  42.  
  43. typedef struct { int power1, Vrms;} PayloadTX;//power2, power3, power4, Vrms;} PayloadTX;      // create structure - a neat way of packaging data for RF comms
  44. PayloadTX emontx;                                                      
  45.  
  46. const int LEDpin = 9;                                                   // On-board emonTx LED
  47.  
  48. boolean settled = false;
  49.  
  50. void setup()
  51. {
  52.   Serial.begin(9600);
  53.    //while (!Serial) {
  54.     ; // wait for serial port to connect. Needed for Leonardo only
  55.  
  56.   Serial.println("emonTX Shield CT123 Voltage example");
  57.   Serial.println("OpenEnergyMonitor.org");
  58.   Serial.print("Node: ");
  59.   Serial.print(nodeID);
  60.   Serial.print(" Freq: ");
  61.   if (freq == RF12_433MHZ) Serial.print("433Mhz");
  62.   if (freq == RF12_868MHZ) Serial.print("868Mhz");
  63.   if (freq == RF12_915MHZ) Serial.print("915Mhz");
  64.  Serial.print(" Network: ");
  65.   Serial.println(networkGroup);
  66.   // }
  67.    rf12_initialize(nodeID, freq, networkGroup);                          // initialize RFM12B
  68.   rf12_sleep(RF12_SLEEP);
  69.  
  70.   if (CT1) ct1.current(1, 84.0);                                     // Setup emonTX CT channel (ADC input, calibration)
  71.   if (CT2) ct2.current(2, 60.606);                                     // Calibration factor = CT ratio / burden resistance
  72.   if (CT3) ct3.current(3, 60.606);                                     // emonTx Shield Calibration factor = (100A / 0.05A) / 33 Ohms
  73.   if (CT4) ct4.current(4, 60.606);
  74.  
  75.   if (CT1) ct1.voltage(0, 95.0, 4.3);       //228.268                              // 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                                        
  76.   if (CT2) ct2.voltage(0, 234.26, 1.7);                                
  77.   if (CT3) ct3.voltage(0, 234.26, 1.7);
  78.   if (CT4) ct4.voltage(0, 234.26, 1.7);
  79.  
  80.  
  81.  
  82.   pinMode(LEDpin, OUTPUT);                                              // Setup indicator LED
  83.   digitalWrite(LEDpin, HIGH);
  84.    Serial.print("real");
  85.     Serial.print("\t");
  86.     Serial.print("apparent");
  87.     Serial.print("\t");
  88.     Serial.print("Vrms");
  89.     Serial.print("\t");
  90.     Serial.print("Irms");
  91.     Serial.print("\t");
  92.     Serial.print("PF");
  93.     Serial.println("\t");
  94.     delay(1000);
  95.                                                                                      
  96. }
  97.  
  98. void loop()
  99. {
  100.     if (CT1) {
  101.     ct1.calcVI(20,2000);    // Calculate all. No.of crossings, time-out
  102.     emontx.power1 = ct1.realPower;
  103.     ct1.serialprint();
  104.   }
  105.  
  106.   emontx.Vrms = ct1.Vrms*100;                                            // AC Mains rms voltage
  107.   /*
  108.   if (CT2) {
  109.     ct2.calcVI(20,2000);                                                  // Calculate all. No.of crossings, time-out
  110.     emontx.power2 = ct2.realPower;
  111.     Serial.print(" "); Serial.print(emontx.power2);
  112.   }
  113.  
  114.   if (CT3) {
  115.     ct3.calcVI(20,2000);                                                  // Calculate all. No.of crossings, time-out
  116.     emontx.power3 = ct3.realPower;
  117.     Serial.print(" "); Serial.print(emontx.power3);
  118.   }
  119.  
  120.    if (CT4) {
  121.      ct4.calcVI(20,2000);                                                  // Calculate all. No.of crossings, time-out
  122.     emontx.power4 = ct4.realPower;
  123.     Serial.print(" "); Serial.print(emontx.power4);
  124.   }
  125.   */
  126.   //Serial.print(" "); Serial.print(ct1.Vrms);
  127.  
  128.   //Serial.println();
  129.   delay(200);
  130.  
  131.   // because millis() returns to zero after 50 days !
  132.   if (!settled && millis() > FILTERSETTLETIME) settled = true;
  133.  
  134.   if (settled)                                                            // send data only after filters have settled
  135.   {
  136.     send_rf_data();                                                       // *SEND RF DATA* - see emontx_lib
  137.     digitalWrite(LEDpin, HIGH); delay(2); digitalWrite(LEDpin, LOW);      // flash LED
  138.     delay(3000);                                                          // delay between readings in ms
  139.   }
  140. }
  141.  
  142. void send_rf_data()
  143. {
  144.   rf12_sleep(RF12_WAKEUP);
  145.   // if ready to send + exit loop if it gets stuck as it seems too
  146.   int i = 0; while (!rf12_canSend() && i<10) {rf12_recvDone(); i++;}
  147.   rf12_sendStart(0, &emontx, sizeof emontx);
  148.   // set the sync mode to 2 if the fuses are still the Arduino default
  149.   // mode 3 (full powerdown) can only be used with 258 CK startup fuses
  150.   rf12_sendWait(2);
  151.   rf12_sleep(RF12_SLEEP);
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement