Reverse Proxy

Tags

, , , ,

A Reverse Proxy is a type of proxy server that retrieves resources on behalf of a client from one or more servers.These resources are then returned to the client as though they originated from the serverr itself. This is illustrated in the diagram below.

Reverse_Proxy_setup

Reverse Proxy setup

Reverse proxy is different from forward proxy as reverse proxy only returns resources provided by the associated servers while forward proxy returns resources accessible on the internet. This page explains further on the differences between them. It can hide the existence and characteristics of an origin server or servers.It can be used with firewall to protect against web-based attacks.

Reverse proxy seems to be a solution to my project’s problem where I have 2 servers running on a Raspberry Pi – Apache and CherryPy. Nginx (engine x) is an HTTP and reverse proxy server. It could be deployed with CherryPy as explained in this tutorial – How to Deploy Python WSGI Applications Using a CherryPy Web Server Behind Nginx.

I am left with 2 weeks to submit my FYP draft report and reverse proxy is something that I have never encountered before. I was in the dilemma of whether to pick it up and try to implement it or forgo the link between the 2 servers.

HOWEVER I suddenly realized that there is no need for a reverse proxy because i have only 1 server machine running 2 servers at different ports! 😀 Thanks to Zen from CtrlWorks.

Update 17/3

Tags

, , , , , ,

Happy Saint Patrick’s Day! It’s a lucky Shamrock day!

Today I met my dear Prof to discuss the Table of Contents for my report and also update her on the possible solutions for the problem of using different web servers. One of the solution was using Reverse Proxy but that would take too much of my time trying to learn something new. I remembered I couldn’t do a question on forward proxy for my Computer Security exam. 😦

Anyway, i stayed in the lab till quite late and I met a group of guys from a startup company, CtrlWorks. They are doing their products in the lab. I find that they are quite helpful and lucky enough, one of the guy, Zen, knows Python very well and is familiar with Raspberry Pi!

I discussed with him regarding the problems that I faced when trying to use 2 web servers in Raspberry Pi to do simple things. He understood the problems immediately and clarified some of my doubts, for example, PHP is indeed can only be put in /var/www and execute by Apache and no way by CherryPy. Also, there is no need to wander off to complicated solution such as using Reverse Proxy (I knew there must be a simpler solution!).

The fact is, the two servers are both on 1 machine- Raspberry Pi, so therefore the web app are basically hosted on the same IP address. Reverse Proxy is normally used for different domain names. Therefore, I can use URL to direct the 2 pages (one at localhost and the other at :8080). With this in mind, I can also embed a page in another page using iframe. This, however may not look nice as the page embedded may not fit nicely in the iframe window. Another possible solution is to embed URL directly as a link. However, absolute URL has to be used because if I write static/icon.png, this will refer to the page that is calling it. Samewise, if I write http://localhost:8080/static/icon.png and display this page from my network laptop, the link will refer to my current laptop’s directory. Therefore, the best way is to use the ip address of Raspberry Pi directly, such as http://192.168.1.109:8080/static/icon.png.

The main problem with my Raspberry Pi is that I can only set a static IP using my room’s router, not my campus’s wireless or LAN. Well, i guess this can’t be helped for now. 🙂

Another thing to consider is cron job where Raspberry Pi can be scheduled to carry out certain tasks such as run a script, reboot, or shutdown. Here are something to look at:
How to Schedule a Job on the Raspberry Pi
Bright Idea: Using Raspberry Pi as a Lamp Timer <– worth trying!
This will satisfy the requirement of "eating the right amount of right pill at the right time!”
I can also start the cherrypy python script at boot up. I have not try this but see this tutorial! Basically it is just this 2 lines:
sudo crontab -e
@reboot python /home/pi/MyScript.py &

Defintely going to add this to my project before it ends. B)

Database

Tags

, , , ,

I decided to try the database at port 80. I was unable to transfer files to /var/www due to permission issue. So I have to give access to the folder to user “pi” as follow:
sudo chown -R pi /var/www

The database works fine when i access it from port 80, but not for port 8080. CherryPy seems to have some issues with the php files.

Anyway, I could add new entry to the database table now.

Too much things to finish up.

Update 16/3

Tags

, , ,

I finally cleared out some spaces just enough to do backup for my 16Gb SD card image.

The backup image includes:

– CherryPy installed and tested

– Camera App running

– PiGPIO installed

– RPi-GPIO installed

– Servo tested

– LED lights with switch control

– Arduino installed

– Sensors tested and able to control LED

May be time to play with Homegenie?

Raspberry Pi with Sensors

Tags

, , , , , ,

Sensors usually need an analog input port to read their value. To process the values from analog input, Raspberry Pi would need an external Analog to Digital Converter (ADC) chip or interface with Gertboard or Arduino. However, a simple method making use of the capacitor can be used to get the values of a sensor. This is because some sensors such as a photocell, thermistor, and force sensitive resistor, behave like a resistor. If a capacitor is connected to power (3.3V) through “resistor”, it will charge up slowly and we could count how long it takes. This method is not as accurate as an ADC but it is good enough to give 2 distinct state – sensor triggered as on or off. The diagram here gives a better illustration.

So i used a 1uF capacitor and a photocell to test it out. The photocell cell has no polarity so one of its side is connected to 3.3V (Pin1) and the other side is connected to GPIO pin 18 (physical pin 12). However the capacitor has polarity and the side with negative sign (shorter leg) is connected to the ground (pin 6) while the other is connected to GPIO pin 18 too. See diagram below:

photocell connect to raspberry pi

Photocell to Raspberry Pi connection diagram

This sample code could give some readings from the photocell:

#!/usr/bin/env python

import RPi.GPIO as GPIO, time, os

DEBUG = 1
GPIO.setmode(GPIO.BCM)

def RCtime (RCpin):
    reading = 0
    GPIO.setup(RCpin, GPIO.OUT)
    GPIO.output(RCpin, GPIO.LOW)
    time.sleep(0.1)

    GPIO.setup(RCpin, GPIO.IN)
    # This takes about 1 millisecond per loop cycle
    while (GPIO.input(RCpin) == GPIO.LOW):
        reading += 1
    return reading

while True:
    print RCtime(18) # Read RC timing using pin #18

I was able to get some readings from the photocell where the values get larger when i cover my hand over it.

Photocell readings

Photocell readings

To use this to control a LED, connect a LED to GPIO pin 17 (see diagram above). The code will include the following:

if sensorvalue > 1000:
                print "switch on light"
                GPIO.output(11, True) #switch on
                lightstate ="On"
                time.sleep(1)
            else:
                print "switch off light"
                GPIO.output(11, False) #switch off
                lightstate ="Off"
                time.sleep(1)

Full script here: http://pastebin.com/Asp7Gixa
The following video demonstrate the control.

I have also tried force sensitive resistor (FSR). The connection is as followed:

FSR connection diagram to Raspberry Pi

FSR connection diagram to Raspberry Pi

FSR connection to Raspberry Pi

FSR connection to Raspberry Pi

The reading that it gave:

fsr readings

FSR readings. The larger force, the smaller value

However it is a bit different because it does not give reading when the sensor is not pressed.
Full script here: http://pastebin.com/RSEJ3Y7y

Problem:
Unable to terminate the function properly, see forum here. -probably has to do with terminating the while loop.
Unable to display the sensor value on the web, see forum here. – probably can be solved using setInterval but from my past experience it has been causing much problem and increase work load for Raspberry Pi.

Note:
The GPIO.BOARD option specifies that you are referring to the pins by the number of the pin the the plug – i.e the numbers printed on the board (e.g. P1).

The GPIO.BCM option means that you are referring to the pins by the “Broadcom SOC channel” number.

Reference:
Photocell

Force Sensitive Resistor

Information overflow

Tags

, , , ,

Caffeine rush. Brain malfunction. Limited buffer size. Information overflow.

So apparently there are 2 options to be explored in order to solve the multiple server problems. First of all is to use a reverse proxy, probably Nginx. Not really sure how it is going to work but seems legit.

Secondly, mod_wsgi with CherryPy. Still hasn’t understand what it is for.

Do they actually solve my problems? A few forum questions been asked:

Cannot Access phpmyadmin from Cherrypy port 8080

Access phpmyadmin from Cherrypy port 8080

How to merge port 80 and port 8080?

Accessing MySQL database

If I have one more sem to restudy all these as one module, I may be able to come up with the solution. Now I only have at most 1 more week to settle all these and write a report.

misc links for ref:
http://www.cyberciti.biz/tips/using-nginx-as-reverse-proxy.html
http://en.wikipedia.org/wiki/Reverse_proxy
http://stackoverflow.com/questions/224664/difference-between-proxy-server-and-reverse-proxy-server

Redo

Tags

, , ,

Yesterday I gave myself a fright while performing a demonstration to my prof.

My SD card was corrupted, for the second time.

In fact, it was because I plugged in my prof’s keyboard and mouse while starting up which caused a sudden large amount of current being drawn from Raspberry Pi. Luckily I had another backup SD card (8Gb) which contains almost the same thing except that the web app is run using Flask.

Well, I have to rewrite the image into the corrupted 16Gb SD card using the first back up file (in Jan). Some of the things that i had to install and configure since last night till this morning were:

  1. Camera App
  2. CherryPy
  3. PiGPIO
  4. Transfered all app files to the card
  5. Tested servo
  6. RPi-GPIO
  7. Tested LED control

And now, time for another backup!

UPDATE: OMG, I need a 16Gb space to back up my 16Gb SD card!!! and my D:/ has only 14.7.Gb left.

Controlling LED from CherryPy Web App

Tags

, , , , , ,

This is another simple demonstration on how to control a LED light from CherryPy web app.

The connection is exactly the same as the previous post regarding the blinking LED.
LED connection to Raspberry Pi GPIO

A closer look at GPIO side
But now, instead of blinking, I can control it using a “slide switch” from my web app.

Simple Web App

Simple Web App

The web app is done by jQuery. First of all, create a web element (slide switch) called “light_1” as follow:

<label for="light_1">Light 1</label>
<select name="light_1" id="light_1" data-role="slider" data-track-theme="b" data-theme="b">
<option value="off">Off</option>
<option value="on">On</option>
</select>

Then write this line with other jQuery scripts:

$("#light_1").change(function () {$.post('/switch',{key_pressed:"power_"+$(this).val()})});

Under Python section, write the following:

 @cherrypy.expose
    def switch(self, **data):
        import RPi.GPIO as GPIO #Import GPIO library
        import time #Import 'time' library so that we can use 'sleep()'

        # Use physical board pin numbers
        GPIO.setmode(GPIO.BOARD)
        # Set up header pin 11 (GPIO17) as an output
        print "Setup Pin 11"
        GPIO.setup(11, GPIO.OUT)

        key = data['key_pressed']
        if key=="power_on":
            print "switch on light"
            GPIO.output(11, True) #switch on
            time.sleep(1)
        elif key=="power_off":
            print "switch off light"
            GPIO.output(11, False) #switch off
            time.sleep(1)

Here is the full code: http://pastebin.com/FdKRLne4

Have fun! Next up, combine this servo motor – may need external supply so that both of them will not draw too much current from R-Pi. Test multiple servos as well.

NOTE: From now onwards I have to use sudo python pilldispense.py because only root has access to /dev/mem.

LED Blink test with Raspberry Pi GPIO

Tags

, , , , ,

Back to the very basic – lighting up a LED and blink it! Well this is just to test the GPIO control. I should have done this before trying out on Servo motor. :p

But anyway, here is the circuit diagram.

LED connection to RPi GPIO

LED connection to RPi GPIO

I actually connected it to pin 11 (GPIO 17) instead of pin 7 shown in the diagram above. I used 330 Ohms resistor and connected it to Gnd at pin 6.
Reference: Resistor Color Code

Resistor color code

I wrote a simple Python program to make the LED blink. Alternatively, you can test out your circuit by connecting the signal wire to pin 1 (3.3V). If it lights up, then it’s good.

Remember to install RPI.GPIO first.

See below for the simple code for blinking the LED:


import RPi.GPIO as GPIO #Import GPIO library
import time #Import 'time' library so that we can use 'sleep()'

# Use physical board pin numbers
GPIO.setmode(GPIO.BOARD)
# Set up header pin 11 (GPIO17) as an output
print "Setup Pin 11"
GPIO.setup(11, GPIO.OUT)

var=1
print "Start loop"
while var==1 :  #run loop forever until CTRL C is pressed
    print "Set Output False" 
    GPIO.output(11, False) #switch off
    time.sleep(1)  #wait
    print "Set Output True"
    GPIO.output(11, True) #switch on
    time.sleep(1)

Here is the video for demonstration:

Next up, integrate this with a switch from web app.

Update 13/3

Tags

, , , ,

-On 16Gb SD Card-

Camera App working
CherryPy working – managed to solve the issue of not being able to display css and static files.

The tutorial.conf has to be modified as follow (added the tools.staticdir.root):

[global]
server.socket_host = "0.0.0.0"
server.socket_port = 8080
server.thread_pool = 10
[/]
tools.staticdir.root = "/home/pi/CherryPy/control_test"
[/static]
tools.staticdir.on = True
tools.staticdir.dir = "static"

Installed PIGPIO

Able to control servo from the web page and display servo position. 🙂 – servo moves from position 1500 to 900 when i click on “dispense pill button”.

dbmgmt.php is in static folder – working but addEntry.php not yet working – waiting for solution from cherrypy experts on the forum.

TO-DO:
Simulate simple light on and off.
http://www.thirdeyevis.com/pi-page-2.php
http://www.raspberrypi-spy.co.uk/2012/06/control-led-using-gpio-output-pin/

Installed RPI-GPIO
Tried LED blink test- success. See next post for details.