Web control of GPIO with auto control


36 posts   Page 1 of 2   1, 2
by abishur » Mon Feb 04, 2013 3:23 pm
The past week I've been having a lot of fun writing a program to control my sprinklers from a website. This is a working draft which means that there are some tweaks/clean-up I'd like to do but that it does work as it. The one short coming that I don't know how to fix involved the php file (download link is in bullet point list). I'd like for it to auto update an on/off image depending on the state of the gpio as reported by mySQL, but I don't know how to do that so it might take me a *long* time to learn how to. If someone wants to add this feature for me I certainly wouldn't mind. ;-)

Here's some rough instructions for what I did

You'll need the following:
• Raspberry pi (obviously)
• relays (I used Sainsmart 8 channel solid state relay. It's safe to drive it right off the gpio, writing a 1 turns on the relay, writing a 0 turns the relay off; that's not true for other Sainsmart relay boards I'm told)
• phpMyAdmin, mySQL, & apache (I basically used drcurzon's instructables for setting this up located here http://www.instructables.com/id/Raspberry-Pi-Web-Server/)
• You'll need to build the sql database with my .sql file located on gist here: https://gist.github.com/4707325
• Gordon's wiringPi (https://projects.drogon.net/raspberry-pi/wiringpi/)
• mySQL connector
Code: Select all
apt-get install libmysqlclient-dev libmysqlclient16

• the program and header files (I have them on gist for the moment https://gist.github.com/4707059)
• Php webpage (This is based heavily on drcurzon's script, I've added auto functions to it and changed the main page layout. Also on gist: https://gist.github.com/abishur/4958422

Basic Instructions:

1. Install phpMyAdmin, mySQL, and Apache

2. Install mySQL connector

3. Install Gordon's wiringPi

4. Download and configure sql with my gpio.sql file

5. Download control.php to /var/www/ and edit the username/password in the file to be able to connect to your mySQL database

6. Download sprinklers.cpp and associate header files (For me I made a directory /home/pi/scripts and saved the files there)

7. Change the #defines in sprinkler.cpp to what's true for you system and compile. using

Code: Select all
g++ sprinklers.cpp -I/usr/local/include -L/usr/local/lib -lwiringPi -lmysqlclient -I/home/pi/scripts


the last include probably isn't necessary as it should search the directory with sprinklers.cpp by default, but I don't like leaving things to chance

8. You'll now have a program called a.out. This program must be run as sudo for wiringPi to work. As long as this program is running you can control the GPIO pins from http:\\your_pi_IP_address/control.php

You'll be able to turn them on and off manually or configure automatic settings for them to turn on on:
Even numbered days
Odd numbered days
User defined days

All of them use a specific start time and a duration to be on (in minutes)

As I said above I'll try and clean this up and make a proper instructables of it this week when I have some time. Enjoy!
Dear forum: Play nice ;-)
User avatar
Moderator
Moderator
Posts: 4221
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
by abishur » Mon Feb 04, 2013 3:25 pm
Also here's some screen shots

Log in

Image

Here's the photo of the main page

Image

And here's the page I've added for setting auto commands

Image
Dear forum: Play nice ;-)
User avatar
Moderator
Moderator
Posts: 4221
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
by abishur » Tue Feb 05, 2013 4:41 am
Fixed the way it calculates which days to run
Also fixed the way it calculates if it's time to start and stop
Dear forum: Play nice ;-)
User avatar
Moderator
Moderator
Posts: 4221
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
by chickenjoe » Wed Feb 13, 2013 5:01 pm
Hey,
when I start the control.php in the browser it wont show th page, it says error 500.

Did someone have the same problem?

thanks
Posts: 1
Joined: Wed Feb 13, 2013 4:59 pm
by abishur » Wed Feb 13, 2013 7:13 pm
chickenjoe wrote:Hey,
when I start the control.php in the browser it wont show th page, it says error 500.

Did someone have the same problem?

thanks



obviously I haven't :lol:

My apologies for not putting together a better guide and basically point you to someone else's. I'll try and get that done by the weekend. Also, I've updated the code in a couple spots (specifically how it know to turn on/off the relay when the day changes), I'll try and update that as soon as I get home tonight.
Dear forum: Play nice ;-)
User avatar
Moderator
Moderator
Posts: 4221
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
by abishur » Fri Feb 15, 2013 3:51 am
Okay, I've updated the files on my gist. I went ahead an uploaded all of them fresh just for safety, but the only thing I actively remember changing was the compTimes function in sprinklers.cpp. I've finished my testing and this works quite well on all counts. Here's what I'm continuing to work on (in more or less order of importance to me, we'll see what order I actually do it in :lol: ) :

1) change the way the web side of it works so that it uses javascript.

