Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@libbymiller
Last active April 22, 2016 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save libbymiller/40348f6a3ce35436b683dd236132ceeb to your computer and use it in GitHub Desktop.
Save libbymiller/40348f6a3ce35436b683dd236132ceeb to your computer and use it in GitHub Desktop.
Petey autostarted with supervisor on a Pi B+ with physical buttons and wifi enabled.

install the wifi stuff

git clone https://github.com/radiodan/provision
cd provision
sudo ./provision node

git fetch origin
git checkout -b minimal origin/minimal

sudo apt-get install libv8-dev -y
sudo ./provision iptables wifi-connect

sudo apt-get install supervisor -y
git clone https://github.com/njh/petey.git
cd 
cd petey

sudo apt-get install ruby bundler pulseaudio pulseaudio-utils libssl-dev -y

cat Gemfile

source 'https://rubygems.org'

gem 'mqtt', '~>0.3.1'
gem 'pi_piper'

install ruby libs for petey

bundle install

edit petey a bit /home/pi/petey/petey.rb

#!/usr/bin/env ruby

require 'mqtt'
require 'io/console'
require 'pi_piper'

username = ENV['USER'] || 'anonymous'
client = MQTT::Client.connect('test.mosquitto.org')

def record_audio
  IO.popen('parec --raw --format=alaw --channels=1 --rate=8000', 'rb') do |parec|
    while_pressing_space
    Process.kill('TERM', parec.pid)
    parec.read
  end
end

# Create a seperate thread, that deals with playing the audio

receive = Thread.new do
  client.get('petey/+/audio') do |topic,message|
  puts "Got audio from #{topic}."
    IO.popen("runuser -l pi -c 'paplay --raw --format=alaw --channels=1 --rate=8000 /home/pi/petey/oink.alaw'", 'wb') do |paplay|
      paplay.write(message)
    end
  end
end


listen = Thread.new do
  puts "listen thread called"
  PiPiper.watch :pin => 17,:trigger => :falling , :pull => :down do
    puts "oink!"
    client.publish("petey/#{username}/audio", File.read('/home/pi/petey/oink.alaw'))
  end
  PiPiper.wait
end


STDOUT.sync = true
puts "Welcome to Petey!"

loop do
end

client.disconnect

supervisor for petey on start /etc/supervisor/conf.d/petey.conf

# run a petey instance
[program:petey]
command=/usr/bin/ruby /home/pi/petey/petey.rb
directory=/home/pi/petey
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=1
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=1

edit the hostname and wifi name

/etc/systemd/system/wifi-connect.service

/etc/hosts
/etc/hostname

reboot

connect to the wifi network (radiodan-configuration by default) tell it your wifi network click the button to oink

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment