Compare commits
3 commits
f485d0b2f7
...
c2e6c645b3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c2e6c645b3 | ||
|
|
34d7d5a013 | ||
|
|
5cc09385f2 |
6 changed files with 90 additions and 10 deletions
3
.gitmodules
vendored
3
.gitmodules
vendored
|
|
@ -2,3 +2,6 @@
|
||||||
path = components/esp_netif_lora
|
path = components/esp_netif_lora
|
||||||
url = https://git.oit.cloud/morgan/esp_netif_lora.git
|
url = https://git.oit.cloud/morgan/esp_netif_lora.git
|
||||||
branch = development
|
branch = development
|
||||||
|
[submodule "esp-idf"]
|
||||||
|
path = esp-idf
|
||||||
|
url = https://github.com/espressif/esp-idf.git
|
||||||
|
|
|
||||||
|
|
@ -5,4 +5,4 @@ cmake_minimum_required(VERSION 3.16)
|
||||||
add_compile_options(-fdiagnostics-color=always)
|
add_compile_options(-fdiagnostics-color=always)
|
||||||
|
|
||||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
project(coatt)
|
project(lora_netif_example)
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
Subproject commit 990f338826b50c3bca9953dcea7411ac841cd1c1
|
Subproject commit a893586025d4f8591de124b42a9ba529fd397a31
|
||||||
1
esp-idf
Submodule
1
esp-idf
Submodule
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 3b8741b172dc951e18509698dee938304bcf1523
|
||||||
|
|
@ -1,2 +1,5 @@
|
||||||
idf_component_register(SRCS "main.c"
|
idf_component_register(
|
||||||
INCLUDE_DIRS ".")
|
INCLUDE_DIRS "."
|
||||||
|
SRCS "main.c"
|
||||||
|
REQUIRES esp_netif_lora
|
||||||
|
)
|
||||||
|
|
|
||||||
85
main/main.c
85
main/main.c
|
|
@ -1,14 +1,19 @@
|
||||||
#include <string.h>
|
#include "esp_random.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "esp_mac.h"
|
#include "esp_mac.h"
|
||||||
#include "esp_wifi.h"
|
#include "socket.h"
|
||||||
|
|
||||||
#include "nvs_flash.h"
|
|
||||||
|
|
||||||
#include "esp_netif_lora.h"
|
#include "esp_netif_lora.h"
|
||||||
|
|
||||||
|
#define TAG "netif"
|
||||||
|
|
||||||
void app_main() {
|
void app_main() {
|
||||||
esp_netif_lora_config_t config;
|
static esp_netif_lora_config_t config;
|
||||||
|
|
||||||
|
config.netif = NULL;
|
||||||
|
config.lora = NULL;
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "config.lora: %p", (void*)config.lora);
|
||||||
|
|
||||||
esp_efuse_mac_get_default((uint8_t*)&config.mac);
|
esp_efuse_mac_get_default((uint8_t*)&config.mac);
|
||||||
|
|
||||||
|
|
@ -16,6 +21,74 @@ void app_main() {
|
||||||
esp_netif_set_ip4_addr(&config.ip_info.gw, 10, 10 , 0, 1);
|
esp_netif_set_ip4_addr(&config.ip_info.gw, 10, 10 , 0, 1);
|
||||||
esp_netif_set_ip4_addr(&config.ip_info.netmask, 255, 255 , 255, 0);
|
esp_netif_set_ip4_addr(&config.ip_info.netmask, 255, 255 , 255, 0);
|
||||||
|
|
||||||
esp_log_level_set("LoRa32", ESP_LOG_INFO);
|
//esp_log_level_set("spi_master", ESP_LOG_DEBUG);
|
||||||
|
//esp_log_level_set("LoRa32", ESP_LOG_DEBUG);
|
||||||
|
|
||||||
esp_netif_lora_init(&config);
|
esp_netif_lora_init(&config);
|
||||||
|
|
||||||
|
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
|
|
||||||
|
struct sockaddr_in local_addr;
|
||||||
|
local_addr.sin_family = AF_INET;
|
||||||
|
//local_addr.sin_addr.s_addr = config.ip_info.ip.addr;
|
||||||
|
local_addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
local_addr.sin_port = htons(666);
|
||||||
|
|
||||||
|
//ESP_ERROR_CHECK(ret);
|
||||||
|
|
||||||
|
struct timeval timeout;
|
||||||
|
|
||||||
|
timeout.tv_sec = esp_random() % 4 + 1;
|
||||||
|
timeout.tv_usec = 0;
|
||||||
|
|
||||||
|
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
||||||
|
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, NULL, 0);
|
||||||
|
|
||||||
|
ip_mreq mreq;
|
||||||
|
mreq.imr_multiaddr.s_addr = inet_addr("224.0.1.187");
|
||||||
|
mreq.imr_interface.s_addr = config.ip_info.ip.addr;
|
||||||
|
|
||||||
|
setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq));
|
||||||
|
|
||||||
|
// TODO double check this is actually part of accepting multicast
|
||||||
|
// or if it was just binding to INADDR_ANY
|
||||||
|
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void*)1, 1);
|
||||||
|
|
||||||
|
int ret = bind(sock, (struct sockaddr *)&local_addr, sizeof(local_addr));
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "looping");
|
||||||
|
|
||||||
|
uint8_t buf[200];
|
||||||
|
uint8_t client_addr_len;
|
||||||
|
ssize_t recieved;
|
||||||
|
ssize_t sent;
|
||||||
|
|
||||||
|
buf[0] = 0x06;
|
||||||
|
buf[1] = 0x06;
|
||||||
|
buf[2] = 0x06;
|
||||||
|
|
||||||
|
struct sockaddr_in broadcast;
|
||||||
|
broadcast.sin_family = AF_INET;
|
||||||
|
broadcast.sin_addr.s_addr = inet_addr("224.0.1.187");
|
||||||
|
broadcast.sin_port = htons(666);
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
recieved = recvfrom(sock, &buf, 255, 0, (struct sockaddr *)&local_addr, (socklen_t *)&client_addr_len);
|
||||||
|
|
||||||
|
if((int)recieved == -1) {
|
||||||
|
sent = sendto(sock, &buf, sizeof(buf), 0, (struct sockaddr *)&broadcast, sizeof(broadcast));
|
||||||
|
|
||||||
|
if(sent == -1) {
|
||||||
|
ESP_LOGI(TAG, "sendto errno: %d", errno);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ESP_LOGI(TAG, "buf[0] = %d", buf[0]);
|
||||||
|
|
||||||
|
buf[0]++;
|
||||||
|
|
||||||
|
sent = sendto(sock, &buf, sizeof(buf), 0, (struct sockaddr *)&broadcast, sizeof(broadcast));
|
||||||
|
}
|
||||||
|
|
||||||
|
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue