python python树莓派cpu ip
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python python树莓派cpu ip相关的知识,希望对你有一定的参考价值。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
import sys
import socket
import psutil
#gpio's :
SCLK = 18
DIN = 15
DC = 13
RST = 11
font =[
0x00, 0x00, 0x00, 0x00, 0x00, # sp 32
0x00, 0x00, 0x2f, 0x00, 0x00, # !
0x00, 0x07, 0x00, 0x07, 0x00, # "
0x14, 0x7f, 0x14, 0x7f, 0x14, # #
0x24, 0x2a, 0x7f, 0x2a, 0x12, # $
0x62, 0x64, 0x08, 0x13, 0x23, # %
0x36, 0x49, 0x55, 0x22, 0x50, # &
0x00, 0x05, 0x03, 0x00, 0x00, # '
0x00, 0x1c, 0x22, 0x41, 0x00, # ( 40
0x00, 0x41, 0x22, 0x1c, 0x00, # )
0x14, 0x08, 0x3E, 0x08, 0x14, # *
0x08, 0x08, 0x3E, 0x08, 0x08, # +
0x00, 0x00, 0xA0, 0x60, 0x00, # ,
0x08, 0x08, 0x08, 0x08, 0x08, # -
0x00, 0x60, 0x60, 0x00, 0x00, # .
0x20, 0x10, 0x08, 0x04, 0x02, # /
0x3E, 0x51, 0x49, 0x45, 0x3E, # 0 48
0x00, 0x42, 0x7F, 0x40, 0x00, # 1
0x42, 0x61, 0x51, 0x49, 0x46, # 2
0x21, 0x41, 0x45, 0x4B, 0x31, # 3
0x18, 0x14, 0x12, 0x7F, 0x10, # 4
0x27, 0x45, 0x45, 0x45, 0x39, # 5
0x3C, 0x4A, 0x49, 0x49, 0x30, # 6
0x01, 0x71, 0x09, 0x05, 0x03, # 7
0x36, 0x49, 0x49, 0x49, 0x36, # 8
0x06, 0x49, 0x49, 0x29, 0x1E, # 9 57
0x00, 0x36, 0x36, 0x00, 0x00, # :
0x00, 0x56, 0x36, 0x00, 0x00, # ;
0x08, 0x14, 0x22, 0x41, 0x00, # < 60
0x14, 0x14, 0x14, 0x14, 0x14, # =
0x00, 0x41, 0x22, 0x14, 0x08, # > 62
0x02, 0x01, 0x51, 0x09, 0x06, # ? 63
0x32, 0x49, 0x59, 0x51, 0x3E, # @ 64
0x7C, 0x12, 0x11, 0x12, 0x7C, # A 65
0x7F, 0x49, 0x49, 0x49, 0x36, # B
0x3E, 0x41, 0x41, 0x41, 0x22, # C
0x7F, 0x41, 0x41, 0x22, 0x1C, # D
0x7F, 0x49, 0x49, 0x49, 0x41, # E
0x7F, 0x09, 0x09, 0x09, 0x01, # F
0x3E, 0x41, 0x49, 0x49, 0x7A, # G
0x7F, 0x08, 0x08, 0x08, 0x7F, # H
0x00, 0x41, 0x7F, 0x41, 0x00, # I
0x20, 0x40, 0x41, 0x3F, 0x01, # J
0x7F, 0x08, 0x14, 0x22, 0x41, # K
0x7F, 0x40, 0x40, 0x40, 0x40, # L
0x7F, 0x02, 0x0C, 0x02, 0x7F, # M
0x7F, 0x04, 0x08, 0x10, 0x7F, # N
0x3E, 0x41, 0x41, 0x41, 0x3E, # O
0x7F, 0x09, 0x09, 0x09, 0x06, # P
0x3E, 0x41, 0x51, 0x21, 0x5E, # Q
0x7F, 0x09, 0x19, 0x29, 0x46, # R
0x46, 0x49, 0x49, 0x49, 0x31, # S
0x01, 0x01, 0x7F, 0x01, 0x01, # T
0x3F, 0x40, 0x40, 0x40, 0x3F, # U
0x1F, 0x20, 0x40, 0x20, 0x1F, # V
0x3F, 0x40, 0x38, 0x40, 0x3F, # W
0x63, 0x14, 0x08, 0x14, 0x63, # X
0x07, 0x08, 0x70, 0x08, 0x07, # Y
0x61, 0x51, 0x49, 0x45, 0x43, # Z 90
0x00, 0x7F, 0x41, 0x41, 0x00, # [
0x55, 0x2A, 0x55, 0x2A, 0x55, # \
0x00, 0x41, 0x41, 0x7F, 0x00, # ]
0x04, 0x02, 0x01, 0x02, 0x04, # ^
0x40, 0x40, 0x40, 0x40, 0x40, # _
0x00, 0x01, 0x02, 0x04, 0x00, # ' 96
0x20, 0x54, 0x54, 0x54, 0x78, # a 97
0x7F, 0x48, 0x44, 0x44, 0x38, # b
0x38, 0x44, 0x44, 0x44, 0x20, # c
0x38, 0x44, 0x44, 0x48, 0x7F, # d
0x38, 0x54, 0x54, 0x54, 0x18, # e
0x08, 0x7E, 0x09, 0x01, 0x02, # f
0x18, 0xA4, 0xA4, 0xA4, 0x7C, # g
0x7F, 0x08, 0x04, 0x04, 0x78, # h
0x00, 0x44, 0x7D, 0x40, 0x00, # i
0x40, 0x80, 0x84, 0x7D, 0x00, # j
0x7F, 0x10, 0x28, 0x44, 0x00, # k
0x00, 0x41, 0x7F, 0x40, 0x00, # l
0x7C, 0x04, 0x18, 0x04, 0x78, # m
0x7C, 0x08, 0x04, 0x04, 0x78, # n
0x38, 0x44, 0x44, 0x44, 0x38, # o
0xFC, 0x24, 0x24, 0x24, 0x18, # p
0x18, 0x24, 0x24, 0x18, 0xFC, # q
0x7C, 0x08, 0x04, 0x04, 0x08, # r
0x48, 0x54, 0x54, 0x54, 0x20, # s
0x04, 0x3F, 0x44, 0x40, 0x20, # t
0x3C, 0x40, 0x40, 0x20, 0x7C, # u
0x1C, 0x20, 0x40, 0x20, 0x1C, # v
0x3C, 0x40, 0x30, 0x40, 0x3C, # w
0x44, 0x28, 0x10, 0x28, 0x44, # x
0x1C, 0xA0, 0xA0, 0xA0, 0x7C, # y
0x44, 0x64, 0x54, 0x4C, 0x44, # z 122
0x00, 0x08, 0x36, 0x41, 0x00, # {
0x00, 0x00, 0x7F, 0x00, 0x00, # |
0x00, 0x41, 0x36, 0x08, 0x00, # }
0x08, 0x10, 0x08, 0x04, 0x08 # ~ 126
]
def main():
myAddr = get_local_ip()
begin(0xbc) # contrast - may need tweaking for each display
gotoxy(0,0)
text("Hi! Mr BianLe")
gotoxy(0,2)
text("My IP's")
gotoxy(0,3)
text(myAddr)
gotoxy(0,4)
text("keep in mind !")
try:
interval = 0
while 1:
gotoxy(8,5)
text(getCPUstate(interval)+" ")
interval = 10
except (KeyboardInterrupt, SystemExit):
pass
def gotoxy(x,y):
lcd_cmd(x+128)
lcd_cmd(y+64)
def text(words):
for i in range(len(words)):
# print (words[i])
display_char(words[i])
def display_char(char):
index=(ord(char)-32)*5
if ord(char) >=32 and ord(char) <=126:
for i in range(5):
# print (index+i)
lcd_data(font[index+i])
lcd_data(0) # space inbetween characters
#elif ord(char)==32:
# lcd_data(0)
# lcd_data(0)
# lcd_data(0)
# lcd_data(0)
# lcd_data(0)
# lcd_data(0)
def cls():
gotoxy(0,0)
for i in range(84):
for j in range(6):
lcd_data(0)
def setup():
# set pin directions
GPIO.setmode(GPIO.BOARD)
GPIO.setup(DIN, GPIO.OUT)
GPIO.setup(SCLK, GPIO.OUT)
GPIO.setup(DC, GPIO.OUT)
GPIO.setup(RST, GPIO.OUT)
def begin(contrast):
setup()
# toggle RST low to reset
GPIO.output(RST, False)
time.sleep(0.100)
GPIO.output(RST, True)
lcd_cmd(0x21) # extended mode
lcd_cmd(0x14) # bias
lcd_cmd(contrast) # vop
lcd_cmd(0x20) # basic mode
lcd_cmd(0xc) # non-inverted display
cls()
def SPI(c):
# data = DIN
# clock = SCLK
# MSB first
# value = c
for i in xrange(8):
GPIO.output(DIN, (c & (1 << (7-i))) > 0)
GPIO.output(SCLK, True)
GPIO.output(SCLK, False)
def lcd_cmd(c):
# print ("lcd_cmd sent :",hex(c))
GPIO.output(DC, False)
SPI(c)
def lcd_data(c):
# print ("data sent :",hex(c))
GPIO.output(DC, True)
SPI(c)
def get_local_ip(ifname = 'wlan0'):
import socket, fcntl, struct
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))
ret = socket.inet_ntoa(inet[20:24])
return ret
def getCPUstate(interval=1):
return ("CPU:" + str(psutil.cpu_percent(interval)) + "%")
if __name__ == "__main__":
main()
import threading
def timeout():
print 'time out'
global t
t = threading.Timer(5.0,timeout)
t.start()
t = threading.Timer(5.0,timeout)
t.start()
class Timer(threading.Thread):
def __init__(self,seconds):
self.runTime = seconds
threading.Thread.__init__(self)
def run(self):
time.sleep(self.runTime)
print 'Buzzzzzz!'
import threading
def sayhello():
print "hello world"
global t #Notice: use global variable!
t = threading.Timer(5.0, sayhello)
t.start()
t = threading.Timer(5.0, sayhello)
t.start()
import web
"""import sys
import RPi.GPIO as GPIO"""
import time
import threading
import re
PORT=15
STA='0'
LOC='0'
urls=(
"/on","on",
"/off","off",
"/timer","timer",
"/index","index")
app = web.application(urls,globals())
class on:
def GET(self):
"""GPIO.setmode(GPIO.BOARD)
GPIO.setup(PORT,GPIO.OUT)
GPIO.setup(PORT,GPIO.LOW)"""
STA='1'
return STA
class off:
def GET(self):
"""GPIO.setmode(GPIO.BOARD)
GPIO.setup(PORT,GPIO.OUT)
GPIO.setup(PORT,GPIO.HIGH)"""
STA='0'
return STA
class index:
def GET(self):
"""STA=GPIO.input(PORT)"""
return STA
class timer:
# It's time up
def timeout(self):
global LOC
print 'buzzzzzz!'
LOC='0'
def GET(self):
global LOC
form = web.input(dl='')
if(form.dl!='' and LOC=='0'):
t = threading.Timer(int(form.dl),self.timeout)
t.start()
LOC='1'
print 'thread started'
return ""
if __name__=="__main__":
app.run()
# -*- coding: utf-8 -*-
def get_local_ip(ifname = 'wlan0'):
import socket, fcntl, struct
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))
ret = socket.inet_ntoa(inet[20:24])
return ret
print get_local_ip()
#!/usr/bin/env python
#
# $Id: iotop.py 1160 2011-10-14 18:50:36Z g.rodola@gmail.com $
#
# Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Transplant to NT system by hui.wang, 2012-11-28
# Add function of get cpu state and get memory state by hui.wang,2012-11-29
"""
Shows real-time network statistics.
Author: Giampaolo Rodola' <g.rodola@gmail.com>
"""
import sys
import os
import atexit
import time
import psutil
print "Welcome,current system is",os.name," 3 seconds late start to get data..."
time.sleep(3)
line_num = 1
def print_line(str):
print str
#function of Get CPU State
def getCPUstate(interval=1):
return (" CPU: " + str(psutil.cpu_percent(interval)) + "%")
#function of Get Memory
def getMemorystate():
phymem = psutil.phymem_usage()
buffers = getattr(psutil, 'phymem_buffers', lambda: 0)()
cached = getattr(psutil, 'cached_phymem', lambda: 0)()
used = phymem.total - (phymem.free + buffers + cached)
line = " Memory: %5s%% %6s/%s" % (
phymem.percent,
str(int(used / 1024 / 1024)) + "M",
str(int(phymem.total / 1024 / 1024)) + "M"
)
return line
def bytes2human(n):
"""
>>> bytes2human(10000)
'9.8 K'
>>> bytes2human(100001221)
'95.4 M'
"""
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
prefix = {}
for i, s in enumerate(symbols):
prefix[s] = 1 << (i+1)*10
for s in reversed(symbols):
if n >= prefix[s]:
value = float(n) / prefix[s]
return '%.2f %s' % (value, s)
return '%.2f B' % (n)
def poll(interval):
"""Retrieve raw stats within an interval window."""
tot_before = psutil.network_io_counters()
pnic_before = psutil.network_io_counters(pernic=True)
# sleep some time
time.sleep(interval)
tot_after = psutil.network_io_counters()
pnic_after = psutil.network_io_counters(pernic=True)
# get cpu state
cpu_state = getCPUstate(interval)
# get memory
memory_state = getMemorystate()
return (tot_before, tot_after, pnic_before, pnic_after,cpu_state,memory_state)
def refresh_window(tot_before, tot_after, pnic_before, pnic_after,cpu_state,memory_state):
os.system("cls")
"""Print stats on screen."""
#print current time #cpu state #memory
print_line(time.asctime()+" | "+cpu_state+" | "+memory_state)
# totals
print_line(" NetStates:")
print_line("total bytes: sent: %-10s received: %s" \
% (bytes2human(tot_after.bytes_sent),
bytes2human(tot_after.bytes_recv))
)
print_line("total packets: sent: %-10s received: %s" \
% (tot_after.packets_sent, tot_after.packets_recv)
)
# per-network interface details: let's sort network interfaces so
# that the ones which generated more traffic are shown first
print_line("")
nic_names = pnic_after.keys()
nic_names.sort(key=lambda x: sum(pnic_after[x]), reverse=True)
for name in nic_names:
stats_before = pnic_before[name]
stats_after = pnic_after[name]
templ = "%-15s %15s %15s"
print_line(templ % (name, "TOTAL", "PER-SEC"))
print_line(templ % (
"bytes-sent",
bytes2human(stats_after.bytes_sent),
bytes2human(stats_after.bytes_sent - stats_before.bytes_sent) + '/s',
))
print_line(templ % (
"bytes-recv",
bytes2human(stats_after.bytes_recv),
bytes2human(stats_after.bytes_recv - stats_before.bytes_recv) + '/s',
))
print_line(templ % (
"pkts-sent",
stats_after.packets_sent,
stats_after.packets_sent - stats_before.packets_sent,
))
print_line(templ % (
"pkts-recv",
stats_after.packets_recv,
stats_after.packets_recv - stats_before.packets_recv,
))
print_line("")
try:
interval = 0
while 1:
args = poll(interval)
refresh_window(*args)
interval = 10
except (KeyboardInterrupt, SystemExit):
pass
以上是关于python python树莓派cpu ip的主要内容,如果未能解决你的问题,请参考以下文章