Bradderz
Bradderz

Reputation: 21

Run a Python script from a HTML page using nginx on a Raspberry Pi

I've been working on a project recently where basically I need to make a motor spin at certain times throughout the day for a few seconds, that can be customised using your phone.

So far I have followed many tutorials and done a lot of browsing and I've managed to have my Pi zero host its own network(using nginx, hostapd and dnsmasq), which you can connect to on your phone and go to 192.168.4.1 to access an index.html page in /var/www/html/

I also have a Python script which, when run, turns one of the GPIO pins on for a few seconds and then off again, and this GPIO pin is in turn connected to the motor.

The trouble I am having is setting up the rest of the web side, where you are able to connect to the network, go to a page, insert 2 or 3 different times, submit them, and then when it's that time the Python scripts will run.

Since I've set up the pi as a access point, I'm not sure how to reverse it and allow it to connect to wifi again without ruining the access point and current set up, so I'm not sure if there's an easy way to download any packages or modules I may need.

Anyway, any help anyone could give me would be incredibly useful - many thanks!!

Upvotes: 0

Views: 2833

Answers (2)

hcheung
hcheung

Reputation: 4024

Unless you are planning for a production web server, for simple application like display sensor status or control sensor via web page, there is a simpler solution for beginners and for python programmers. Since you are using python, so you don't have to use PHP, and you probably don't need to have Nginx at this stage. There are actually two ways in my experiences to do it.

1) Using http.server

The 'simple way' to serve web page using python based on python standard library http.server, utilising python build-in socket based http server. But it is less intuitive to set it up for GET/POST requests/responses. It is too long to describe it here, but I have a blog post on how to do it here.

2) Using Flask web development micro framework

Flask allows you to setup html template, handling route and run a web server easily within python environment. You need to install Flask package for python web development. The simplest flask python code that addressed your question of serving the data to a web page would be:

from flask import Flask, render_template_string


app = Flask(__name__)
data = 200    #assuming this is the data you want to show in your web page

@app.route('/')
def index():
    return render_template_string('''
         <h1>My Sensor Web Page</h1>
         <p>My sensor reading is {}".format(data))</p>
         '''


if __name__ == '__main__':
    app.run(debug=True)

Launch your browser and point it to http://localhost:8000, you should see the data to be rendered as webpage per our simple example code.

What you will need is to either import your code into this flask example, or integrate it into the example, and pass the data you want to display to render_template_string function.

Upvotes: 1

cemersoz
cemersoz

Reputation: 43

I would suggest using a web framework to host the "website". Flask is one that I have used for similar applications. Since this method allows you to directly call python functions in response to http requests it should be fairly easy to implement what you are trying to do.

As a bonus, you can use flask with nginx but I really don't think you need it for this specific application.

Upvotes: 0

Related Questions