aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2021-11-29 17:38:06 -0700
committerJosh Rahm <joshuarahm@gmail.com>2021-11-29 17:38:06 -0700
commitc6a0cf2ff3403e9726ea6429af50bedcfd4f9a01 (patch)
tree428d4348bbf9ea44fa279368002d2c58b9f388c9
parent1888bc20144559265b0719e5a0f83a5150a867c6 (diff)
downloadesp32-ws2812b-c6a0cf2ff3403e9726ea6429af50bedcfd4f9a01.tar.gz
esp32-ws2812b-c6a0cf2ff3403e9726ea6429af50bedcfd4f9a01.tar.bz2
esp32-ws2812b-c6a0cf2ff3403e9726ea6429af50bedcfd4f9a01.zip
Added simple routine to use a server socket.
-rw-r--r--include/station.h2
-rw-r--r--main/main.c36
-rw-r--r--main/station.c16
3 files changed, 46 insertions, 8 deletions
diff --git a/include/station.h b/include/station.h
index 70d1b14..39d7f53 100644
--- a/include/station.h
+++ b/include/station.h
@@ -2,6 +2,6 @@
#ifndef STATION_H_
#define STATION_H_
-void wifi_init_station();
+void wifi_init_station(const char* ssid, const char* psk);
#endif /* STATION_H_ */
diff --git a/main/main.c b/main/main.c
index 06fb4ef..5723c4c 100644
--- a/main/main.c
+++ b/main/main.c
@@ -2,6 +2,7 @@
#include "drv/ws2812b.h"
#include "esp_spi_flash.h"
#include "esp_system.h"
+#include "lwip/sockets.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "sdkconfig.h"
@@ -84,7 +85,40 @@ void app_main(void)
printf("Configuration complete!!\n");
- wifi_init_station();
+ wifi_init_station("Wort", "JoshIsBau5");
+
+ int s = socket(AF_INET, SOCK_STREAM, 0);
+ struct sockaddr_in addr;
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_port = htons(1234);
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = INADDR_ANY;
+ int err = bind(s, (struct sockaddr*) &addr, sizeof(addr));
+
+ if (err) {
+ printf("Error binding to sockaddr: %d\n", err);
+ return;
+ }
+
+ printf("Listening ...\n");
+ listen(s, 1);
+
+ while (true) {
+ printf("Waiting for connection... \n");
+ char buf[128];
+
+ struct sockaddr_in client_addr;
+ socklen_t size;
+ int sock = accept(s, (struct sockaddr*)&client_addr, &size);
+
+ printf("Accepted connection\n");
+ ssize_t len;
+
+ while ((len = read(sock, buf, sizeof(buf) - 1)) > 0) {
+ buf[len] = 0;
+ printf("Got %s\n", buf);
+ }
+ }
ws2812b_t* drv = ws2812b_init(spi);
ws2812b_buffer_t* buffer = ws2812b_new_buffer(5);
diff --git a/main/station.c b/main/station.c
index fa1dd0d..8f69f49 100644
--- a/main/station.c
+++ b/main/station.c
@@ -40,7 +40,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
}
}
-void wifi_init_station()
+void wifi_init_station(const char* ssid, const char* psk)
{
/** Initialize the flash. Not exactly sure why this is required, but it is,
* otherwise there will be a fault. */
@@ -76,12 +76,12 @@ void wifi_init_station()
wifi_config_t wifi_config = {
.sta = {
- .ssid = "Wort",
- .password = "psk",
+ // .ssid = ssid,
+ // .password = psk,
/* Setting a password implies station will connect to all security modes including WEP/WPA.
* However these modes are deprecated and not advisable to be used. Incase your Access point
* doesn't support WPA2, these mode can be enabled by commenting below line */
- .threshold.authmode = WIFI_AUTH_WPA2_PSK,
+ .threshold.authmode = WIFI_AUTH_WPA2_PSK,
.pmf_cfg = {
.capable = true,
@@ -89,6 +89,10 @@ void wifi_init_station()
},
},
};
+
+ memcpy(wifi_config.sta.ssid, ssid, strlen(ssid));
+ memcpy(wifi_config.sta.password, psk, strlen(psk));
+
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
@@ -106,10 +110,10 @@ void wifi_init_station()
* happened. */
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
- "Wort", "psk");
+ ssid, psk);
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
- "Wort", "psk");
+ ssid, psk);
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}