家里有个斐讯M1的空气检测仪,原来可以发送数据到服务器,再从手机app查看的。后来厂家倒闭了,服务器也关了,,,因为联系不到服务器,wifi信号灯就会一直一闪一闪的。这几天尝试模拟搭建服务器,获得成功,记录一下。
一、首先原服务器的域名是aircat.phicomm.com ,这个是改不了的,但是可以通过在路由器的dns上做设置,把它解析为自己局域网内的服务器IP,修改完ping一下域名,能成功的解析为本地地址就对了。
二、服务器通信的端口是tcp 9000 。
最简单是在模拟的服务器上执行
netcat -l -p 9000 ,
防火墙也要开放9000端口
sudo ufw allow 9000
此时M1能访问到aircat.phicomm.com:9000 , 信号灯就不会闪了。而且可以看到netcat不断收到M1发送的数据。
三、如果要进一步,拿到发送的数据,就需要再做些开发了。尝试用python做了个简单实现
# coding: utf-8
import select
import socket
import queue
from time import sleep
responseMsg = b'\x00\x18\x00\x00\x02{"type":5,"status":1}\xff#END#'
# Create a TCP/IP
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking(False)
# Bind the socket to the port
server_address = ('192.168.88.17', 9000)
print ('starting up on %s port %s' % server_address)
server.bind(server_address)
# Listen for incoming connections
server.listen(5)
# Sockets from which we expect to read
inputs = [server]
# Sockets to which we expect to write
# 处理要发送的消息
outputs = []
# Outgoing message queues (socket: Queue)
message_queues = {}
while inputs:
# Wait for at least one of the sockets to be ready for processing
print ('waiting for the next event')
# 开始select 监听, 对input_list 中的服务器端server 进行监听
# 一旦调用socket的send, recv函数,将会再次调用此模块
readable, writable, exceptional = select.select(inputs, outputs, inputs)
# Handle inputs
# 循环判断是否有客户端连接进来, 当有客户端连接进来时select 将触发
for s in readable:
# 判断当前触发的是不是服务端对象, 当触发的对象是服务端对象时,说明有新客户端连接进来了
# 表示有新用户来连接
if s is server:
# A "readable" socket is ready to accept a connection
connection, client_address = s.accept()
print ('connection from', client_address)
# this is connection not server
connection.setblocking(0)
# 将客户端对象也加入到监听的列表中, 当客户端发送消息时 select 将触发
inputs.append(connection)
# Give the connection a queue for data we want to send
# 为连接的客户端单独创建一个消息队列,用来保存客户端发送的消息
message_queues[connection] = queue.Queue()
else:
# 有老用户发消息, 处理接受
# 由于客户端连接进来时服务端接收客户端连接请求,将客户端加入到了监听列表中(input_list), 客户端发送消息将触发
# 所以判断是否是客户端对象触发
data = s.recv(1024)
# 客户端未断开
if len(data)>0:
# A readable client socket has data
print ('received [%s %s] from %s' % (data[:23].hex(), data[23:], s.getpeername()))
# 将收到的消息放入到相对应的socket客户端的消息队列中
responsedata=data[:23] + responseMsg
message_queues[s].put(responsedata)
# Add output channel for response
# 将需要进行回复操作socket放到output 列表中, 让select监听
if s not in outputs:
outputs.append(s)
else:
# 客户端断开了连接, 将客户端的监听从input列表中移除
# Interpret empty result as closed connection
print ('closing', client_address)
# Stop listening for input on the connection
if s in outputs:
outputs.remove(s)
inputs.remove(s)
s.close()
# Remove message queue
# 移除对应socket客户端对象的消息队列
del message_queues[s]
# Handle outputs
# 如果现在没有客户端请求, 也没有客户端发送消息时, 开始对发送消息列表进行处理, 是否需要发送消息
# 存储哪个客户端发送过消息
for s in writable:
try:
# 如果消息队列中有消息,从消息队列中获取要发送的消息
message_queue = message_queues.get(s)
send_data = ''
if message_queue:
send_data = message_queue.get_nowait()
else:
# 客户端连接断开了
print ("has closed")
except queue.Empty:
# 客户端连接断开了
errinfo=s.getpeername()
print ( errinfo )
outputs.remove(s)
else:
if message_queue :
if len(send_data)>0:
print ("send %s " % send_data)
s.send(send_data)
else:
print ("has closed ")
# writable.remove(s)
# print "Client %s disconnected" % (client_address)
# # Handle "exceptional conditions"
# 处理异常的情况
for s in exceptional:
print ('exception condition on', s.getpeername())
# Stop listening for input on the connection
inputs.remove(s)
if s in outputs:
outputs.remove(s)
s.close()
# Remove message queue
del message_queues[s]
sleep(1)
#start
#[aa0f01350742398f0b0000000000000000b0f89324705300 b'\x00\x00\x04{ "humidity": "47.29", "temperature": "23.66", "value": "1", "hcho": "10" }\xff#END#'] from ('192.168.130.40', 23741)
#[aa0f01350742398f0b0000000000000000b0f89324705300 b'\x00\x00\x01\xff#END#']
#[aa0f01350742398f0b0000000000000000b0f8932470 b'\x00\x03\x00\x00\x01\xff#END#'
代码tcp通信的框架基本复制自网上,原作可能是在python2开发的,自己微调了一下,在python3可以跑。这里面关键是通信的报文解析,经过观察是这样的:
1、M1往服务器送的报文
aa 0f 01 35 07 42 39 8f 0b 00 00 00 00 00 00 00 00 b0 f8 93 24 70 53 b'\x00\x00\x00\x00\x04{ "humidity": "47.29", "temperature": "23.66", "value": "1", "hcho": "10" }\xff#END#
(1)前23个字节(红色部分,十六进制表示)一般是固定的,把它分为四组
aa 0f 01 | 35 07 42 39 8f 0b | 00 00 00 00 00 00 00 00 | b0 f8 93 24 70 53 | |
第一组 | 第二组 | 第三组 | 第四组 |
第一组3个字节aa 0f 01是固定的,可以看为是报文标志符,可以用它来帮助判断是否M1的报文;
第三组固定是8个全00字节;第四组是mac地址 ;
第二组经过观察,其实是第四组(mac地址)的镜像,可能是为了校验或者迷惑?
(2)随后是5个字节 + Json数据
5个字节定义不明,但目前只看到b'\x00\x00\x00\x00\x04 ' ,这个不影响数据解析
Json数据就是发送的温度、湿度、pm2.5等数据,稍作解析就可以利用。
(3)最后是结束标志: b'\xff#END# ' ,
跟开头的报文标志联合起来可以应该可以用正则提取报文了。
2、服务器往M1发的报文
服务器往M1发的报文有两种
(1)心跳报文
aa 0f 01 35 07 42 39 8f 0b 00 00 00 00 00 00 00 00 b0 f8 93 24 70 53 b'\x00\x18\x00\x00\x02{"type":5,"status":1}\xff#END#'
这个报文红色部分跟M1发过来的前23个字节是一样的,可以从M1送来的报文截取后原封不动发回去。(调试时试过如果不一样,会导致M1主动关闭连接,估计是m1判断送来的报文mac地址跟自己mac地址不一样,认为不是自己的数据。)后面的定义不明,不过也是固定的。
这个报文的作用好像是会激发M1上报数据,如果不发的话,M1在连接成功最初,会密集报送一定量的数据,之后如果没有服务器反馈会明显减慢发送频率。在代码里看见有人定义它为“心跳报文”。
(2)设置亮度
aa 0f 01 35 07 42 39 8f 0b 00 00 00 00 00 00 00 00 b0 f8 93 24 70 53 b'\x00\x18\x00\x00\x02{"brightness":"50.0","type":2}\xff#END#'
这个报文是设置屏幕亮度,brightness后面的数字一般是0、 25、 50、 75、 100,亮度逐渐增强
( 观察发现发送了设置屏幕亮度的报文后,M1会暂停上报数据了,转而发送
[aa0f01350742398f0b0000000000000000b0f893247053 b'\x00\x03\x00\x00\x01\xff#END#']
可能是对于设置亮度成功的一种应答。
此时需要服务器发一条心跳报文给M1,就会开始继续上报数据)