Unable to connect to the socket

I am using nrf7002-dk as client and running the server python code in  my PC and I am using ngrok command line tool get alternate http link.

I am using  wifi-fund lesson4 exercise 1 project: client can connect to the default server "echo.thing.rocks" but i can't connect to my server.

if I tried to connect I am getting this error 

from http.server import SimpleHTTPRequestHandler, HTTPServer
# import cgi

# Define the host and port
HOST = '127.0.0.1'
PORT = 8000

# Create a custom request handler
class MyHandler(SimpleHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write(b'Hello, world!!!!!')

    def do_POST(self):
        content_length = int(self.headers['Content-Length'])
        post_data = self.rfile.read(content_length)
        post_data_str = post_data.decode('utf-8')
        print(post_data_str)
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write(b'<html><body>')
        self.wfile.write(f'<h1>Posted Data:</h1><pre>{post_data_str}</pre>'.encode())
        self.wfile.write(b'</body></html>')
    

# Create and start the server
def run(server_class=HTTPServer, handler_class=MyHandler):
    server_address = (HOST, PORT)
    httpd = server_class(server_address, handler_class)
    print(f'Server running on http://{HOST}:{PORT}')
    httpd.serve_forever()

if __name__ == '__main__':
    run()
 <---- server code

/*
 * Copyright (c) 2023 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <zephyr/kernel.h>
#include <zephyr/types.h>
#include <zephyr/logging/log.h>

#include <dk_buttons_and_leds.h>

#include <zephyr/net/wifi.h>
#include <zephyr/net/wifi_mgmt.h>
#include <zephyr/net/net_mgmt.h>
#include <net/wifi_mgmt_ext.h>
#include <net/wifi_credentials.h>
#include <zephyr/net/socket.h>

/* STEP 1.2 - Include the header file of the HTTP client library */
#include <zephyr/net/http/client.h>

LOG_MODULE_REGISTER(Lesson5_Exercise1, LOG_LEVEL_INF);

#define EVENT_MASK (NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED)

/* STEP 2 - Define the macros for the HTTP server hostname and port */
#define HTTP_HOSTNAME "http://3c19-49-249-24-106.ngrok-free.app"
#define HTTP_PORT     8000

/* STEP 3 - Declare the necessary buffers for receiving messages */
#define RECV_BUF_SIZE  2048
#define CLIENT_ID_SIZE 50

static char recv_buf[RECV_BUF_SIZE];
static char client_id_buf[CLIENT_ID_SIZE + 2];

/* STEP 4 - Define the variable for the counter as 0 */
static int counter = 0;

static int sock;
static struct sockaddr_storage server;

static struct net_mgmt_event_callback mgmt_cb;
static bool connected;
static K_SEM_DEFINE(run_app, 0, 1);

static void net_mgmt_event_handler(struct net_mgmt_event_callback *cb, uint32_t mgmt_event,
				   struct net_if *iface)
{
	if ((mgmt_event & EVENT_MASK) != mgmt_event) {
		return;
	}
	if (mgmt_event == NET_EVENT_L4_CONNECTED) {
		LOG_INF("Network connected");
		connected = true;
		dk_set_led_on(DK_LED1);
		k_sem_give(&run_app);
		return;
	}
	if (mgmt_event == NET_EVENT_L4_DISCONNECTED) {
		if (connected == false) {
			LOG_INF("Waiting for network to be connected");
		} else {
			dk_set_led_off(DK_LED1);
			LOG_INF("Network disconnected");
			connected = false;
		}
		k_sem_reset(&run_app);
		return;
	}
}

static int server_resolve(void)
{
	int err;
	struct addrinfo *result;
	struct addrinfo hints = {.ai_family = AF_INET, .ai_socktype = SOCK_STREAM};

	err = getaddrinfo(HTTP_HOSTNAME, STRINGIFY(HTTP_PORT), &hints, &result);
	if (err != 0) {
		LOG_ERR("getaddrinfo failed, err: %d, %s", err, gai_strerror(err));
		return -EIO;
	}

	if (result == NULL) {
		LOG_ERR("Error, address not found");
		return -ENOENT;
	}

	struct sockaddr_in *server4 = ((struct sockaddr_in *)&server);
	server4->sin_addr.s_addr = ((struct sockaddr_in *)result->ai_addr)->sin_addr.s_addr;
	server4->sin_family = AF_INET;
	server4->sin_port = ((struct sockaddr_in *)result->ai_addr)->sin_port;

	char ipv4_addr[NET_IPV4_ADDR_LEN];
	inet_ntop(AF_INET, &server4->sin_addr.s_addr, ipv4_addr, sizeof(ipv4_addr));
	LOG_INF("IPv4 address of HTTP server found %s", ipv4_addr);

	freeaddrinfo(result);

	return 0;
}

static int server_connect(void)
{
	int err;
	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (sock < 0) {
		LOG_ERR("Failed to create socket, err: %d, %s", errno, strerror(errno));
		return -errno;
	}

	err = connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr_in));
	if (err < 0) {
		LOG_ERR("Connecting to server failed, err: %d, %s", errno, strerror(errno));
		return -errno;
	}

	LOG_INF("Connected to server");
	return 0;
}

static void response_cb(struct http_response *rsp, enum http_final_call final_data, void *user_data)
{
	/* STEP 9 - Define the callback function to print the body */
	LOG_INF("Response status: %s", rsp->http_status);

	if (rsp->body_frag_len > 0) {
		char body_buf[rsp->body_frag_len];
		strncpy(body_buf, rsp->body_frag_start, rsp->body_frag_len);
		body_buf[rsp->body_frag_len] = '\0';
		LOG_INF("Received: %s", body_buf);
	}

	LOG_INF("Closing socket: %d", sock);
	close(sock);
}

static void client_id_cb(struct http_response *rsp, enum http_final_call final_data, void *user_data)
{
	/* STEP 6.1 - Log the HTTP response status */
	LOG_INF("Response status: %s\n", rsp->http_status);
	LOG_INF("data %s\n",(const char*)user_data);
	/* STEP 6.2 - Retrieve and format the client ID */
	if(rsp->body_frag_len>0){
	char client_id_buf_tmp[CLIENT_ID_SIZE + 1];
	strncpy(client_id_buf_tmp, rsp->body_frag_start, CLIENT_ID_SIZE);
	client_id_buf_tmp[CLIENT_ID_SIZE] = '\0';
	client_id_buf[0] = '/';
	strcat(client_id_buf, "3c19-49-249-24-106.ngrok-free.app");
	LOG_INF("Successfully acquired client ID: %s", client_id_buf);
	}
	/* STEP 6.3 - Close the socket */
	LOG_INF("Closing socket: %d", sock);
	close(sock);
}

static int client_http_put(void)
{
	/* STEP 7 - Define the function to send a PUT request to the HTTP server */
	int err = 0;
	int bytes_written;
	const char *headers[] = {"Connection: close\r\n", NULL};

	struct http_request req;
	memset(&req, 0, sizeof(req));

	char buffer[12] = {0};
	bytes_written = snprintf(buffer, 12, "%d", counter);
	if (bytes_written < 0) {
		LOG_INF("Unable to write to buffer, err: %d", bytes_written);
		return bytes_written;
	}

	req.header_fields = headers;
	req.method = HTTP_PUT;
	req.url = client_id_buf;
	req.host = HTTP_HOSTNAME;
	req.protocol = "HTTP/1.1";
	req.payload = buffer;
	req.payload_len = bytes_written;
	req.response = response_cb;
	req.recv_buf = recv_buf;
	req.recv_buf_len = sizeof(recv_buf);

	LOG_INF("HTTP PUT request: %s", buffer);
	err = http_client_req(sock, &req, 5000, NULL);
	if (err < 0) {
		LOG_ERR("Failed to send HTTP PUT request %s, err: %d", buffer, err);
	}

	return err;
}

static int client_http_get(void)
{
	/* STEP 8 - Define the function to send a GET request to the HTTP server */
	int err = 0;
	const char *headers[] = {"Connection: close\r\n", NULL};
	
	struct http_request req;
	memset(&req, 0, sizeof(req));

	req.header_fields = headers;
	req.method = HTTP_GET;
	req.url = client_id_buf;
	req.host = HTTP_HOSTNAME;
	req.protocol = "HTTP/1.1";
	req.response = response_cb;
	req.recv_buf = recv_buf;
	req.recv_buf_len = sizeof(recv_buf);

	LOG_INF("HTTP GET request");
	err = http_client_req(sock, &req, 5000, NULL);
	if (err < 0) {
		LOG_ERR("Failed to send HTTP GET request, err: %d", err);
	}

	return err;
}

static int client_get_new_id(void)
{
	int err = 0;

	/* STEP 5.1 - Define the structure http_request and fill the block of memory */
	struct http_request req;
	memset(&req, 0, sizeof(req));

	/* STEP 5.2 - Populate the http_request structure */
	const char *headers[] = {"Connection: close\r\n", NULL};
	req.header_fields = headers;
	req.method = HTTP_POST;
	req.url = "/";
	req.host = HTTP_HOSTNAME;
	req.protocol = "HTTP/1.1";
	req.response = client_id_cb;
	req.recv_buf = recv_buf;
	req.recv_buf_len = sizeof(recv_buf);

	/* STEP 5.3 - Send the request to the HTTP server */
	LOG_INF("HTTP POST request");
	err = http_client_req(sock, &req, 5000, NULL);
	if (err < 0) {
		LOG_ERR("Failed to send HTTP POST request, err: %d", err);
	}
	return err;
}

static void button_handler(uint32_t button_state, uint32_t has_changed)
{
	/* STEP 10 - Define the button handler to send requests upon button triggers */
	if (has_changed & DK_BTN1_MSK && button_state & DK_BTN1_MSK) {
		if (server_connect() >= 0) {
			client_http_put();
			counter++;
		}
	} else if (has_changed & DK_BTN2_MSK && button_state & DK_BTN2_MSK) {
		if (server_connect() >= 0) {
			client_http_get();
		}
	}
}

