作者归档:admin

ML307R 设备

有3个类型 AT DTU RTU

AT指令版打包了很多很多方法 HTTP MQTT TCP UDP

其余两个版本得你自己手写,只要基础的TCP和UDP,至于HTTP MQTT需要你自己封装~~

结案 ML307R 买AT指令版 切记 !切记!

micropython 操作ML307R

完全可用的ML307的库

https://github.com/maysrp/ML307
from machine import UART
import time

class ML307(object):
    def __init__(self,rx,tx,u_num=1):
        self.uart=UART(u_num,115200,rx=rx,tx=tx,rxbuf=2048,txbuf=2048)
        self.header=[]
        time.sleep(3)
    def command(self,cmd,t=1):
        self.uart.write(cmd+'\r\n')
        time.sleep(t*0.1)
        return self.uart.read()
    def get_response(self,info):
        nx=info.split(b'MHTTPURC: "content"')
        rep_1=nx[1].split(b",")
        rep=b"".join(rep_1[5:])
        return rep
    def get_url(self,url_1,url_2,t=15):
        self.command('AT+MHTTPCREATE="'+url_1+'"')
        self.header_def()
        info=self.command('AT+MHTTPREQUEST=0,1,0,"'+url_2+'"',t)
        self.command('AT+MHTTPHEADER=0')
        return self.get_response(info)
    def get_url_ssl(self,url_1,url_2,t=15):
        self.command('AT+MHTTPCREATE="'+url_1+'"')
        self.command('AT+MHTTPCFG="ssl",0,1,1')
        self.header_def()
        info=self.command('AT+MHTTPREQUEST=0,1,0,"'+url_2+'"',t)
        self.command('AT+MHTTPHEADER=0')
        return self.get_response(info)
    def make_url(self,url):
        url_1="/".join(url.split("/")[0:3])
        url_2="/"+"/".join(url.split("/")[3:])
        return [url_1,url_2]
    def get(self,url):
        ua=self.make_url(url)
        try:
            if "https" in ua[0]:
                ba=self.get_url_ssl(ua[0],ua[1])
            else:
                ba=self.get_url(ua[0],ua[1])
            return ba
        except Exception as e:
            return False
    def post_form(self,info):
        form=""
        for key in info:
            form=form+key+"="+str(info[key])+"&"
        return form
    def post_url(self,url_1,url_2,form_str,t=15):
        self.command('AT+MHTTPCREATE="'+url_1+'"')
        self.header_def()
        self.command('AT+MHTTPHEADER=0,0,0,"Content-Type: application/x-www-form-urlencoded"')
        self.command('AT+MHTTPCONTENT=0,0,0,"'+form_str+'"')
        info=self.command('AT+MHTTPREQUEST=0,2,0,"'+url_2+'"',t)
        self.command('AT+MHTTPHEADER=0')
        return self.get_response(info)
    def post_url_ssl(self,url_1,url_2,form_str,t=15):
        self.command('AT+MHTTPCREATE="'+url_1+'"')
        self.command('AT+MHTTPCFG="ssl",0,1,1')
        self.header_def()
        self.command('AT+MHTTPHEADER=0,0,0,"Content-Type: application/x-www-form-urlencoded"')
        self.command('AT+MHTTPCONTENT=0,0,0,"'+form_str+'"')
        info=self.command('AT+MHTTPREQUEST=0,2,0,"'+url_2+'"',t)
        self.command('AT+MHTTPHEADER=0')
        return self.get_response(info)
    def post(self,url,dic_info):
        form_str=self.post_form(dic_info)
        ua=self.make_url(url)
        try:
            if "https" in ua[0]:
                ba=self.post_url_ssl(ua[0],ua[1],form_str)
            else:
                ba=self.post_url(ua[0],ua[1],form_str)
            return ba
        except Exception as e:
            return False
    def header_def(self):
        lh=len(self.header)
        if lh>1:
            for i in range(lh):
                if i <lh-1:
                    self.command('AT+MHTTPHEADER=0,1,0,"'+self.header[i]+'"')
                else:
                    self.command('AT+MHTTPHEADER=0,0,0,"'+self.header[i]+'"')
        elif lh==1:
            self.command('AT+MHTTPHEADER=0,0,0,"'+self.header[0]+'"')
        else:
            pass
    
        

