ich habe gerade die Anbindung S0 Stromzähler durch Raspberry Pi mit der WiringPi Bibliothek per Interrupt realisiert, so daß der aktuelle Stromverbrauch ermittelt wird. Da Spannung des S0+ Impulses unbekannt, Zener Diode dazugeschaltet.
Hier der Code (sorry for the delay, nachwuchsbedingt...):
// gcc -Wall -lwiringPi stromzaehler.c -o stromzaehler
//
// ohne Verzögerung Ergebnisse wegschreiben:
// stdbuf -oL -eL ./stromzaehler >> ./stromzaehler.txt
//
// test mit gpio Pin Manipulation moeglich auch ohne S0 Stromzähler
//
// verwendete Pins:
// wiringPi Pin 1 ist gleich 12 physikalisch fuer S0+
// 06 physikalisch fuer S0-
//
// gpio -v
// Raspberry Pi Details altes Modell
// Type: Model B, Revision: 2, Memory: 512MB, Maker: Sony
// gpio readall
// +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
// | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
// +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
// | | | 3.3v | | | 1 || 2 | | | 5v | | |
// | 2 | 8 | SDA.1 | ALT0 | 1 | 3 || 4 | | | 5V | | |
// | 3 | 9 | SCL.1 | ALT0 | 1 | 5 || 6 | | | 0v | | |
// | 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT0 | TxD | 15 | 14 |
// | | | 0v | | | 9 || 10 | 1 | ALT0 | RxD | 16 | 15 |
// | 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 |
// | 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
// | 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
// | | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 |
// | 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | |
// | 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 |
// | 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 |
// | | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 |
// +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
// | 28 | 17 | GPIO.17 | IN | 0 | 51 || 52 | 0 | IN | GPIO.18 | 18 | 29 |
// | 30 | 19 | GPIO.19 | IN | 0 | 53 || 54 | 0 | IN | GPIO.20 | 20 | 31 |
// +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
//
// gpio -v
// Raspberry Pi Details neues Modell
// Type: Model 2, Revision: 1.1, Memory: 1024MB, Maker: Sony
// gpio readall
// +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+
// | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
// +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
// | | | 3.3v | | | 1 || 2 | | | 5v | | |
// | 2 | 8 | SDA.1 | ALT0 | 1 | 3 || 4 | | | 5V | | |
// | 3 | 9 | SCL.1 | ALT0 | 1 | 5 || 6 | | | 0v | | |
// | 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT0 | TxD | 15 | 14 |
// | | | 0v | | | 9 || 10 | 1 | ALT0 | RxD | 16 | 15 |
// | 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 1 | IN | GPIO. 1 | 1 | 18 |
// | 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
// | 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
// | | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 |
// | 10 | 12 | MOSI | ALT0 | 0 | 19 || 20 | | | 0v | | |
// | 9 | 13 | MISO | ALT0 | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 |
// | 11 | 14 | SCLK | ALT0 | 0 | 23 || 24 | 1 | ALT0 | CE0 | 10 | 8 |
// | | | 0v | | | 25 || 26 | 1 | ALT0 | CE1 | 11 | 7 |
// | 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 |
// | 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | |
// | 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 |
// | 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | |
// | 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 |
// | 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 |
// | | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 |
// +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <time.h>
// What GPIO input are we using? (wiringPi pin number)
#define BUTTON_PIN 1
// analog http://93.93.130.214/forums/viewtopi...10857&p=121284
char TimeString[128];
struct tm *timeinfo;
char strResponse [128];
unsigned int globalCounter = 0; // zaehlt alle interrupts
unsigned int localCounter = 0; // zaehlt alle berechneten Messungen
unsigned int mytime_last = 0;
unsigned int mytime_delta = 0;
unsigned int time_isr_call = 0;
unsigned int time_isr_call_delta = 0;
unsigned int time_isr_call_last = 0;
double watt = 0;
void myInterrupt (void) {
++globalCounter;
time_isr_call = millis ();
time_isr_call_delta = time_isr_call - time_isr_call_last;
time_isr_call_last = time_isr_call;
// Watt Berechnung nur falls "echter" Interrupt ("Prellen" vermeiden)
// Impulsdauer angeblich 30ms, also sind 40ms OK (entsprechen 45kW)...
// => ab 45kW steigt die Software aus, das sollte reichen...
if ( time_isr_call_delta > 40 ) {
++localCounter; // zaehlt nur 'echte' Messungen
// aktuelle Zeit holen & formatieren
time_t rawtime;
rawtime = time (NULL);
timeinfo = localtime(&rawtime);
// Format siehe http://www.cplusplus.com/reference/ctime/strftime/
strftime(strResponse,128,"%Y%m%d-%H%M%S",timeinfo);
// Leistung in Watt ausrechnen:
// 2000 Impulse je kWh
// kW = 1000W
// h = 3600s
// mytime_delta ist in ms, daher Faktor 1000
mytime_delta = time_isr_call - mytime_last;
watt = (1000 * 3600 / (double) mytime_delta / 2);
mytime_last = time_isr_call;
// ersten Wert nicht ausspucken da mytime_delta erst ab der zweiten Berechnung korrekt
if ( localCounter > 1 ) {
printf ("%s ", strResponse);
printf ("%07.1f\n", watt);
}
}
}
int main (void) {
if (wiringPiSetup () < 0) {
fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno));
return 1;
}
if (wiringPiISR (BUTTON_PIN, INT_EDGE_RISING, &myInterrupt) < 0) {
fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
return 1;
}
// Pin konfigurieren
pinMode(BUTTON_PIN, INPUT);
pullUpDnControl(BUTTON_PIN, PUD_UP);
for ( ; ; ) {
delay (100000);
}
return 0;
}
Hier der Code (sorry for the delay, nachwuchsbedingt...):
// gcc -Wall -lwiringPi stromzaehler.c -o stromzaehler
//
// ohne Verzögerung Ergebnisse wegschreiben:
// stdbuf -oL -eL ./stromzaehler >> ./stromzaehler.txt
//
// test mit gpio Pin Manipulation moeglich auch ohne S0 Stromzähler
//
// verwendete Pins:
// wiringPi Pin 1 ist gleich 12 physikalisch fuer S0+
// 06 physikalisch fuer S0-
//
// gpio -v
// Raspberry Pi Details altes Modell
// Type: Model B, Revision: 2, Memory: 512MB, Maker: Sony
// gpio readall
// +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
// | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
// +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
// | | | 3.3v | | | 1 || 2 | | | 5v | | |
// | 2 | 8 | SDA.1 | ALT0 | 1 | 3 || 4 | | | 5V | | |
// | 3 | 9 | SCL.1 | ALT0 | 1 | 5 || 6 | | | 0v | | |
// | 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT0 | TxD | 15 | 14 |
// | | | 0v | | | 9 || 10 | 1 | ALT0 | RxD | 16 | 15 |
// | 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 |
// | 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
// | 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
// | | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 |
// | 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | |
// | 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 |
// | 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 |
// | | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 |
// +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
// | 28 | 17 | GPIO.17 | IN | 0 | 51 || 52 | 0 | IN | GPIO.18 | 18 | 29 |
// | 30 | 19 | GPIO.19 | IN | 0 | 53 || 54 | 0 | IN | GPIO.20 | 20 | 31 |
// +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
//
// gpio -v
// Raspberry Pi Details neues Modell
// Type: Model 2, Revision: 1.1, Memory: 1024MB, Maker: Sony
// gpio readall
// +-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+
// | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
// +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
// | | | 3.3v | | | 1 || 2 | | | 5v | | |
// | 2 | 8 | SDA.1 | ALT0 | 1 | 3 || 4 | | | 5V | | |
// | 3 | 9 | SCL.1 | ALT0 | 1 | 5 || 6 | | | 0v | | |
// | 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT0 | TxD | 15 | 14 |
// | | | 0v | | | 9 || 10 | 1 | ALT0 | RxD | 16 | 15 |
// | 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 1 | IN | GPIO. 1 | 1 | 18 |
// | 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
// | 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
// | | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 |
// | 10 | 12 | MOSI | ALT0 | 0 | 19 || 20 | | | 0v | | |
// | 9 | 13 | MISO | ALT0 | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 |
// | 11 | 14 | SCLK | ALT0 | 0 | 23 || 24 | 1 | ALT0 | CE0 | 10 | 8 |
// | | | 0v | | | 25 || 26 | 1 | ALT0 | CE1 | 11 | 7 |
// | 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 |
// | 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | |
// | 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 |
// | 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | |
// | 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 |
// | 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 |
// | | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 |
// +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <time.h>
// What GPIO input are we using? (wiringPi pin number)
#define BUTTON_PIN 1
// analog http://93.93.130.214/forums/viewtopi...10857&p=121284
char TimeString[128];
struct tm *timeinfo;
char strResponse [128];
unsigned int globalCounter = 0; // zaehlt alle interrupts
unsigned int localCounter = 0; // zaehlt alle berechneten Messungen
unsigned int mytime_last = 0;
unsigned int mytime_delta = 0;
unsigned int time_isr_call = 0;
unsigned int time_isr_call_delta = 0;
unsigned int time_isr_call_last = 0;
double watt = 0;
void myInterrupt (void) {
++globalCounter;
time_isr_call = millis ();
time_isr_call_delta = time_isr_call - time_isr_call_last;
time_isr_call_last = time_isr_call;
// Watt Berechnung nur falls "echter" Interrupt ("Prellen" vermeiden)
// Impulsdauer angeblich 30ms, also sind 40ms OK (entsprechen 45kW)...
// => ab 45kW steigt die Software aus, das sollte reichen...
if ( time_isr_call_delta > 40 ) {
++localCounter; // zaehlt nur 'echte' Messungen
// aktuelle Zeit holen & formatieren
time_t rawtime;
rawtime = time (NULL);
timeinfo = localtime(&rawtime);
// Format siehe http://www.cplusplus.com/reference/ctime/strftime/
strftime(strResponse,128,"%Y%m%d-%H%M%S",timeinfo);
// Leistung in Watt ausrechnen:
// 2000 Impulse je kWh
// kW = 1000W
// h = 3600s
// mytime_delta ist in ms, daher Faktor 1000
mytime_delta = time_isr_call - mytime_last;
watt = (1000 * 3600 / (double) mytime_delta / 2);
mytime_last = time_isr_call;
// ersten Wert nicht ausspucken da mytime_delta erst ab der zweiten Berechnung korrekt
if ( localCounter > 1 ) {
printf ("%s ", strResponse);
printf ("%07.1f\n", watt);
}
}
}
int main (void) {
if (wiringPiSetup () < 0) {
fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno));
return 1;
}
if (wiringPiISR (BUTTON_PIN, INT_EDGE_RISING, &myInterrupt) < 0) {
fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
return 1;
}
// Pin konfigurieren
pinMode(BUTTON_PIN, INPUT);
pullUpDnControl(BUTTON_PIN, PUD_UP);
for ( ; ; ) {
delay (100000);
}
return 0;
}
Kommentar