Sunday, August 19, 2012

Raspberry PI – Display Time On rPI Web Server (quick hack)

Here is a python program for the rPI that will generate a new index.html web page every 5 seconds (yes, very crude way of doing it, but it works :-)


print ('Start')
import datetime
import time
count = 0
while (count<1000):
     out_file = open("/var/www/index.html", "wt")
     print ("Writing file, Date and time = ", str(now))
     out_file.write("<html><body><h1>It works!</h1>\n")
     out_file.write("<p>This is Eric Pierce's Raspberry Pi Webserver.</p>\n")
     out_file.write("<p>Date and time = " + str(now) + "</p>\n")
print ('End')


If that is saved in your default folder with the name "" you can run it as so (it needs root privs to do this, that is what the sudo prefix does):

  $ sudo python3

Then if you connect to the raspberry pi with a browser from another computer, you see the page with the time. If you hit the browser refresh button evety 5 seconds, you will see the time, date, etc.

Only a few more lines of code to read digital events in your house from switches, and a bit more to read analog values. Open up a port in your firewall, and you can access it from the world.

With a bit more work, you could put buttons on the page to control things!


Eric Pierce VA3EP - See the Disclaimer in the Introduction

© Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", 1952-2099. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", with appropriate and specific direction to the original content.

No comments:

Post a Comment