Hallo Forum,
ich möchte mit dem Raspberry meine Zähler mit S0-Bus auswerten, bzw. die Impulse zählen und die Zählerstände per Webseite verfügbar machen um diese mit IPS dann auszuwerten.
Hierzu habe ich mir 2 fertige Projekte ausgesucht:
Lib für die GPIO-Pins: https://projects.drogon.net/raspberry-pi/wiringpi/
das Beispiel isr.c konnte ich erweitern, dass 2 Pins gezählt werden und das Ergebnis in eine Datei geschrieben wird.
kleiner Webserver: http://konteck.github.com/wpp/
der Webserver liest nach einem HTTP-Request die Datei ein und gibt sie aus.
Das ganze könnte man optimieren, in dem man beide Programme kombiniert und sich dadurch die Schreib-/Lese-Operationen auf der SD-Karte spart, leider habe ich von C nicht wirklich einen Plan und bekomme das nicht hin. Vielleicht kann mir hier jemand auf die Sprünge helfen …
Vielleicht ist das ja auch für den einen oder anderen hier interessant
Danke für Eure Hilfe !!
Die Quellcodes:
Zähler:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>
// What GPIO input are we using?
// This is a wiringPi pin number
#define BUTTON_PIN 0
#define BUTTON_PIN1 1
// globalCounter:
// Global variable to count interrupts
// Should be declared volatile to make sure the compiler doesn't cache it.
static volatile int globalCounter = 0 ;
static volatile int globalCounter1 = 0 ;
/*
* myInterrupt:
*********************************************************************************
*/
void myInterrupt (void)
{
++globalCounter ;
}
void myInterrupt1 (void)
{
++globalCounter1 ;
}
/*
*********************************************************************************
* main
*********************************************************************************
*/
int main (void)
{
int myCounter = 0 ;
if (wiringPiSetup () < 0)
{
fprintf (stderr, "Unable to setup wiringPi: %s
", strerror (errno)) ;
return 1 ;
}
if (wiringPiISR (BUTTON_PIN, INT_EDGE_FALLING, &myInterrupt) < 0)
{
fprintf (stderr, "Unable to setup ISR: %s
", strerror (errno)) ;
return 1 ;
}
if (wiringPiISR (BUTTON_PIN1, INT_EDGE_FALLING, &myInterrupt1) < 0)
{
fprintf (stderr, "Unable to setup ISR: %s
", strerror (errno)) ;
return 1 ;
}
for (;;)
{
printf ("Waiting ... ") ; fflush (stdout) ;
while (myCounter == globalCounter+globalCounter1)
delay (100) ;
//printf (" Done. counter: %5d ** counter1: %5d
", globalCounter, globalCounter1) ;
FILE *fp;
fp = fopen("/tmp/counter.txt", "w");
fprintf(fp, "0:%d;1:%d
", globalCounter, globalCounter1);
fclose(fp);
myCounter = globalCounter+globalCounter1 ;
}
return 0 ;
}
Webserver:
#include <iostream>
#include <fstream>
#include <map>
#include "web++.hpp"
using namespace WPP;
static string Counter = "" ;
void counter(Request* req, Response* res) {
ifstream Quelldatei("/tmp/counter.txt");
getline (Quelldatei, Counter);
Quelldatei.close();
res->body << Counter;
}
int main(int argc, const char* argv[]) {
try {
std::cout << "Listening on port 80" << std::endl;
WPP::Server server;
server.get("/counter", &counter);
server.start(80);
} catch(WPP::Exception e) {
std::cerr << "WebServer: " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}