#范例
mm=ML307(6,7)
mm.header=["User-Agent: MyCustomAgent/1.0","mam: i am hera"]
c=mm.get("http://httpbin.org/get?id=33")
print(c)
info={"aa":23,"zz":11}
cc=mm.post("http://httpbin.org/post",info)
print(cc)


from machine import UART
c=UART(1,115200,rx=6,tx=7,rxbuf=2048,txbuf=2048)


c.write('AT\r\n')
c.read()
c.write('AT+CGDCONT=1,"IPV4V6","cmnet"\r\n')
c.read()
c.write('AT+MIPCALL=1,1\r\n')
c.read()


c.write('AT+MHTTPCREATE="http://qt.gtimg.cn"\r\n')
c.read()
c.write('AT+MHTTPREQUEST=0,1,0,"/q=sh600100"\r\n')
c.read()


关闭实例
AT+MHTTPDEL=0

#下面是操作的步骤
class ml307(object):
    def __init__(self,rx,tx,u_num):
        self.uart=UART(1,115200,rx=rx,tx=tx,rxbuf=2048,txbuf=2048)
    def command(self,cmd,t=1):
        self.uart.write(cmd+'\r\n')
        time.sleep(t*0.1)
        return self.uart.read()
    def get_response(self,info):
        nx=info.split(b'MHTTPURC: "content"')
        rep_1=nx[1].split(b",")
        rep=b"".join(rep_1[5:])
        return rep
    def get_url(self,url,uri):
        self.command('AT+MHTTPCREATE="'+url+'"')
        info=self.command('AT+MHTTPREQUEST=0,1,0,"'+uri+'"',5)
        return self.get_response(info)

ML307

from machine import UART
import time

# c=UART(1,115200,rx=6,tx=7,rxbuf=2048,txbuf=2048)
# c.write('AT\r\n')
# c.read()
# c.write('AT+CGDCONT=1,"IPV4V6","cmnet"\r\n')
# c.read()
# c.write('AT+MIPCALL=1,1\r\n')
# c.read()
# c.write('AT+MHTTPCREATE="http://qt.gtimg.cn"\r\n')
# c.read()
# c.write('AT+MHTTPREQUEST=0,1,0,"/q=sh600100"\r\n')
# c.read()

class ML307(object):
    def __init__(self,rx,tx,u_num=1):
        self.uart=UART(u_num,115200,rx=rx,tx=tx,rxbuf=2048,txbuf=2048)
        time.sleep(3)
#         初始化3s
    def command(self,cmd,t=1):
        self.uart.write(cmd+'\r\n')
        time.sleep(t*0.1)
        return self.uart.read()
    def get_response(self,info):
        nx=info.split(b'MHTTPURC: "content"')
        rep_1=nx[1].split(b",")
        rep=b"".join(rep_1[5:])
        return rep
    def get_url(self,url_1,url_2):
        self.command('AT+MHTTPCREATE="'+url_1+'"')
        info=self.command('AT+MHTTPREQUEST=0,1,0,"'+url_2+'"',10)
        self.command('AT+MHTTPHEADER=0')
        return self.get_response(info)
    def get_url_ssl(self,url_1,url_2):
        self.command('AT+MHTTPCREATE="'+url_1+'"')
        self.command('AT+MHTTPCFG="ssl",0,1,1')
        info=self.command('AT+MHTTPREQUEST=0,1,0,"'+url_2+'"',10)
        self.command('AT+MHTTPHEADER=0')
        return self.get_response(info)
    def make_url(self,url):
        url_1="/".join(url.split("/")[0:3])
        url_2="/"+"/".join(url.split("/")[3:])
        return [url_1,url_2]
    def get(self,url):
        ua=self.make_url(url)
        try:
            if "https" in ua[0]:
                ba=self.get_url_ssl(ua[0],ua[1])
            else:
                ba=self.get_url(ua[0],ua[1])
            return ba
        except Exception as e:
            return False
    def post_form(self,info):
        form=""
        for key in info:
            form=form+key+"="+str(info[key])+"&"
        return form
    def post_url(self,url_1,url_2,form_str):
        self.command('AT+MHTTPCREATE="'+url_1+'"')
        self.command('AT+MHTTPHEADER=0,0,0,"Content-Type: application/x-www-form-urlencoded"')
        self.command('AT+MHTTPCONTENT=0,0,0,"'+form_str+'"')
        info=self.command('AT+MHTTPREQUEST=0,2,0,"'+url_2+'"',15)
        self.command('AT+MHTTPHEADER=0')
        return self.get_response(info)
    def post_url_ssl(self,url_1,url_2,form_str):
        self.command('AT+MHTTPCREATE="'+url_1+'"')
        self.command('AT+MHTTPCFG="ssl",0,1,1')
        self.command('AT+MHTTPHEADER=0,0,0,"Content-Type: application/x-www-form-urlencoded"')
        self.command('AT+MHTTPCONTENT=0,0,0,"'+form_str+'"')
        info=self.command('AT+MHTTPREQUEST=0,2,0,"'+url_2+'"',15)
        self.command('AT+MHTTPHEADER=0')
        return self.get_response(info)
    def post(self,url,dic_info):
        form_str=self.post_form(dic_info)
        ua=self.make_url(url)
        try:
            if "https" in ua[0]:
                ba=self.post_url_ssl(ua[0],ua[1],form_str)
            else:
                ba=self.post_url(ua[0],ua[1],form_str)
            return ba
        except Exception as e:
            return False
    
        

mm=ML307(6,7)
c=mm.get("http://httpbin.org/get?id=33")
print(c)
info={"aa":23,"zz":11}
cc=mm.post("http://httpbin.org/post",info)
print(cc)


ml307r 股票API返回信息

+MHTTPURC: "header",0,200,270,HTTP/1.1 200 OK
Date: Sun, 23 Feb 2025 14:09:30 GMT
Content-Type: text/html; charset=GBK
Transfer-Encoding: chunked
Connection: keep-alive
Server: openresty/1.11.2.1
Expires: Sun, 23 Feb 2025 14:09:31 GMT
Cache-Control: max-age=0
Access-Control-Allow-Origin: *


+MHTTPURC: "content",0,0,499,499,v_sh600100="1~同方股份~600100~8.62~7.84~7.89~2627663~1356592~1271071~8.62~17268~8.61~2174~8.60~1227~8.59~4196~8.58~1920~0.00~0~0.00~0~0.00~0~0.00~0~0.00~0~~20250221155918~0.78~9.95~8.62~7.77~8.62/2627663/2188005662~2627663~218801~7.84~-420.05~~8.62~7.77~10.84~288.80~288.80~1.91~8.62~7.06~1.84~26785~8.33~290.11~-37.76~~~1.43~218800.5662~0.0000~0~ ~GP-A~21.24~3.23~0.00~-0.46~-0.12~8.87~4.51~14.17~31.20~15.39~3350297713~3350297713~100.00~53.93~3350297713~~~37.92~0.00~~CNY~0~___D__F__N~8.57~3171";

ML307R AT指令实测 HTTP GET POST

首先模块的启动电压是5V,3.3v无法启动 切记!

AT+MHTTPCREATE="http://qt.gtimg.cn"
AT+MHTTPREQUEST=0,1,0,"/q=sh600100"
AT+MHTTPREQUEST=0,1,0,"/q=sz0008558"

#AT+MHTTPCREATE="http://你的网址:8080" 后面:8080为端口,不填则是默认80;创建HTTP实例
#在同一个HTTP网址下(同域名 uri不同),可以用 AT+MHTTPREQUEST=0,1,0,"/你的网址去访问"
AT+MHTTPDEL=0
#关闭当前HTTP实例,如果现需要访问其他域名,则需要先关闭实例,再创建实例

