aeyah14
Posts: 19
Joined: Wed Jun 18, 2014 9:56 pm

Send Data from RPi to RPi via WiFi

Mon Sep 22, 2014 10:35 am

I have a project im building and have 2 RPis the 1st RPiI(server) is acting as a AP using Tp-linkwn725n adapter and my 2nd RPi(client) is connected to it using a different adapter.

I plan to run a simple 60 second countdown timer at the 1st RPi and use a push button switch to trigger it to send the countdown timer data to the 2nd RPi hopefully with little delay...

I did some searching and i plan on using this
http://www.codeproject.com/Articles/810 ... t-Protocol
as my concept in making it. would it be possible? or are there better/easier alternatives in sending the data

Thank you to them pro's. really have difficulty in programming >.<

User avatar
DougieLawson
Posts: 40828
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Send Data from RPi to RPi via WiFi

Mon Sep 22, 2014 10:37 am

Look at RabbitMQ or MQTT.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

billio
Posts: 71
Joined: Thu Dec 15, 2011 8:25 am
Contact: Website

Re: Send Data from RPi to RPi via WiFi

Mon Sep 22, 2014 10:13 pm

You might make some progress by reading this :

https://docs.python.org/3/howto/sockets.html

which is a basic introduction to network programming in Python.

aeyah14
Posts: 19
Joined: Wed Jun 18, 2014 9:56 pm

Re: Send Data from RPi to RPi via WiFi

Sun Oct 05, 2014 11:17 am

sorry for the very late reply just had my midterms so uhm... i made progress using RabbitMQ and i successfully installed it.
i'm at loss at coding tho' because i made a simple program that looks like this:

Code: Select all

import RPi.GPIO as GPIO
import time
import math
import sys
import pika
GPIO.setmode(GPIO.BOARD)
RG = 8
GR = 10
GPIO.setwarnings(False)
GPIO.setup(RG, GPIO.OUT) #L1
GPIO.setup(GR, GPIO.OUT) #L2
i=1;

while i > 0:
        RG = GPIO.output(8,1)
        GR = GPIO.output(10,0)
        print '\nLane 1:[Red] | Lane 2:{Green}'
        for i in xrange(20,0,-1):
                time.sleep(1)
                sys.stdout.write(str(i)+' \n')
                connection = pika.BlockingConnection(pika.ConnectionParameters(
                host='192.168.42.9'))
                channel = connection.channel()
                channel.queue_declare(queue='hello')
                channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='i')
                print " [x] Sent"
                sys.stdout.flush()
        RG = GPIO.output(8,0)
        GR = GPIO.output(10,1)
        print '\nLane 1 Green Lane 2 Red'
        for i in xrange(20,0,-1):
                time.sleep(1)
                sys.stdout.write(str(i)+' \n')
                sys.stdout.flush()
        i = i+1

what happens is that i can print the value of 'i' but the pika code im working with i think can only send characters? not the changing value of the 'i' so on my receiving raspberry pi it just receives the character 'i'.. any help? thank you.

User avatar
joan
Posts: 15379
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Send Data from RPi to RPi via WiFi

Sun Oct 05, 2014 11:30 am

It's not clear what you are trying to do.

Is this a classroom exercise? If it's not it might be useful to clarify why you are doing what you are doing.

aeyah14
Posts: 19
Joined: Wed Jun 18, 2014 9:56 pm

Re: Send Data from RPi to RPi via WiFi

Sun Oct 05, 2014 11:39 am

its like a simple traffic light system...
i have 2 rgb led's connected in parallel the red-green colors
so the program sets the red-green high and the other green-red low and
then it prints the sleep count from 20..
then switches the red-green low and the gree-red led high
then counts down from 20 again...

what im trying to achieve is to send the countdown from 20 to another Rpi connected. i'm able to send through it but not the countdown values. is there a way to go about in sending it? or should i reprogram the countdown?

User avatar
joan
Posts: 15379
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Send Data from RPi to RPi via WiFi

Sun Oct 05, 2014 12:20 pm

I'm still not sure about the scope you have for any solution.

However this code allows you to control a slave Pi from a master Pi and write to their gpios.

In this case the same gpio numbers are being used on both Pis. The slave Pi is networked with the name "tom."

The code just alternates gpios on/off for a minute.

Code: Select all

#!/usr/bin/env python

# master-client.py
# 2014-10-05
# Public Domain

import time

import pigpio

RED_LED=14
GREEN_LED=15

master = pigpio.pi() # Connect to local Pi (master)
slave  = pigpio.pi("tom") # Connect to remote Pi (slave)

start = time.time()

