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...")
 
Line 45: Line 45:
 
2、Download micro python examples:[https://www.waveshare.com/w/upload/5/5d/Pico-SIM7020X-NB-loT-Demo-Code.zip 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.
 
2、Download micro python examples:[https://www.waveshare.com/w/upload/5/5d/Pico-SIM7020X-NB-loT-Demo-Code.zip 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:
 
* HTTP GET:
  def httpGetTest():
+
# HTTP GET TEST
    sendCMD_waitResp("AT+CHTTPCREATE=\"http://api.seniverse.com\"")    #Create HTTP host instance
+
  def http_get():
    sendCMD_waitResp("AT+CHTTPCON=0")           #Connect server
+
  send_at('AT+SHDISC', 'OK')
    sendCMD_waitRespLine("AT+CHTTPSEND=0,0,\"/v3/weather/now.json?key=SwwwfskBjB6fHVRon&location=shenzhen&language=en&unit=c\"") #send HTTP request
+
  send_at('AT+SHCONF="URL",\"'+http_get_server[0]+'\"', 'OK')
    waitResp()
+
   set_http_length()
    sendCMD_waitResp("AT+CHTTPDISCON=0")     #Disconnected from server
+
  send_at('AT+SHCONN', 'OK', 3000)
    sendCMD_waitResp("AT+CHTTPDESTROY=0")     #Destroy HTTP instance
+
  if send_at('AT+SHSTATE?', '1'):
 +
      set_http_content()
 +
      resp = str(send_at_wait_resp('AT+SHREQ=\"'+http_get_server[1]+'\",1', 'OK',8000))
 +
      # print("resp is :", resp)
 +
      try:
 +
          get_pack_len = int(resp[resp.rfind(',')+1:-5])
 +
          if get_pack_len > 0:
 +
              send_at_wait_resp('AT+SHREAD=0,'+str(get_pack_len), 'OK', 5000)
 +
              send_at('AT+SHDISC', 'OK')
 +
          else:
 +
              print("HTTP Get failed!\n")
 +
      except ValueError:
 +
          print("ValueError!\n")
 +
  else:
 +
      print("HTTP connection disconnected, please check and try again\n")
  
 
* HTTP POST:
 
* HTTP POST:
  def httpPostTest():
+
  def http_post():
    global i
+
  send_at('AT+SHDISC', 'OK')
    i=i+1
+
  send_at('AT+SHCONF="URL",\"' + http_post_server[0] + '\"', 'OK')
    sendCMD_waitResp("AT+CHTTPCREATE=\"http://pico.wiki/post-data.php\"")    #Create HTTP host instance
+
  set_http_length()
    sendCMD_waitResp("AT+CHTTPCON=0")           #Connect server
+
  send_at('AT+SHCONN', 'OK', 3000)
    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
+
   if send_at('AT+SHSTATE?', '1'):
    waitResp()
+
      set_http_content()
    sendCMD_waitResp("AT+CHTTPDISCON=0")     #Disconnected from server
+
      send_at('AT+SHCPARA', 'OK')
    sendCMD_waitResp("AT+CHTTPDESTROY=0")     #Destroy HTTP instance
+
      if send_at('AT+SHBOD=62,10000', '>', 1000) :
 +
          send_at(http_post_tmp, 'OK')
 +
          resp = str(send_at_wait_resp('AT+SHREQ=\"/'+http_post_server[1]+'\",3','OK', 8000))
 +
          # print("resp is :", resp)
 +
          try:
 +
              get_pack = int(resp[resp.rfind(',')+1:-5])
 +
              print(get_pack)
 +
              if get_pack > 0:
 +
                  send_at_wait_resp('AT+SHREAD=0,' + str(get_pack), 'OK', 3000)
 +
                  send_at('AT+SHDISC', 'OK')
 +
              else:
 +
                  print("HTTP Post failed!\n")
 +
          except ValueError:
 +
              print("ValueError!\n")
 +
      else:
 +
          print("Send failed\n")
 +
  else:
 +
      print("HTTP connection disconnected, please check and try again\n")
  
<font color="red">For more information about the HTTP function of SIM7020X module, please refer to [https://www.waveshare.com/w/upload/e/ee/SIM7020_Series_HTTP_Application_Note_V1.02.pdf SIM7020_Series_HTTP_Application_Note_V1.02.pdf]</font>
+
<font color="red">For more information about the HTTP function of SIM7020X module, please refer to [[:File:SIM7070/SIM7080_Series_HTTP_Application_Note_V1.02.pdf]]</font>
  
 
3、Connect the Pico-SIM7020X-NB-IoT to Pico and connect the Pico to Raspberry Pi or PC by USB cable.<br />
 
3、Connect the Pico-SIM7020X-NB-IoT to Pico and connect the Pico to Raspberry Pi or PC by USB cable.<br />
Line 78: Line 109:
 
At the same time, users can go to the [http://pico.wiki/esp-chart.php http://pico.wiki/esp-chart.php] webpage to check the uploaded data which are showed by charts.For example:<br />
 
At the same time, users can go to the [http://pico.wiki/esp-chart.php http://pico.wiki/esp-chart.php] webpage to check the uploaded data which are showed by charts.For example:<br />
 
:[[File:Pico-SIM7020X-NB-IoT-HTTP-GET-Result.png|800px]]<br />[[File:Pico-SIM7020X-NB-IoT-HTTP-POST-Result.png|800px]]
 
:[[File:Pico-SIM7020X-NB-IoT-HTTP-GET-Result.png|800px]]<br />[[File:Pico-SIM7020X-NB-IoT-HTTP-POST-Result.png|800px]]
 
===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.<br />
 
SIM7020 TCPIP support multiple clients and one TCP server, it supports six sockets includes TCP and UDP.<br>
 
[[File:Pico-SIM7020-HTTP-Demo-4.png|800px]]
 
 
====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.
 
:[[File:Pico-SIM7020-HTTP-Demo-5.png|800px]]
 
 
===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.
 
:[[File:Pico-SIM7020-HTTP-Demo-8.png|800px]]
 
 
====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 [https://iot.console.aliyun.com/product 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.<br>
 
2.Create product<br>
 
[[File:Pico-SIM7020-HTTP-Demo-9.png|800px]]<br>
 
[[File:Pico-SIM7020-HTTP-Demo-10.png|800px]]<br>
 
Click ok and choose Add Device<br>
 
3.Set the DeviceName, and check the DeivceSecret of device. You need to save these data and modify the MQTT codes.<br>
 
[[File:Pico-SIM7020-HTTP-Demo-11.png|800px]]<br>
 
[[File:Pico-SIM7020-HTTP-Demo-12.png|800px]]<br>
 
4.Register topic, click Products--> check 7020_test-->Topic Categories-->Edit Topic Category-->Choose Publish and Subscribe<br>
 
[[File:Pico-SIM7020-HTTP-Demo-13.png|800px]]<br>
 
====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.<br>
 
[[File:Pico-SIM7020-HTTP-Demo-7.png|800px]]<br>
 
At the same time, you can check the topic subscribed in the device list.<br>
 
[[File:Pico-SIM7020-HTTP-Demo-6.png|800px]]
 

Revision as of 04:01, 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:
# HTTP GET TEST
def http_get():
  send_at('AT+SHDISC', 'OK')
  send_at('AT+SHCONF="URL",\"'+http_get_server[0]+'\"', 'OK')
  set_http_length()
  send_at('AT+SHCONN', 'OK', 3000)
  if send_at('AT+SHSTATE?', '1'):
      set_http_content()
      resp = str(send_at_wait_resp('AT+SHREQ=\"'+http_get_server[1]+'\",1', 'OK',8000))
      # print("resp is :", resp)
      try:
          get_pack_len = int(resp[resp.rfind(',')+1:-5])
          if get_pack_len > 0:
              send_at_wait_resp('AT+SHREAD=0,'+str(get_pack_len), 'OK', 5000)
              send_at('AT+SHDISC', 'OK')
          else:
              print("HTTP Get failed!\n")
      except ValueError:
          print("ValueError!\n")
  else:
      print("HTTP connection disconnected, please check and try again\n")
  • HTTP POST:
def http_post():
  send_at('AT+SHDISC', 'OK')
  send_at('AT+SHCONF="URL",\"' + http_post_server[0] + '\"', 'OK')
  set_http_length()
  send_at('AT+SHCONN', 'OK', 3000)
  if send_at('AT+SHSTATE?', '1'):
      set_http_content()
      send_at('AT+SHCPARA', 'OK')
      if send_at('AT+SHBOD=62,10000', '>', 1000) :
          send_at(http_post_tmp, 'OK')
          resp = str(send_at_wait_resp('AT+SHREQ=\"/'+http_post_server[1]+'\",3','OK', 8000))
          # print("resp is :", resp)
          try:
              get_pack = int(resp[resp.rfind(',')+1:-5])
              print(get_pack)
              if get_pack > 0:
                  send_at_wait_resp('AT+SHREAD=0,' + str(get_pack), 'OK', 3000)
                  send_at('AT+SHDISC', 'OK')
              else:
                  print("HTTP Post failed!\n")
          except ValueError:
              print("ValueError!\n")
      else:
          print("Send failed\n")
  else:
      print("HTTP connection disconnected, please check and try again\n")

For more information about the HTTP function of SIM7020X module, please refer to File:SIM7070/SIM7080_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