pulkin / micropython

MicroPython implementation on Ai-Thinker GPRS module A9 (RDA8955)
https://micropython.org
MIT License
103 stars 30 forks source link

GPS and GPRS not working at the same time #127

Open GDev-4664 opened 6 months ago

GDev-4664 commented 6 months ago

I am trying to implement a tracker device in my car. I am using A9G board to develop the tracker. The GPS and GPRS works individually very fine. But when I try to push real gps data to mqtt over gprs gps package provides 0 tracked satellites. Is there any problem with power or code ? Do I need to customize any hardware? I am using XL4015 DC-DC converter. The output of XL4015 is 5V-2A

import machine
import time
import cellular
import gps
import ujson
from umqtt import simple
import utime

# Constants
LED_PIN = 27
CONNECT_RETRY_DELAY = 5
TOPIC = "username/feeds/mycar"
MQTT_NAME = "a9g-micropython-board"
MQTT_SERVER = "io.adafruit.com"
MQTT_PORT = 1883
MQTT_USERNAME = "username"
MQTT_PASSWORD = "pass"
SECONDS_IN_DAY = 24 * 60 * 60
SECONDS_MICROPYTHON_TO_UNIX = 946684800
led = machine.Pin(LED_PIN, machine.Pin.OUT, 1)

green_led = machine.Pin(30, machine.Pin.OUT, 0)
red_led = machine.Pin(26, machine.Pin.OUT, 0)
blue_led = machine.Pin(25, machine.Pin.OUT, 0)
isFirstTime = True
last_published_time = 0
client = simple.MQTTClient(MQTT_NAME, MQTT_SERVER, MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD)

def allOutLed():
    green_led.value(0)
    red_led.value(0)
    blue_led.value(0)

def publish_gps_data(client, satellites, location, formatted_datetime):
    data_array = [
        satellites[0],
        satellites[1],
        formatted_datetime,
        location[0],
        location[1],
    ]
    json_data = ujson.dumps(data_array)
    client.publish(topic=TOPIC, msg=json_data, retain=False, qos=1)
    led.value(1)

def get_current_time():
    return utime.time()

def connect_to_cellular():
    global isFirstTime

    while True:

        print("I am in cellular Loop")
        allOutLed()
        red_led.value(1)
        if not isFirstTime:
            if cellular.gprs():
                break
        try:
            cellular.gprs("apn_name", "", "")
            break
        except Exception as e:
            print("Error connecting to cellular:", e)
            time.sleep(CONNECT_RETRY_DELAY)
    isFirstTime = False
    return

def connect_to_mqtt():
    while True:
        allOutLed()
        blue_led.value(1)
        try:
            client = simple.MQTTClient(MQTT_NAME, MQTT_SERVER, MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD)
            client.connect()
            break
        except Exception as e:
            print(e)
            continue
    return

def main():
    allOutLed()
    while True:
        connect_to_cellular()
        try:
            connect_to_mqtt()
        except Exception as e:
            print("Error:", e)
            continue
        gps.on()
        allOutLed()
        green_led.value(1)
        publish_gps_data(client, gps.get_satellites(), gps.get_location(), gps.time())
        time.sleep(20)

if __name__ == "__main__":
    main()
pulkin commented 6 months ago

2 amps is probably not enough

On Fri, Jan 5, 2024, 10:58 GDev-4664 @.***> wrote:

I am trying to implement a tracker device in my car. I am using A9G board to develop the tracker. The GPS and GPRS works individually very fine. But when I try to push real gps data to mqtt over gprs gps package provides 0 tracked satellites. Is there any problem with power or code ? Do I need to customize any hardware? I am using XL4015 DC-DC converter. The output of XL4015 is 5V-2A

import machine import time import cellular import gps import ujson from umqtt import simple import utime

Constants

LED_PIN = 27 CONNECT_RETRY_DELAY = 5 TOPIC = "username/feeds/mycar" MQTT_NAME = "a9g-micropython-board" MQTT_SERVER = "io.adafruit.com" MQTT_PORT = 1883 MQTT_USERNAME = "username" MQTT_PASSWORD = "pass" SECONDS_IN_DAY = 24 60 60 SECONDS_MICROPYTHON_TO_UNIX = 946684800 led = machine.Pin(LED_PIN, machine.Pin.OUT, 1)

green_led = machine.Pin(30, machine.Pin.OUT, 0) red_led = machine.Pin(26, machine.Pin.OUT, 0) blue_led = machine.Pin(25, machine.Pin.OUT, 0) isFirstTime = True last_published_time = 0 client = simple.MQTTClient(MQTT_NAME, MQTT_SERVER, MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD)

def allOutLed(): green_led.value(0) red_led.value(0) blue_led.value(0)

def publish_gps_data(client, satellites, location, formatted_datetime): data_array = [ satellites[0], satellites[1], formatted_datetime, location[0], location[1], ] json_data = ujson.dumps(data_array) client.publish(topic=TOPIC, msg=json_data, retain=False, qos=1) led.value(1)

def get_current_time(): return utime.time()

def connect_to_cellular(): global isFirstTime

while True:

    print("I am in cellular Loop")
    allOutLed()
    red_led.value(1)
    if not isFirstTime:
        if cellular.gprs():
            break
    try:
        cellular.gprs("apn_name", "", "")
        break
    except Exception as e:
        print("Error connecting to cellular:", e)
        time.sleep(CONNECT_RETRY_DELAY)
isFirstTime = False
return

def connect_to_mqtt(): while True: allOutLed() blue_led.value(1) try: client = simple.MQTTClient(MQTT_NAME, MQTT_SERVER, MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD) client.connect() break except Exception as e: print(e) continue return

def main(): allOutLed() while True: connect_to_cellular() try: connect_to_mqtt() except Exception as e: print("Error:", e) continue gps.on() allOutLed() green_led.value(1) publish_gps_data(client, gps.get_satellites(), gps.get_location(), gps.time()) time.sleep(20)

if name == "main": main()

— Reply to this email directly, view it on GitHub https://github.com/pulkin/micropython/issues/127, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADQ2BTGM3J4NRD3QBUHTFO3YM7FE3AVCNFSM6AAAAABBOF3WQKVHI2DSMVQWIX3LMV43ASLTON2WKOZSGA3DOMBVGY3DQNQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

GDev-4664 commented 6 months ago

Should I Try with 3amps?

pulkin commented 6 months ago

I think you should try turning off gps before sending the data

On Fri, Jan 5, 2024, 17:24 GDev-4664 @.***> wrote:

Should I Try with 3amps?

— Reply to this email directly, view it on GitHub https://github.com/pulkin/micropython/issues/127#issuecomment-1878935388, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADQ2BTEZLSRQBHTACTAXLKDYNASNHAVCNFSM6AAAAABBOF3WQKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZYHEZTKMZYHA . You are receiving this because you commented.Message ID: @.***>

GDev-4664 commented 5 months ago

Not working brother. The problem is when the mqtt is connected the tracked satellite is always zero

Pr0va1der commented 1 month ago

Have you been able to solve this problem? I came across the same one, but I'm sending over HTTP