Establishing HTTP Connection for Real-Time Temperature Monitoring with ATmega2560, MCP9808, Zephyr

I'm building a real-time temperature monitoring system with an ATmega2560 microcontroller, MCP9808 sensor, and Zephyr OS. My goal is to send temperature data to a server via HTTP, but I'm unable to establish a connection. The network interface is configured in Zephyr, and a basic HTTP client is implemented to transmit data. The code creates a socket, attempts to connect to the server (IP: 192.168.1.100, port 80), and send "Temperature data", but it fails with errors: "Failed to create socket." I've used Zephyr's logging module for error handling. Any suggestions to resolve this issue would be appreciated!
this is my code snippet:
#include <zephyr.h>
#include <net/socket.h>
#include <logging/log.h>

LOG_MODULE_REGISTER(main);

void main(void)
{
    int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock < 0) {
        LOG_ERR("Failed to create socket");
        return;
    }

    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(80);
    inet_pton(AF_INET, "192.168.1.100", &server_addr.sin_addr);

    if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        LOG_ERR("Failed to connect to server");
        close(sock);
        return;
    }

    char msg[] = "Temperature data";
    if (send(sock, msg, strlen(msg), 0) < 0) {
        LOG_ERR("Failed to send data");
        close(sock);
        return;
    }

    LOG_INF("Data sent to server");
    close(sock);
}

@Middleware & OS
Solution
The error message “Failed to create socket” indicates that there’s a problem with socket creation
Was this page helpful?