Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Need help on Insert data to phpMyAdmin mySQL database from Shell Script

raspberry pi phpmyadmin shell script mysql gpio

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 aoiregion

aoiregion

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 16 July 2014 - 05:37 PM

Hi guys, I would like to seek help on inserting data whenever the switch is on or off to my sensor mySQL database in phpMyAdmin from my Shell Script. I'm using Raspberry PI as my hardware and follow a few tutorials to create my own Web Control Interface, it works perfectly without insert method. After I implemented insert method to my GPIOServer.sh and execute it, an error stated that today:not found and unexpected operator.

 

This is my GPIOServer.sh:

#!/bin/sh

mysqlusername="mysqlusername"
mysqlname="mysqlname"
mysqlpassword="mysqlpassword"

#Set  Refresh
echo "How long do you want the wait time to be?"
read waitTime

#Invoke GPIO
echo "4" > /sys/class/gpio/export
echo "17" > /sys/class/gpio/export
echo "18" > /sys/class/gpio/export
echo "21" > /sys/class/gpio/export
echo "22" > /sys/class/gpio/export
echo "23" > /sys/class/gpio/export
echo "24" > /sys/class/gpio/export
echo "25" > /sys/class/gpio/export

#Start Loop
while :
do
#Read MySQL Data
#Direction
direction4=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='4'";)
direction17=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='17'";)
direction18=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='18'";)
direction21=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='21'";)
direction22=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='22'";)
direction23=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='23'";)
direction24=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='24'";)
direction25=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='25'";)
#Status
status4=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='4'";)
status17=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='17'";)
status18=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='18'";)
status21=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='21'";)
status22=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='22'";)
status23=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='23'";)
status24=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='24'";)
status25=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='25'";)

#Run Commands
today = $(date)
if [ "$direction4" == "out" ]; then
    echo "out" > /sys/class/gpio/gpio4/direction
    if [ "$status4" == "1" ]; then
        echo "1" > /sys/class/gpio/gpio4/value
        $(mysql -D --user=$mysqlname --password=$mysqlpassword sensor -e "INSERT INTO sensor values("1", "Red LED", $today, "")";)
        echo "GPIO 4 Turned On"
    else
        echo "0" > /sys/class/gpio/gpio4/value
        $(mysql -D --user=$mysqlname --password=$mysqlpassword sensor -e "INSERT INTO sensor values("1", "Red LED", "", $today)";)
        echo "GPIO 4 Turned Off"
    fi
else
    echo "in" > /sys/class/gpio/gpio4/direction
fi
if [ "$direction17" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio17/direction
    if [ "$status17" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio17/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values('2', 'Blue LED', $today, '')";)
                echo "GPIO 17 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio17/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values('2', 'Blue LED', '', $today)";)
                echo "GPIO 17 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio17/direction
fi
if [ "$direction18" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio18/direction
    if [ "$status18" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio18/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values('3', 'Green LED', $today, '')";)
                echo "GPIO 18 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio18/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values('3', 'Green LED', '', $today)";)
                echo "GPIO 18 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio18/direction
fi
if [ "$direction21" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio21/direction
    if [ "$status21" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio21/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values('4', 'Yellow LED', $today, '')";)
                echo "GPIO 21 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio21/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values('4', 'Yellow LED', '', $today)";)
                echo "GPIO 21 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio21/direction
fi
if [ "$direction22" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio22/direction
    if [ "$status22" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio22/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values('5', 'LED', $today, '')";)
                echo "GPIO 22 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio22/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values('5', 'LED', '', $today)";)
                echo "GPIO 22 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio22/direction
fi
if [ "$direction23" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio23/direction
    if [ "$status23" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio23/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values('6', 'LED', $today, '')";)
                echo "GPIO 23 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio23/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values('6', 'LED', '', $today)";)
                echo "GPIO 23 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio23/direction
fi
if [ "$direction24" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio24/direction
    if [ "$status24" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio24/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values('7', 'LED', $today, '')";)
                echo "GPIO 24 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio24/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values('7', 'LED', '', $today)";)
                echo "GPIO 24 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio24/direction
fi
if [ "$direction25" == "out" ]; then
        echo "out" > /sys/class/gpio/gpio25/direction
    if [ "$status25" == "1" ]; then
                echo "1" > /sys/class/gpio/gpio25/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values('8', 'LED', $today, '')";)
                echo "GPIO 25 Turned On"
        else
                echo "0" > /sys/class/gpio/gpio25/value
                $(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword sensor -e "INSERT INTO sensor values('8', 'LED', '', $today)";)
                echo "GPIO 25 Turned Off"
        fi
else
        echo "in" > /sys/class/gpio/gpio25/direction
fi
#Complete Loop
sleep $waitTime
done

This is my sensor mySQL database in phpMyAdmin:

CREATE TABLE IF NOT EXISTS `sensor` (
  `id` int(11) NOT NULL,
  `sensor` text NOT NULL,
  `switchOnLog` datetime NOT NULL,
  `switchOffLog` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

This is the error screenshot I got when executing GPIOServer.sh:

error.png



#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 17 July 2014 - 06:07 AM

Well,

It says it doesn't get the var today.

But, if you are going to insert a datetime, then you have to enclose the value on "" too, else it will mess with your sql sentence parsing.



#3 aoiregion

aoiregion

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 24 July 2014 - 10:49 PM

This thread can be closed cause after reading your comment, I find that I'm doing it in the wrong way cause I shouldn't code the WRITE method in shell script.

Thanks BlackRabbit  :biggrin: