Guide by Tim; 4G and GPS HAT For Raspberry Pi - Waveshare SIM7600X

Hi Aaron, Below is the code I managed to put together. For some reason the Receiving code tends to ‘Error’ out. I think it might have something to do with the way the ‘send_at’ function is arranged.

import RPi.GPIO as GPIO
import serial
import time

GPIO.setwarnings(False) 
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT)
GPIO.output(21, GPIO.LOW)

ser = serial.Serial("/dev/ttyS0",115200)
#ser.flushInput()

phone_number = '+16785577282' #********** change it to the phone number you want to call
text_message = 'Green Monster Hello2'
power_key = 6
#rec_buff = ''

def send_at(command,back,timeout):
	rec_buff = ''
	ser.write((command+'\r\n').encode())
	time.sleep(timeout)
	
	incoming_bytes = ser.inWaiting()
	print(incoming_bytes)
	
	if ser.inWaiting()>=0:
		time.sleep(0.01)
		rec_buff = ser.read(ser.inWaiting())
	if rec_buff != '':
		print(rec_buff.decode())
		if 'red' in rec_buff.decode(): GPIO.output(21, GPIO.HIGH), time.sleep(3), GPIO.output(21, GPIO.LOW)
	if back not in rec_buff.decode():
		#print(command + ' ERROR')
		print(command + ' back:\t' + rec_buff.decode())
		return 0
	else:
		#print(rec_buff.decode())
		global TEXTDATA
		TEXTDATA = str(rec_buff)
		print(TEXTDATA)
		return 1
	
def SendShortMessage(phone_number,text_message):
	
	print("Setting SMS Sending mode...")
	send_at("AT+CMGF=1","OK",1)
	print("Sending Short Message")
	answer = send_at("AT+CMGS=\""+phone_number+"\"",">",2)
	if 1 == answer:
		ser.write(text_message.encode())
		ser.write(b'\x1A')
		answer = send_at('','OK',20)
		if 1 == answer:
			print('send successfully')
		else:
			print('error')
	else:
		print('error%d'%answer)
def ReceiveShortMessage():
	rec_buff = ''
	print('Setting SMS Receving mode...')
	send_at('AT+CMGF=1','OK',1)
	#send_at('AT+CPMS=\"SM\",\"SM\",\"SM\"', 'OK', 1)
	#answer = send_at('AT+CMGR=1','+CMGR:',2)
	send_at('AT+CMGL="REC UNREAD"', 'OK', 1)
	answer = send_at('AT+CMGL="REC UNREAD"', '+CMTI', 1)
	if 1 == answer:
		answer = 0
		if 'red' in rec_buff:
			answer = 1
			print('Turning LEDS onto RED')
#		if 'OK' in rec_buff:
#			answer = 1
#			print(rec_buff)
	else:
#		print('error%d'%answer)
		print('No New text')
		return False
	return True

def power_on(power_key):
	print('SIM7600X is starting:')
	GPIO.setmode(GPIO.BCM)
	GPIO.setwarnings(False)
	GPIO.setup(power_key,GPIO.OUT)
	time.sleep(0.1)
	GPIO.output(power_key,GPIO.HIGH)
	time.sleep(2)
	GPIO.output(power_key,GPIO.LOW)
	time.sleep(20)
	ser.flushInput()
	print('SIM7600X is ready')

def power_down(power_key):
	print('SIM7600X is loging off:')
	GPIO.output(power_key,GPIO.HIGH)
	time.sleep(3)
	GPIO.output(power_key,GPIO.LOW)
	time.sleep(18)
	print('Good bye')
	

#except :
#	if ser != None:
#		ser.close()
#	GPIO.cleanup()
#	

while True:
	ser.flushInput()
	rec_buff = ''
	power_on(power_key)
	print('Sending Short Message Test:')
	SendShortMessage(phone_number,text_message)
	print('Receive Short Message Test:\n')
	print('Please send message to phone ' + phone_number)
	ReceiveShortMessage()
	power_down(power_key)

Was someone able to make it work with Raspberry Pi 5?

Hi @Dan275134 - we don’t expect there will be any difference with a Pi 5

Since the GPS took about 4 mins to send the location as you said, would it be advisable to use this in an application like a drone which would have to continuously send it’s current location to a ground station?

1 Like

This would be very useful for locating the drone after it has crashed. Communication to a local tower would likely be more effective than to the controller. The time delay would not matter, and the GPS could take as much time as needed to get an accurate fix. The ground crew searching through the scrub would probably also have a better communication link.

For position reporting while the drone is flying it would be useful for tracking to the crash site and for post-flight analysis, but not for anything that is reasonably ‘current’.

2 Likes

Hey there,
I have a issue with the sim7600x with rpi5.

When i force to reboot the system, the sim 7600x is not initialized.

In order to inizialite it i have to press the power button, after that i can also connect to the minicom serial.

How can i set to power on the sim7600x automatically when reboot?

I have followed all the procedure that you suggest in the tutorial article

Hi @Marco276148, Welcome to the forums!!!

There should be a way to get this set up.
According to the Wiki there are different power on modes you can use. They depend on if it’s the new or old version so double check which one you have before connecting pins.

I hope this helps :slight_smile:

2 Likes

Many thanks for the SIM7600 guide. I am working with an Orangepi zero 2w attached to a SIM7600 G4 HAT. I am running of Debian Bullseye. I have now spent many hours trying to get this combo working as a wifi router without success. I can get wifi and the cellular mobile to work separately, but whenever I try to get them working together I get port conflicts. I have tried numerous workarounds but without success. Any tips or tricks - or perhaps a guide would be much appreciated.

Hey @Aaron thanks for your reply.

I have checked the wiki and i am sure that i have the new version of SIM7600E-H.

I am attaching the photo of it.

and according to the documentation and my setting in theory the moment I give power to my raspberri pi 5 it should also access the HAT and it does it.

I try to explain the problem in detail.

My rpi 5 has a crontab that when it turns on it immediately starts to check if the GPS is working and that it can find the location.

I note, however, that this fails at the time when:

  1. I force in removing the power supply to the RPI 5
  2. I turn the RPI 5 back on
  3. The RPI 5 and the HAT turn on but the gps fails to acquire the position until I press the pwr button on the HAT.
1 Like

I have solved the problem by simulating the power button pressing with the pin 6.

1 Like

Hi @Marco276148,

Glad to hear you found a solution to the issue!!!

Hey All! Thought I would contribute. I recently purchased this device and tried running the script "GPS-Human-Readable-Final.py’ however I was getting an error on where "Lat’ was getting a value of ‘+\n’ the error was string to datatype long conversion… the system could not convert \n to datatype long. I found that the coordinates were shifted due to some output along with some unexpected characters such as \r\n throughout the GPSDATA variable decode.

Long story short - pun intended… I fixed it for my GPS. I removed all those characters and also removed any pre-pending characters in that output that was causing the mis-alignment. Once I removed everything that was not expected, the math started mathing and the coordinates lined up. I got within 1m accuracy, more accurate than Google Maps. I am in the US (North Carolina) so maybe everything is upside down in Australia :slight_smile:

This was my first crack at the script however parsing data the way this was done, I would never do it this way because… expect the unexpected and the numbers/letters can shift and change.

The proper way I would do this is to break up the data into known sectors, I work in IT system automation and 1 thing I’m certain is… never trust the output! I’ll add an updated script here that should account for any unexpected data and breaks up the data into sectors for parsing. I needed to understand the coordinate system first before I could take on the task of improving. This is an AWESOME DEVICE!!


#!/usr/bin/python
# -*- coding:utf-8 -*-
import RPi.GPIO as GPIO

import serial
import time

ser = serial.Serial('/dev/ttyS0',115200)
ser.flushInput()

power_key = 6
rec_buff = ''
rec_buff2 = ''
time_count = 0

def send_at(command,back,timeout):
	rec_buff = ''
	ser.write((command+'\r\n').encode())
	time.sleep(timeout)
	if ser.inWaiting():
		time.sleep(0.01 )
		rec_buff = ser.read(ser.inWaiting())
	if rec_buff != '':
		if back not in rec_buff.decode():
			print(command + ' ERROR')
			print(command + ' back:\t' + rec_buff.decode())
			return 0
		else:
			
			#print(rec_buff.decode())
			
			#Additions to Demo Code Written by Tim!
			global GPSDATA
			#print(GPSDATA)
			GPSDATA = str(rec_buff.decode()).replace('\n','').replace('\r','').replace('AT','').replace('+CGPSINFO','').replace(': ','')
			Cleaned = GPSDATA
			
			#print(Cleaned)
			
			Lat = Cleaned[:2]
			SmallLat = Cleaned[2:11]
			NorthOrSouth = Cleaned[12]
			
			#print(Lat, SmallLat, NorthOrSouth)
			
			Long = Cleaned[14:17]
			SmallLong = Cleaned[17:26]
			EastOrWest = Cleaned[27]
			
			#print(Long, SmallLong, EastOrWest)   
			FinalLat = float(Lat) + (float(SmallLat)/60)
			FinalLong = float(Long) + (float(SmallLong)/60)
			
			if NorthOrSouth == 'S': FinalLat = -FinalLat
			if EastOrWest == 'W': FinalLong = -FinalLong
			
			print(FinalLat, FinalLong)
			
			#print(FinalLat, FinalLong)
			#print(rec_buff.decode())
			
			return 1
	else:
		print('GPS is not ready')
		return 0

def get_gps_position():
	rec_null = True
	answer = 0
	print('Start GPS session...')
	rec_buff = ''
	send_at('AT+CGPS=1,1','OK',1)
	time.sleep(2)
	while rec_null:
		answer = send_at('AT+CGPSINFO','+CGPSINFO: ',1)
		if 1 == answer:
			answer = 0
			if ',,,,,,' in rec_buff:
				print('GPS is not ready')
				rec_null = False
				time.sleep(1)
		else:
			print('error %d'%answer)
			rec_buff = ''
			send_at('AT+CGPS=0','OK',1)
			return False
		time.sleep(1.5)


def power_on(power_key):
	print('SIM7600X is starting:')
	GPIO.setmode(GPIO.BCM)
	GPIO.setwarnings(False)
	GPIO.setup(power_key,GPIO.OUT)
	time.sleep(0.1)
	GPIO.output(power_key,GPIO.HIGH)
	time.sleep(2)
	GPIO.output(power_key,GPIO.LOW)
	time.sleep(20)
	ser.flushInput()
	print('SIM7600X is ready')

def power_down(power_key):
	print('SIM7600X is loging off:')
	GPIO.output(power_key,GPIO.HIGH)
	time.sleep(3)
	GPIO.output(power_key,GPIO.LOW)
	time.sleep(18)
	print('Good bye')

#Additions to Demo GPS.py Code Added by Tim // Simplfing the GPS Start up process
power_on(power_key)
while True:
	
	get_gps_position()

2 Likes

Hey @Jeremy277667, Welcome to the Forums!!!

Thanks for sharing your updated GPS-Human-Readable-Final.py file.
We will look into updating the script properly to make it useable again.

As will I :slight_smile: Id like to work with yall and perhaps create a web interface for this. I am trying to integrate with Navit however I am lacking on knowledge with these coordinate systems and the Navit configuration with this particular GPS. Any tutorials on that in the future would be helpful. Id like to make this a user-friendly experience.

1 Like

@Aaron I also have the secret sauce for connecting this GPS unit to Navit. If interested.

1 Like

Having trouble finding the IPV4 IP address. Using the current setup depicted in the video with the Waveshare SIM7600G-H modem and Raspberry Pi 4B. Is there an AT command or something to find out what the IPV4 IP address is?

Hey Thad,

You could try using hostname -I in the terminal to show your network interfaces. You should be able to find the IPV4 address there.

Cheers,
Blayden

Thanks for the video everything wok but the problem is that my gsm hat gets power off if i leave it for lets say 2 days stright and i have to press and hold the button to power it on again is there how to do that from the script

thats my gsm hat

Hi @nouh282896, Welcome to the forums!!!

It may be possible. Firstly you’ll need to check the hardware to see if it is capable of working with software power commands.
More info on this can be found on the Product Wiki

I have followed the video and this tutorial and have got it working.
I would like to write a script that listens for an incoming phone call, finds out what the callers number is and then rejects / hangs up the call, without actually answering it. Is that possible? Any help would be appreciated.