1

I'm trying to use WebIOPi but I'm quite lost in getting it to work with my project.

Background: I'm using Raspberry Pi B+ running Wheezy. I'm working on a web-based application that will only be accessed locally. I have a bunch of php files in /var/www that run on Apache. Now I need to get my coin acceptor to with the project. The coin acceptor http://www.adafruit.com/products/787 sends single pulses (I only need one coin). I first tried the coin acceptor with a python script using interrupts and it works fine.

GPIO.setup(PIN_COIN_INTERRUPT,GPIO.IN)
GPIO.add_event_detect(PIN_COIN_INTERRUPT,GPIO.FALLING,callback=coinEventHandler)

But now I need to be able to capture those pulses and show them on a php page, updating the amount for every coin insert. I've been studying WebIOPi for hours but I can only find info on reading a pin's status, not listening for interrupts. Can anybody point me to the right direction?

Any help would be greatly appreciated. Thank you!

Yvette
  • 13
  • 4

1 Answers1

0

So, you seem to have two problems: 1. how do I, on the server, detect a new coin event 2. how do I then push this to the client browser.

I don't know webiopi at all, so I can't say there's not a way to use that to solve both, but as an alternative:

For part 1: you have a python program which you said works; I would suggest running as a background service and just have it do something simple like writing the latest value of coinage to a file:

GPIO.setup(PIN_COIN_INTERRUPT,GPIO.IN)
GPIO.add_event_detect(PIN_COIN_INTERRUPT,GPIO.FALLING,callback=coinEventHandler)
def coinEvenHandler(*arguments):
 try:
   f = open("coin.txt","rt")
   cnt = int(f.read())
   f.close()
 except: # handle file doesn't exist and file doesn't contain an int
   cnt = 0
 f = open("coin.txt","wt")
 f.write(str(cnt))
 f.close()

For part 2: 1. Create a page which returns the value of "coin.txt" 2. Use Ajax (e.g. jquery) to poll for this value from your client page.

Foon
  • 6,148
  • 11
  • 40
  • 42
  • Is it possible to read the latest value of the coin.txt with Ajax? Sorry I have little experience with Ajax. I have to make sure each time a person inserts a coin, the client page immediately shows the updated value. I'll try this out as soon as I get home. Thank you! – Yvette Mar 01 '15 at 22:41
  • It is possible; however, by default, you're going to need to poll from the client (and so there will be a delay equal to about your polling interval; that's the nature of the web/http without using websockets or some other workaround. (With websockets, it'd be possible to avoid polling, but that will require rather a bit more learning curve). Note that if you put coin.txt in an html directory, you can just directly access it (assuming permissions are set appropriately) – Foon Mar 02 '15 at 01:15
  • Hi Foon, I was able to write the coins count to the text file for every coin inserted. Thank you! Now I tried to read the text from my php page: And it works but only for one time reading. I tried looping it and it stopped working. I realized the php won't stop looping and my page won't finish loading. Would you happen to have any example of using Ajax to read the file in a loop? Thank you so much for your help! – Yvette Mar 02 '15 at 09:50
  • http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery (I'd suggest starting with their 1st example to start, assuming your browser is either running on the RPi or at least on a system that is on the same LAN as the RPi) – Foon Mar 02 '15 at 13:57