How to use WebIOPi in existing website - php

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!

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.

Related

running C exec in php background

I have searched and found some suggestions to my question but almost all of them are executing php files, so i don't know if that has something to do with it not working for me.
My goal is for my webpage to load completely without finishing my script that takes x amount of time, but it wont do it with this line of code. Is there something im missing? i have seen this answer in many places and it seems to work for them.
<?php
exec("sudo ./EscalonVel 50 2 100 10 20 &> /dev/null &");
echo "Hello";
?>
If you has to show on the page data calculated on the C script, then "someone" has to wait until it's finished to show it out. If the data comes directly from the command executed (output/stdout of the execution), then you cannot background the command with &: the output might come after request is dispatched, and process backgrounding disconnects it's output from actual execution. So you have 2 options:
Show the page "template" completely and prepare it to accept the contents at the very end of your HTML (suboptimal but possible)
Do a 2 request page: the first shows the template (full page) and the second (AJAX) fills it up with data from command execution. Depending on how you write it, the AJAX can do several request until the command is terminated, which is preferable if the script runs for more than 20 seconds. Then you'll need some kind of process checking and some backend to save (by command) and view (by AJAX request) the data, as a file or database.
Hope it helps!

Execute PHP script server side

Yesterday I created a script which is working fine, but only with an opened Web Browser which isn't that what I wanted. What I want is that the script runs all the time, even with closed Web Browser.
Could not upload a Picture, so its a short sketch:
(lookup.php) -> pass var data1 -> (run_code.php) -> pass var data1 ->
(check.php) = {{refreshes every 5 seconds till var data2 exists in
MySQl.}} -> goto -> lookup.php.....
The only problem is that I have no idea how to send a value from one .php file to another without GET,POST,COOKIE or SESSION. Session would be the best option but "lookup.php" is looking for a value, and if I start session I get the error "header is already set".
So, how should I pass these value from "lookup.php" to "run_code.php"?
Second problem is, "check.php" is a code that checks if value exists in mysql. This code refreshed and executes itself after 5 seconds using META REFRESH. But also this is not working without a browser.
How should I write the script that the script executes itself after a time?
If i understand you want to write a script (and choose php probably because your familiar with its syntax) and you need it to run every X minutes.
Problem is that when you write a web site with PHP you can pass information using HTTP methods (get/post) and using the session
when running a script on a machine you don't have the HTTP traffic and no session (this can be simulated but its a bit complicated)
my suggestion is :
1) combine all the php files into 1 long php file that will be running (this way you can work with variables in the script with no problem) - you can copy past your code into 1 php file and you can include the needed files in your script so you can stile keep the original files
2) add a cron job (if its a Linux system) - http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/ - this way your script with run every X time (your choice)

How can I create a progress-bar using PHP and JavaScript?

I am using PHP and JavaScript in my application.
I want to delete entries from the database, a process which requires a significant amount of time, thus I would like to report the progress to the end-user.
I'd like to know how to achieve this, could anyone explain the theory to me?
You need 2 scripts:
First for delete
Second for status
First make a request to the first script, and then make a second request to the second script that will report you a progress.
Then you can use any of the jQuery plugin to display the progress bar.

How to add a line of text to a text file every thursday with PHP?

I have a textfile with a list of the newest minecraft snapshots which then gets displayed on my personel site, the problem is that I have to manually add the snapshot name every thursday and I cant find any way to automate this process.
The text file is laid out like this:
12w26a;
12w25a;
12w24a;
'12' stands for the current year, 'w' just stands for week, '26' indicated what number snapshot it is and 'a' isn't really important but has to be there.
I found some PHP online to automate the process but apparently it no longer works
http://pastebin.com/LP3WKCiZ
Any help would be much appreciated. :)
EDIT:
Here is a live demo of the above code (not sure why its not working since Mojang have gone back to just using jar files again)
http://langkid1.me/pre/
Create a php page that reads the text file contents and then checks whether another snapshot exists via the file_exists() function, and if it does it should update the text file that maintains the list.
Now create a script that contains a curl command to the php page.
#!/bin/sh
curl http://localhost/update.php
And finally you just need to set up a cron job to run the script every Thursday.
0 0 * * 4 sh /path/script.sh
Hope this helps. :)

PHP - Ncurses - Get current line (cursor position)

I'm currently working on a linux shell app (batch tool) using php+ncurses. I'm trying to create a loadingbar thingy but I can't get the function "ncurses_getyx" working. It doesn't return anything. I've tried using the arguments as pointers and tried as returning values, but none seem to work.
Does anyone know how I can retreive the current cursor position in the terminal window?
I'm using PuTTY on Windows 7 to connect to my server.
Thanks in advance.
I think you put the variables you want to equal the y and x values as arguments in the function, and it modifies them to equal the correct values.

Categories