0

Trying to learn the webiopi and python. I have a simple button to turn off and on a relay. Here is the HTML

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>WebIOPi | Example App</title>
        <script type="text/javascript" src="/webiopi.js"></script>
        <script type="text/javascript">
        webiopi().ready(function() {

                // Create a "Light" labeled button for GPIO 17
                var light_button = webiopi().createGPIOButton(17, "Light", function() {
                    // Call the logtofile macro 
                    webiopi().callMacro("logtofile", [], '');
                });

                // Append buttons to HTML element with ID="controls" using jQuery
                $("#controls").append(light_button);

                // Refresh GPIO buttons
                // pass true to refresh repeatedly of false to refresh once
                webiopi().refreshGPIO(true);
        });

        </script>
        <style type="text/css">
                button {
                        display: block;
                        margin: 5px 5px 5px 5px;
                        width: 160px;
                        height: 45px;
                        font-size: 24pt;
                        font-weight: bold;
                        color: white;
                }

                #gpio17.LOW {
                        background-color: Black;
                }

                #gpio17.HIGH {
                        background-color: Blue;
                }
        </style>
</head>
<body>
        <div id="controls" align="center"></div>
</div>
</body>
</html>

Straight forward stuff you will find in the tutorials around the web. I added this

// Call the logtofile macro 
webiopi().callMacro("logtofile", [], '');

To call a macro. My script file looks like this

import webiopi

GPIO = webiopi.GPIO

BLUE = 17 # GPIO pin using BCM numbering(BOARD 11)
RED = 18 # GPIO pin using BCM numbering(BOARD 12)
GREEN = 27 # GPIO pin using BCM numbering(BOARD 13)

# setup function is automatically called at WebIOPi startup
def setup():
    # set the GPIO used by the light to output
    GPIO.setFunction(BLUE, GPIO.OUT)
    GPIO.setFunction(RED, GPIO.OUT)
    GPIO.setFunction(GREEN, GPIO.OUT)

# loop function is repeatedly called by WebIOPi 
def loop():
    # gives CPU some time before looping again
    webiopi.sleep(1)

# destroy function is called at WebIOPi shutdown
def destroy():
    GPIO.digitalWrite(BLUE, GPIO.LOW)

@webiopi.macro
def logtofile():
    call([" sudo python /home/pi/buttonlog.py "], shell=False)

My logtofile macro is just calling the /home/pi/buttonlog.py that looks like this.

import datetime
i = datetime.datetime.now()

#OPEN FILE & APPEND
f=open('/home/pi/buttonlog.txt','a')
#WRITE DATE THEN NEW LINE WITH THE '\N'
f.write(i.isoformat() + '\n')

Straight forward code that writes the date to the /home/pi/buttonlog.txt However when I click the button I do not get the script to add the date to the file. If I run the buttonlog.py all by itself it writes to the file as expected. Am I missing something? I know this is probably not the simplest way to accomplish this but I am trying to understand how it works.

1 Answer 1

1

I think you're missing the f.close() command that actually syncs the change to the file system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.