![]() You can use any Text editor to create your HTML file. So, we will create a file named index.html, that will be saved on /templates. This is, in fact, important, otherwise, you would complicate the Python Script putting all on it.Ĭreating an HTML file that will be located in the "template" sub-folder, we can use separate files with placeholders for spots where you want dynamic data to be inserted. Let's sophisticated our "Hello World" application, creating an HTML template and a CSS file for styling our page. For each new request, a new line will appear on the terminal as far the application is running. I entered with our RPi Server address on both, the RPI itself (10.1.0.27) and on my Mac (10.1.0.10). the lines show that the two different web browsers requested the root URL and our server returned HTTP status code 200 for "OK". Note that there are 2 lines at bottom of the 2nd picture. The "Hello World" should appear in your browser as shown in the 3rd picture above. Now you must open any web browser that is connected to same wifi network as your Raspberry and type its IP address as shown in the second picture above. The application will be running unless you type +. On the first picture above you can see what will appear on your terminal. Now, run the above application program: sudo python3 helloWorld.py If you are not sure about your RPi Ip address, run on your terminal: ifconfigĪt wlan0: section you will find it. Once this script is running from the command line at the terminal, the server starts to "listen" on port 80, reporting any errors: if _name_ = '_main_': ![]() In this case, only send the text "Hello World!" to the client's web browser thru "return" def index():Ĥ. Run the index() function when someone accesses the root URL ('/') of the server. Create a Flask object called app: app = Flask(_name_)ģ. Load the Flask module into your Python script: from flask import FlaskĢ. Now, let's create our first python WebServer with Flask:Ĭopy the "Hello Word" code below on your IDE and save it for example, as helloWorld.py from flask import FlaskĪpp.run(debug=True, port=80, host='0.0.0.0')ġ. The final folder "tree", will look like: /rpiWebServer Go to your newer created folder: cd rpiWebServerĪnd create the 2 new sub-folders: mkdir static Now, on this folder, let's create 2 other sub-folders: static for CSS and eventually JavaScript files and templates for HTML files (or more precisely, Jinja2 templates. There we will save our python files (application): The above command will create a folder named "rpiWebServer". The best when you start a new project is to create a folder where to have your files organized. Go to Terminal and enter: sudo apt-get install python3-flask The first thing to do is to install Flask on your Raspberry Pi. The above block diagram shows what we want to accomplish and the video below can give a hint about it: On this tutorial, we will use a Raspberry Pi as a local Web Server, where we will control via a simple webpage, 3 of its GPIOs programmed as outputs (acting as actuators) and monitor 2 of its GPIOs, programmed as inputs ( sensors). However, Flask supports extensions that can add application features as if they were implemented in Flask itself. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. a Web Jorney With Flask and SQLiteįlask is called a micro framework because it does not require particular tools or libraries. After you read this tutorial, please give a visit to its continuation: From Data to Graph. With Flask, will be very simple to control Raspberry GPIOs over the internet. For this project, we will use FLASK, a very simple and free microframework for Python. For example, on my tutorial: IoT - Controlling a Raspberry Pi Robot Over Internet With HTML and Shell Scripts Only, we have explored how to control a robot over the local network using the LIGHTTPD WebServer. Let's create a simple WebServer to control things in your home. If you like it, please give your vote by clicking at the above banner. This Instructable is competing on contests: " Remote Control" and " Raspberry Pi".
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |