新人博主Github==>https://github.com/MartinxMax
这个项目能做什么?
解除局域网内限制,实现任意地方远程控制开关电闸,远程控制空调开关等…
用到了哪些编程语言?
C语言,Python3.6以上版本,附源代码
需要什么硬件?
最低总合计约83¥
1.STM32F03CT6开发板(价格23¥左右)
2.I2C驱动的4针脚OLED显示屏 (价格14¥左右)
-
SIM900A模块 (价格28¥左右)
-
可以流量上网的SIM手机卡 (移动或联通)
-
28BYJ-48步进电机(5V)+ULN2003驱动板 (9¥左右)
-
一台装有Python环境的服务器
-
发光二极管(用于网络检测)
-
CP2102(用于调试SIM900A,9¥左右)
-
天线(SIM900A模块天线靠太近容易干扰影响通讯)
硬件接线
配置SIM900A
接线图
插入手机卡配置AT命令
1、AT\r\n
2、AT+CGCLASS="B"\r\n
[ AT+CGCLASS="B",设置移动台类别为 B]
3、AT+CGDCONT=1,"IP","CMNET"\r\n
[设置 PDP 上下文标志为 1,接入点为"CMNET"]
4、AT+CGATT=1\r\n
[GPRS业务]
5、AT+CIPCSGP=1,"CMNET"\r\n
STM32 C语言代码
# 将这一行改写成你的服务器IP
Serial_SendString("AT+CIPSTART=\"TCP\",\"xx.xx.xx.xx\",\"10030\"\r\n");
服务器 Python流量转发代码
#!/usr/bin/python3
import socket
import sys
import time
import datetime
import threading
DEV_SOCK_AND_USERNAME_PORT = list()
HCK_SOCK_AND_USERNAME_PORT = list()
mutex = threading.Lock()
def SOCK_Dev(PORT):
global DEV_SOCK_AND_USERNAME_PORT,HCK_SOCK_AND_USERNAME_PORT
MIAN_DEV_SOCK = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
MIAN_DEV_SOCK.bind(("", PORT))
MIAN_DEV_SOCK.listen(1)
while True:
print(f"[*]等待远程设备连接...\033[4;33m{PORT}\033[0m")
DEV_SOCKET, DevIP = MIAN_DEV_SOCK.accept()
print(f"[\033[34m+\033[0m]设备 \033[32m{DevIP[0]}\033[0m:\033[4;33m{DevIP[1]}"
f"\033[0m ---------- [\033[32mOnline\033[0m]-[\033[4;33m{PORT}\033[0m]")
DEV_SOCK_AND_USERNAME_PORT.append(DEV_SOCKET)
DEV_SOCK_AND_USERNAME_PORT.append(DevIP[0])
DEV_SOCK_AND_USERNAME_PORT.append(DevIP[1])
while True:
try:
DEV_SOCKET.send(b'x')
except:
break
else:
time.sleep(5)
DEV_SOCK_AND_USERNAME_PORT.remove(DEV_SOCKET)
DEV_SOCK_AND_USERNAME_PORT.remove(DevIP[0])
DEV_SOCK_AND_USERNAME_PORT.remove(DevIP[1])
def Get_DATA(SOCK,Name,Mote):
DATA = SOCK.recv(1024).decode()
if len(DATA) <= 0: # 断开连接了
if Mote == 0:
print(f"[\033[34m-\033[0m]\033[32m{Name}\033[0m ---------- [\033[32mOffline\033[0m]")
else:
print(f"[\033[34m-\033[0m]\033[31m{Name}\033[0m ---------- [\033[32mOffline\033[0m]")
return 0
return DATA
def SOCK_Hacker(PORT):
global DEV_SOCK_AND_USERNAME_PORT,HCK_SOCK_AND_USERNAME_PORT
MIAN_HACK_SOCK = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
MIAN_HACK_SOCK.bind(("", PORT))
MIAN_HACK_SOCK.listen(1)
while True:
print(f"[*]等待控制者连接...\033[4;33m{PORT}\033[0m")
HACK_SOCKET, Hacker_IP = MIAN_HACK_SOCK.accept()
print(f"[\033[34m+\033[0m]控制者 \033[31m{Hacker_IP[0]}\033[0m:\033[4;33m{Hacker_IP[1]}"
f"\033[0m ---------- [\033[32mOnline\033[0m]-[\033[4;33m{PORT}\033[0m]")
HACK_SOCKET.send(b"[+]Server Connect You !")
HCK_SOCK_AND_USERNAME_PORT.append(HACK_SOCKET)
HCK_SOCK_AND_USERNAME_PORT.append(Hacker_IP[0])
HCK_SOCK_AND_USERNAME_PORT.append(Hacker_IP[1])
#HCK_SOCK = HACK_SOCKET
while True:
DATA = Get_DATA(HACK_SOCKET, Hacker_IP[0],1)
mutex.acquire()
if DATA != 0:
if 'EXIT' in DATA:
HACK_SOCKET.send("Socket Cloese-----[Success]".encode())
HCK_SOCK_AND_USERNAME_PORT[0].close()
print(f"[\033[34m-\033[0m]Hacker SOCKET 已被关闭")
break
elif 'DEV' in DATA:
DEV_SOCK_AND_USERNAME_PORT[0].send(b'y')
HACK_SOCKET.send("Device Cloese Socket-----[Success]".encode())
if DEV_SOCK_AND_USERNAME_PORT:
HACK_SOCKET.send(f"Device----[Online]\r\n".encode())
now = datetime.datetime.now()
print(
f"\033[31m{Hacker_IP[0]}\033[0m ==> \033[32m{DEV_SOCK_AND_USERNAME_PORT[1]}\033[0m ---------- [\033[32mOK\033[0m] %s" % (
now.strftime("%Y-%m-%d %H:%M:%S")))
if 'HACK' in DATA:
DEV_SOCK_AND_USERNAME_PORT[0].send(b'o')
HACK_SOCKET.send(f"Server Send {DEV_SOCK_AND_USERNAME_PORT[1]} RUN Command !!Success!!".encode())
print(f"启动----------[\033[32mOK\033[0m]")
elif 'STOP' in DATA:
DEV_SOCK_AND_USERNAME_PORT[0].send(b'c')
HACK_SOCKET.send(f"Server Send {DEV_SOCK_AND_USERNAME_PORT[1]} STOP Command !!Success!!".encode())
print(f"停止----------[\033[32mOK\033[0m]")
else:
HACK_SOCKET.send(f"Device----[OffLine]".encode())
mutex.release()
else:
HACK_SOCKET.close()
mutex.release()
break
HCK_SOCK_AND_USERNAME_PORT.remove(HACK_SOCKET)
HCK_SOCK_AND_USERNAME_PORT.remove(Hacker_IP[0])
HCK_SOCK_AND_USERNAME_PORT.remove(Hacker_IP[1])
def main():
Hacker = threading.Thread(target=SOCK_Hacker,args=(10031,))
Device = threading.Thread(target=SOCK_Dev,args=(10030,))
Hacker.start()
Device.start()
if __name__ == '__main__':
main()
运行效果图
这里有些小伙伴可能会出现乱码和报错,把全部中文改成英文即可,若系统并非Linux则将带有print中输出颜色代码(\033[3x和\033)去除即可
无法识别中文修改版
#!/usr/bin/python3
import socket
import sys
import time
import datetime
import threading
DEV_SOCK_AND_USERNAME_PORT = list()
HCK_SOCK_AND_USERNAME_PORT = list()
mutex = threading.Lock()
def SOCK_Dev(PORT):
global DEV_SOCK_AND_USERNAME_PORT,HCK_SOCK_AND_USERNAME_PORT
MIAN_DEV_SOCK = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
MIAN_DEV_SOCK.bind(("", PORT))
MIAN_DEV_SOCK.listen(1)
while True:
print(f"[*]Waiting for remote device connection...\033[4;33m{PORT}\033[0m")
DEV_SOCKET, DevIP = MIAN_DEV_SOCK.accept()
print(f"[\033[34m+\033[0m]Device \033[32m{DevIP[0]}\033[0m:\033[4;33m{DevIP[1]}"
f"\033[0m ---------- [\033[32mOnline\033[0m]-[\033[4;33m{PORT}\033[0m]")
DEV_SOCK_AND_USERNAME_PORT.append(DEV_SOCKET)
DEV_SOCK_AND_USERNAME_PORT.append(DevIP[0])
DEV_SOCK_AND_USERNAME_PORT.append(DevIP[1])
while True:
try:
DEV_SOCKET.send(b'x')
except:
break
else:
time.sleep(5)
DEV_SOCK_AND_USERNAME_PORT.remove(DEV_SOCKET)
DEV_SOCK_AND_USERNAME_PORT.remove(DevIP[0])
DEV_SOCK_AND_USERNAME_PORT.remove(DevIP[1])
def Get_DATA(SOCK,Name,Mote):
DATA = SOCK.recv(1024).decode()
if len(DATA) <= 0:
if Mote == 0:
print(f"[\033[34m-\033[0m]\033[32m{Name}\033[0m ---------- [\033[32mOffline\033[0m]")
else:
print(f"[\033[34m-\033[0m]\033[31m{Name}\033[0m ---------- [\033[32mOffline\033[0m]")
return 0
return DATA
def SOCK_Hacker(PORT):
global DEV_SOCK_AND_USERNAME_PORT,HCK_SOCK_AND_USERNAME_PORT
MIAN_HACK_SOCK = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
MIAN_HACK_SOCK.bind(("", PORT))
MIAN_HACK_SOCK.listen(1)
while True:
print(f"[*]Waiting for the controller to connect...\033[4;33m{PORT}\033[0m")
HACK_SOCKET, Hacker_IP = MIAN_HACK_SOCK.accept()
print(f"[\033[34m+\033[0m]Controller \033[31m{Hacker_IP[0]}\033[0m:\033[4;33m{Hacker_IP[1]}"
f"\033[0m ---------- [\033[32mOnline\033[0m]-[\033[4;33m{PORT}\033[0m]")
HACK_SOCKET.send(b"[+]Server Connect You !")
HCK_SOCK_AND_USERNAME_PORT.append(HACK_SOCKET)
HCK_SOCK_AND_USERNAME_PORT.append(Hacker_IP[0])
HCK_SOCK_AND_USERNAME_PORT.append(Hacker_IP[1])
#HCK_SOCK = HACK_SOCKET
while True:
DATA = Get_DATA(HACK_SOCKET, Hacker_IP[0],1)
mutex.acquire()
if DATA != 0:
if 'EXIT' in DATA:
HACK_SOCKET.send("Socket Cloese-----[Success]".encode())
HCK_SOCK_AND_USERNAME_PORT[0].close()
print(f"[\033[34m-\033[0m]Hacker SOCKET 已被关闭")
break
elif 'DEV' in DATA:
DEV_SOCK_AND_USERNAME_PORT[0].send(b'y')
HACK_SOCKET.send("Device Cloese Socket-----[Success]".encode())
if DEV_SOCK_AND_USERNAME_PORT:
HACK_SOCKET.send(f"Device----[Online]\r\n".encode())
now = datetime.datetime.now()
print(
f"\033[31m{Hacker_IP[0]}\033[0m ==> \033[32m{DEV_SOCK_AND_USERNAME_PORT[1]}\033[0m ---------- [\033[32mOK\033[0m] %s" % (
now.strftime("%Y-%m-%d %H:%M:%S")))
if 'HACK' in DATA:
DEV_SOCK_AND_USERNAME_PORT[0].send(b'o')
HACK_SOCKET.send(f"Server Send {DEV_SOCK_AND_USERNAME_PORT[1]} RUN Command !!Success!!".encode())
print(f"Runing----------[\033[32mOK\033[0m]")
elif 'STOP' in DATA:
DEV_SOCK_AND_USERNAME_PORT[0].send(b'c')
HACK_SOCKET.send(f"Server Send {DEV_SOCK_AND_USERNAME_PORT[1]} STOP Command !!Success!!".encode())
print(f"Stoping----------[\033[32mOK\033[0m]")
else:
HACK_SOCKET.send(f"Device----[OffLine]".encode())
mutex.release()
else:
HACK_SOCKET.close()
mutex.release()
break
HCK_SOCK_AND_USERNAME_PORT.remove(HACK_SOCKET)
HCK_SOCK_AND_USERNAME_PORT.remove(Hacker_IP[0])
HCK_SOCK_AND_USERNAME_PORT.remove(Hacker_IP[1])
def main():
Hacker = threading.Thread(target=SOCK_Hacker,args=(10031,))
Device = threading.Thread(target=SOCK_Dev,args=(10030,))
Hacker.start()
Device.start()
if __name__ == '__main__':
main()
运行效果图
控制端 Python代码
#!/usr/bin/python3
import socket
import sys
import time
import datetime
def OPTIONS():
# OPEN是正转动 CLOSE是反转 Q是断开与服务器的连接 D是将远控设备踢下线
CS = input("\n-----#(OPEN||CLOSE|Q|D)>>>")
return CS
def main(MoteIP,MotePort):
SOCKS = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
SOCKS.connect((MoteIP, MotePort))
print("[+]正在接入远程主机")
print(SOCKS.recv(1024).decode())
while True:
CS = OPTIONS()
if "OPEN" in CS:
SOCKS.send(b'HACK') # o 6F # c 63
print("[+]发送运行指令成功!")
elif "CLOSE" in CS:
SOCKS.send(b'STOP') # o 6F # c 63
print("[+]发送停止指令成功!")
elif "Q" in CS:
SOCKS.send(b'EXIT')
print("[-]准备关闭会话")
print("[+]服务器返回了一条消息:",SOCKS.recv(1024).decode())
sys.exit(0)
elif "D" in CS:
SOCKS.send(b'DEV')
print("[-]准备远程关闭设备")
print("[+]服务器返回了一条消息:", SOCKS.recv(1024).decode())
else:
continue
print("[+]服务器返回了一条消息:", SOCKS.recv(1024).decode())
if __name__ == '__main__':
main("这里输入服务器IP",int(10031))
效果演示
在此之前应该开放出入站端口,否则会连不上
连接服务器
python3 Connect.py
服务器端接监听控制者连接后显示效果
python3 Service.py
将4G卡取出插入SIM900A模块,并将设备上电准备连接服务器
服务器端接监听设备连接后显示效果(设备上线)
OLED显示屏显示连接成功
在电机上定一个A点
控制方发送命令,服务器返回设备的在线状态
服务器收到来自控制方的信息,并执行命令
受控端收到信号并执行,移动至点B,旋转夹角约30°
视频效果