Difference between revisions of "Template:Pico-SIM7080G-Manual"

From Waveshare Wiki
Jump to: navigation, search
(Created page with "=== AT Test === ==== Example description ==== This routine is mainly to facilitate the user to directly test the AT command transmission and reception of the verification modu...")
(No difference)

Revision as of 03:58, 11 June 2021

AT Test

Example description

This routine is mainly to facilitate the user to directly test the AT command transmission and reception of the verification module through the Thony software. The main program will first power on the module directly, and then check the network condition, then it will loop to detect the AT command input by the user, send it to the module through the serial port, and then return the AT command of the module to the Pico serial port for printing.
For the detailed AT command set, please see: SIM7080_Series_AT_Command_Manual_V1.02.pdf

Expected result

SIM7080G-AT-TEST.png

HTTP

With this example, The Raspberry Pico can connect to the network by NB-IoT. You can use HTTP GET to get the weather information from weather websites and post the temperature of Pico to the server by HTTP POST .
You can access the webpage and check the real-time data posted.
Pico-SIM7020-HTTP-demo-diagram.png

Setup Hardware

Solder female/male pin headers to Pico-SIM7020X-NB-IoT and connect the NB-IoT board to Pico. Connect the battery, Antenna and Insert the NB-IoT card.
Pico-SIM7020X-NB-IoT-connection.png

Setup Server

Use http://pico.wiki/esp-chart.php as example, we setup the webpage of server for testing.
Pico-SIM7020-HTTP-Demo-2.png
1、Build envrionments like php and mysql, etc. Create database file, for example:

  • Database:example_esp_data
  • Password:your_password
  • User Name:your_username
  • Create database table:
CREATE TABLE Sensor (
   id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
   value1 VARCHAR(10),
   value2 VARCHAR(10),
   value3 VARCHAR(10),
   reading_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)

2、Two example files are provided for the server. they are post-data.php and esp-chart.php.

  • post-data.php:API of HTTP POST (server). SIM7020X module can POST data to the server by using this API.
  • esp-chart.php:Webpage of Client. You can get the newest data that SIM7020X uploaded by this webpage and show it with the chart.

Software setup in Pico

Please follow the guides of Raspberry Pi to install and set up Pico for the Pico.

For easy use, we recommend you use the Thonny tool.

  • Thonny website
  • Please set the Thonny development environment to be RaspberryPi when setting
Pico-R3-Tonny1.png

2、Download micro python examples:Pico-SIM7020X-NB-loT-HTTP.py. Here we only list part of the sample codes, for more information on sample codes, please download the codes.

  • HTTP GET:
def httpGetTest():
   sendCMD_waitResp("AT+CHTTPCREATE=\"http://api.seniverse.com\"")    #Create HTTP host instance
   sendCMD_waitResp("AT+CHTTPCON=0")           #Connect server
   sendCMD_waitRespLine("AT+CHTTPSEND=0,0,\"/v3/weather/now.json?key=SwwwfskBjB6fHVRon&location=shenzhen&language=en&unit=c\"")  #send HTTP request
   waitResp()
   sendCMD_waitResp("AT+CHTTPDISCON=0")      #Disconnected from server
   sendCMD_waitResp("AT+CHTTPDESTROY=0")      #Destroy HTTP instance
  • HTTP POST:
def httpPostTest():
   global i
   i=i+1
   sendCMD_waitResp("AT+CHTTPCREATE=\"http://pico.wiki/post-data.php\"")    #Create HTTP host instance
   sendCMD_waitResp("AT+CHTTPCON=0")           #Connect server
   sendCMD_waitRespLine("AT+CHTTPSEND=0,1,\"/post-data.php\",4163636570743a202a2f2a0d0a436f6e6e656374696f6e3a204b6565702d416c6976650d0a557365722d4167656e743a2053494d434f4d5f4d4f44554c450d0a,\"application/x-www-form-urlencoded\","+str_to_hexStr("api_key=tPmAT5Ab3j888&value1="+str(temperature)+"&value2="+str(ADC0_reading)+"&value3="+str(i)))  #send HTTP request
   waitResp()
   sendCMD_waitResp("AT+CHTTPDISCON=0")      #Disconnected from server
   sendCMD_waitResp("AT+CHTTPDESTROY=0")      #Destroy HTTP instance

For more information about the HTTP function of SIM7020X module, please refer to SIM7020_Series_HTTP_Application_Note_V1.02.pdf

3、Connect the Pico-SIM7020X-NB-IoT to Pico and connect the Pico to Raspberry Pi or PC by USB cable.

  • Open the Thonny software, Choose MicroPython(Raspberry Pi Pico) and open the sample codes.
  • Please first click Stop(No.1), and then click the Run(No.2) buttons to run the demo codes. You can check the status by the Shell window(No. 3) for example:
Pico-SIM7020X-NB-IoT-Run-Code.png

4、You can check, modify or run/debug the sample code by the Thonny software.

  • If you want to make the codes auto-run, please choose File -> Save as -> Raspberry Pi Pico,and save it as main.py.
Pico-Save-as-main py.png

Expected Result

With the Pico-SIM7020X-NB-loT-HTTP.py example, Pico can get the weather information from the weather website by HTTP GET and post the temperature of Puco to pico.wiki by HTTP POST.
At the same time, users can go to the http://pico.wiki/esp-chart.php webpage to check the uploaded data which are showed by charts.For example:

