I am creating a sketch to read my electricity meter which has a serial port (P1, Dutch Smart Meter). The meter sends out a telegram which contains the consumption info. I found a sketch that reads the messages from this port with the ‘AltSoftSerial’ library and spits out the figures in a readable way to the serial monitor. This sketch can be found here: http://phoenixinteractive.nl/websitebb/viewtopic.php?f=23&t=37
I want to adapt that sketch so it sends those figures (mEVLT, mEVHT etc.) to a MySQL database. I adapted the sketch to my needs, compiled it, and uploaded it to my Arduino (Duemilanove clone from China). However i get ZERO output from the Serial Monitor on my PC (Linux Mint). If i upload the sketch from the link i provided, it displays everything nicely in the serial monitor.
Can anybody please give some hints where the sketch is wrong? Searching the error in my sketch is impossible for me this way, with no feedback from the Arduino.
Connected to the Arduino is a ENC28J60 Ethernet module, which i provided with his own 3V3 powersupply (because the onboard 3V3 from Arduino is not strong enough)
Findings:
- Sketch compiles without any errors/warnings
- Serial monitor is @ 9600 baud
- I can’t ping the Arduino
- If i type something in the serial monitor and press Enter, the RX led on the Arduino flickers (So serial connections is OK)
- An other sketch with serial output works (So Arduino is OK)
- An other sketch that utilises the ENC28J60 works (So Ethernet hardware is OK)
I think the initalization hangs somewhere. But i can’t figure out why. I just merged a few sketches together to get to this result.
My code: (for the people who prefer syntax marking, i have also put it on Pastebin: http://pastebin.com/DvdQZSmE )
#include <AltSoftSerial.h>
#include <SPI.h>
#include <UIPEthernet.h>
// AltSoftSerial always uses these pins:
//
// Board Transmit Receive PWM Unusable
// ----- -------- ------- ------------
// Teensy 2.0 9 10 (none)
// Teensy++ 2.0 25 4 26, 27
// Arduino Uno 9 8 10
// Arduino Mega 46 48 44, 45
// Wiring-S 5 6 4
// Sanguino 13 14 12
AltSoftSerial altSerial;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,4,7);
IPAddress server(192,168,4,4);
EthernetClient client;
const int requestPin = 4;
char input; // incoming serial data (byte)
bool readnextLine = false;
#define BUFSIZE 75
char buffer[BUFSIZE]; //Buffer for serial data to find \n .
int bufpos = 0;
long mEVLT = 0; //Meter reading Electrics - consumption low tariff
long mEVHT = 0; //Meter reading Electrics - consumption high tariff
long mETLT = 0; //Meter reading Electrics - generated electricity (Solar panels) low tariff
long mETHT = 0; //Meter reading Electrics - generated electricity (Solar panels) high tariff
long mEAV = 0; //Meter reading Electrics - Actual consumption
long mEAT = 0; //Meter reading Electrics - Actual generated electricity (Solar panels)
float mG = 0; //Meter reading Gas
void setup() {
Serial.begin(9600);
delay(1000);
altSerial.begin(9600);
delay(1000);
Ethernet.begin(mac, ip);
delay(1000);
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
//Set RTS pin high, so smart meter will start sending telegrams
pinMode(requestPin, OUTPUT);
digitalWrite(requestPin, HIGH);
}
void loop() {
long tl = 0;
long tld =0;
if (altSerial.available()) {
input = altSerial.read();
// --- 7 bits setting ---
input &= ~(1 << 7);
char inChar = (char)input;
// --- 7 bits setting ---
//Serial.print(input); //Debug
// Fill buffer up to and including a new line (\n)
buffer[bufpos] = input&127;
bufpos++;
if (input == '\n') { // We received a new line (data up to \n)
// 1-0:1.8.1 = Electricity consumption low tariff (DSMR v4.0)
if (sscanf(buffer,"1-0:1.8.1(%ld%.%ld%*s" , &tl, &tld) >0 ) {
mEVLT = tl * 1000 + tld;
if (mEVLT > 0) {
Serial.print(F("Elektra - meterstand verbruik LAAG tarief (Wh): "));
Serial.println(mEVLT);
//mEVLT = 0;
}
}
// 1-0:1.8.2 = Electricity consumption high tariff (DSMR v4.0)
if (sscanf(buffer,"1-0:1.8.2(%ld%.%ld%*s" , &tl, &tld) >0 ) {
mEVHT = tl * 1000 + tld;
if (mEVHT > 0) {
Serial.print(F("Elektra - meterstand verbruik HOOG tarief (Wh): "));
Serial.println(mEVHT);
//mEVHT = 0;
}
}
// 1-0:1.7.0 = Electricity consumption actual usage (DSMR v4.0)
if (sscanf(buffer,"1-0:1.7.0(%ld.%ld%*s" , &tl , &tld) >0 ) {
mEAV = tl * 1000 + tld * 10;
if (mEAV > 0) {
Serial.print(F("Elektra - actueel verbruik (W): "));
Serial.println(mEAV);
//mEAV = 0;
}
}
// 1-0:2.8.1 = Generated electricity low tariff (DSMR v4.0)
if (sscanf(buffer,"1-0:2.8.1(%ld%.%ld%*s" , &tl, &tld) >0 ) {
mETLT = tl * 1000 + tld;
if (mETLT > 0) {
Serial.print(F("Elektra - meterstand teruglevering LAAG tarief (Wh): "));
Serial.println(mETLT);
//mETLT = 0;
}
}
// 1-0:2.8.2 = Generated electricity high tariff (DSMR v4.0)
if (sscanf(buffer,"1-0:2.8.2(%ld%.%ld%*s" , &tl, &tld) >0 ) {
mETHT = tl * 1000 + tld;
if (mETHT > 0) {
Serial.print(F("Elektra - meterstand teruglevering HOOG tarief (Wh): "));
Serial.println(mETHT);
//mETHT = 0;
}
}
// 1-0:2.7.0 = Actual generated electricity (DSMR v4.0)
if (sscanf(buffer,"1-0:2.7.0(%ld.%ld%*s" , &tl , &tld) >0 ) {
mEAT = tl * 1000 + tld * 10;
if (mEAT > 0) {
Serial.print(F("Elektra - actueel teruglevering (W): "));
Serial.println(mEAT);
//mEAT = 0;
}
}
// 0-1:24.3.0 = Gas (DSMR v4.0)
if (sscanf(buffer,"0-1:24.3.0(%6ld%4ld%*s" , &tl, &tld) > 0 ) {
readnextLine = true; // we have to go to the next line
}
if (readnextLine){
if (sscanf(buffer,"(%ld.%ld%*s" , &tl, &tld) >0 ) {
mG = float ( tl * 1000 + tld ) / 1000;
Serial.print(F("Gas - meterstand (m3): "));
Serial.println(mG);
Serial.println("");
readnextLine = false;
}
}
httpRequest();
// Empty buffer again (whole array)
for (int i=0; i<75; i++)
{ buffer[i] = 0;}
bufpos = 0;
mEVLT = 0;
mEVHT = 0;
mEAV = 0;
mETLT = 0;
mETHT = 0;
mEAT = 0;
delay(1000);
client.stop();
}
}
}
void httpRequest() {
// if there's a successful connection:
if (client.connect(server, 80)) {
client.print("GET /www/slimmemeter/p1.php?mEVLT=");
client.print(mEVLT);
client.print("&mEVHT=");
client.print(mEVHT);
client.print("&mEAV=");
client.print(mEAV);
client.print("&mG=");
client.print(mG);
client.println(" HTTP/1.1");
client.println("Host: 192.168.4.4");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
Serial.println("Connection OK!");
Serial.print("GET /www/slimmemeter/p1.php?mEVLT=");
Serial.print(mEVLT);
Serial.print("&mEVHT=");
Serial.print(mEVHT);
Serial.print("&mEAV=");
Serial.print(mEAV);
Serial.print("&mG=");
Serial.print(mG);
Serial.println(" HTTP/1.1");
Serial.println("Host: 192.168.4.4");
Serial.println("User-Agent: arduino-ethernet");
Serial.println("Connection: close");
Serial.println();
}
else {
Serial.println("connection failed");
client.stop();
}
}