Tylon

stromzaehler.c

Feb 23rd, 2022 (edited)
973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1. /*
  2.  * isr.c:
  3.  *  Wait for Interrupt test program - ISR method
  4.  *
  5.  *  How to test:
  6.  *    Use the SoC's pull-up and pull down resistors that are avalable
  7.  *  on input pins. So compile & run this program (via sudo), then
  8.  *  in another terminal:
  9.  *      gpio mode 0 up
  10.  *      gpio mode 0 down
  11.  *  at which point it should trigger an interrupt. Toggle the pin
  12.  *  up/down to generate more interrupts to test.
  13.  *
  14.  * Copyright (c) 2013 Gordon Henderson.
  15.  ***********************************************************************
  16.  * This file is part of wiringPi:
  17.  *  https://projects.drogon.net/raspberry-pi/wiringpi/
  18.  *
  19.  *    wiringPi is free software: you can redistribute it and/or modify
  20.  *    it under the terms of the GNU Lesser General Public License as published by
  21.  *    the Free Software Foundation, either version 3 of the License, or
  22.  *    (at your option) any later version.
  23.  *
  24.  *    wiringPi is distributed in the hope that it will be useful,
  25.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  26.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27.  *    GNU Lesser General Public License for more details.
  28.  *
  29.  *    You should have received a copy of the GNU Lesser General Public License
  30.  *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
  31.  ***********************************************************************
  32.  */
  33.  
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <errno.h>
  37. #include <stdlib.h>
  38. #include <wiringPi.h>
  39.  
  40.  
  41. // What GPIO input are we using?
  42. //  This is a wiringPi pin number
  43.  
  44. #define BUTTON_PIN  1
  45.  
  46. // globalCounter:
  47. //  Global variable to count interrupts
  48. //  Should be declared volatile to make sure the compiler doesn't cache it.
  49.  
  50. static volatile int globalCounter = 0 ;
  51.  
  52.  
  53. /*
  54.  * myInterrupt:
  55.  *********************************************************************************
  56.  */
  57.  
  58. void myInterrupt (void)
  59. {
  60.   ++globalCounter ;
  61. }
  62.  
  63.  
  64. /*
  65.  *********************************************************************************
  66.  * main
  67.  *********************************************************************************
  68.  */
  69.  
  70. int main (void)
  71. {
  72.   int myCounter = 0 ;
  73.   FILE *datei;
  74.  
  75.   if (wiringPiSetup () < 0)
  76.   {
  77.     fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ;
  78.     return 1 ;
  79.   }
  80.  
  81.   if (wiringPiISR (BUTTON_PIN, INT_EDGE_FALLING, &myInterrupt) < 0)
  82.   {
  83.     fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ;
  84.     return 1 ;
  85.   }
  86.  
  87.  
  88.   for (;;)
  89.   {
  90. //    printf ("Waiting ... ") ; fflush (stdout) ;
  91.  
  92.     while (myCounter == globalCounter)
  93.       delay (100) ;
  94.  
  95. //    printf (" Done. counter: %5d\n", globalCounter) ;
  96.     datei = fopen ("/var/strom/stromcounter", "w");
  97.     fprintf (datei, "%d\n", globalCounter);
  98.     fclose (datei);
  99.     popen("curl http://192.168.178.22:81/middleware.php/data/8d98dde0-936c-11ec-9ecb-XXXXXX.json?operation=add&value=1","w");
  100.     myCounter = globalCounter ;
  101.   }
  102.  
  103.   return 0 ;
  104. }
  105.  
Add Comment
Please, Sign In to add comment