Emontx Shield + Ethernet Shield: Help with Code

Hello,

I've just finished assembling an Arduino Uno+Ethernet Shield + Emontx Shield. Hardware issues are passed, Emontx Shield is working with modifications indicated in the wiki, but being an arduino beginner, I've got some code issues. I'm using the github emontx sample code and the ethernet client library.

I can compile the code, then the arduino pass the emontx.power1 variable to my emoncms server, so far it's ok, but it stops everytime at the 8th request, and it's stucked. I assume that it's a problem in the ethernet client part of the code, maybe because the repeating more of eight times flood something, and I have problems to structure my code. Can you help me ? Here's my code, thanks in advance

Guillaume

/*
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
*/
#include 
#include 

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x0C, 0xC0 };
byte ip[] = { 192,168,1,67 };
byte gateway[] ={ 192, 168, 1, 254 };

IPAddress server(192,168,1,10); // My server

EthernetClient client;


#define FILTERSETTLETIME 10000 // 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 = 0;
const int CT4 = 0;


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

#include  // Download JeeLib: http://github.com/jcw/jeelib
#include "EmonLib.h"
EnergyMonitor ct1,ct2,ct3, ct4; // Create instances for each CT channel

typedef struct { int power1, power2, power3, power4;} PayloadTX; // create structure - a neat way of packaging data for RF comms
PayloadTX emontx;

const int LEDpin = 9; // On-board emonTx LED

boolean settled = false;

void setup()
{
  Serial.begin(9600);
  Serial.println("emonTX Shield CT123 example");
  Serial.println("OpenEnergyMonitor.org");
  Serial.print("Node: ");
  Serial.print(nodeID);
  Serial.print(" Freq: ");
  if (freq == RF12_433MHZ) Serial.print("433Mhz");
  if (freq == RF12_868MHZ) Serial.print("868Mhz");
  if (freq == RF12_915MHZ) Serial.print("915Mhz");
 Serial.print(" Network: ");
  Serial.println(networkGroup);
             
  if (CT1) ct1.current(1, 59.960); // Setup emonTX CT channel (channel, calibration)
  if (CT2) ct2.current(2, 59.960); // Calibration factor = CT ratio / burden resistance
  if (CT3) ct3.current(3, 38.885);
  if (CT4) ct4.current(4, 38.885);
  
 //emonTx Shield Calibration = (100A / 0.05A) / 33 Ohms
  rf12_set_cs(8);
  rf12_initialize(nodeID, freq, networkGroup); // initialize RFM12B
  rf12_sleep(RF12_SLEEP);

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

void loop()
{
  if (CT1) {
    emontx.power1 = ct1.calcIrms(1480) * 230.0; //ct.calcIrms(number of wavelengths sample)*AC RMS voltage
    Serial.print(" "); Serial.print(emontx.power1);
    }
  
  if (CT2) {
    emontx.power2 = ct2.calcIrms(1480) * 230.0;
    Serial.print(" "); Serial.print(emontx.power2);
  }

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

  // 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
    
    // Open serial communications and wait for port to open:
  Serial.begin(9600);
  EthernetClient client;
  
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // give the Ethernet shield a second to initialize:
  //delay(1000);

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.print("GET /powerbase/api/post?apikey=d356882fe194da4aa7297ace154d0807&json={arduino:");
    client.print(emontx.power1); //POST CT1 VALUE
    client.print("}");
    client.println(" HTTP/1.0");
    client.println();
    Serial.println("ARDUINO: HTTP message sent");
    delay(1000);
    client.flush();
    //client.stop();
    
    } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
      }
    
  }

 }
TrystanLea's picture

Re: Emontx Shield + Ethernet Shield: Help with Code

Hello Guillaume

Yes maybe try putting:

// Open serial communications and wait for port to open:
  Serial.begin(9600);
  EthernetClient client;
 
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }

in void setup() rather than void loop() that might solve it.

gcarlier's picture

Re: Emontx Shield + Ethernet Shield: Help with Code

Hello Trystan,

Thanks very much, it's working like that.

Guillaume

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.