connecting sim800l and neo_6m to nrf52840dk

hi , i'm  trying to connet sim800l and neo_6m  modules to nrf52840dk using zephyr ,however i can't find any code source to use .,or do i have to creat the code from scratch if so can you give me the main steps to do

Parents Reply Children
  • hi , i'm having trouble in using uarts ,since i'm using 2 modules do i have 2 use uart0 and uart1 or i can use just one uart 

  • There is a sample you can get to start Low Power UART — nRF Connect SDK 2.6.1 documentation (nordicsemi.com). You may use one uart to switch between these two modules, but this will make your hardware and software design complex and may introduce some limitation since two modules can not communicate with host at the same time.

    Best regards,

    Charlie

  • hi this is what i did i created a folder in which there is:

    main.c:

    #include <zephyr.h>
    #include <device.h>
    #include <drivers/uart.h>
    #include <logging/log.h>
    #include <string.h>
    #include <stdio.h>
    #define LOG_MODULE_NAME app
    LOG_MODULE_REGISTER(LOG_MODULE_NAME);

    #define UART_BUF_SIZE 256

    const struct device *uart_gps; // UART1 for GPS
    const struct device *uart_gsm; // UART0 for GSM

    static void uart_callback(const struct device *dev, struct uart_event *evt, void *user_data);
    static void gps_read_data(void);
    static void gsm_send_sms(const char *message);

    static uint8_t uart_buffer[UART_BUF_SIZE];
    static size_t uart_buf_len;

    void main(void)
    {
        int err;

        // Initialize UART0 for GPS
        uart_gps = device_get_binding("UART_1");
        if (!uart_gps) {
            LOG_ERR("Cannot bind UART1");
            return;
        }

        err = uart_callback_set(uart_gps, uart_callback, NULL);
        if (err) {
            LOG_ERR("Cannot set UART1 callback");
            return;
        }

        // Initialize UART1 for GSM
        uart_gsm = device_get_binding("UART_0");
        if (!uart_gsm) {
            LOG_ERR("Cannot bind UART0");
            return;
        }

        err = uart_callback_set(uart_gsm, uart_callback, NULL);
        if (err) {
            LOG_ERR("Cannot set UART0 callback");
            return;
        }

        LOG_INF("UARTs initialized");

        // Main loop to read GPS data and send SMS
        while (1) {
            gps_read_data();
            k_sleep(K_SECONDS(10)); // Adjust sleep duration as needed
        }
    }

    static void uart_callback(const struct device *dev, struct uart_event *evt, void *user_data)
    {
        switch (evt->type) {
            case UART_RX_RDY:
                memcpy(uart_buffer + uart_buf_len, evt->data.rx.buf, evt->data.rx.len);
                uart_buf_len += evt->data.rx.len;
                break;
            case UART_RX_DISABLED:
                uart_rx_enable(dev, uart_buffer, sizeof(uart_buffer), 50);
                break;
            default:
                break;
        }
    }

    static void gps_read_data(void)
    {
        uart_rx_enable(uart_gps, uart_buffer, sizeof(uart_buffer), 50);
        k_sleep(K_SECONDS(1)); // Wait for GPS data

        // Process GPS data (example parsing)
        char latitude[20], longitude[20];
        if (sscanf((const char *)uart_buffer, "Latitude: %[^,], Longitude: %[^\n]", latitude, longitude) == 2) {
            char gps_message[128];
            snprintf(gps_message, sizeof(gps_message), "Location: %s, %s", latitude, longitude);

            // Send GPS data via GSM
            gsm_send_sms(gps_message);
        } else {
            LOG_WRN("Failed to parse GPS data");
        }
    }

    static void gsm_send_sms(const char *message)
    {
        // Example of sending SMS AT commands
        const char *at_commands[] = {
            "AT\r\n",
            "AT+CMGF=1\r\n",                 // Set SMS to text mode
            "AT+CMGS=\"+1234567890\"\r\n",   // Replace with the destination phone number
            message,
            "\x1A"                            // ASCII code for Ctrl+Z to send the SMS
        };

        for (int i = 0; i < ARRAY_SIZE(at_commands); i++) {
            uart_tx(uart_gsm, at_commands[i], strlen(at_commands[i]), SYS_FOREVER_MS);
            k_sleep(K_MSEC(100)); // Wait for the command to be processed
        }
    }
    prj.conf:
    CONFIG_NEWLIB_LIBC=y
    CONFIG_STDOUT_CONSOLE=y
    CONFIG_UART_INTERRUPT_DRIVEN=y  # Enable interrupt-driven UART

    CONFIG_LOG=y
    CONFIG_LOG_DEFAULT_LEVEL=4
    # General configurations
    CONFIG_MAIN_STACK_SIZE=2048
    CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048

    # Enable UART
    CONFIG_SERIAL=y

    # Enable logging
    CONFIG_LOG=y
    CONFIG_LOG_DEFAULT_LEVEL=4

    # UART settings
    CONFIG_UART_INTERRUPT_DRIVEN=y
    CONFIG_UART_ASYNC_API=y
     
    nrf52840dk_nrf52840.overlay:
    &uart0 {
        compatible = "nordic,nrf-uarte";
        current-speed = <9600>;
        status = "okay";
        pinctrl-0 = <&uart0_default>;
        pinctrl-1 = <&uart0_sleep>;
        pinctrl-names = "default", "sleep";
    };

    &uart1 {
        compatible = "nordic,nrf-uarte";
        current-speed = <9600>;
        status = "okay";
        pinctrl-0 = <&uart1_default>;
        pinctrl-1 = <&uart1_sleep>;
        pinctrl-names = "default", "sleep";
    };

    &pinctrl {
        uart0_default: uart0_default {
            group1 {
                psels = <NRF_PSEL(UART_TX, 0, 6)>,   // TX pin for UART0
                        <NRF_PSEL(UART_RX, 0, 8)>;   // RX pin for UART0
            };
        };

        uart0_sleep: uart0_sleep {
            group1 {
                psels = <NRF_PSEL(UART_TX, 0, 6)>,   // TX pin for UART0
                        <NRF_PSEL(UART_RX, 0, 8)>;   // RX pin for UART0
            };
        };

        uart1_default: uart1_default {
            group1 {
                psels = <NRF_PSEL(UART_TX, 1, 5)>,   // TX pin for UART1
                        <NRF_PSEL(UART_RX, 1, 7)>;   // RX pin for UART1
            };
        };

        uart1_sleep: uart1_sleep {
            group1 {
                psels = <NRF_PSEL(UART_TX, 1, 5)>,   // TX pin for UART1
                        <NRF_PSEL(UART_RX, 1, 7)>;   // RX pin for UART1
            };
        };
    };
     and when i flash the code and then i press boot /reset button nothing happens 
    if you can help localisate the problem
Related