master.set_mode(RED_LED, pigpio.OUTPUT)
master.set_mode(GREEN_LED, pigpio.OUTPUT)

slave.set_mode(RED_LED, pigpio.OUTPUT)
slave.set_mode(GREEN_LED, pigpio.OUTPUT)

off_state = True

while (time.time()-start) < 60:

   master.write(RED_LED, off_state)
   master.write(GREEN_LED, not off_state)

   slave.write(RED_LED, not off_state)
   slave.write(GREEN_LED, off_state)

   off_state = not off_state

   time.sleep(1)

slave.stop() # Disconnect from slave
master.stop() # Disconnect from master

asandford
Posts: 1998
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Send Data from RPi to RPi via WiFi

Sun Oct 05, 2014 3:28 pm

Using Mosquitto and DougieLawson's code, I've just knocked up these pair:
Publisher

Code: Select all

$ cat mqtt_pub.py
#!/usr/bin/env python
import mosquitto
import os
import time
import sys
from subprocess import call
from time import sleep
broker = "192.168.0.25"
port = 1883
mypid = os.getpid()
sub_uniq = "subclient_"+str(mypid)
mqtts = mosquitto.Mosquitto(sub_uniq)
mqtts.connect(broker, port, 60)
count = 20

while count > 0:
    msg = str(count)
    print("Publishing " + msg)
    mqtts.publish("test/countdown",msg)
    sleep(1)
    count = count -1
$
Subscriber

Code: Select all

$ cat mqtt_sub.py
#!/usr/bin/env python

import mosquitto
import os
import time
import sys
from subprocess import call
from time import sleep

def on_message(obj, msg):
  print ("Message received on topic: "+msg.topic+" with QoS: "+str(msg.qos)+" and payload: "+msg.payload)

broker = "192.168.0.25"
port = 1883
mypid = os.getpid()
sub_uniq = "subclient_"+str(mypid)
mqtts = mosquitto.Mosquitto(sub_uniq)
mqtts.connect(broker, port, 60)

mqtts.on_message = on_message

mqtts.subscribe("test/countdown", 0)

rc = 0
while rc == 0:
    rc = mqtts.loop()
$
Running the sub code in one window and the pub in another results in this:
Sub

Code: Select all

Message received on topic: test/countdown with QoS: 0 and payload: 20
Message received on topic: test/countdown with QoS: 0 and payload: 19
Message received on topic: test/countdown with QoS: 0 and payload: 18
Message received on topic: test/countdown with QoS: 0 and payload: 17
Message received on topic: test/countdown with QoS: 0 and payload: 16
Message received on topic: test/countdown with QoS: 0 and payload: 15
Message received on topic: test/countdown with QoS: 0 and payload: 14
Message received on topic: test/countdown with QoS: 0 and payload: 13
Message received on topic: test/countdown with QoS: 0 and payload: 12
Message received on topic: test/countdown with QoS: 0 and payload: 11
Message received on topic: test/countdown with QoS: 0 and payload: 10
Message received on topic: test/countdown with QoS: 0 and payload: 9
Message received on topic: test/countdown with QoS: 0 and payload: 8
Message received on topic: test/countdown with QoS: 0 and payload: 7
Message received on topic: test/countdown with QoS: 0 and payload: 6
Message received on topic: test/countdown with QoS: 0 and payload: 5
Message received on topic: test/countdown with QoS: 0 and payload: 4
Message received on topic: test/countdown with QoS: 0 and payload: 3
Message received on topic: test/countdown with QoS: 0 and payload: 2
Message received on topic: test/countdown with QoS: 0 and payload: 1
Pub

Code: Select all

Publishing 20
Publishing 19
Publishing 18
Publishing 17
Publishing 16
Publishing 15
Publishing 14
Publishing 13
Publishing 12
Publishing 11
Publishing 10
Publishing 9
Publishing 8
Publishing 7
Publishing 6
Publishing 5
Publishing 4
Publishing 3
Publishing 2
Publishing 1

aeyah14
Posts: 19
Joined: Wed Jun 18, 2014 9:56 pm

Re: Send Data from RPi to RPi via WiFi

Sun Oct 05, 2014 4:02 pm

thanks for the alternative solutions, will try using the mosquitto, it looks good >.<
can i follow this guide in installing mosquitto for your code? http://jpmens.net/2013/09/01/installing ... pberry-pi/
or i should install any specifics?

asandford
Posts: 1998
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Send Data from RPi to RPi via WiFi

Sun Oct 05, 2014 4:43 pm

For the broker, client CLI utils and python support:

Code: Select all

sudo apt-get install mosquitto mosquitto-clients python-mosquitto 
Only install the broker on one pi (unless you want to bridge them).

Return to “Python”