I'm doing this for several reasons, but the main one is I've already put in programming for the pins to act as inputs, but haven't included any functionality for it. Once I work out how to dynamically update the page then I can add some functionality for it back into the web page. It's slow going as I don't do webpage programming, but I am making progress ;-)

2) Add functionality into the web side of things for GPIO inputs (change pin direction, add description)

3) Add max number of relays protection - Right now you can turn on all the relays at once. For a sprinkler system, this is not ideal. I need to add some code that lets you set how many relays can be on at once, and then checks the times to make sure there are no conflicts.

4) Make an instructable for it - This will be a complete redo from the start to make sure that it really does work the way I've described.

5) Clean up code - add more comments, get line indentations sorted out

6) Make the web end of it more aesthetically pleasing - This may ultimately be outside my abilities, but ideally this should look nice when run from a tablet or phone.
Dear forum: Play nice ;-)
User avatar
Moderator
Moderator
Posts: 4221
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
by joeman2116 » Wed Feb 27, 2013 3:42 am
abishur,

I have been following the Web control project and have the original coding a set and working fine on my Pi.

I see you have made some excellent functional add-ons and am very interested in using your upgrades.

I downloaded the control.php and changed the user and password. The page comes up but no control.

Would you steer me in the right direction to get your code working... I am close but obviously need to do something else

Thanks for your current works , looking forward to its development
Joe :)
Posts: 12
Joined: Mon Feb 25, 2013 4:27 pm
by joeman2116 » Wed Feb 27, 2013 4:39 am
I went thru your steps , all looked ok..

However, when i got to compiling in step7-8

root@raspberrypi://home/pi/scripts# sudo g++ sprinkler.cpp -I/usr/local/include -L/usr/local/lib -lwiringPi -lmysqlclient -I/home/pi/scripts


sprinkler.cpp: In function âint main()â:
sprinkler.cpp:96:88: error: expected primary-expression before â)â token
sprinkler.cpp:96:88: error: expected â;â before â)â token
sprinkler.cpp:287:1: error: expected â}â at end of input
sprinkler.cpp:287:1: error: expected â}â at end of input
sprinkler.cpp:287:1: error: expected âwhileâ at end of input
sprinkler.cpp:287:1: error: expected â(â at end of input
sprinkler.cpp:287:1: error: expected primary-expression at end of input
sprinkler.cpp:287:1: error: expected â)â at end of input
sprinkler.cpp:287:1: error: expected â;â at end of input
sprinkler.cpp:287:1: error: expected â}â at end of input
sprinkler.cpp:287:1: error: expected â}â at end of input
sprinkler.cpp:287:1: error: expected â}â at end of input
root@raspberrypi://home/pi/scripts# ls
my_sql_header.h sprinkler.cpp time_calcs.h

Any idea what i might be doing wrong?
Thanks

joe
Posts: 12
Joined: Mon Feb 25, 2013 4:27 pm
by abishur » Wed Feb 27, 2013 4:58 am
hmm... from the looks of things it's saying a semi-colon is missing. I've been adding some extra features that I hope finish with soon, but I'll check the code I've uploaded to see if I missed something when I copied it and get back to you.

The things I'm adding are:

Seasonal Adjust (sprinkler controller feature). Let's you set up a base time for GPIO to be on and then adds or subtracts a percentage of that duration. It's useful for sprinkler controllers (which is the main application for me)

Minimum Temperature (sprinkler controller feature). It checks the day's current low and disables the gpio from turning on if the daily low is colder than a user set minimum temperature. Requires internet.
Dear forum: Play nice ;-)
User avatar
Moderator
Moderator
Posts: 4221
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
by joeman2116 » Wed Feb 27, 2013 1:44 pm
Thanks for the quick reply,

I am looking forward to getting the compile working so I can try out your new addons.

I would like use it as part of a home control. The ability to manually or automatically have items turn / off makes it very multipurpose. I have been playing around adding some relays to the pi, so it would be great to try some of your timed functions..

Thanks,
Your efforts and contributions to expand on this project are much appreciated.

Joe
Posts: 12
Joined: Mon Feb 25, 2013 4:27 pm
by abishur » Wed Feb 27, 2013 4:30 pm
Okay I just took a quick look at the code where the error was and it was missing a parenthesis.


The line as it was read

Code: Select all
if ((compTimes(*theTime, channelTimeEnd[x]) != -1) || pinStart[x] == 0)){


It should be changed to

Code: Select all
if ((compTimes(*theTime, channelTimeEnd[x]) != -1) || (pinStart[x] == 0)){


The change is you need to add a ( in front of pinStart[x] == 0

It *should* compile after that, but if it missed one thing, I fear there may have been a second copy error :( (PS, I made the change on gist if you just want to re-copy the whole thing)

Like I said, I'll try to finish the next major revision here quickly. It's really just eye candy features, but I really want them! :lol:
Dear forum: Play nice ;-)
User avatar
Moderator
Moderator
Posts: 4221
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
by joeman2116 » Wed Feb 27, 2013 7:04 pm
I made the correction and it compiled with no errors.
I then ran sudo ./a.out
no errors
But no control - see the web page fine
Did a reboot
logged in as pi then cd to home/pi/scripts
I see the a.out file
I entered sudo ./a.out
no error indicated, cursor came back clean
I went to the webpage and logged in, can see the buttons etc
GPIO # GPIO Description Status Mode Action Manual Duration (0 = Infinite) Edit

But buttons say manual mode, and when i click nothing happens.
Same with turnon buttons
Edit buttons works
..

so close but not there

Joe
Posts: 12
Joined: Mon Feb 25, 2013 4:27 pm
by abishur » Wed Feb 27, 2013 8:12 pm
joeman2116 wrote:no error indicated, cursor came back clean



Sorry, it came back clean? If the program is working correctly, when you enter

Code: Select all
sudo ./a.out


It should just sit there quietly running the program. You shouldn't be able to enter anything into the bash prompt without doing a ctrl+c first.

If it returns you to the bash prompt it hit an error and exited the program. I'll update the code tonight with my latest. It will have the frame work for the features I mentioned, but they're not implemented yet.
Dear forum: Play nice ;-)
User avatar
Moderator
Moderator
Posts: 4221
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
by joeman2116 » Wed Feb 27, 2013 9:01 pm
Ok, will forward to your latest.

Looks like it must have crashed out as I can enter a command when a.out is entered


This is where i have my files:

The files needed for a compile:?
location /home/pi/scripts

a.out
my_sql_header.h
sprinkler.cpp
time_calcs.h
-------------------------------------

my files I have in var/www

control.php
index.php
on.jpg
off.jpg
gpio.sql - (which i imported to mysql)

Does it look like i am missing anything?

Thanks
Joe
Posts: 12
Joined: Mon Feb 25, 2013 4:27 pm
by abishur » Wed Feb 27, 2013 9:12 pm
No everything looks good :-)

Well everything except the part where my code isn't working for you :roll: Shesh, come on me!
Dear forum: Play nice ;-)
User avatar
Moderator
Moderator
Posts: 4221
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
by joeman2116 » Wed Feb 27, 2013 11:11 pm
I went over everything
And decided to delete the gpio database
and re-install a new one and then import your gpio.sql file
worked

I recompiled
now it compiles and the a.out when run is running the way it should
----------------------------
Now a new problem

When i try to log in it says incorrect login - never had this one before

I double checked the database and gpio is the user and has privileges
I checked index.php, control.php and sprinkler.cpp user name is gpio and pw match

here are my database users:

User Host Password Global privileges Grant Action
Any % -- USAGE No Edit Privileges Export
Any localhost No USAGE No Edit Privileges Export
Any raspberrypi No USAGE No Edit Privileges Export
debian-sys-maint localhost Yes ALL PRIVILEGES Yes Edit Privileges Export
gpio localhost Yes USAGE No Edit Privileges Export
phpmyadmin localhost Yes USAGE No Edit Privileges Export
root 127.0.0.1 Yes ALL PRIVILEGES Yes Edit Privileges Export
root ::1 Yes ALL PRIVILEGES Yes Edit Privileges Export
root localhost Yes ALL PRIVILEGES Yes Edit Privileges Export
root raspberrypi Yes ALL PRIVILEGES Yes Edit Privileges Export


cant seem to find the problem

Joe
Posts: 12
Joined: Mon Feb 25, 2013 4:27 pm
by joeman2116 » Thu Feb 28, 2013 12:46 am
UPDATE:

Everything appears ok now with the exception of not being able to log in

I double checked the sprinkler.cpp, control.php,index.php - and database gpio user,
user is gpio password is joebas
they all match

When i go to 192.168.1.161:
the login in appears
I try to login in with:
gpio
joebas