AT+MHTTPCREATE="http://httpbin.org"
AT+MHTTPREQUEST=0,1,0,"/get"
#上面就是创建了其他网址的链接

#杂项 HEADER设置
AT+MHTTPHEADER=0,1,0,"Accept: application/json"
AT+MHTTPHEADER=0,1,0,"Content-Type: application/json"
AT+MHTTPHEADER=0,0,0,"User-Agent: MyCustomAgent/1.0"


#若后续还有请求头设置那么,必须为0,1,0 AT+MHTTPHEADER=0,1,0 ;若没有则设置为0,0,0 AT+MHTTPHEADER=0,0,0

AT+MHTTPHEADER=0
#查看当前请提头

HTTPS 请求

AT+MHTTPCFG="ssl",0,1,1

全部例子:

AT+MHTTPCREATE="https://httpbin.org"
#默认443 如果不是这个端口请设置端口
AT+MHTTPCFG="ssl",0,1,1
AT+MHTTPREQUEST=0,1,0,"/get"

关闭实例
AT+MHTTPDEL=0

POST 表单形式 “key1=value1&key2=value2&key3=123”

AT+MHTTPCREATE="https://httpbin.org"
AT+MHTTPCFG="ssl",0,1,1
AT+MHTTPHEADER=0,1,0,"Content-Type: application/x-www-form-urlencoded"
AT+MHTTPHEADER=0,0,0,"User-Agent: MyCustomAgent/1.0"
AT+MHTTPCONTENT=0,0,0,"key1=value1&key2=value2&key3=123"
AT+MHTTPREQUEST=0,2,0,"/post"
AT+MHTTPDEL=0

JSON形式

AT+MHTTPCREATE="https://httpbin.org"
AT+MHTTPCFG="ssl",0,1,1
AT+MHTTPHEADER=0,1,0,"Content-Type: application/json"
AT+MHTTPHEADER=0,0,0,"User-Agent: MyCustomAgent/1.0"
AT+MHTTPCONTENT=0,0,0,"{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":123}"
AT+MHTTPREQUEST=0,2,0,"/post"
AT+MHTTPDEL=0
#https://httpbin.org不支持json形式,范例而已

POST文件传输

AT+MHTTPCREATE="https://httpbin.org"
AT+MHTTPCFG="ssl",0,1,1
AT+MHTTPHEADER=0,0,0,"Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
AT+MHTTPCONTENT=0,0,0,"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"example.txt\"\r\nContent-Type: text/plain\r\n\r\nFile content goes here\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
AT+MHTTPREQUEST=0,2,0,"/upload"
  • “Content-Type: multipart/form-data; boundary=—-WebKitFormBoundary7MA4YWxkTrZu0gW

为设置边界,即AT+MHTTPCONTENT=0,0,0,中开头结尾必须都是它。

  • AT+MHTTPCONTENT=0,0,0,”——WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\”file\”; filename=\”example.txt\”\r\nContent-Type: text/plain\r\n\r\nFile content goes here\r\n——WebKitFormBoundary7MA4YWxkTrZu0gW–“

其中 开头与结尾的 boundary;

Content-Disposition: form-data; 必须设置请不要修改

name=\”file\”; 为传入类型file

filename=\”example.txt\”\r\n 传入的文件名

Content-Type: text/plain\r\n\r\n 传入文件类型

File content goes here\r\n 文件信息,图片请以二进制格式读入

因为multipart/form-data有格式限制:filename=\”example.txt\”\r\nContent-Type: text/plain\r\n\r\nFile content goes here\r\n

  • 其实格式:
  • filename=\”example.txt\”
  • Content-Type: text/plain
  • \r\n
  • File content goes here

上传文件的请求格式

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file";filename="example.txt"
Content-Type: text/plain

File content goes here
------WebKitFormBoundary7MA4YWxkTrZu0gW--

最后的 --multipart/form-data 请求体的结束标记