Hi all,
I have read various tutorials and forum post bust I can't get this to work. What I would like to to is pass on values that I will eventually get from serial input on the arduino side to be processed by a php-file on the linux side.
- I know for a fact that my php-file is working.
- For simplicity I have commented out the whole section that should get the values out of the serial input and just used constant values (1-7). Can anybody tell me where I go wrong? Thanks!
/* Arduino 'slimme meter' P1-port reader.
This sketch reads data from a Dutch smart meter that is equipped with a P1-port.
Connect 'RTS' from meter to Arduino pin 5
Connect 'GND' from meter to Arduino GND
Connect 'RxD' from meter to Arduino pin 0 (RX)
Baudrate 115200, 8N1.
BS170 transistor & 10k resistor is needed to make data readable if meter spits out inverted data
A .php file is requested (with consumption numbers in the GET request) every minute (interval set at line #52)
created by 'ThinkPad' @ Tweakers.net, september 2014
http://gathering.tweakers.net/forum/list_messages/1601301
*/
#include <AltSoftSerial.h>
#include <SPI.h>
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.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
// Yun (Leonardo) 5 13 (none)
//IPAddress server(127,0,0,1);
YunServer server;
const int requestPin = 3;
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 mEGLT = 1; //Meter reading Electrics - consumption low tariff
long mEGHT = 2; //Meter reading Electrics - consumption high tariff
long mELLT = 3; //Meter reading Electrics - production low tariff
long mELHT = 4; //Meter reading Electrics - production high tariff
long mEGAC = 5; //Meter reading Electrics - Actual consumption
long mELAP = 6; //Meter reading Electrics - Actual production
long mG = 7; //Meter reading Gas
long lastTime = 0; // will store last time
long interval = 60000; // interval at which to blink (milliseconds)
void setup() {
Serial.begin(115200);
delay(1000);
Bridge.begin();
pinMode(4, OUTPUT); // SD select pin
digitalWrite(4, HIGH); // Explicitly disable SD
server.listenOnLocalHost();
server.begin();
delay(1000);
//Set RTS pin high, so smart meter will start sending telegrams
pinMode(requestPin, OUTPUT);
digitalWrite(requestPin, HIGH);
}
void loop() {
YunClient client = server.accept();
// decodeTelegram();
if(millis() - lastTime > interval) {
lastTime = millis();
//send data to PHP/MySQL
httpRequest(client);
/* //Reset variables to zero for next run
mEGLT = 0;
mEGHT = 0;
mELLT = 0;
mELHT = 0;
mEGAC = 0;
mELAP = 0;
mG = 0; */
//Stop client
client.stop();
}
} //Einde loop
/*void decodeTelegram() {
long tl = 0;
long tld = 0;
if (Serial.available()) {
input = Serial.read();
char inChar = (char)input;
// 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) => levering hoog tarief
if (sscanf(buffer,"1-0:1.8.1(%ld.%ld" ,&tl, &tld)==2){
tl *= 1000;
tl += tld;
mEGLT = tl;
}
// 1-0:1.8.2 = Elektra verbruik hoog tarief (DSMR v4.0)
if (sscanf(buffer,"1-0:1.8.2(%ld.%ld" ,&tl, &tld)==2){
tl *= 1000;
tl += tld;
mEGHT = tl;
}
//1-0:2.8.1 = Elektra levering laag tarief (DSMR v4.0)
if (sscanf(buffer,"1-0:2.8.1(%ld.%ld" ,&tl, &tld)==2){
tl *= 1000;
tl += tld;
mELLT = tl;
}
//1-0:2.8.2 = Elektra levering hoog tarief (DSMR v4.0)
if (sscanf(buffer,"1-0:2.8.1(%ld.%ld" ,&tl, &tld)==2){
tl *=1000;
tl += tld;
mELHT = tl;
}
// 1-0:1.7.0 = Electricity consumption actual usage (DSMR v4.0)
if (sscanf(buffer,"1-0:1.7.0(%ld.%ld" ,&tl , &tld) == 2){
mEGAC = (tl*1000)+tld;
}
// Hier moet nog Elektra huidige levering (DSMR v4.0)
if (sscanf(buffer,"1-0:2.7.0(%ld.%ld" ,&tl , &tld) ==2){
mELAP = (tl*1000)+tld;
}
// 0-1:24.2.1 = Gas (DSMR v4.0) on Kaifa MA105 meter
if (strncmp(buffer, "0-1:24.2.1", strlen("0-1:24.2.1")) == 0) {
if (sscanf(strrchr(buffer, '(') + 1, "%d.%d", &tl, &tld) == 2) {
mG = (tl*1000)+tld;
}
}
// Empty buffer again (whole array)
for (int i=0; i<75; i++)
{
buffer[i] = 0;
}
bufpos = 0;
}
} //Einde 'if AltSerial.available'
} //Einde 'decodeTelegram()' functie*/
void httpRequest(YunClient client) {
// if there's a successful connection:
if (client) {
client.print("GET /www/elekgas/p1.php?mEGLT=");
client.print(mEGLT);
client.print("&mEGHT=");
client.print(mEGHT);
client.print("&mELLT=");
client.print(mELLT);
client.print("&mELHT=");
client.print(mELHT);
client.print("&mEGAC=");
client.print(mEGAC);
client.print("&mELAP=");
client.print(mELAP);
client.print("&mG=");
client.print(mG);
client.println(" HTTP/1.1");
client.println("Host: 127.0.0.1/srv/mysql/elekgas");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
//Request complete; empty recieve buffer
while (client.available()) { //data available
char c = client.read(); //gets byte from ethernet buffer
}
client.println();
}
else {
client.stop();
}
}
This is a hack on the code produced by 'ThinkPad' as said in the basic info at top of the code. He got it working, just his server was placed somewhere else so I tried to alter it that it would just use the bridge to reach the Linux-side and not go via the network (although that didn't work either on my Yun).