Nothing Special   »   [go: up one dir, main page]

Skip to content

lastralab/RaspberryPi

Repository files navigation

raspberry-logo

Blink + Pushbutton example

First step:

blink

Wiring:

screenshot 2017-04-04 21 32 03

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:

ledpush

Write a simple server

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.

rpi

Tweeting from Raspberry Pi

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:

2017-04-24-200516_1280x720_scrot

#myFirstTweet:

screenshot 2017-04-24 20 21 08

Counting tweets

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")

Use PWM to make an LED fade

The Wiring:

screenshot 2017-04-28 10 46 52

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:

2017-04-28-104910_1280x720_scrot

You can press Ctrl+C to exit:

screenshot 2017-04-28 10 51 11

Demo:




l'astra lab icon