Skip to content

Commit daf1fb1

Browse files
committed
wifi and mqtt
0 parents  commit daf1fb1

File tree

13 files changed

+2359
-0
lines changed

13 files changed

+2359
-0
lines changed

.gitignore

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
.config
2+
*.o
3+
*.pyc
4+
5+
# gtags
6+
GTAGS
7+
GRTAGS
8+
GPATH
9+
10+
# emacs
11+
.dir-locals.el
12+
13+
# emacs temp file suffixes
14+
*~
15+
.#*
16+
\#*#
17+
18+
# eclipse setting
19+
.settings
20+
21+
# MacOS directory files
22+
.DS_Store
23+
24+
# cache dir
25+
.cache/
26+
27+
# Components Unit Test Apps files
28+
components/**/build/
29+
components/**/build_*_*/
30+
components/**/sdkconfig
31+
components/**/sdkconfig.old
32+
33+
# Example project files
34+
examples/**/build/
35+
examples/**/build_esp*_*/
36+
examples/**/sdkconfig
37+
examples/**/sdkconfig.old
38+
39+
# Doc build artifacts
40+
docs/_build/
41+
docs/doxygen_sqlite3.db
42+
43+
# Downloaded font files
44+
docs/_static/DejaVuSans.ttf
45+
docs/_static/NotoSansSC-Regular.otf
46+
47+
# Unit test app files
48+
tools/unit-test-app/sdkconfig
49+
tools/unit-test-app/sdkconfig.old
50+
tools/unit-test-app/build
51+
tools/unit-test-app/build_*_*/
52+
tools/unit-test-app/output
53+
tools/unit-test-app/test_configs
54+
55+
# Unit Test CMake compile log folder
56+
log_ut_cmake
57+
58+
# test application build files
59+
tools/test_apps/**/build/
60+
tools/test_apps/**/build_*_*/
61+
tools/test_apps/**/sdkconfig
62+
tools/test_apps/**/sdkconfig.old
63+
64+
TEST_LOGS
65+
66+
# gcov coverage reports
67+
*.gcda
68+
*.gcno
69+
coverage.info
70+
coverage_report/
71+
72+
test_multi_heap_host
73+
74+
# VS Code Settings
75+
.vscode/
76+
77+
# VIM files
78+
*.swp
79+
*.swo
80+
81+
# Sublime Text files
82+
*.sublime-project
83+
*.sublime-workspace
84+
85+
# Clion IDE CMake build & config
86+
.idea/
87+
cmake-build-*/
88+
89+
# Results for the checking of the Python coding style and static analysis
90+
.mypy_cache
91+
flake8_output.txt
92+
93+
# ESP-IDF default build directory name
94+
build
95+
96+
# lock files for examples and components
97+
dependencies.lock
98+
99+
# managed_components for examples
100+
managed_components
101+
102+
# pytest log
103+
pytest_embedded_log/
104+
list_job_*.txt
105+
size_info.txt
106+
107+
# clang config (for LSP)
108+
.clangd

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The following lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.16)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
project(iot)

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
flash:
2+
idf.py build && idf.py -p /dev/cu.usbserial-0001 flash
3+
4+
monitor:
5+
idf.py -p /dev/cu.usbserial-0001 monitor

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
2+
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- |
3+
4+
# Hello World Example
5+
6+
Starts a FreeRTOS task to print "Hello World".
7+
8+
(See the README.md file in the upper level 'examples' directory for more information about examples.)
9+
10+
## How to use example
11+
12+
Follow detailed instructions provided specifically for this example.
13+
14+
Select the instructions depending on Espressif chip installed on your development board:
15+
16+
- [ESP32 Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/stable/get-started/index.html)
17+
- [ESP32-S2 Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/get-started/index.html)
18+
19+
20+
## Example folder contents
21+
22+
The project **hello_world** contains one source file in C language [hello_world_main.c](main/iot.c). The file is located in folder [main](main).
23+
24+
ESP-IDF projects are built using CMake. The project build configuration is contained in `CMakeLists.txt` files that provide set of directives and instructions describing the project's source files and targets (executable, library, or both).
25+
26+
Below is short explanation of remaining files in the project folder.
27+
28+
```
29+
├── CMakeLists.txt
30+
├── pytest_hello_world.py Python script used for automated testing
31+
├── main
32+
│ ├── CMakeLists.txt
33+
│ └── hello_world_main.c
34+
└── README.md This is the file you are currently reading
35+
```
36+
37+
For more information on structure and contents of ESP-IDF projects, please refer to Section [Build System](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/build-system.html) of the ESP-IDF Programming Guide.
38+
39+
## Troubleshooting
40+
41+
* Program upload failure
42+
43+
* Hardware connection is not correct: run `idf.py -p PORT monitor`, and reboot your board to see if there are any output logs.
44+
* The baud rate for downloading is too high: lower your baud rate in the `menuconfig` menu, and try again.
45+
46+
## Technical support and feedback
47+
48+
Please use the following feedback channels:
49+
50+
* For technical queries, go to the [esp32.com](https://esp32.com/) forum
51+
* For a feature request or bug report, create a [GitHub issue](https://github.com/espressif/esp-idf/issues)
52+
53+
We will get back to you as soon as possible.

main/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRCS "iot.c"
2+
INCLUDE_DIRS "")

main/iot.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdio.h>
2+
#include <inttypes.h>
3+
#include "sdkconfig.h"
4+
#include "freertos/FreeRTOS.h"
5+
#include "freertos/task.h"
6+
#include "esp_chip_info.h"
7+
#include "esp_flash.h"
8+
#include "esp_system.h"
9+
#include "sending.c"
10+
#include "storage.c"
11+
12+
void app_main(void)
13+
{
14+
init_nvs();
15+
16+
push_data();
17+
}

main/mqtt.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "freertos/FreeRTOS.h"
2+
#include "esp_log.h"
3+
#include "mqtt_client.h"
4+
5+
#define MQTT_BROKER_URI "mqtt://srv3.enteam.pl:1883"
6+
#define MQTT_TOPIC "sensors"
7+
#define MQTT_USERNAME ""
8+
#define MQTT_PASSWORD ""
9+
10+
static esp_mqtt_client_handle_t client;
11+
12+
static EventGroupHandle_t mqtt_event_group;
13+
14+
#define MQTT_CONNECTED_BIT BIT0
15+
#define MQTT_DISCONNECTED_BIT BIT1
16+
17+
static esp_err_t mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
18+
{
19+
esp_mqtt_event_handle_t event = event_data;
20+
ESP_LOGD(TAG, "Event: %d", event->event_id);
21+
switch (event->event_id) {
22+
case MQTT_EVENT_CONNECTED:
23+
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
24+
xEventGroupSetBits(mqtt_event_group, MQTT_CONNECTED_BIT);
25+
break;
26+
case MQTT_EVENT_DISCONNECTED:
27+
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
28+
xEventGroupSetBits(mqtt_event_group, MQTT_DISCONNECTED_BIT);
29+
break;
30+
default:
31+
break;
32+
}
33+
return ESP_OK;
34+
}
35+
36+
bool connect_mqtt(){
37+
mqtt_event_group = xEventGroupCreate();
38+
39+
esp_mqtt_client_config_t mqtt_cfg = {
40+
.broker.address.uri = MQTT_BROKER_URI,
41+
};
42+
43+
client = esp_mqtt_client_init(&mqtt_cfg);
44+
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
45+
esp_mqtt_client_start(client);
46+
47+
EventBits_t bits = xEventGroupWaitBits(mqtt_event_group,
48+
MQTT_CONNECTED_BIT | MQTT_DISCONNECTED_BIT,
49+
pdFALSE,
50+
pdFALSE,
51+
portMAX_DELAY);
52+
vEventGroupDelete(mqtt_event_group);
53+
54+
if (bits & MQTT_CONNECTED_BIT) {
55+
ESP_LOGI(TAG, "connected to mqtt");
56+
return true;
57+
} else if (bits & MQTT_DISCONNECTED_BIT) {
58+
ESP_LOGI(TAG, "failed to connect to mqtt");
59+
return false;
60+
}
61+
return false;
62+
}
63+
64+
void send_message() {
65+
esp_mqtt_client_publish(client, MQTT_TOPIC, "Hello, MQTT!", 0, 0, 0);
66+
}

main/network.c

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <string.h>
2+
#include "freertos/FreeRTOS.h"
3+
#include "freertos/event_groups.h"
4+
#include "esp_system.h"
5+
#include "esp_wifi.h"
6+
#include "esp_event.h"
7+
#include "esp_log.h"
8+
#include "nvs_flash.h"
9+
10+
#define EXAMPLE_ESP_MAXIMUM_RETRY 1
11+
12+
static EventGroupHandle_t s_wifi_event_group;
13+
14+
#define WIFI_CONNECTED_BIT BIT0
15+
#define WIFI_FAIL_BIT BIT1
16+
17+
static const char *TAG = "wifi";
18+
19+
static int s_retry_num = 0;
20+
21+
static void event_handler(void* arg, esp_event_base_t event_base,
22+
int32_t event_id, void* event_data)
23+
{
24+
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
25+
esp_wifi_connect();
26+
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
27+
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
28+
esp_wifi_connect();
29+
s_retry_num++;
30+
ESP_LOGI(TAG, "retry to connect to the AP");
31+
} else {
32+
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
33+
}
34+
ESP_LOGI(TAG,"connect to the AP fail");
35+
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
36+
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
37+
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
38+
s_retry_num = 0;
39+
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
40+
}
41+
}
42+
43+
bool connect_wifi(const char* ssid, const char * pass)
44+
{
45+
s_wifi_event_group = xEventGroupCreate();
46+
47+
ESP_ERROR_CHECK(esp_netif_init());
48+
49+
ESP_ERROR_CHECK(esp_event_loop_create_default());
50+
esp_netif_create_default_wifi_sta();
51+
52+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
53+
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
54+
55+
esp_event_handler_instance_t instance_any_id;
56+
esp_event_handler_instance_t instance_got_ip;
57+
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
58+
ESP_EVENT_ANY_ID,
59+
&event_handler,
60+
NULL,
61+
&instance_any_id));
62+
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
63+
IP_EVENT_STA_GOT_IP,
64+
&event_handler,
65+
NULL,
66+
&instance_got_ip));
67+
68+
wifi_config_t wifi_config = {
69+
.sta = {
70+
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
71+
},
72+
};
73+
strncpy((char*)wifi_config.sta.ssid,(char*)ssid, 32);
74+
strncpy((char*)wifi_config.sta.password,(char*)pass, 64);
75+
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
76+
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
77+
ESP_ERROR_CHECK(esp_wifi_start() );
78+
79+
ESP_LOGI(TAG, "wifi_init_sta finished.");
80+
81+
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
82+
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
83+
pdFALSE,
84+
pdFALSE,
85+
portMAX_DELAY);
86+
87+
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
88+
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
89+
vEventGroupDelete(s_wifi_event_group);
90+
91+
if (bits & WIFI_CONNECTED_BIT) {
92+
ESP_LOGI(TAG, "connected to ap");
93+
return true;
94+
} else if (bits & WIFI_FAIL_BIT) {
95+
ESP_LOGI(TAG, "failed to connect to ap");
96+
return false;
97+
}
98+
99+
return false;
100+
}

main/sending.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdio.h>
2+
#include "network.c"
3+
#include "mqtt.c"
4+
5+
void hello() {
6+
printf("Hello world!\n");
7+
}
8+
9+
void push_data(){
10+
printf("Pushing data...\n");
11+
connect_wifi("iPhone (Mateusz)", "12345678");
12+
connect_mqtt();
13+
send_message();
14+
}

0 commit comments

Comments
 (0)