This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "freertos/FreeRTOS.h" | |
#include "freertos/task.h" | |
#include "driver/ledc.h" | |
#include "esp_err.h" | |
#include "string.h" | |
#include "driver/gpio.h" | |
//WiFi headers | |
#include "esp_wifi.h" | |
#include "esp_log.h" | |
#include "esp_event_loop.h" | |
#include "nvs_flash.h" | |
//MQTT headers | |
#include "esp_mqtt.h" | |
//LEDC constants | |
#define LEDC_HS_TIMER LEDC_TIMER_0 | |
#define LEDC_HS_MODE LEDC_HIGH_SPEED_MODE | |
#define LEDC_HS_CH0_GPIO (23) //BLUE | |
#define LEDC_HS_CH0_CHANNEL LEDC_CHANNEL_0 | |
#define LEDC_HS_CH1_GPIO (22) //GREEN | |
#define LEDC_HS_CH1_CHANNEL LEDC_CHANNEL_1 | |
#define LEDC_HS_CH2_GPIO (21) //RED | |
#define LEDC_HS_CH2_CHANNEL LEDC_CHANNEL_2 | |
#define LEDC_TEST_CH_NUM (3) | |
#define LEDC_TEST_DUTY (1023) | |
#define WIFI_INDICATOR_LED (2) | |
//WiFi constants | |
#define DEFAULT_SSID "Connection Ting" | |
#define DEFAULT_PWD "0717817569" | |
#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN | |
//Config SSID SORT METHOD | |
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY | |
//Config FAST SCAN Threshold | |
//I suspect I can cut all this code to default rssi of -127 and auth mode wpa2-psk | |
#if CONFIG_FAST_SCAN_THRESHOLD | |
#define DEFAULT_RSSI CONFIG_FAST_SCAN_MINIMUM_SIGNAL | |
#if CONFIG_EXAMPLE_OPEN | |
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN | |
#elif CONFIG_EXAMPLE_WEP | |
#define DEFAULT_AUTHMODE WIFI_AUTH_WEP | |
#elif CONFIG_EXAMPLE_WPA | |
#define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK | |
#elif CONFIG_EXAMPLE_WPA2 | |
#define DEFAULT_AUTHMODE WIFI_AUTH_WPA2_PSK | |
#else | |
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN | |
#endif | |
#else | |
#define DEFAULT_RSSI -127 | |
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN | |
#endif | |
//MQTT Global Vars | |
const char *host = "Server Address"; //I replaced this with my server's IP Address | |
int port = 1883; | |
const char *client_id = "ESP32_MQLED"; | |
const char *username = NULL; | |
const char *password = NULL; | |
const char *Mytopic = "EspLyts"; | |
int qos = 1; | |
size_t buffer_size = 256; | |
int command_timeout = 3000; | |
static const char *Mypayload = "Karanja Baby"; | |
static const char *TAG = "scan"; | |
static TaskHandle_t task = NULL; | |
//Function that I use to create an RTOS Task | |
//This function doesn't seem to work at all. No SUB or PUB | |
//Its attached to the RTOS at the very end of the code | |
static void process() | |
{ | |
esp_mqtt_subscribe(Mytopic, 0); | |
while(1) | |
{ | |
esp_mqtt_publish(Mytopic, (uint8_t *)"world", 5, 2, false); | |
vTaskDelay(1000/portTICK_PERIOD_MS); | |
} | |
} | |
//WIFI CONFIGS -- Ignore | |
//Declare a |bool fucntion| to handle different events/scenarios | |
static esp_err_t event_handler(void *ctx, system_event_t *event) | |
{ | |
gpio_pad_select_gpio(WIFI_INDICATOR_LED); | |
gpio_set_direction(WIFI_INDICATOR_LED, GPIO_MODE_OUTPUT); | |
gpio_set_level(WIFI_INDICATOR_LED,0); | |
switch(event->event_id) | |
{ | |
case SYSTEM_EVENT_STA_START : | |
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); | |
ESP_ERROR_CHECK(esp_wifi_connect()); | |
break; | |
case SYSTEM_EVENT_STA_GOT_IP : | |
ESP_LOGI(TAG, "SYSTEM_EVENT_GOT_IP"); | |
ESP_LOGI(TAG, "Got IP: %s \n", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); | |
//Light the LED | |
gpio_set_level(WIFI_INDICATOR_LED,1); | |
//Connect MQTT | |
esp_mqtt_start(host, port, client_id, NULL, NULL); | |
break; | |
case SYSTEM_EVENT_STA_DISCONNECTED : | |
ESP_LOGI(TAG,"SYSTEM_EVENT_STA_DISCONNECTED"); | |
ESP_ERROR_CHECK(esp_wifi_connect()); | |
gpio_set_level(WIFI_INDICATOR_LED,0); | |
esp_mqtt_stop(); | |
break; | |
default: | |
break; | |
} | |
return ESP_OK; //The bool that gets returned; | |
}; | |
//MQTT MESSAGE CALLBACK | |
static void message_callback(const char *topic, uint8_t *payload, size_t len) | |
{ | |
// printf("incoming: %s => %s (%d) \n", topic, payload, (int)len); | |
printf("Message Received \n"); | |
} | |
static void status_callback(esp_mqtt_status_t status) | |
{ | |
switch(status) | |
{ | |
case ESP_MQTT_STATUS_CONNECTED: //I commented the code in here because I thought the callback wasnt working. The printf doesn't fire | |
printf("Attempting to subscribe and run the process \n"); | |
//esp_mqtt_subscribe(Mytopic, 0); | |
//xTaskCreatePinnedToCore(process, "process", 1024, NULL, 10, &task,1); | |
break; | |
case ESP_MQTT_STATUS_DISCONNECTED: | |
// vTaskDelete(task); | |
printf("MQTT Disconnected"); | |
} | |
} | |
//define worker function to do establish the actual wifi connection | |
static void wifi_scan(void) | |
{ | |
tcpip_adapter_init(); | |
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); | |
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); | |
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); | |
wifi_config_t wifi_config = { | |
.sta = { | |
.ssid = DEFAULT_SSID, | |
.password = DEFAULT_PWD, | |
.scan_method = DEFAULT_SCAN_METHOD, | |
.sort_method = DEFAULT_SORT_METHOD, | |
.threshold.rssi = DEFAULT_RSSI, | |
.threshold.authmode = DEFAULT_AUTHMODE | |
}, | |
}; | |
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); | |
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); | |
ESP_ERROR_CHECK(esp_wifi_start()); | |
} | |
void app_main() | |
{ | |
// gpio_set_level(WIFI_INDICATOR_LED,0); | |
//Initialize Non-Volatile Storage | |
esp_err_t ret = nvs_flash_init(); | |
if(ret == ESP_ERR_NVS_NO_FREE_PAGES) | |
{ | |
ESP_ERROR_CHECK(nvs_flash_erase()); | |
ret = nvs_flash_init(); | |
} | |
ESP_ERROR_CHECK(ret); | |
wifi_scan(); | |
//-------------------------------------------------------------------------------------------------------------------- | |
//Set up LEDC | |
int ch; | |
/* | |
* Prepare and set configuration of timers | |
* that will be used by LED Controller | |
*/ | |
ledc_timer_config_t ledc_timer = { | |
.duty_resolution = LEDC_TIMER_10_BIT, // resolution of PWM duty | |
.freq_hz = 50, // frequency of PWM signal | |
.speed_mode = LEDC_HS_MODE, // timer mode | |
.timer_num = LEDC_HS_TIMER // timer index | |
}; | |
ledc_timer_config(&ledc_timer); | |
/* | |
* Prepare individual configuration | |
* for each channel of LED Controller | |
* by selecting: | |
* - controller's channel number | |
* - output duty cycle, set initially to 0 [REMEMBER THIS!!!!] | |
* - GPIO number where LED is connected to | |
* - speed mode, either high or low | |
* - timer servicing selected channel | |
* Note: if different channels use one timer, | |
* then frequency and bit_num of these channels | |
* will be the same | |
*/ | |
ledc_channel_config_t ledc_channel[LEDC_TEST_CH_NUM] = { | |
{ | |
.channel = LEDC_HS_CH0_CHANNEL, | |
.duty = 0, | |
.gpio_num = LEDC_HS_CH0_GPIO, | |
.speed_mode = LEDC_HS_MODE, | |
.timer_sel = LEDC_HS_TIMER | |
}, | |
{ | |
.channel = LEDC_HS_CH1_CHANNEL, | |
.duty = 0, | |
.gpio_num = LEDC_HS_CH1_GPIO, | |
.speed_mode = LEDC_HS_MODE, | |
.timer_sel = LEDC_HS_TIMER | |
}, | |
{ | |
.channel = LEDC_HS_CH2_CHANNEL, | |
.duty = 0, | |
.gpio_num = LEDC_HS_CH2_GPIO, | |
.speed_mode = LEDC_HS_MODE, | |
.timer_sel = LEDC_HS_TIMER | |
} | |
}; | |
// Set LED Controller with previously prepared configuration. Config all PWM Channels | |
for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) { | |
ledc_channel_config(&ledc_channel[ch]); | |
} | |
//Define struct for storing the color PWM values | |
typedef struct | |
{ | |
int redVal; | |
int greenVal; | |
int blueVal; | |
} colors_t; | |
colors_t blue = {0,0,1023}; //0,0,100 | |
colors_t red = {1023,0,0}; //100,0,0 | |
colors_t green = {0, 1023, 0}; //0,100,0 | |
colors_t magenta = {700,300,1000}; //100,0,100 | |
colors_t purple = {320,0,320}; //33,0,33 | |
colors_t white = {1023,1023,1023}; //100,100,100 | |
colors_t aqua = {0, 1023,1023}; //0,100,100 | |
colors_t yellow = {1023,1023,0}; //100,100,0 | |
//Define func for changing color. Takes struct pointer as a param. | |
void changeColor(colors_t *colorVals) | |
{ | |
int redVal = colorVals->redVal; | |
int greenVal = colorVals->greenVal; | |
int blueVal = colorVals->blueVal; | |
for(int a =0;a<LEDC_TEST_CH_NUM;a++) | |
{ | |
if(a==0) | |
{ | |
ledc_set_duty(ledc_channel[a].speed_mode, ledc_channel[a].channel,redVal); | |
ledc_update_duty(ledc_channel[a].speed_mode, ledc_channel[a].channel); | |
printf("Updated channel %d with %d\n", a, redVal); | |
} | |
else if(a == 1) | |
{ | |
ledc_set_duty(ledc_channel[a].speed_mode, ledc_channel[a].channel,greenVal); | |
ledc_update_duty(ledc_channel[a].speed_mode, ledc_channel[a].channel); | |
printf("Updated channel %d with %d\n", a, greenVal); | |
} | |
else | |
{ | |
ledc_set_duty(ledc_channel[a].speed_mode, ledc_channel[a].channel,blueVal); | |
ledc_update_duty(ledc_channel[a].speed_mode, ledc_channel[a].channel); | |
printf("Updated channel %d with %d\n", a, blueVal); | |
} | |
} | |
printf("Changed Color \n"); | |
} | |
esp_mqtt_init(message_callback,status_callback,buffer_size, command_timeout); | |
xTaskCreate(process,"process",1024,NULL,2,&task); | |
//configASSERT(task); | |
if(task != NULL){ | |
vTaskDelete(task); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment