Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This involves a raspberry pi and apache2 so I thought the question would be good to go here..

have never used web sockets and things like that before so I'm pretty lost and hoping for some guidance...

I have a python script that gives me two pieces of data...the angle of a servo motor and the distance of an object from the ultrasonic sensor.

I need this data to be sent to my website which has a radar imsge. As the angle changes the sweep on the radar image should change, and where there is an obstacle it should show on my radar.

My question is how can I get this data to the website in a continuous way? I have apache2 running on my website. Right now I'm already using Ajax requests to make motors run on my raspberry pi (it's like an RC car).

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
TRIG = 22
ECHO =18
GPIO.setup(TRIG, GPIO.OUT)
GPIO.output(TRIG,0)
GPIO.setup(ECHO, GPIO.IN)
GPIO.setup(33, GPIO.OUT)
pwm=GPIO.PWM(33,100)
pwm.start(5)
time.sleep(0.1)

class rotateRead():
    def To180andBackRead():

        angle=0
        for angle in range(0, 180, 1):
            GPIO.setmode(GPIO.BOARD)
            duty=float(angle)/10+2.5
            pwm.ChangeDutyCycle(duty)
            print angle
            time.sleep(0.1)
            GPIO.output(TRIG,1)
            time.sleep(0.00001)
            GPIO.output(TRIG,0)
            while GPIO.input(ECHO) ==0:
                pass
            start =time.time()

            while GPIO.input(ECHO) ==1:
                pass
            stop=time.time()

            print (stop-start) *17000



        angle=180
        for angle in range(180, 0, -1):
            GPIO.setmode(GPIO.BOARD)
            duty=float(angle)/10+2.5
            pwm.ChangeDutyCycle(duty)
            print angle
            time.sleep(0.1)
            GPIO.output(TRIG,1)
            time.sleep(0.00001)
            GPIO.output(TRIG,0)
            while GPIO.input(ECHO) ==0:
                pass
            start =time.time()

            while GPIO.input(ECHO) ==1:
                pass
            stop=time.time()

            print (stop-start) *17000


    for x in range(0,1,1):
        To180andBackRead()

GPIO.cleanup()
share|improve this question

First of all you'll need to create an endpoint in your website. The simpliest way is to create a file called robot_update.php with the following content.

<?php
$input_data = file_get_contents('php://input');
$decoded_data = json_decode($input_data);
//Do something with $decoded_data, update image or write it to the database.
?>

Next, Send your data via http from python like so

import json
import urllib2

def update_robot(angle, distance):
    data = {
        'angle' : angle,
        'distance' : distance
    }
    req = urllib2.Request('http://yourwebsite.com/robot_update.php')
    req.add_header('Content-Type', 'application/json')
    res = urllib2.urlopen(req, json.dumps(data))
    res.read() 

Call this function in place where your angle and distance is calculated. This will work just fine but remember to add proper restrictions to your robot_update.php.

share|improve this answer
    
This is all new to me, im sorry. The python script continuously produces a new angle (from 0-180) using a for loop, and within each loop it gives a distance of an object. Can you explain this a bit more, how it would work with my script. i have never seen this before. Will this continuously send each new piece of data produced? in your code you wrote "data-{}, but my data is continuously produced, its not set. i added my python code to the question, i would just like a small explanation as to how this works. – Ahsin Apr 16 at 16:26
    
Take my python code and create function that takes two parameters (angle and distance). Then call it when your angle and distance is ready. It can be send many times. I'll update my answer. – Inclooder Apr 16 at 20:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.