Thanks to commenter Fishscene for pointing out the following:
livestreamer for twitch is broken. Twitch made a change to require an OAUTH token and disallow annonymous viewing. livestreamer hasn’t been updated in over a year. Project was forked to streamlink and includes twitch fix:
I’ll leave this for prosperity, it has been linked to several time.
I’m not too proud of this but my laptop couldn’t handle streaming ‘Twitch Plays Pokemon’ and me doing day to day tasks and I can’t stop watching now. I’ve decided to use a spare Raspberry Pi have to keep the stream open 24/7 on a spare monitor I have.
Here’s a video:
Thanks to Livestreamer it is unbelievably easy to view this stream.
To make things even simpler, I have also written the following python program that will open the stream to HDMI. You can set the line that runs this program as a cron job to run at boot if you wish.
Shell
1
2
import os
os.system("livestreamer twitch.tv/twitchplayspokemon best -np 'omxplayer -o hdmi'")
Turns out it’s not that hard at all! Here’s a video of the whole thing working:
This basically works around concepts I first explained here. It’s still really cool though! SPI is really fast and really easy to use, perfect for a novice like me.
Here are the physical representations and schematics of the setup seen on my desk:
So in my last post I showed you a way that I used a single arduino to talk to a Raspberry Pi (or any other computer) over HID. I’ve updated the project a bit and now I can input any number of things into the pi. Basically this is how it works.
Serial Console on computer -> Arduino Mega -> software serial port -> Arduino micro -> HID on other second computer (in this case the Pi)
There are two buttons that handle pressing the enter key and the delete key as those are hard to send over serial.
There are a few bits of relevant code, both for the arduino. Here’s the mega’s code:
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
intsent_led=44;
inttest_button=30;
byteinread;
#include <SoftwareSerial.h>
SoftwareSerial SoftSerial(34,36);// RX, TX
voidsetup(){
pinMode(sent_led,OUTPUT);
pinMode(test_button,INPUT);
Serial.begin(9600);
SoftSerial.begin(4800);
SoftSerial.println("Hello, world?");
}
voidloop(){
if(Serial.available()>0){
digitalWrite(sent_led,HIGH);
Serial.print("Sending: ");
inread=Serial.read();
Serial.write(inread);
SoftSerial.write(inread);
Serial.print(" , ");
Serial.print(inread);
Serial.println("");
digitalWrite(sent_led,LOW);
}
}
And here’s the side for the arduino micro, that writes as an HID.
Desperate times call for desperate measures. I recently found out that a club at my school is essentially a hackerspace equipped with 3D printers so I could finally print enclosures for the PiPlanter and the DSFU. But that means I would need to finial the designs for both of them. I needed to find a way to interface with this thing here. So being the idiot that I am, I forgot a USB keyboard, I forgot an analog video cable, I forgot a monitor for the pi and forgot to update the Pi to the latest Raspian build.
What I did bring was an Easy Cap Capture Card in addition to my standard idea-kit which among other things consists of an Arduino Micro, some buttons, some resistors, and wire.
Essentially all I needed to do was connect the Pi to the EasyCap, and be able to send 4 different phrases to it over the keyboard. I needed to be able to send ‘pi’,’password’ (not my actual password), ‘ifconfig’ and enter. I realized that I could easily send this data to the Pi via the Micro as it has the Keyboard. functions built in.
As you can see in the video it worked! And I’m kind of stunned that it did. I can now SSH into the Pi.
Now everything should be good to go, now for the python.
You can debug this any way you like, but my favorite way to do it is using the program geany. I like to start up a VNC server with root so I don’t get into any trouble with the GPIO permissions.
But here’s the program.
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/python
# -*- coding: utf-8 -*-
importspidev
importtime
spi=spidev.SpiDev()
spi.open(0,0)
count=0
defreadadc(adcnum):
# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
ifadcnum>7oradcnum<0:
return-1
r=spi.xfer2([1,8+adcnum<<4,0])
adcout=((r[1]&3)<<8)+r[2]
returnadcout
whileTrue:
tmp1=int(round(readadc(0)/10.24))
print"in1:",tmp1
count=count+1
time.sleep(0.2)
And that’s pretty much it, the result should look something like this:
Hello! as you can probably tell, my last post was written in a fury of incoherency, but I needed to get the code out there so it is what it is.
The main focus of this post is to showcase the arduino program. The visual basic in the video is very simple, and there will be much more on that later.
This below program will take a string of characters fed to the arduino and split them into usable parts. This is a very valuable tool for working with serial and arduino. It’s pretty well commented, but if you have any questions, PLEASE leave a comment. I’d love to see some conversation here.
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
constcharEOPmarker='.';//This is the end of packet marker
charserialbuf[32];//This gives the incoming serial some room. Change it if you want a longer incoming.
#include // we'll need this for subString
#define MAX_STRING_LEN 20 // like 3 lines above, change as needed.
#include //we'll need this for the lcd
LiquidCrystal lcd(7,8,9,10,11,12);//pins for the lcd, I set it up using the ladyada tutorial.
voidsetup(){
lcd.begin(16,2);
Serial.begin(9600);//changing this to other speeds has not been tested using this meathod
}
voidloop(){
if(Serial.available()>0){//makes sure something is ready to be read
lcd.clear();//clears for incoming stuff, won't clear if there isin't data to be read
staticintbufpos=0;//starts the buffer back at the first position in the incoming serial.read
charinchar=Serial.read();//assigns one byte (as serial.read()'s only input one byte at a time
if(inchar!=EOPmarker){//if the incoming character is not the byte that is the incoming package ender
serialbuf[bufpos]=inchar;//the buffer position in the array get assigned to the current read
bufpos++;//once that has happend the buffer advances, doing this over and over again until the end of package marker is read.
}
else{//once the end of package marker has been read
serialbuf[bufpos]=0;//restart the buff
bufpos=0;//restart the position of the buff
lcd.write(subStr(serialbuf,",",1));//witres the first bit of content before the first comma (or other seperator) to the lcd. You could also do math or anything else with these. You could use atoi to change them to integers.
lcd.write("|separator|");//this signifies that the first seperation has occured
lcd.write(subStr(serialbuf,",",2));//same thing as 2 lines above, but with the second parts. this can be repeated
}
}
}
// below is just function logic, which I do not fully understand. but it works.
//Thanks to http://arduino.cc/forum/index.php?topic=119429
So for example if you inputted
[code]
123,456.
[/code]
it would output
[code]
123|separator|456
[/code]
to the lcd, or the serial monitor if you tweaked the code.
Now for the code in the video. The only different part about this is that it writes the two values to the servos.
C
1
const char EOPmarker = ‘.’; //This is the end of packet marker
char serialbuf[32]; //This gives the incoming serial some room. Change it if you want a longer incoming.
#include // we’ll need this for subString
#define MAX_STRING_LEN 20 // like 3 lines above, change as needed.
#include //we’ll need this for the lcd
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); //pins for the lcd, I set it up using the ladyada tutorial.
#include
Servo left_servo;
Servo right_servo;
int left_servo_pos;
int right_servo_pos;
void setup(){
lcd.begin(16, 2);
left_servo.attach(2);
right_servo.attach(3);
Serial.begin(9600); //changing this to other speeds has not been tested using this meathod
}
void loop() {
if (Serial.available() > 0) { //makes sure something is ready to be read
lcd.clear(); //clears for incoming stuff, won’t clear if there isin’t data to be read
static int bufpos = 0; //starts the buffer back at the first position in the incoming serial.read
char inchar = Serial.read(); //assigns one byte (as serial.read()’s only input one byte at a time
if (inchar != EOPmarker) { //if the incoming character is not the byte that is the incoming package ender
serialbuf[bufpos] = inchar; //the buffer position in the array get assigned to the current read
bufpos++; //once that has happend the buffer advances, doing this over and over again until the end of package marker is read.
}
else { //once the end of package marker has been read
serialbuf[bufpos] = 0; //restart the buff
bufpos = 0; //restart the position of the buff
left_servo_pos = atoi(subStr(serialbuf, “,”, 1));
lcd.write(“Left Servo:”);
lcd.write(subStr(serialbuf, “,”, 1)); //witres the first bit of content before the first comma (or other seperator) to the lcd
left_servo.write(left_servo_pos);
lcd.setCursor(0, 1);
right_servo_pos = atoi(subStr(serialbuf, “,”, 2));
lcd.write(“Right Servo:”); //this signifies that the first seperation has occured
lcd.write(subStr(serialbuf, “,”, 2)); //same thing as 2 lines above, but with the second parts. this can be repeated
right_servo.write(right_servo_pos);
}
}
}
// below is just function logic, which I do not fully understand. but it works.
char* subStr (char* input_string, char *separator, int segment_number) {
char *act, *sub, *ptr;
static char copy[MAX_STRING_LEN];
int i;
strcpy(copy, input_string);
for (i = 1, act = copy; i <= segment_number; i++, act = NULL) {
sub = strtok_r(act, separator, &ptr);
if (sub == NULL) break;
}
return sub;
}
//esologic.com
//Thanks to http://arduino.cc/forum/index.php?topic=119429
Hello! sorry about my absence, I’ve been very busy with school/baseball over the past few weeks, but summer is soon, and I’ll be updating with a higher frequency once that happens.
So. you’ll need a few things in order to make sure this whole process works.
1. Multiple ethernet connections.
2. An Arduino and a compatible wiznet device (ethernet shield)
Here I’m using an arduino knockoff I got 2 years ago when I first got into hobby electronics, and an “ETHERNET 4 NANO” by gravitech. I didn’t want to permanently implement my arduino nano, but I still wanted to be able to do the project. It’s powered by 5v from my computer (well from a powered hub) and the brown striped wire goes to the + on the powerswitch tail II. The blue cable is ethernet.
3. A webserver with php installed on it. (Mine’s just LAMP’d)
This is the important part. The arduino is accessing a .php document on the server to tell weather or not the lamp should be on. The UI is also hosted out of the servo. That laptop is my old HP laptop that I’ve had for almost 6 years, it’s now got ubuntu 10.04 LTS on it and it’s been LAMP’d among other things.
4. Something that can SAFELY switch line voltage. (I’m using a powerswitch tail II, because I really don’t want to get killed, and it’s also very simple – if you’re using naked relays, please for the love of god, be careful with line voltages.)
This thing is a beast. I can drive it via 5v easily, and it switches line like a champ. It’s been in constant use since my last post about home automation.
START OF GUIDE
1. So first, we need to hook up the shield, the arduino and the PSTII. It’s really easy.
2. Get linux on the computer you want to run the server out of. I made a video a long time ago back when I was first getting into linux. The installation process is still the same, you can find that video here.
3. Install various programs your server. First things first, you have to install openssh by running the command:
sudo apt-get install openssh
This will make the server headless, and it will enable you to access the server via command line. Specifically through PuTTY for windows. You can then ssh to your server which is key. You then need to install LAMP by running the command:
sudo apt-get install LAMP
This will install PHP on your server as wells apache and mysql. The two ladder objects are not essential for this project, but if you are going to use the server for other things, I strongly recommend you get them as well. The install process is very easy, just make sure you WRITE EVERY USERNAME AND PASSWORD YOU USE DOWN. I’ve had to reinstall several times before I learned my lesson on this.
After you are done with that, install vim by running the command:
sudo apt-get install vim
Vim is a text editing program for linux. You also need to get pure ftp to upload and download files from your server. Get it by running the command:
sudo apt-get install pure-ftpd
Congrats! you are now an owner of a very powerful linux server. Wasn’t that easy?
4. Upload the call.php, res.php documents to your server.
This is the call.php text, you just need to make it into a .php document and upload it to the server. This document creates a form that the user accesses to turn the lamp on and off.
This is the res.php. It is what the user is taken to once they have entered the information in the call.php.
If you walk though the code. You will see that once the correct password gets entered, the res.php document creates a functioning document stat.php. This is the same document that the arduino reads, and compares to.
Here it is, upload it to your board with the shield attached, and everything should work. Access the call.php via your server, and go through the process.
At this point everything should work, please let me know if it doesn’t in the comments.
This is a round about way of using a servo to move an analog sensor to 3 points to triangulate the servo degree where the photocell experiences the most light.
The servo sweeps back and forth and then another servo points to where the brightest point was.
It is very simple wiring, but the code is a little fancy. It’s all commented up here, but as always deduction is your friend.