Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WiFi auto connect + reboot counter #47

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 174 additions & 0 deletions arduino/libraries/WiFi/src/RGBled.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#include "esp_log.h"
#include <esp_wifi.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "RGBled.h"
#include "esp_event_base.h"
#include "../private_include/esp_event_internal.h"

#include "wiring_digital.h"

// Event loop
static esp_event_loop_handle_t RGB_event_loop;

bool rgb_stop = false;
bool ap_connected = true;
int blink_time = 0;

ESP_EVENT_DEFINE_BASE(RGB_LED_EVENT)


void RGBmode(uint32_t mode)
{
pinMode(25, mode);
pinMode(26, mode);
pinMode(27, mode);
}

void clearLed(void)
{
analogWrite(25, 0); //GREEN
analogWrite(26, 0); //RED
analogWrite(27, 0); //BLU
}

void RGBcolor(uint8_t red, uint8_t green, uint8_t blue)
{
if (rgb_stop == true) {
clearLed();
}
else {
analogWrite(25, green);
analogWrite(26, red);
analogWrite(27, blue);
}
}

void APconnection(void)
{
wifi_sta_list_t stadevList;
esp_wifi_ap_get_sta_list(&stadevList);

uint8_t num_dev = stadevList.num;
int blink_time = 1000 - 250*(num_dev-1);

RGBcolor(127, 0, 255); //VIOLET
delay(blink_time);
RGBcolor(0, 255, 0); //GREEN
delay(blink_time);
if (uxQueueMessagesWaiting(((esp_event_loop_instance_t*) RGB_event_loop)->queue) == 0) {
esp_event_post_to(RGB_event_loop, RGB_LED_EVENT, RGB_EVENT_AP_STACONNECTED, NULL, 0, 0);
}
}

void APdisconnection(void)
{
wifi_sta_list_t stadevList;
esp_wifi_ap_get_sta_list(&stadevList);

uint8_t num_dev = stadevList.num;
int blink_time = 1000 - 250*num_dev;

RGBcolor(127, 0, 255); //VIOLET
delay(blink_time);
RGBcolor(255, 0, 0); //GREEN
delay(blink_time);
if (uxQueueMessagesWaiting(((esp_event_loop_instance_t*) RGB_event_loop)->queue) == 0) {
esp_event_post_to(RGB_event_loop, RGB_LED_EVENT, RGB_EVENT_AP_STADISCONNECTED, NULL, 0, 0);
}
}

static void run_on_rgb_event(void* handler_args, esp_event_base_t base, int32_t id, void* event_data)
{
switch (id)
{
case RGB_EVENT_STA_CONNECTED:
ESP_EARLY_LOGI(TAG, "State: RGB_EVENT_STA_CONNECTED");

RGBcolor(0, 255, 0);
break;

case RGB_EVENT_STA_DISCONNECTED:
ESP_EARLY_LOGI(TAG, "State: RGB_EVENT_STA_DISCONNECTED");

RGBcolor(255, 0, 0);
break;

case RGB_EVENT_AP_START:
ESP_EARLY_LOGI(TAG, "State: RGB_EVENT_AP_START");

RGBcolor(255, 255, 0);
break;

case RGB_EVENT_AP_STACONNECTED:
ESP_EARLY_LOGI(TAG, "State: RGB_EVENT_AP_STACONNECTED");

ap_connected = true;

if (uxQueueMessagesWaiting(((esp_event_loop_instance_t*) RGB_event_loop)->queue) == 0) {
APconnection();
}
break;

case RGB_EVENT_AP_STADISCONNECTED:
ESP_EARLY_LOGI(TAG, "State: RGB_EVENT_AP_STADISCONNECTED");

ap_connected = false;

if (uxQueueMessagesWaiting(((esp_event_loop_instance_t*) RGB_event_loop)->queue) == 0) {
APdisconnection();
}
break;

case RGB_EVENT_LED_STOP:
ESP_EARLY_LOGI(TAG, "State: RGB_EVENT_LED_STOP");

rgb_stop = true;

clearLed();

break;

case RGB_EVENT_LED_RESTART:
ESP_EARLY_LOGI(TAG, "State: RGB_EVENT_LED_RESTART");

rgb_stop = false;
break;

default:
break;
}
}

void ledRGBEventSource(int32_t event_id)
{
if (event_id == SYSTEM_EVENT_STA_CONNECTED || event_id == RGB_EVENT_STA_DISCONNECTED || event_id == RGB_EVENT_AP_START || event_id == RGB_EVENT_AP_STACONNECTED || event_id == RGB_EVENT_AP_STADISCONNECTED || event_id == RGB_EVENT_LED_STOP || event_id == RGB_EVENT_LED_RESTART) {
esp_event_post_to(RGB_event_loop, RGB_LED_EVENT, event_id, NULL, 0, 0);
}
}

void rgb_init()
{
RGBmode(OUTPUT);

esp_event_loop_args_t loop_args = {
.queue_size = 5,
.task_name = "RGB_loop_task",
.task_priority = uxTaskPriorityGet(NULL),
.task_stack_size = 2048,
.task_core_id = tskNO_AFFINITY
};

esp_event_loop_create(&loop_args, &RGB_event_loop);

esp_event_handler_register_with(RGB_event_loop, RGB_LED_EVENT, RGB_EVENT_STA_CONNECTED, run_on_rgb_event, NULL);
esp_event_handler_register_with(RGB_event_loop, RGB_LED_EVENT, RGB_EVENT_STA_DISCONNECTED, run_on_rgb_event, NULL);
esp_event_handler_register_with(RGB_event_loop, RGB_LED_EVENT, RGB_EVENT_AP_START, run_on_rgb_event, NULL);
esp_event_handler_register_with(RGB_event_loop, RGB_LED_EVENT, RGB_EVENT_AP_STACONNECTED, run_on_rgb_event, NULL);
esp_event_handler_register_with(RGB_event_loop, RGB_LED_EVENT, RGB_EVENT_AP_STADISCONNECTED, run_on_rgb_event, NULL);
esp_event_handler_register_with(RGB_event_loop, RGB_LED_EVENT, RGB_EVENT_LED_STOP, run_on_rgb_event, NULL);
esp_event_handler_register_with(RGB_event_loop, RGB_LED_EVENT, RGB_EVENT_LED_RESTART, run_on_rgb_event, NULL);

}
176 changes: 176 additions & 0 deletions arduino/libraries/WiFi/src/RGBled.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#include "wiring_digital.h"

#include "WiFi.h"
#include <esp_wifi.h>
#include "esp_log.h"

#include "RGBled.h"

#define TAG "RGBled"

RGBClass::RGBClass()
{
}

void RGBClass::init(void)
{
RGBmode(OUTPUT);
}

void RGBClass::RGBmode(uint32_t mode)
{
pinMode(25, mode);
pinMode(26, mode);
pinMode(27, mode);
}

void RGBClass::RGBcolor(uint8_t red, uint8_t green, uint8_t blue)
{
analogWrite(25, green);
analogWrite(26, red);
analogWrite(27, blue);
}

void RGBClass::clearLed(void)
{
analogWrite(25, 0); //GREEN
analogWrite(26, 0); //RED
analogWrite(27, 0); //BLU
}

void RGBClass::ledRGBEvent(system_event_t* event)
{
switch (event->event_id) {
case SYSTEM_EVENT_SCAN_DONE:
ESP_EARLY_LOGI(TAG, "State: SYSTEM_EVENT_SCAN_DONE");
break;

case SYSTEM_EVENT_STA_START:
ESP_EARLY_LOGI(TAG, "State: SYSTEM_EVENT_STA_START");
break;

case SYSTEM_EVENT_STA_STOP:
ESP_EARLY_LOGI(TAG, "State: SYSTEM_EVENT_STA_STOP");
break;

case SYSTEM_EVENT_STA_CONNECTED:

ESP_EARLY_LOGI(TAG, "State: SYSTEM_EVENT_STA_CONNECTED");

RGBcolor(0, 255, 0); //GREEN
break;

case SYSTEM_EVENT_STA_GOT_IP:
ESP_EARLY_LOGI(TAG, "State: SYSTEM_EVENT_STA_GOT_IP");
break;

case SYSTEM_EVENT_STA_DISCONNECTED: {
ESP_EARLY_LOGI(TAG, "State: SYSTEM_EVENT_STA_DISCONNECTED");
uint8_t status;
status = WiFi.status();

if (status == WL_CONNECT_FAILED) {
RGBcolor(0, 127, 255); //LIGHT BLUE
}
else {
RGBcolor(255, 0, 0); //RED
}
break;
}

case SYSTEM_EVENT_STA_LOST_IP:
ESP_EARLY_LOGI(TAG, "State: SYSTEM_EVENT_STA_LOST_IP");
break;

case SYSTEM_EVENT_AP_START:
ESP_EARLY_LOGI(TAG, "State: SYSTEM_EVENT_AP_START");

RGBcolor(255, 255, 0); //YELLOW
break;

case SYSTEM_EVENT_AP_STOP:
ESP_EARLY_LOGI(TAG, "State: SYSTEM_EVENT_AP_STOP");
break;

case SYSTEM_EVENT_AP_STACONNECTED:
ESP_EARLY_LOGI(TAG, "State: SYSTEM_EVENT_AP_STOP. Before asking tasks states");
if (RGBtask_handle != NULL) {
vTaskSuspend( RGBtask_handle );
vTaskDelete( RGBtask_handle );
}
xTaskCreatePinnedToCore(RGBClass::APconnection, "APconnection", 8192, NULL, 1, &RGBtask_handle, 1);
break;

case SYSTEM_EVENT_AP_STADISCONNECTED:
if (RGBtask_handle != NULL) {
vTaskSuspend( RGBtask_handle );
vTaskDelete( RGBtask_handle );
}
xTaskCreatePinnedToCore(RGBClass::APdisconnection, "APdisconnection", 8192, NULL, 1, &RGBtask_handle, 1);
break;

default:
break;
}
}

void RGBClass::RGBstop(void*)
{
ESP_EARLY_LOGI(TAG, "RGBstop thread created");
while(1) {
if (RGB.RGBtask_handle != NULL) {
vTaskSuspend( RGB.RGBtask_handle );
vTaskDelete( RGB.RGBtask_handle );
}

RGB.clearLed();
RGB.RGBmode(INPUT);
}
}


void RGBClass::APconnection(void*)
{
wifi_sta_list_t stadevList;

ESP_EARLY_LOGI(TAG, "State: SYSTEM_EVENT_AP_STACONNECTED");
esp_wifi_ap_get_sta_list(&stadevList);

uint8_t num_dev = stadevList.num;
int blink_time = 1000 - 250*(num_dev-1);

for (int i = 0; i < 3; i++) {
RGB.RGBcolor(127, 0, 255); //VIOLET
delay(blink_time);
RGB.RGBcolor(0, 255, 0); //GREEN
delay(blink_time);
}

RGB.clearLed();

vTaskSuspend( NULL );
vTaskDelete( NULL );
}

void RGBClass::APdisconnection(void*)
{
wifi_sta_list_t stadevList;

ESP_EARLY_LOGI(TAG, "State: SYSTEM_EVENT_AP_STADISCONNECTED");
esp_wifi_ap_get_sta_list(&stadevList);

uint8_t num_dev = stadevList.num;
int blink_time = 1000 - 250*(num_dev-1);

for (int j = 0; j < 3; j++) {
RGB.RGBcolor(127, 0, 255); //VIOLET
delay(blink_time);
RGB.RGBcolor(255, 0, 0); //GREEN
delay(blink_time);
}

RGB.clearLed();

vTaskSuspend( NULL );
vTaskDelete( NULL );
}
38 changes: 38 additions & 0 deletions arduino/libraries/WiFi/src/RGBled.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef RGBLED_H_
#define RGBLED_H_

#include "esp_event.h"
#include "esp_event_loop.h"
#include "esp_timer.h"

#ifdef __cplusplus
extern "C" {
#endif

// Declarations for the event source
#define TASK_ITERATIONS_COUNT 10 // number of times the task iterates
#define TASK_PERIOD 500 // period of the task loop in milliseconds

ESP_EVENT_DECLARE_BASE(RGB_LED_EVENT); // declaration of the task events family

static const char* TAG = "RGBled";

enum {
RGB_EVENT_STA_CONNECTED = SYSTEM_EVENT_STA_CONNECTED, /* Connected to WiFi */
RGB_EVENT_STA_DISCONNECTED = SYSTEM_EVENT_STA_DISCONNECTED, /* Disconnected from WiFi */
RGB_EVENT_AP_START = SYSTEM_EVENT_AP_START, /* Access point created */
RGB_EVENT_AP_STACONNECTED = SYSTEM_EVENT_AP_STACONNECTED, /* Device connected to Access Point */
RGB_EVENT_AP_STADISCONNECTED = SYSTEM_EVENT_AP_STADISCONNECTED, /* Device disconnected from Access Point */
RGB_EVENT_LED_STOP = (SYSTEM_EVENT_MAX + 1), /* Stop RGB led */
RGB_EVENT_LED_RESTART = (SYSTEM_EVENT_MAX + 2) /* Restart RGB led */
};

void rgb_init(void);

void ledRGBEventSource(int32_t event_id); //system_event_t* event

#ifdef __cplusplus
}
#endif

#endif // #ifndef RGBLED_H_
Loading