Pico-SIM7020X-NB-IoT-HTTP-GET-Result.png
Pico-SIM7020X-NB-IoT-HTTP-POST-Result.png

TCP examples

This example is used to create a TCP client with NB-IoT network in Raspberry Pico, then connect to a remote server and send requests. The TCP server used in this example is just for testing and it will echo the data sent by the client.
SIM7020 TCPIP support multiple clients and one TCP server, it supports six sockets includes TCP and UDP.
Pico-SIM7020-HTTP-Demo-4.png

Code analysis

  • Defind IP of TCP server and the port.
ServerIP = '118.190.93.84'
Port = '2317'
  • Pico-SIM7020C-NB-IoT networking test
#AT commands test
def atCommandTest():
   sendCMD_waitRespLine("AT")                     #Test connection
   sendCMD_waitRespLine("ATE1")                #Set command echo mode
   sendCMD_waitRespLine("AT+CGMM")
   sendCMD_waitRespLine("AT+CPIN?")        #whether some password is required or not
   sendCMD_waitRespLine("AT+CSQ")          #received signal strength
   sendCMD_waitRespLine("AT+CGREG?")        #the registration of the ME.
   sendCMD_waitRespLine("AT+CGATT?")       #GPRS Service’s status
   sendCMD_waitRespLine("AT+CGACT?")       #PDN active status
   sendCMD_waitRespLine("AT+COPS?")       #Query Network information
   sendCMD_waitRespLine("AT+CGCONTRDP")       #Attached PS domain and got IP address automatically
  • Send request to TCP server
def tcpclientTest():
   sendCMD_waitRespLine("AT+CSOC=1,1,1")    #create TCP socket 
   sendCMD_waitRespLine("AT+CSOCON=0,"+Port+",\""+ServerIP+"\"")    #socket_id = 0,connect TCP server
   sendCMD_waitRespLine("AT+CSOSEND=0,0,\"Hello,World\"")    #send TCP data
   sendCMD_waitRespLine("AT+CSOCL=0")    #close TCP socket
  • Convert the data from HEX to string which sent from server
if send_data in uart.readline().decode():
   print((uart.readline()).decode())
   print((uart.readline()).decode())
   word = (uart.readline()).decode().split(',') #hexStr_to_str
   get_word = word[2].split('\r')
   word[2] = hexStr_to_str(get_word[0]) 
   print(word[0]+','+word[1]+','+word[2])

Expected result

With the Pico-SIM7020X-NB-loT-TCP.py example, the Client sent the "Hello,World" string to the TCP server and receive the same data from the server.

Pico-SIM7020-HTTP-Demo-5.png

MQTT

This example is used to send MQTT requests by the MB-IoT network in Raspberry Pi Pico. It will connect to the Alibaba Cloud MQTT server, subscribe to a topic, and post a message.

Pico-SIM7020-HTTP-Demo-8.png

Codes analysis

  • Define device information of MQTT server
ProductKey = "a1IBIPnZU9G"
Broker_Address = ProductKey + ".iot-as-mqtt.cn-shanghai.aliyuncs.com"
DeviceName = "7020"
DeviceSecret = "8e870f4e611d3e4c2b8f9f017e6a88ab"
  • Test MQTT function
 # MQTT Test
def mqttconnectTest():
   sendCMD_waitRespLine("AT+CMQNEW="+"\""+Broker_Address+"\""+",\"1883\",12000,1024")  #create tcp connection 
   sendCMD_waitRespLine("AT+CMQALICFG=0,\""+ProductKey+"\",\""+DeviceName+"\",\""+DeviceSecret+"\"")     #set device parameters  
   sendCMD_waitRespLine("AT+CMQALICON=0,600,1")     # send MQTT request
   time.sleep(3)  
   sendCMD_waitRespLine("AT+CMQSUB=0,\"/"+ProductKey+"/"+DeviceName+"/"+"user/TEST1\",1")               # subscribe to topics
   time.sleep(3)
   sendCMD_waitRespLine("AT+CMQPUB=0,\"/"+ProductKey+"/"+DeviceName+"/user/TEST1\",1,0,0,16,\"3132333435363738\"")  # release the news
   time.sleep(3)
   sendCMD_waitRespLine("AT+CMQUNSUB=0,\"/"+ProductKey+"/"+DeviceName+"/user/TEST1\"")  # unsubscribe topic
   time.sleep(3)
   sendCMD_waitRespLine("AT+CMQDISCON=0")   # disconnect MQTT connection

Buid Alibaba Cloud MQTT Server

1. Go to the Alibaba Cloud IoT platform, the platform is not free and you need to pay for it. The enterprise version is free in the first month.
2.Create product
Pico-SIM7020-HTTP-Demo-9.png
Pico-SIM7020-HTTP-Demo-10.png
Click ok and choose Add Device
3.Set the DeviceName, and check the DeivceSecret of device. You need to save these data and modify the MQTT codes.
Pico-SIM7020-HTTP-Demo-11.png
Pico-SIM7020-HTTP-Demo-12.png
4.Register topic, click Products--> check 7020_test-->Topic Categories-->Edit Topic Category-->Choose Publish and Subscribe
Pico-SIM7020-HTTP-Demo-13.png

Expected Result

With the Pico-SIM7020X-NB-loT-MQTT.py example, you can find that you have subscribed to a topic and send a message to it. The server will push this message to you.
Pico-SIM7020-HTTP-Demo-7.png
At the same time, you can check the topic subscribed in the device list.
Pico-SIM7020-HTTP-Demo-6.png