First step:
Wiring:
Pin Reference:
Using VNC Viewer to control the Raspberry Pi from your phone:
Download VNC Viewer from here: https://www.realvnc.com/download/viewer/ and get creative!
Python Code:
#Pushbutton + LED
#Author: Niam Moltta
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(16, GPIO.OUT)
try:
while True:
inputValue= GPIO.input(12)
if (inputValue == False): #Not pressed
print("LED is blinking")
GPIO.output(16, True)
time.sleep(1)
GPIO.output(16, False)
time.sleep(1)
else: #Pressed
GPIO.output(16, True) #LED ON
time.sleep(0.03)
print("LED is on")
except KeyboardInterrupt:
print("Cleaning up GPIO...")
GPIO.cleanup()
The instruction is that the LED must be blinking until you press the button. When you press the button it has to be turned on until you release the button.
Demo:
Print "Got a request" on Raspberry Pi screen when the server has received a request.
#Author: Niam Moltta
import socket
import sys
ms = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
msinfo = socket.getaddrinfo(None, 1234)
print(msinfo[3][4])
try:
ms.bind(msinfo[3][4])
except socket.error:
print"Failed to bind"
sys.exit()
ms.listen(5)
while True:
conn, addr = ms.accept()
data = conn.recv(1024)
if not data:
break
print "Got a request!"
print data
conn.close()
ms.close()
You access the socket through a terminal window and send messages to the server.
You need:
-A twitter account
-Go to www.apps.twitter.com
-Create your app
-Create your python program and run it from your raspberry pi
from twython import Twython, TwythonError
#be sure to keep these keys safe or else, everybody could manage your twitter account
C_KEY = "your own API KEY"
C_SECRET= "your API SECRET"
A_TOKEN = "your ACCESS TOKEN"
A_SECRET = "your ACCESS TOKEN SECRET"
pipitan = Twython(C_KEY, C_SECRET, A_TOKEN, A_SECRET)
message= 'This is my Raspberry Pi tweeting. #myFirstTweet'
try:
pipitan.update_status(status= message)
except TwythonError as e:
print e
print status +" ------tweeted"
Tweeting from Raspberry Pi:
#myFirstTweet:
Python code:
#Author: Niam Moltta
from twython import TwythonStreamer
C_K = "your key"
C_S = "your secret key"
A_T = "your token"
A_S = "your secret token"
tweetcount = 0
def increment():
global tweetcount
tweetcount = tweetcount+1
class MyStreamer(TwythonStreamer):
def on_success(self, data):
text = 'text'
for text in data:
increment()
print tweetcount
break
if tweetcount == 3:
print"I have to pee a lot"
stream = MyStreamer(C_K, C_S, A_T, A_S)
stream.statuses.filter(track="I have to pee")
The Wiring:
Python Code:
#Author: Niam Moltta
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
pwm = GPIO.PWM(12, 50)
pwm.start(0)
print "Let's fade!"
try:
while True:
for i in range(100):
pwm.ChangeDutyCycle(i)
time.sleep(0.02)
for i in range(100, 0, -1):
pwm.ChangeDutyCycle(i)
time.sleep(0.02)
except KeyboardInterrupt:
GPIO.cleanup() #Ctrl+C to exit
print "Good bye!"
Program working:
You can press Ctrl+C to exit:
Demo: