Mega cannot read serial output from Current Cost CC128

What I have:

Current Cost CC128 power meter Current Cost CC128 Display Unit
Arduino Mega 2560
Official Ethernet Sheild (new one which is compatible with the Mega) plugged in to Arduino
Windows Vista machine to load the Mega with code

What I am trying to achieve:

(Initially) get the Mega to read and understand the serial data coming from the CC128
(Eventually) get the Mega to publish this on the web (e.g. on Pachube)

What is happening now:

The Arduino is reading the data as rubbish corrupted characters.

Connections:

Current Cost pin 4 (GND) to Arduino ground. Current Cost pin 8 (TX) to Arduino pin 2. (CAT5 cable about 2m long).
Wall wort plugged in to Arduino
2m USB connecting Arduino/PC
Ethernet cable plugged in to Ethernet Shield

Here is the sketch I am using:

#include <SoftwareSerial.h>

// Set up the serial port
SoftwareSerial softSerial =  SoftwareSerial(2, 300); // 300 is dummy TX pin (not needed)
 
void setup() {
  pinMode(2, INPUT);
 softSerial.begin(57600); // << This speed is changed for testing
 Serial.begin(9600); // Hardware serial for diagnostic
 }
 
void loop() {
 // Listen for serial data coming in:
 
 char someChar = softSerial.read();
 Serial.print(someChar); // echo to hardware serial monitor

 }

This is the output I am getting for each softSerial speed. In all cases the "data" is appearing every 6 seconds, it is being corrupted and there are lots of "ÿ" (y with umlaut) characters appearing too:

57600

ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿN?îNd[.N3d(N?î?IbÓ2jNïKtÎåÁN?\hN;$ÿÅhÅÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÎ?fNd[]3dVYN?n%ÿbNÿºjÎçOtN?KÿRòÅN³DEªøÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿN?æNa?Nÿ¢eãNj?
¦NÿË+Nî?ÿNOyN<pNÏÿpE^ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

9600:

ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿýÃÓÁ÷LãúÿãSñÏÃÉÒÒÔyWÏOñÁÂÒmR³æÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿýËÒÅ÷mãûÚãsñïãÉÓÓÖyw¿ÐÁßCMSß×ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

2400:

ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ6?þkOÀÏðKòÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿïüø~_ÐÀú[öÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

If I change the code to use NewSoftSerial instead of SoftwareSerial (at all 3 speeds), I just get a load of "ÿ" characters with no "data" interspersed:

ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

Interestingly, when I go back to using SoftwareSerial at 57600 and remove the wall wort (leaving the Arduino powered by the USB) I still get the "data" but I get far fewer "ÿ" characters:

ÿÿÿÿΝæÎd[]Nÿ%N?j¥IæNÿ¸îÎî?®?¼ÁNëTNpS¼Ç¯ÿÿÿNæ?l_]Nÿ?j?LjNÓ2jNë?ÿ?ãÁNã\NetH~yǾÿÿΝfN?ÿN;àeÆ?î?JæNÿLjNëËðÎÿÿÿXzhNpÌvq<ÿÿÿÿNÿ?Nl_]Nÿ%YNÿ%HbNÓºjNïKtNãÿÿøhNxHmóÇ<ÿÿÿΝæNh[]Nÿ%VYNÿ¥OjNÿ¸jÎëOtN?KÿZrÃNe°ÿmqǼÿÿÿÿÿÿÿΝfNe?Nÿ¢eçN?î?MbNÛÿjNn?ÇN<ÄNç\NxÖ[8Ϗÿÿÿ

Similar effect at the other speeds.

And if I try the hardware port (Serial.read()) I just get a load of "ÿ" characters again, no data.

The Current Cost appears to be sending out data. I have cycled the power a few times. I have also hooked it up to Meniscus on the PC and received this from it:

<msg><src>CC128-v0.11</src><dsb>00641</dsb><time>05:46:29</time><tmpr>23.7</tmpr><sensor>0</sensor><id>00077</id><type>1</type><ch1><watts>00914</watts></ch1></msg>

I have searched the web but cannot find anyone who is doing this using a Mega.

There is a similar problem here on the read-only forum but with no solution:
http://arduino.cc/forum/index.php/topic,44879.0.html

Thank you for reading this far! :slight_smile: I would love to hear anyone's ideas on what might be going on here. I am hoping I have made a simple (stupid) mistake....?!

I am now off to hit my head against a brick wall.

Thanks!

I finally managed to solve my problems.
That's what I can catch now:

date>00349


162807CC02028591
1.0600185
date>00349
162813CC02028591
1.0600180

And the sketch that I use, I hope it can help you.

/*

  • Current Cost Logger
  • This sketch log the data coming form the software serial port 2 on wich I plug the Current Cost output.
  • The Current Cost Energy Monitor split out the data every 6 second. This sketch log that data in a buffer of 260 char
  • and then write that data into an SD using the time window between two data burst.
  • The buffer starts only when the serial become available and the first "<" character come out; it stops after 260
  • logged chars. It also put a "LF" after every buffer.
  • On hardware side, I use a micro-sd connected to arduino on SPI mode. I used an HCF4050B to drop the current of arduino
  • logics I/O 5v.
  • The cable for Current Cost is a standard catV Ethernet cable. I connect the brown to pin 2 and the blue to gnd.
  • By buffering only 260 bytes the currentcost history burst are truncated on actual power consumption reading.
  • Andrea - 08-oct-2010

*/

#include <SdFat.h>
#include <SdFatUtil.h>
#include <NewSoftSerial.h>

Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
void error_P(const char *str)
{
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode()) {
PgmPrint("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
while(1);
}

NewSoftSerial currentcost(2, 300); //connected on pin 2 the rx and on nonexistent pin the tx that isn't used
char data[248]; //the buffer
int i = 0; //counter
boolean endline = false; //used to cut the buffer
boolean startline = false; //used to start the buffer

void setup(void)
{
currentcost.begin(9600);
Serial.begin(9600);

// initialize the SD card
if (!card.init()) error("card.init");

// initialize a FAT volume
if (!volume.init(card)) error("volume.init");

// open the root directory
if (!root.openRoot(volume)) error("openRoot");

}

void loop(void){

file.writeError = false;
if (currentcost.available()) //log only if data are coming
{
char t = (char)currentcost.read();

if (t == 0x64) //start the buffering when catch the "d" character
startline = true; //start the buffer
if (startline) //buffer is started
{
data*=t; //start filling the buffer*

  • if ( i == 162 ) // stop the buffer on the 260th character*

  • endline = true; //stop the buffer*

  • i +=1; //move on to next character*

  • }//end of add to our buffer*

  • if (endline)*

  • {*

  • int j = 0; //counter*

  • while ( j < i) {*

  • !file.open(root, "CurrCost.log", O_CREAT | O_APPEND | O_WRITE);*

  • Serial.print((char)data[j]);*

  • file.print((char)data[j]);*

  • !file.close() || file.writeError ; *

  • j+=1;*

  • }*

  • !file.open(root, "CurrCost.log", O_CREAT | O_APPEND | O_WRITE);*

  • Serial.println();*

  • file.println();*

  • !file.close() || file.writeError ;*

  • endline = false;*

  • startline = false;*

  • i = 0; //reset counter and line flags*

  • }//end of send line to PC*

}//end of data available
}

@ruffandrea thank you very much for your reply! I am glad you managed to solve your problems.

I took your code and removed the SD stuff (I don't need this). I did not add any code. This is what I ended up with:

/*
 * Current Cost Logger
 *
 * From ruffandrea
 *
 */
 
#include <NewSoftSerial.h>


NewSoftSerial currentcost(2, 300); //connected on pin 2 the rx and on nonexistent pin the tx that isn't used
char data[248];  //the buffer
int i = 0;  //counter
boolean endline = false; //used to cut the buffer
boolean startline = false; //used to start the buffer

void setup(void)
{  
  currentcost.begin(9600);
  Serial.begin(9600);
  
}


void loop(void){

    if (currentcost.available()) //log only if data are coming
    {
     char t = (char)currentcost.read(); 

       if (t == 0x64)  //start the buffering when catch the "d" character
       startline = true; //start the buffer
       if (startline)  //buffer is started
       {
        data=t;  //start filling the buffer
        if ( i == 162 ) // stop the buffer on the 260th character
        endline = true; //stop the buffer
        i +=1; //move on to next character
  
       }//end of add to our buffer
     if (endline)
     {
     int j = 0;  //counter
     while ( j < i) {
     
     Serial.print((char)data[j]);
     j+=1;
     
     }
     Serial.println();
     
     endline = false;
     startline = false;
     i = 0; //reset counter and line flags
     }//end of send line to PC
    
}//end of data available


}

My environment (version 0022) is showing an error for this line:

data=t;  //start filling the buffer

The error is "incompatible types in assignment of 'char' to 'char [248]'"

I am not sure how to correct that :~

perhaps data[i] = t ; is meant?

Thank you MarkT - the code now runs without errors with your modification.

However

if (currentcost.available())

never becomes true so the code doesn't work for me. I tried 9600 and 57600 for the currentcost.begin speeds.

Back to my original code, and I pulled off my ethernet shield in case it was that - no difference.

So I am still stuck with my Mega (2560) which is not able to understand my CC128. :frowning:

Any hints, ideas, suggestions will be gratefully received.

So - I solved it! More by moving the goalposts than cracking the actual problem.

I began to think back to basic - and thought about what I had and what I wanted to achieve. Then I remembered that the Arduino Mega has several hardware serial ports!

And I quickly found in the IDE: Examples -> Communication -> MultiSerialMega:

void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(9600);
}

void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.print(inByte, BYTE);
}
}

I changed the Serial1 speed from 9600 to 57600. Plugged the CurrentCost in to pin 19 (RX1) and ground. And bingo, my Mega was reading the serial data and outputting it.

I don't know why my original code didn't work, but I am happy now. I have now extended the code to make the Mega a client and send the current data to my server every 6 seconds; the server stores this and can publish it in several ways - very much work in progress at the moment.

Hi,

Does anyone have this working on software serial or will it just not go fast enough to do 57600?

I'm having the same problem but using an ATMega328 so there is only one hardware serial which i need for debugging over the programming adapter.

Thanks,

Ian

I never got it working with software serial. I believe you're right about the speed, but then I'm a relative noob Arduino-wise.

My Mega is still logging power using Serial1, it's on the desk in front of me now. Still not been put in an enclosure. :slight_smile:

Good luck.

Hi,

Ok, thanks for that.

Ian