no go - incorrect login

I was looking at the gpio database and your tables
One is called users and has a user name of admin with a super long set of numbers for its password
could that be the issue?

grasping at straws...

joe
Posts: 12
Joined: Mon Feb 25, 2013 4:27 pm
by abishur » Thu Feb 28, 2013 3:08 am
joeman2116 wrote:UPDATE:

Everything appears ok now with the exception of not being able to log in

I double checked the sprinkler.cpp, control.php,index.php - and database gpio user,
user is gpio password is joebas
they all match

When i go to 192.168.1.161:
the login in appears
I try to login in with:
gpio
joebas


no go - incorrect login

I was looking at the gpio database and your tables
One is called users and has a user name of admin with a super long set of numbers for its password
could that be the issue?

grasping at straws...

joe


are you trying to log into phpmyadmin? It should be

user: root
password: whatever you entered when you installed mysql

You'll also need to put that information in sprinkler.cpp and control.php
Dear forum: Play nice ;-)
User avatar
Moderator
Moderator
Posts: 4221
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
by joeman2116 » Thu Feb 28, 2013 4:15 am
I can log in to phpmyadmin with root and pw joebas - no problem

I see the gpio database, tables have been import with your gpio file and user gpio has priviledges
--------------------------------
in the /var/www directory:

I have edited 3 files index.php,control.php,sprinkler.cpp - and put "root" as user and pw as joebas
------------------------------------

I run sudo ./a.out - looks good
----------------------------------------------------
Problem area:

I go to my webpage http://192.168.1.161/index.php
The login screen appears
I enter root for id and joebas for pw
but no matter what i enter it does not let me go any further
url shows - (http://192.168.1.161/control.php?error=incorrectLogin)

As an experiment, i took another sd card and did a 100 percent rebuild - but still getting same results
I cannot get past the login screen

got me stumped
Thanks for your help..
Joe
Posts: 12
Joined: Mon Feb 25, 2013 4:27 pm
by abishur » Thu Feb 28, 2013 1:21 pm
hmm... I suspect I know what's wrong and it's my fault :lol: (sorry!) Let me test something and I'll hopefully have the fix for you
Dear forum: Play nice ;-)
User avatar
Moderator
Moderator
Posts: 4221
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
by joeman2116 » Thu Feb 28, 2013 3:23 pm
No problem, looking forward to a solution

On the bright side I am getting lots of practice installing etc...

Joe
Posts: 12
Joined: Mon Feb 25, 2013 4:27 pm
by abishur » Fri Mar 01, 2013 1:33 am
I updated the gpio.sql script on gist. Once you run it it will let you log in as

username: admin
password: admin

After you log in there's a link that will let you change the password. Sorry about that!
Dear forum: Play nice ;-)
User avatar
Moderator
Moderator
Posts: 4221
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
by joeman2116 » Fri Mar 01, 2013 4:30 am
Yeah!
You did it.....Good job....

I Deleted old database and imported the new gpio.sql

Logged in perfectly!! with admin admin
Changed pw - worked

Just finished building an interface and put four relays on it

------------------------------------------------------
Testing interface Results:

1. It takes several clicks to turn relay on /off
2. Lots times led will be off - yet relay is on
3. Also led will be on but relay is off.
4. Sometimes when i turn 1 item off it shuts all off

All testing done in manual mode 0 = duration
Something not quite right.
--------------------------------------------------
As a side test:

I tried the same commands on the original Web gpio and all the commands worked perfect
1 click and led goes on or off as well as applicable relay
no other button interacts with the other

All testing done i manual mode 0 duration

Thanks for your work..
Joe
Posts: 12
Joined: Mon Feb 25, 2013 4:27 pm
by abishur » Fri Mar 01, 2013 2:46 pm
Yeah, the changes I've made mess with the timing of the web page just enough to have the relay display incorrectly on the screen (the LED is always right, if it's on, the relay is on). One of the big changes I'm trying to work out is to get the page over to an auto update for the relay on/off status. Not only is that important for relays when they turn on/off automatically, but it will also fix the issue where the refresh happens faster than the GPIO status update.

When the web page status indicator and the LED don't match up, try waiting a second and hitting F5 for the short term fix.
Dear forum: Play nice ;-)
User avatar
Moderator
Moderator
Posts: 4221
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
by joeman2116 » Fri Mar 01, 2013 6:07 pm
Thanks for the update

Joe
Posts: 12
Joined: Mon Feb 25, 2013 4:27 pm