Quantcast
Channel: Intel Communities: Message List
Viewing all articles
Browse latest Browse all 17516

Setting up Flask Web Server on Galileo

$
0
0

Hi all
I hope I don't offend anyone, but it seems to me that most of the material that is put out about the Galileo is either for complete newbies or advanced hardware/software engineers and there is not much for people like me who have been using Arduino & Raspberry Pi boards for a while and want to see what they can do with the Galileo.

 

So to start I would like to explain how I got set up my Galileo to make it easy to use and got distribute and Flask working.
Distribute is a way of installing programs, and you may have used it on other platforms with the command “easy_install” and Flask is a really nice, easy to use, Web Server.

 

First things is to get the Galileo up and running.
There are a number of good tutorials, such as one I like at SparkFun. https://learn.sparkfun.com/tutorials/galileo-getting-started-guide
Do not forget to update the firmware or will have problems.

 

When you reach the part about installing the bigger form of  Linux, download Alex's developmental version of Linux.
You can check https://communities.intel.com/message/239581#239581 to find the latest version.
But for now it is at http://telekinect.media.mit.edu/galileo/image-devtools-1.0.1-3.tar.bz2
Uncompress and copy to a micro SD card. Remove the USB cable (important or will kill the board) power down and back up.

 

Finding the IP address.
As discussed on Clay and Galileo (http://www.hofrock.com/connecting-to-linux) there are a number of ways to connect to the Galileo.
If you want to use SSH, you need to find the IP. An easy way is to run a sketch that prints the IP address onto an LCD screen.

 

/*
  LCD_Print_IP
Vietnamese-German University
Richard Bradley
Program to display IP Address on DFRobot LCD Shield
Note: Ethernet cable must be plugged in prior to starting Galileo or it won't get an address
Note: Need to put electrical tape on LCD or shorts against Ethernet port connector
Note: Needs updated Liquid Crystal Library
  https://github.com/mikalhart/galileo-LiquidCrystal/releases/
  Download and put in Sketches/library directory
Based on sketches from
http://www.arduino.cc/en/Tutorial/LiquidCrystal
and http://www.cyberciti.biz/faq/how-to-find-out-the-ip-address-assigned-to-eth0-and-display-ip-only/
and https://communities.intel.com/thread/45335
---------------------------------------------------
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/

 

// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
char FileBuffer[15]; //set up space to read in files

 

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.setCursor(0, 0);
  lcd.print("Welcome to VGU!");
  // initialize serial communications:
  //Serial.begin(9600);
  // Start the telnet server on Galileo
   system("telnetd -l /bin/sh");
   delay(5000);
}

 

void loop()
{
  // Call ifconfig, pipe through grep to get ipaddress and redirect to file in /home/root
     system("ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'>/home/root/ip.txt");
     FILE *fp;
     fp=fopen("/home/root/ip.txt","r");
     fgets(FileBuffer, 15, fp);
     fclose(fp);
     lcd.setCursor(0, 1);
     lcd.print(FileBuffer);
     delay(5000);
}

 

Set up access to Alex's repository
Now that you can SSH into your Galileo
ssh root@172,16,3,147 (using the IP address displayed)
Alex of Intel (not part of Galileo team) has put up a repository
Go to his blog at http://alextgalileo.altervista.org/ and click on Package repo configuration HowTo.
The Linux on Galileo (Openwrt) uses opkg instead of apt(like Debian Linux on Raspberry Pi)

 

Easy way to set up access to his repository so can download files it first go to the opkg directory
>cd /etc/opkg
Then use echo to create a base-feeds.conf file (the original is blank)
>echo src all http://repo.opkg.net/galileo/all  > base-feeds.conf
>echo src clanton http://repo.opkg.net/galileo/clanton  >> base-feeds.conf
>echo src i586    http://repo.opkg.net/galileo/i586  >> base-feeds.conf

 

You can check with:
> cat base-feeds.conf

 

Now you can run opkg update (same as apt-get update on Raspberry) and install packages like openssh-sftp-server that allows you to upload/download files using filezilla.
>opkg install openssh-sftp-server

 

Install Nano

If you hate vi as much as I do, you can install nano by first going to your home directory
>cd       
or better to the unused part of you SD card
>cd /media/realroot
creating a work directory
>mkdir arduino
>cd arduino
and  installing nano with the following commands

 

>wget http://www.nano-editor.org/dist/v2.2/nano-2.2.6.tar.gz
t>ar -xzf nano*.gz
>cd nano*
.>/configure && make && make install

 

Install distribute and Flask
Now that everything is set up you can start to do some things.
Downloaded distribute from  https://pypi.python.org/pypi/distribute/0.6.49
Unpack and copy to the Galileo with filezilla, or whatever way you want
Go to the directory you copied and install distribute
>python distribute_setup.py
You now can install flask
> easy_install flask
Test it by creating a simple web page - hello.py per flask tutorial
Create a new directory
>mkdir /media/realroot/www
> cd  /media/realroot/www
Create a python file with nano
>nano hello.py
and type in the following
-------------------------------------------
from flask import Flask
app = Flask(__name__)

 

@app.route("/")
def hello():
   return "Hello World!"

 

if __name__ == "__main__":
    app.run(host='0.0.0.0')
save with control + x
Run it with
>python hello.py
and you should see returned
* Running on http://0.0.0.0:5000/

 

You can then access your web page at the IP address and socket 5000
ex: 172.16.3.247:5000

 

You can follow the tutorials on the Flask web page, or ones that exist elsewhere on the net.
Regards
rick


Viewing all articles
Browse latest Browse all 17516

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>