根据文章https://blog.csdn.net/ccccc_Jun/article/details/77319593实现树莓派自动连接WiFi
并修改/etc/wpa_supplicant/wpa_supplicant.conf如下,即可在开启手机热点的时候让树莓派自动连接“WIFI_ID”,未开启手机热点时连接 ZJUWLAN。
注意:当树莓派连接了 WIFI 之后,除非 WIFI 断开了连接,否则即使是高优先级的 WIFI 开启,树莓派也不会重新连接到高优先级的 WIFI 上。
network={
ssid="WIFI_ID"
psk="WIFI_passwd"
priority=10
}
network={
ssid="ZJUWLAN"
key_mgmt=NONE
priority=5
}
根据文章https://www.kawabangga.com/posts/1398稍加修改代码实现了树莓派自动上报 IP和自动连接 ZJUWLAN 的功能
#!/usr/bin/python
# -*- coding:utf-8 -*-
__author__ = 'laixintao'
import fcntl
import os
import re
import smtplib
import socket
import struct
import time
import urllib
import urllib2
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# the e-mail config
# this is just a simple format,this e-mail doesn't exist.
smtpserver = "smtp service for sender mail"
username = "ID for sender mail"
password = "password for sender mail"
sender = "ID for sender mail"
receiver = [list of "ID for receiver mail"]
subject = "[RPI]IP CHANGED"
# file_path config
file_path = "/root/rootcrons/lastip.txt"
def sendEmail(msghtml):
msgRoot = MIMEMultipart('related')
msgRoot["To"] = ','.join(receiver)
msgRoot["From"] = sender
msgRoot['Subject'] = subject
msgText = MIMEText(msghtml, 'html', 'utf-8')
msgRoot.attach(msgText)
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
def check_network():
result = os.system("ping baidu.com -c 2")
if result:
print("Network is not ready")
return False
print("Already connect Network")
return True
def get_lan_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("1.1.1.1", 80))
ipaddr = s.getsockname()[0]
s.close()
return ipaddr
class Getmyip:
def getip(self):
try:
myip = self.visit("https://ip.sb/")
except:
try:
myip = self.visit("http://1111.ip138.com/ic.asp")
except:
try:
myip = self.visit("http://ip.chinaz.com/")
except:
try:
myip = self.visit("http://www.whereismyip.com/")
# if you want to add more,use the format "except try"
# make sure the most useful link be the first
except:
print "Fail to get the Network ip."
print "Get the LAN ip."
myip = get_lan_ip()
return myip
def visit(self, url):
opener = urllib2.urlopen(url, timeout=20)
if url == opener.geturl():
str = opener.read()
print "IP information from", url
return re.search('\d+\.\d+\.\d+\.\d+', str).group(0)
def get_network_ip():
getmyip = Getmyip()
localip = getmyip.getip()
return localip
if __name__ == '__main__':
if not check_network():
os.system(
"curl \"http://net.zju.edu.cn/srun_portal_pc.php?&ac_id=3\" --data \"action=login&username=“your ID for ZJUWLAN”&password=“your password for ZJUWLAN”&ac_id=3&user_ip=&nas_ip=&user_mac=&save_me=1&ajax=1\""
)
if check_network():
ipaddr = get_network_ip()
lanip = get_lan_ip()
emailip = str(ipaddr) + " " + str(lanip)
print(emailip)
ip_file = open(file_path)
last_ip = ip_file.read()
ip_file.close()
if last_ip == emailip:
print "IP not change."
else:
print "IP changed. New ip: {}".format(emailip)
sendEmail(emailip)
print "Successfully send the e-mail."
ip_file = open(file_path, "w")
ip_file.write(str(emailip))
ip_file.close()