Community
OpenEnergyMonitor

Community

emonTx Arduino Shield SMT (RF) with RTC

emontx-shield
Tags: #<Tag:0x00007f880e0a6930>
(ALBERTO ALVAREZ ALVAREZ) #1

Hi all.

I have a ARDUINO UNO with the emonTx SHIELD plus RFM69CW. Due I need activate relays based in power measurements and timing I need add a RTC (DS3231).
As you know, the RTC needs the SDA (and SCL) signal connected to A4/ADC4 but in the SHIELD and asociated sketches the A4/ADC4 is reserved for CT4.
I insulate the pin A4 from the ARDUINO to EMONTX_SHIELD and uploading only the sketch RTC the system gives the right date/time: works well. Now I loos the CT4: is not matter for me.

After that I modify the sketch “Shield_CT1234” adding the proper lines for run the RTC but doesn’t work, probably because the software/libraries “Shield_CT1234” and the RTC sketch uses the same A4 and going to conflict. The question is: how can I modify the software/libraries for:

  • deactivate/erase all CT4 in “Shield_CT1234” or library? in the way that there is not conflict with the RTC code. I try with “const int CT4 = 0;” at the begin of the code but doesnot work. (I prefer this solution)
    or
  • move A4/SDA signal in the RTC sketch/libray (Wire.h or RTClib.h?)to another free pin?

Thanks and best regards.

(Robert Wall) #2

The Library itself does not go to CT4 - it only goes there when requested by the sketch.

I cannot see why changing

const int CT4 = 1;

does not work.

(ALBERTO ALVAREZ ALVAREZ) #3

Sorry if I dont explain properly.

Now, with the ARDUINO UNO+SHIELD (with RF) and the A4 fisical isolated between ARDUINO and SHIELD:

  • only with the sketch “Shield_CT1234”, the sketch works well, less CT4
  • only with the sketch RTC, the sketch works well

the problem is when I merge both sketch “Shield_CT1234”+RTC, then the sketch doesn’t answer back the date/time, probably because A4 is used by both sketch. The question is how change the use of A4 in one of the two sketch.

Best regards.

(Robert Wall) #4

Is it that A4 is being used by the RTC as the OneWire input line? I am not an Arduino expert, I looked at the RTC library you mentioned, but I could not see anything that related to AI 4.

(Greebo) #5

In the Arduino, A4 is used by I2C communications (along with A5). The RTC is I2C based, so Alberto needs to use A4 solely for the RTC.

@alberto, did you try disabling CT4 as Robert suggested? i.e. change this line from const int CT4 = 1; to const int CT4 = 0; as per the comment:

If you did, please post your “combined” script here. (please wrap it in “code tags” (```) before and after)

(Robert Wall) #6

@Greebo Thanks for joining in. @albertin said he did, and with no success (I try with “const int CT4 = 0;” at the begin of the code but doesnot work. in the top post). But the Arduino libraries must surely leave the analog inputs lines free for whatever other purpose is available because of the way the pins are multi-purpose, therefore as long as there’s no physical connection to the Shield, and no reference in the sketch to AI 4, I would expect the RTC to work.

(Greebo) #7

I realise this was in the first post, but it doesn’t describe clearly exactly what was done. Was the original line edited from 1 to 0, we can only assume that it was. My question was to thoroughly and categorically test that assumption :slight_smile:

They absolutely do, yes. I think we need to see the actual combined sketch he’s using to help any further.

(Robert Wall) #8

Agreed - I think the merging of the two sketches has gone wrong somewhere.

(ALBERTO ALVAREZ ALVAREZ) #9

Hello.
Thanks for your support.

Here it is the merge code:

/*
 EmonTx Shield 4 x CT example
 
  An example sketch for the emontx Arduino shield module for
 CT only electricity monitoring.
 
 Part of the openenergymonitor.org project
 Licence: GNU GPL V3
 
 Authors: Glyn Hudson, Trystan Lea
 Builds upon JeeLabs RF12 library and Arduino
 
 emonTx documentation: 	http://openenergymonitor.org/emon/modules/emontxshield/
 emonTx firmware code explination: http://openenergymonitor.org/emon/modules/emontx/firmware
 emonTx calibration instructions: http://openenergymonitor.org/emon/modules/emontx/firmware/calibration

 THIS SKETCH REQUIRES:

 Libraries in the standard arduino libraries folder:
	- JeeLib		https://github.com/jcw/jeelib
	- EmonLib		https://github.com/openenergymonitor/EmonLib.git

 Other files in project directory (should appear in the arduino tabs above)
	- emontx_lib.ino
 
*/

/*Recommended node ID allocation
------------------------------------------------------------------------------------------------------------
-ID-	-Node Type- 
0	- Special allocation in JeeLib RFM12 driver - reserved for OOK use
1-4     - Control nodes 
5-10	- Energy monitoring nodes
11-14	--Un-assigned --
15-16	- Base Station & logging nodes
17-30	- Environmental sensing nodes (temperature humidity etc.)
31	- Special allocation in JeeLib RFM12 driver - Node31 can communicate with nodes on any network group
-------------------------------------------------------------------------------------------------------------
*/

#define FILTERSETTLETIME 5000                                           //  Time (ms) to allow the filters to settle before sending data

const int CT1 = 1; 
const int CT2 = 1;                                                      // Set to 0 to disable 
const int CT3 = 1;
const int CT4 = 0;


#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.
//const int nodeID = 6;                                                  // emonTx RFM12B node ID
const int nodeID = 17;                                                  // emonTx RFM12B node ID
const int networkGroup = 210;                                           // emonTx RFM12B wireless network group - needs to be same as emonBase and emonGLCD                                                 

#define RF69_COMPAT 1 // set to 1 to use RFM69CW 
#include <JeeLib.h>   // make sure V12 (latest) is used if using RFM69CW
#include "EmonLib.h"
EnergyMonitor ct1,ct2,ct3, ct4;                                              // Create  instances for each CT channel

typedef struct { int hora, power1, power2, power3, power4, TEMP1 ;} PayloadTX;      // create structure - a neat way of packaging data for RF comms, nothing is added a a 5th integer to match data structure of voltage version
PayloadTX emontx;                                                       

const int LEDpin = 9;                                                   // On-board emonTx LED 
boolean settled = false;
int x;

// TEMPERATURA
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(4);                    // Setup one-wire on digital input pin 4
DallasTemperature sensors(&oneWire);   // Pass the oneWire reference to Dallas Temperature.
DeviceAddress address_T1 = { 0x28, 0xFD, 0xBE, 0xBC, 0x07, 0x00, 0x00, 0x32 };   

// RTC
#include <Wire.h>   
#include "RTClib.h" 
RTC_DS3231 rtc;     
String daysOfTheWeek[7] = { "Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado" };
String monthsNames[12] = { "Enero", "Febrero", "Marzo", "Abril", "Mayo",  "Junio", "Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre" };
int hour;   //para RTC
int minute; //para RTC
int second; //para RTC



void setup() 
{
  // TEMPERATURA
  sensors.begin();                    
  
  Serial.begin(9600);
  Serial.println("emonTX Shield CT123"); 
  Serial.print("Node: "); 
  Serial.print(nodeID); 
  Serial.print(" Freq: "); 
  if (RF_freq == RF12_433MHZ) Serial.print("433Mhz");
 Serial.print(" Network: "); 
  Serial.println(networkGroup);
             
  if (CT1) ct1.current(1, 60.606);                                     // Setup emonTX CT channel (channel, calibration)
  if (CT2) ct2.current(2, 60.606);                                     // Calibration factor = CT ratio / burden resistance
  if (CT3) ct3.current(3, 60.606); 
  if (CT4) ct4.current(4, 60.606); 
  
 // emonTx Shield Calibration = (100A / 0.05A) / 33 Ohms
  
  rf12_initialize(nodeID, RF_freq, networkGroup);                          // initialize RFM12B
  rf12_sleep(RF12_SLEEP);                                             

  pinMode(LEDpin, OUTPUT);                                              // Setup indicator LED
  digitalWrite(LEDpin, HIGH);


// para el RTC
   if (!rtc.begin()) {
      Serial.println(F("Couldn't find RTC"));
      while (1);
   }  
 //rtc.adjust(DateTime(2018, 12, 18, 20, 52, 0));       // AAA: descomentar esta linea para poner en hora
  
}

// para el RTC
void printDate(DateTime date)
{
   Serial.print(date.year(), DEC);
   Serial.print('/');
   Serial.print(date.month(), DEC);
   Serial.print('/');
   Serial.print(date.day(), DEC);
   Serial.print(" (");
   Serial.print(daysOfTheWeek[date.dayOfTheWeek()]);
   Serial.print(") ");
   Serial.print(date.hour(), DEC);
   Serial.print(':');
   Serial.print(date.minute(), DEC);
   Serial.print(':');
   Serial.print(date.second(), DEC);
   Serial.println();
   hour = (date.hour());
   minute = (date.minute());
   second = (date.second());   
}

void loop() 
{ 

  // RTC
emontx.hora =  (hour * 100) + minute ;  // se empaqueta la hora y el minuto para emviar a EMONCMS
Serial.print("\t"); Serial.print("RTC: "); Serial.print(hour); Serial.print(minute); Serial.print(second);  Serial.print("\t");

  // TEMP
  sensors.requestTemperatures();
  emontx.TEMP1 = sensors.getTempC(address_T1); 
  Serial.print("emontx.TEMP1: ");Serial.println(emontx.TEMP1);  
  if (emontx.TEMP1 < 0) emontx.TEMP1 = 0; if (emontx.TEMP1 > 120) emontx.TEMP1 = 120; 
  
  // CT´s  
  if (CT1) {
    emontx.power1 = ct1.calcIrms(1480) * 240.0;                         //ct.calcIrms(number of wavelengths sample)*AC RMS voltage
    Serial.print(emontx.power1);                                         
  }
  
  if (CT2) {
    emontx.power2 = ct2.calcIrms(1480) * 240.0;
    Serial.print(" "); Serial.print(emontx.power2);
  } 

  if (CT3) {
    emontx.power3 = ct3.calcIrms(1480) * 240.0;
    Serial.print(" "); Serial.print(emontx.power3);
  } 
  
   if (CT4) {
    emontx.power4 = ct4.calcIrms(1480) * 240.0;
    Serial.print(" "); Serial.print(emontx.power4);
  } 
  
  Serial.println(); delay(100);

  // RF
  // because millis() returns to zero after 50 days ! 
  if (!settled && millis() > FILTERSETTLETIME) settled = true;

  if (settled)                                                            // send data only after filters have settled
  { 
    send_rf_data();                                                       // *SEND RF DATA* - see emontx_lib
    digitalWrite(LEDpin, HIGH); delay(2); digitalWrite(LEDpin, LOW);      // flash LED
    delay(2000);                                                          // delay between readings in ms
  }
}
(Greebo) #10

What is the exact behavior when you load this sketch?

(Robert Wall) #11

Are you using the official Arduino libraries for the I2C and the RTC? If not, please tell use which ones you are using.

(ALBERTO ALVAREZ ALVAREZ) #12

The variables:
emontx.hora
hour
minute
etc

always shown 0

Best regards

(ALBERTO ALVAREZ ALVAREZ) #13

Are you using the official Arduino libraries for the I2C and the RTC? If not, please tell use which ones you are using.

Yes for sure. I’m using the same PC and without touch the hardware (Arduino UNO + shield + RTC connected in A4 and A5) when upload the RCD code: works properly. When I upload the “Shield_CT1234” sketch: works properly less CT4 (A4 fisicaly dissconected between Arduino and Shield).

Under my point of view the problem is that both sketch when are merged uses the same A4… and a conflict appears… but not sure.

Best regards.

(Robert Wall) #14

Do you see an error message in the Arduino console window?

(dBC) #15

It looks like hour, minute and second only get written to in printDate() and I can’t see where that gets called?

(ALBERTO ALVAREZ ALVAREZ) #16

I found the error.
I forget (in the fact I don´t know why is necessary) add two lines in void loop:

DateTime now = rtc.now();
printDate(now);

now works well: CT1-2-3, temperature and clock.

The complete code is:

/*
 EmonTx Shield 4 x CT example
 
  An example sketch for the emontx Arduino shield module for
 CT only electricity monitoring.
 
 Part of the openenergymonitor.org project
 Licence: GNU GPL V3
 
 Authors: Glyn Hudson, Trystan Lea
 Builds upon JeeLabs RF12 library and Arduino
 
 emonTx documentation: 	http://openenergymonitor.org/emon/modules/emontxshield/
 emonTx firmware code explination: http://openenergymonitor.org/emon/modules/emontx/firmware
 emonTx calibration instructions: http://openenergymonitor.org/emon/modules/emontx/firmware/calibration

 THIS SKETCH REQUIRES:

 Libraries in the standard arduino libraries folder:
	- JeeLib		https://github.com/jcw/jeelib
	- EmonLib		https://github.com/openenergymonitor/EmonLib.git

 Other files in project directory (should appear in the arduino tabs above)
	- emontx_lib.ino
 
*/

/*Recommended node ID allocation
------------------------------------------------------------------------------------------------------------
-ID-	-Node Type- 
0	- Special allocation in JeeLib RFM12 driver - reserved for OOK use
1-4     - Control nodes 
5-10	- Energy monitoring nodes
11-14	--Un-assigned --
15-16	- Base Station & logging nodes
17-30	- Environmental sensing nodes (temperature humidity etc.)
31	- Special allocation in JeeLib RFM12 driver - Node31 can communicate with nodes on any network group
-------------------------------------------------------------------------------------------------------------
*/

#define FILTERSETTLETIME 5000                                           //  Time (ms) to allow the filters to settle before sending data

const int CT1 = 1; 
const int CT2 = 1;                                                      // Set to 0 to disable 
const int CT3 = 1;
//const int CT4 = 0;


#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.
//const int nodeID = 6;                                                  // emonTx RFM12B node ID
const int nodeID = 17;                                                  // emonTx RFM12B node ID
const int networkGroup = 210;                                           // emonTx RFM12B wireless network group - needs to be same as emonBase and emonGLCD                                                 

#define RF69_COMPAT 1 // set to 1 to use RFM69CW 
#include <JeeLib.h>   // make sure V12 (latest) is used if using RFM69CW
#include "EmonLib.h"
//EnergyMonitor ct1,ct2,ct3,ct4;                                              // Create  instances for each CT channel
EnergyMonitor ct1,ct2,ct3;                                              // Create  instances for each CT channel

typedef struct { int hora, CT1, CT2, CT3, TEMP1 ;} PayloadTX;      // create structure - a neat way of packaging data for RF comms, nothing is added a a 5th integer to match data structure of voltage version
PayloadTX emontx;                                                       

const int LEDpin = 9;                                                   // On-board emonTx LED 
boolean settled = false;
int x;

// TEMPERATURA
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(4);                    // Setup one-wire on digital input pin 4
DallasTemperature sensors(&oneWire);   // Pass the oneWire reference to Dallas Temperature.
DeviceAddress address_T1 = { 0x28, 0xFD, 0xBE, 0xBC, 0x07, 0x00, 0x00, 0x32 };   

// RTC
#include <Wire.h>   
#include "RTClib.h" 
RTC_DS3231 rtc;     
String daysOfTheWeek[7] = { "Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado" };
String monthsNames[12] = { "Enero", "Febrero", "Marzo", "Abril", "Mayo",  "Junio", "Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre" };
int hour;   
int minute; 

void setup() {

  // TEMP
  sensors.begin();                    

  // PERESENTACION
  Serial.begin(9600);
  Serial.println("emonTX Shield CT123"); 
  Serial.print("Node: "); 
  Serial.print(nodeID); 
  Serial.print(" Freq: "); 
  if (RF_freq == RF12_433MHZ) Serial.print("433Mhz");
  Serial.print(" Network: "); 
  Serial.println(networkGroup);
             
  if (CT1) ct1.current(1, 60.606);                                     // Setup emonTX CT channel (channel, calibration)
  if (CT2) ct2.current(2, 60.606);                                     // Calibration factor = CT ratio / burden resistance
  if (CT3) ct3.current(3, 60.606); 
//  if (CT4) ct4.current(4, 60.606); 
  
 // emonTx Shield Calibration = (100A / 0.05A) / 33 Ohms
  
  rf12_initialize(nodeID, RF_freq, networkGroup);                          // initialize RFM12B
  rf12_sleep(RF12_SLEEP);                                             

  pinMode(LEDpin, OUTPUT);                                              // Setup indicator LED
  digitalWrite(LEDpin, HIGH);


// RTC
   if (!rtc.begin()) {
      Serial.println(F("Couldn't find RTC"));
      while (1);
   }  
 //rtc.adjust(DateTime(2018, 12, 18, 20, 52, 0));       // AAA: descomentar esta linea para poner en hora
}

// RTC
void printDate(DateTime date)
{
/*
   Serial.print(date.year(), DEC);
   Serial.print('/');
   Serial.print(date.month(), DEC);
   Serial.print('/');
   Serial.print(date.day(), DEC);
   Serial.print(" (");
   Serial.print(daysOfTheWeek[date.dayOfTheWeek()]);
   Serial.print(") ");
   Serial.print(date.hour(), DEC);
   Serial.print(':');
   Serial.print(date.minute(), DEC);
   Serial.print(':');
   Serial.print(date.second(), DEC);
   Serial.println();
*/   
   hour = (date.hour());
   minute = (date.minute());
}

void loop() { 
  // RTC
  DateTime now = rtc.now();
  printDate(now);
  emontx.hora =  (hour * 100) + minute ;  // se empaqueta la hora y el minuto para emviar a EMONCMS
  Serial.print("emontx.hora= "); Serial.print(emontx.hora); Serial.print("\t");

  // TEMP
  sensors.requestTemperatures();
  emontx.TEMP1 = sensors.getTempC(address_T1); 
  if (emontx.TEMP1 < 0) emontx.TEMP1 = 0; if (emontx.TEMP1 > 120) emontx.TEMP1 = 120;
  Serial.print("emontx.TEMP1: ");Serial.print(emontx.TEMP1);  Serial.print("\t");
 
  
  // CT´S
  if (CT1) {
    emontx.CT1 = ct1.calcIrms(1480) * 240.0;                         //ct.calcIrms(number of wavelengths sample)*AC RMS voltage
    if (emontx.CT1 < 100) emontx.CT1 = 0; if (emontx.CT1 > 6000) emontx.CT1 = 6000;                                  
  }
  
  if (CT2) {
    emontx.CT2 = ct2.calcIrms(1480) * 240.0;
    if (emontx.CT2 < 100) emontx.CT2 = 0; if (emontx.CT2 > 6000) emontx.CT2 = 6000;                                  
  } 

  if (CT3) {
    emontx.CT3 = ct3.calcIrms(1480) * 240.0;
    if (emontx.CT3 < 100) emontx.CT3 = 0; if (emontx.CT3 > 6000) emontx.CT3 = 6000;                                  
  } 
  
  Serial.print("CT1= ");Serial.print(emontx.CT1);    
  Serial.print(" CT2= "); Serial.print(emontx.CT2);     
  Serial.print(" CT3= "); Serial.print(emontx.CT3);
  Serial.println(); delay(1000);

  // because millis() returns to zero after 50 days ! 
  if (!settled && millis() > FILTERSETTLETIME) settled = true;
  if (settled)                                                            // send data only after filters have settled
  { 
    send_rf_data();                                                       // *SEND RF DATA* - see emontx_lib
    digitalWrite(LEDpin, HIGH); delay(2); digitalWrite(LEDpin, LOW);      // flash LED
    delay(3000);                                                          // delay between readings in ms
  }
}

I focused my attention in the A4 y A5 signals and forget add the two lines from the RTC sketch.
Any question let me know.

Thanks for everybody for the support.