0

I am trying to program a GPRS/GNSS/... Raspberry PI HAT. Sending commands with Minicom works fine. Sending commands with a Python script works for most of the commands but not for all. Using the Python script commands are received but gives me back errors. I have tried a lot of init sequences, all of those works in Minicom, but not in the Python script. Once every two executions AT+CSTT="APNNAME" works And AT+CIICR never worked in the script. I have restarted the board, the result is the same. The board answer me but with a "+PDP: DEACT" and the "ERROR"

Here is the code:

#!/usr/bin/env python 
from serial import *
import time 
import io 
import sys 
import RPi.GPIO as GPIO 

# --- Use GPIO PINs to boot the Board 
def toggle_board_onoff(): 
    GPIO.setmode(GPIO.BCM) 
    GPIO.setup(4,GPIO.OUT)

    GPIO.output(4,GPIO.HIGH)
    GPIO.output(4,GPIO.LOW)
    time.sleep(3)
    GPIO.output(4,GPIO.HIGH)

# --- Send AT command to the board. (sio ~ serial port, cmd=command, wait=time to wait after execution before read the result)
def send_cmd(sio, cmd, wait=0): 
    # - Write section 
    sio.flush()
    sio.write(unicode(cmd)) 
    sio.flush()
    print("CMD-> '" + str(cmd) + "'") 

    # - Wait section 
    if wait > 0:
        print("[ DEBUG ] <" + cmd.rstrip() + "> executed, will wait " + str(wait) + " sec")
        time.sleep(wait) 
        print("[ DEBUG ] Wait for " + str(wait) + " sec [ DONE ]") 

    # - Read section
    ligne = sio.readline() 
    print(ligne) 

    # - Expect 'OK' answer
    while(ligne.rstrip() != "OK"):
        if(ligne.rstrip() == "ERROR"): 
            return -1
            #sys.exit(1) 
        ligne = sio.readline()
        print("[" + ligne + "]")  

    # - Split screen for next AT command
    print("----------------------------------")      


# --- Commands to execute in order to bring the network UP 
def init_gprs_network_short(sio):

    com4=b"AT+CSTT='FREE'\n"
    com5=b'AT+CIICR\n'

    if(send_cmd(sio, com4,3) == -1):
        return -1
    if(send_cmd(sio, com5,3) == -1):
        return -1


# --- Serial communication with the board 
def serial_init(): 
    # - This function might be called in a loop for debug (auto reboot if error on network auth) 
    # -------------- Reboot section --------------- 
    time.sleep(2) 
    print("Board reBoot [ ... ]")
    print("Off...")
    toggle_board_onoff()
    time.sleep(8) 
    print("On...")
    toggle_board_onoff() 
    print("Board reBoot [ OK ]") 
    #print("Board is warming [ ... ]") 
    #time.sleep(5) 
    #print("Board is warming [ OK ]") 
    # ---------------------------------------------
    with Serial(port="/dev/ttyS0", baudrate=115200, timeout=1, writeTimeout=1) as port_serie:
            if port_serie.isOpen():
                sio = io.TextIOWrapper(io.BufferedRWPair(port_serie, port_serie)) 
                # - Let the board show boot msg (SMS ok, Call ok etc) 
                for i in range(0,4):
                    print("Boot msg iter [" + str(i) + "]")  
                    time.sleep(0.5) 
                    ligne = sio.readline() 
                    print(ligne)

                # - Try to bring the network UP...          
                network_active = init_gprs_network_short(sio) 
                if network_active == -1:
                    return network_active  

                #while True:
                #       ligne = sio.readline()
                #       print(ligne == unicode("ligne\n"))  
                #       print(ligne) i
    return -1


def main(): 
    # - Boot the board
    print("Board Boot [ ... ]") 
    toggle_board_onoff() 
    print("Board Boot [ OK ]") 

    board_connected_gprs = serial_init()
    # - While we got errors when trying to bring the network UP...
    while(board_connected_gprs == -1):
        board_connected_gprs = serial_init()

main()  

And here is the execution content:

Board Boot [ ... ]
serial_at_gprs_2.py:11: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(4,GPIO.OUT)
Board Boot [ OK ]
Board reBoot [ ... ]
Off...
On...
Board reBoot [ OK ]
Boot msg iter [0]

Boot msg iter [1]

Boot msg iter [2]

Boot msg iter [3]

CMD-> 'AT+CSTT='FREE'
'
[ DEBUG ] <AT+CSTT='FREE'> executed, will wait 3 sec
[ DEBUG ] Wait for 3 sec [ DONE ]
AT+CSTT='FREE'

[OK
]
----------------------------------
CMD-> 'AT+CIICR
'
[ DEBUG ] <AT+CIICR> executed, will wait 3 sec
[ DEBUG ] Wait for 3 sec [ DONE ]
AT+CIICR

[+PDP: DEACT
]
[
]
[ERROR
]
Board reBoot [ ... ]
Off...

The problem does no seams to come from the board as it works fine in Minicom. I first thought that it was an encoding problem, but the command is well read as the board answer me...

Do you have any ideas that could help me with this ? If you need more information about the config or anything else, please tell me.

Board: https://www.waveshare.com/wiki/GSM/GPRS/GNSS_HAT

SIM868_GNSS_Application Note_V1.00: https://www.waveshare.com/w/upload/3/3d/SIM868_GNSS_Application_Note_V1.00.pdf

Thank you very much !

Milos
  • 21
  • 3
  • 1
    Have you tried sending "\r\n" after the commands ? – tomgalpin Feb 27 '20 at 13:44
  • Does this answer your question? [Console does not show the entire answar from serial port](https://stackoverflow.com/questions/57427052/console-does-not-show-the-entire-answar-from-serial-port) – B.Letz Feb 27 '20 at 13:52
  • Before you send the second command, read the output to see if anything else was received. – slybloty Feb 27 '20 at 13:56
  • I tried "\r\n" at the end: com4=b"AT+CSTT='FREE'\r\n" but I got the same result on multiple executions.. – Milos Feb 27 '20 at 13:56
  • I added : ligne = sio.readline() and print(ligne) at the top of send_cmd() but nothing special shows up... – Milos Feb 27 '20 at 13:58
  • Have you tried sending ASCII codes instead of the actual characters? – slybloty Feb 27 '20 at 14:09
  • What do you mean ? Just the line breaks or the whole command ? I said that the problem might be due to encoding, but I also said that other commands return positive answers like "AT", "AT+CSQ" or "AT+CREG?". Thanks to 'ord()' I got 10 and 13 for \n and \r but how am I supposed to send it as its ASCII ? – Milos Feb 28 '20 at 13:12

1 Answers1

1
  1. use AT+CIPSHUT
  2. use \r\n (CR and LF, you can check the difference here > Difference between CR LF, LF and CR line break types?)

These steps below worked for me. using on my code (UART connection baudrate=9600)

import sys, time
from machine import UART

sim800 = UART(1, baudrate=9600, tx=Pin(8), rx=Pin(9)) ##this pinout for raspberry pi pico

# [...] something about my code that is not the question point 

#try to connect AT+CSTT in APN mode
sim800.write('AT+CSTT="host","username","password"\r\n') ## this is to connect into APN
time.sleep(.5)

if 'ERROR' in str(sim800.read()):
    print('error to connect 1, trying again...')
    sim800.write('AT+CIPSTATUS\r\n') ## this is to check the status, probably it will be 'IP START' or 'PDP: DEAC'
    time.sleep(.5)

    if 'IP INITIAL' not in str(sim800.read()):
        sim800.write('AT+CIPSHUT\r\n') ## after this command the cipstatus will change to IP INITIAL
        time.sleep(.5)

        # now we can connect to apn using AT+CSTT
        sim800.write('AT+CSTT="host","username","password"\r\n')
        time.sleep(.5)

        if 'ERROR' in str(sim800.read()):
            print('error to connect 2. closing')
            sys.exit()

adjusting it to your code, will be like:

[...]
com6 = b'AT+CIPSTATUS\r\n'
com7 = b'AT+CIPSHUT\r\n'

if('IP INITIAL' not in send_cmd(sio, com6,3)):
    send_cmd(sio, com7,3)
[...]