1

I am using Sonic Pi 3.3.1 on my Windows PC and a Python script(Python3.7.2) on my raspberry pi(Raspbian Buster) that detects distance from an HC-SR04-Ultrasonic Sensor .The program then creates a tune with a pitch that ranges higher if the object is further away, this tune is then sent over OSC to Sonic Pi. External OSC is enabled on my windows PC. I also checked the Port and IP addresses, and they are correct.

I have tested my circuit extensively and I can confidently say this isn't the source of the problem, and that I added it for documentation purposes only at the bottom of my post for anyone who is interested, so I will move on.

My Python code:

from gpiozero import DistanceSensor
from time import sleep

from pythonosc import osc_message_builder
from pythonosc import udp_client

sensor = DistanceSensor(echo=17, trigger=4)
sender = udp_client.SimpleUDPClient('10.0.0.119', 4560)

while True:
    pitch = round(sensor.distance * 100 + 30)
    print(pitch)
    sender.send_message('/play_this', pitch)
    sleep(0.1)

Output:

60
60
58
59

The problem doesn't seem to be my python code as it logs the correct values and successfully sends the pitch so once again I will move on.

My sonic Pi code:

live_loop :listen do
  use_real_time
  note = sync "/osc/play_this"
  play note[0]
end

Output:

Sonic Pi Output

Sonic Pi log's the notes correctly but doesn't play it and this so the data is being received but isn't being played so the problem is most likely how I am trying to play the note but I can't find the correct way to do this


I then tried this, to test if sonic pi installed correctly and is able to play sound:

live_loop :foo do
  play 60
  sleep 1
end

Sonic Pi did play a sound so I can rule out a defective installation where sound is not installed properly.


My Circuit:

The Vcc pin on the HC-SR04-Ultrasonic Sensor is connected to the 5V power supply on the raspberry pi, then the Trig pin is connected to the 4th GPIO connector on the board the echo pin is then connected to the 17th GPIO connector between a voltage divider consisting of a 330 ohm resistor and a 470 ohm resistor which protects the raspberry from a full 5V.

My circuit looks like: circuit

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
TERMINATOR
  • 1,180
  • 1
  • 11
  • 24

1 Answers1

0

Solution


The problem is in the Sonic Pi que path, that receives the note from the python scipt over a UDP connection.More specifically it is in the sync statement of the que path. The sync statement is missing the senders Ip address and Port which is necessary when using multiple computers unlike in a local environment

You can either explicitly mention the Ip address like so:

live_loop :listen do
  use_real_time
  note = sync "/osc/10.0.0.80:56505/play_this "#Mention the senders Ip address and port
  play note[0]
end

Or you can tell it to accept input regardless of the sender like so:

live_loop :listen do
  use_real_time
  note = sync "/osc*/play_this "#This accepts a message regardless of the sender
  play note[0]
end

I would like to end my post by thanking Sam Aaron who gave me insight on this problem on the Raspberry pi Stack Exchange community which enabled me to answer my this question

TERMINATOR
  • 1,180
  • 1
  • 11
  • 24