int main(void)
{

	if (dk_leds_init() != 0) {
		LOG_ERR("Failed to initialize the LED library");
	}

	/* Sleep to allow initialization of Wi-Fi driver */
	k_sleep(K_SECONDS(1));

	net_mgmt_init_event_callback(&mgmt_cb, net_mgmt_event_handler, EVENT_MASK);
	net_mgmt_add_event_callback(&mgmt_cb);

	LOG_INF("Waiting to connect to Wi-Fi");
	k_sem_take(&run_app, K_FOREVER);

	if (dk_buttons_init(button_handler) != 0) {
		LOG_ERR("Failed to initialize the buttons library");
	}
	if (server_resolve() != 0) {
		LOG_ERR("Failed to resolve server name");
		return 0;
	}

	if (server_connect() != 0) {
		LOG_ERR("Failed to initialize client");
		return 0;
	}	

	/* STEP 11 - Retrieve the client ID upon connection */
	if (client_get_new_id() < 0) {
		LOG_ERR("Failed to get client ID");
		return 0;
	}

	return 0;
}
 <----client code 

  • I solved the above issue by changing the PORT number to 80.

    But I am facing new issue where my python client code can connect and get the GET response back  from the server but on nrf client I am getting this>  << nrf client output

     <<python code output.

    and also getting a same thing  from the HTTPie website  >>  

    BR,

    Karthik 

  • Hi

    Have you checked out the lesson5_exer1 solution project file to make sure that you've done all the steps correctly, I'm specifically thinking this might be related to step 9 which handles how to define the callback function for printing the body.

    Can you also upload the full log so I can see what happens after these temporary redirected messages? Does it keep looping to "temporary redirect" messages or does it resolve/crash at some point?

    Best regards,

    Simon

  • HI,

    My web server is locally running but I am tunneling it using ngkor command line tool. 

    I am using the same url in the HTTPie I am able to get the output but In nrf it's not working but if I replays the HOST_NAME from my URL to your default URL = "echo.thingy.rocks".

    this howl log from UART: 

    uart:~$ OK
    OK
    OK
    OK
    OK
    OK
    OK
    OK
    OK
    OK
    OK
    *** Booting nRF Connect SDK v2.5.2 ***
    [00:00:02.119,934] <inf> wifi_mgmt_ext: Connection requested
    [00:00:02.120,605] <inf> Lesson5_Exercise1: Waiting to connect to Wi-Fi
    uart:~$ Received all data: HTTP/1.1 307 Temporary Redirect
    Content-Type: text/html; charset=utf-8
    Location: still-bug-perfectly.ngrok-free.app/
    Date: Fri, 21 Jun 2024 05:45:07 GMT
    Content-Length: 79

    <a href="">still-bug-perfectly.ngrok-free.app/">Temporary Redirect</a>.

    only data:<a href="">still-bug-perfectly.ngrok-free.app/">Temporary Redirect</a>.

    [00:00:07.516,510] <inf> Lesson5_Exercise1: Network connected
    [00:00:07.715,332] <inf> Lesson5_Exercise1: IPv4 address of HTTP server found 3.6.98.232
    [00:00:07.790,435] <inf> Lesson5_Exercise1: Connected to server
    [00:00:07.790,466] <inf> Lesson5_Exercise1: HTTP GET request :
    [00:00:07.923,339] <inf> Lesson5_Exercise1: Response status: Temporary Redirect
    [00:00:07.923,339] <inf> Lesson5_Exercise1: total length 266
    [00:00:07.923,370] <inf> Lesson5_Exercise1: data length 79
    [00:00:07.923,370] <inf> Lesson5_Exercise1: more data 1
    [00:00:07.957,550] <inf> Lesson5_Exercise1: Closing socket: 9

    Does it keep looping to "temporary redirect"

    yes.

    BR,

    Karthik 

  • /*
     * Copyright (c) 2023 Nordic Semiconductor ASA
     *
     * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
     */
    
    #include <errno.h>
    #include <stddef.h>
    #include <string.h>
    #include <stdio.h>
    #include <zephyr/kernel.h>
    #include <zephyr/types.h>
    #include <zephyr/logging/log.h>
    
    #include <dk_buttons_and_leds.h>
    
    #include <zephyr/net/wifi.h>
    #include <zephyr/net/wifi_mgmt.h>
    #include <zephyr/net/net_mgmt.h>
    #include <net/wifi_mgmt_ext.h>
    #include <net/wifi_credentials.h>
    #include <zephyr/net/socket.h>
    
    /* STEP 1.2 - Include the header file of the HTTP client library */
    #include <zephyr/net/http/client.h>
    
    LOG_MODULE_REGISTER(Lesson5_Exercise1, LOG_LEVEL_INF);
    
    #define EVENT_MASK (NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED)
    
    /* STEP 2 - Define the macros for the HTTP server hostname and port */
    #define HTTP_HOSTNAME "still-bug-perfectly.ngrok-free.app"
    #define HTTP_PORT     80
    
    /* STEP 3 - Declare the necessary buffers for receiving messages */
    #define RECV_BUF_SIZE  2048
    #define CLIENT_ID_SIZE 36
    
    static char recv_buf[RECV_BUF_SIZE];
    static char client_id_buf[CLIENT_ID_SIZE + 2];
    
    /* STEP 4 - Define the variable for the counter as 0 */
    static int counter = 0;
    
    static int sock;
    static struct sockaddr_storage server;
    
    static struct net_mgmt_event_callback mgmt_cb;
    static bool connected;
    static K_SEM_DEFINE(run_app, 0, 1);
    
    static void net_mgmt_event_handler(struct net_mgmt_event_callback *cb, uint32_t mgmt_event,
    				   struct net_if *iface)
    {
    	if ((mgmt_event & EVENT_MASK) != mgmt_event) {
    		return;
    	}
    	if (mgmt_event == NET_EVENT_L4_CONNECTED) {
    		LOG_INF("Network connected");
    		connected = true;
    		dk_set_led_on(DK_LED1);
    		k_sem_give(&run_app);
    		return;
    	}
    	if (mgmt_event == NET_EVENT_L4_DISCONNECTED) {
    		if (connected == false) {
    			LOG_INF("Waiting for network to be connected");
    		} else {
    			dk_set_led_off(DK_LED1);
    			LOG_INF("Network disconnected");
    			connected = false;
    		}
    		k_sem_reset(&run_app);
    		return;
    	}
    }
    
    static int server_resolve(void)
    {
    	int err;
    	struct addrinfo *result;
    	struct addrinfo hints = {.ai_family = AF_INET, .ai_socktype = SOCK_STREAM};
    
    	err = getaddrinfo(HTTP_HOSTNAME, STRINGIFY(HTTP_PORT), &hints, &result);
    	if (err != 0) {
    		LOG_ERR("getaddrinfo failed, err: %d, %s", err, gai_strerror(err));
    		return -EIO;
    	}
    
    	if (result == NULL) {
    		LOG_ERR("Error, address not found");
    		return -ENOENT;
    	}
    
    	struct sockaddr_in *server4 = ((struct sockaddr_in *)&server);
    	server4->sin_addr.s_addr = ((struct sockaddr_in *)result->ai_addr)->sin_addr.s_addr;
    	server4->sin_family = AF_INET;
    	server4->sin_port = ((struct sockaddr_in *)result->ai_addr)->sin_port;
    
    	char ipv4_addr[NET_IPV4_ADDR_LEN];
    	inet_ntop(AF_INET, &server4->sin_addr.s_addr, ipv4_addr, sizeof(ipv4_addr));
    	LOG_INF("IPv4 address of HTTP server found %s", ipv4_addr);
    
    	freeaddrinfo(result);
    
    	return 0;
    }
    
    static int server_connect(void)
    {
    	int err;
    	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if (sock < 0) {
    		LOG_ERR("Failed to create socket, err: %d, %s", errno, strerror(errno));
    		return -errno;
    	}
    
    	err = connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr_in));
    	if (err < 0) {
    		LOG_ERR("Connecting to server failed, err: %d, %s", errno, strerror(errno));
    		return -errno;
    	}
    
    	LOG_INF("Connected to server");
    	return 0;
    }
    
    static void response_cb(struct http_response *rsp, enum http_final_call final_data, void *user_data)
    {
    	/* STEP 9 - Define the callback function to print the body */
    	LOG_INF("Response status: %s", rsp->http_status);
    	LOG_INF("total length %d",rsp->data_len);
    	LOG_INF("data length %d",rsp->body_frag_len);
    	LOG_INF("more data %d",final_data);
    	if (rsp->data_len > 0) {
    		char body_buf[rsp->data_len];
    		strncpy(body_buf, rsp->recv_buf, rsp->data_len);
    		body_buf[rsp->data_len] = '\0';
    		printf("Received all data: %s", body_buf);
    	}
    	if(rsp->body_frag_len>0)
    	{
    		char data_buf[rsp->body_frag_len];
    		strncpy(data_buf, rsp->body_frag_start, rsp->body_frag_len);
    		data_buf[rsp->body_frag_len] = '\0';
    		printf("only data:%s",data_buf);
    
    	}
    	LOG_INF("Closing socket: %d", sock);
    	close(sock);
    	
    }
    
    static void client_id_cb(struct http_response *rsp, enum http_final_call final_data,
    			 void *user_data)
    {
    	/* STEP 6.1 - Log the HTTP response status */
    	LOG_INF("Response status: %s", rsp->http_status);
    
    	/* STEP 6.2 - Retrieve and format the client ID */
    	char client_id_buf_tmp[CLIENT_ID_SIZE + 1];
    	strncpy(client_id_buf_tmp, rsp->body_frag_start, CLIENT_ID_SIZE);
    	client_id_buf_tmp[CLIENT_ID_SIZE] = '\0';
    	client_id_buf[0] = '/';
    	strcat(client_id_buf, client_id_buf_tmp);
    
    	LOG_INF("Successfully acquired client ID: %s", client_id_buf);
    
    	/* STEP 6.3 - Close the socket */
    	LOG_INF("Closing socket: %d", sock);
    	close(sock);
    }
    
    static int client_http_put(void)
    {
    	/* STEP 7 - Define the function to send a PUT request to the HTTP server */
    	int err = 0;
    	int bytes_written;
    	const char *headers[] = {"Connection: close\r\n", NULL};
    
    	struct http_request req;
    	memset(&req, 0, sizeof(req));
    
    	char buffer[12] = {0};
    	bytes_written = snprintf(buffer, 12, "%d", counter);
    	if (bytes_written < 0) {
    		LOG_INF("Unable to write to buffer, err: %d", bytes_written);
    		return bytes_written;
    	}
    
    	req.header_fields = headers;
    	req.method = HTTP_PUT;
    	req.url = client_id_buf;
    	req.host = HTTP_HOSTNAME;
    	req.protocol = "HTTP/1.1";
    	req.payload = buffer;
    	req.payload_len = bytes_written;
    	req.response = response_cb;
    	req.recv_buf = recv_buf;
    	req.recv_buf_len = sizeof(recv_buf);
    
    	LOG_INF("HTTP PUT request: %s", buffer);
    	err = http_client_req(sock, &req, 5000, NULL);
    	if (err < 0) {
    		LOG_ERR("Failed to send HTTP PUT request %s, err: %d", buffer, err);
    	}
    
    	return err;
    }
    
    static int client_http_get(void)
    {
    	/* STEP 8 - Define the function to send a GET request to the HTTP server */
    	int err = 0;
    	const char *headers[] = {"Content-Type: text/plain; charset=utf-8\r\n", NULL};
    	
    	struct http_request req;
    	memset(&req, 0, sizeof(req));
    
    	req.header_fields = headers;
    	req.method = HTTP_GET;
    	req.url = "/data";//changed removed "s"
    	req.host = HTTP_HOSTNAME;
    	req.protocol = "HTTP/1.1";//changed removed "S"
    	req.response = response_cb;
    	req.recv_buf = recv_buf;
    	req.recv_buf_len = sizeof(recv_buf);
    
    	LOG_INF("HTTP GET request");
    	err = http_client_req(sock, &req, 5000, NULL);
    	if (err < 0) {
    		LOG_ERR("Failed to send HTTP GET request, err: %d", err);
    	}
    
    	return err;
    }
    
    static int client_post(void)
    {
    	int err = 0;
    
    	/* STEP 5.1 - Define the structure http_request and fill the block of memory */
    	struct http_request req;
    	memset(&req, 0, sizeof(req));
    
    	/* STEP 5.2 - Populate the http_request structure */
    	const char *headers[] = {"Connection: close\r\n", NULL};
    	req.header_fields = headers;
    	req.method = HTTP_POST;
    	req.url = "/";
    	req.host = HTTP_HOSTNAME;
    	req.protocol = "HTTP/1.1";
    	req.response = client_id_cb;
    	req.recv_buf = recv_buf;
    	req.recv_buf_len = sizeof(recv_buf);
    
    	/* STEP 5.3 - Send the request to the HTTP server */
    	LOG_INF("HTTP POST request");
    	err = http_client_req(sock, &req, 5000, NULL);
    	if (err < 0) {
    		LOG_ERR("Failed to send HTTP POST request, err: %d", err);
    	}
    	return err;
    }
    static void server_get(void){
    /* STEP 8 - Define the function to send a GET request to the HTTP server */
    	int err = 0;
    	const char *headers[] = {"Connection: close\r\n", NULL};
    	
    	struct http_request req;
    	memset(&req, 0, sizeof(req));
    
    	req.header_fields = headers;
    	req.method = HTTP_GET;
    	req.url = "/";//changed removed "s"
    	req.host = HTTP_HOSTNAME;
    	req.protocol = "HTTP/1.1";//changed removed "S"
    	req.response = response_cb;
    	req.recv_buf = recv_buf;
    	req.recv_buf_len = sizeof(recv_buf);
    	
    	LOG_INF("HTTP GET request :%s",req.recv_buf);
    	err = http_client_req(sock, &req, 5000, NULL);
    	if (err < 0) {
    		LOG_ERR("Failed to send HTTP GET request, err: %d", err);
    	}
    	
    	return err;	
    }
    static void button_handler(uint32_t button_state, uint32_t has_changed)
    {
    	/* STEP 10 - Define the button handler to send requests upon button triggers */
    	if (has_changed & DK_BTN1_MSK && button_state & DK_BTN1_MSK) {
    		if (server_connect() >= 0) {
    			server_get(); 
    			counter++;
    		}
    	} else if (has_changed & DK_BTN2_MSK && button_state & DK_BTN2_MSK) {
    		if (server_connect() >= 0) {
    			client_http_get();
    		}
    	}
    }
    
    int main(void)
    {
    
    	if (dk_leds_init() != 0) {
    		LOG_ERR("Failed to initialize the LED library");
    	}
    
    	/* Sleep to allow initialization of Wi-Fi driver */
    	k_sleep(K_SECONDS(1));
    
    	net_mgmt_init_event_callback(&mgmt_cb, net_mgmt_event_handler, EVENT_MASK);
    	net_mgmt_add_event_callback(&mgmt_cb);
    
    	LOG_INF("Waiting to connect to Wi-Fi");
    	k_sem_take(&run_app, K_FOREVER);
    
    	if (dk_buttons_init(button_handler) != 0) {
    		LOG_ERR("Failed to initialize the buttons library");
    	}
    	if (server_resolve() != 0) {
    		LOG_ERR("Failed to resolve server name");
    		return 0;
    	}
    
    	if (server_connect() != 0) {
    		LOG_ERR("Failed to initialize client");
    		return 0;
    	}	
    
    	/* STEP 11 - Retrieve the client ID upon connection */
    	server_get(); 
    	k_msleep(200);
    	// server_get();	
    	
    
    	return 0;
    }

    83257.prj.conf

    # SPDX-License-Identifier: Apache-2.0
    
    cmake_minimum_required(VERSION 3.20.0)
    
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    project(wifi_fundamentals)
    
    target_sources(app PRIVATE src/main.c)

  • Hi

    It seems to me like you might have missed a step somewhere in lesson 5 of the Wi-Fi course, as I can't see why your http link would respond with this value. Did you follow the steps exactly as described on step 15 of the guide for example? You can also try with the "solution" project that is downloadable as part of the course to see if that works better.

    Best regards,

    Simon

Related