Guest User

Untitled

a guest
Mar 30th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "driver/ledc.h"
  5. #include "esp_err.h"
  6. #include "string.h"
  7. #include "driver/gpio.h"
  8.  
  9. //WiFi headers
  10. #include "esp_wifi.h"
  11. #include "esp_log.h"
  12. #include "esp_event_loop.h"
  13. #include "nvs_flash.h"
  14.  
  15. //MQTT headers
  16. #include "esp_mqtt.h"
  17.  
  18. //LEDC constants
  19. #define LEDC_HS_TIMER LEDC_TIMER_0
  20. #define LEDC_HS_MODE LEDC_HIGH_SPEED_MODE
  21. #define LEDC_HS_CH0_GPIO (23) //BLUE
  22. #define LEDC_HS_CH0_CHANNEL LEDC_CHANNEL_0
  23. #define LEDC_HS_CH1_GPIO (22) //GREEN
  24. #define LEDC_HS_CH1_CHANNEL LEDC_CHANNEL_1
  25. #define LEDC_HS_CH2_GPIO (21) //RED
  26. #define LEDC_HS_CH2_CHANNEL LEDC_CHANNEL_2
  27. #define LEDC_TEST_CH_NUM (3)
  28. #define LEDC_TEST_DUTY (1023)
  29. #define WIFI_INDICATOR_LED (2)
  30.  
  31. //WiFi constants
  32. #define DEFAULT_SSID "Connection Ting"
  33. #define DEFAULT_PWD "0717817569"
  34. #define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
  35.  
  36. //Config SSID SORT METHOD
  37. #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
  38.  
  39. //Config FAST SCAN Threshold
  40. //I suspect I can cut all this code to default rssi of -127 and auth mode wpa2-psk
  41. #if CONFIG_FAST_SCAN_THRESHOLD
  42. #define DEFAULT_RSSI CONFIG_FAST_SCAN_MINIMUM_SIGNAL
  43. #if CONFIG_EXAMPLE_OPEN
  44. #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
  45. #elif CONFIG_EXAMPLE_WEP
  46. #define DEFAULT_AUTHMODE WIFI_AUTH_WEP
  47. #elif CONFIG_EXAMPLE_WPA
  48. #define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK
  49. #elif CONFIG_EXAMPLE_WPA2
  50. #define DEFAULT_AUTHMODE WIFI_AUTH_WPA2_PSK
  51. #else
  52. #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
  53. #endif
  54. #else
  55. #define DEFAULT_RSSI -127
  56. #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
  57. #endif
  58.  
  59. //MQTT Global Vars
  60. const char *host = "Server Address"; //I replaced this with my server's IP Address
  61. int port = 1883;
  62. const char *client_id = "ESP32_MQLED";
  63. const char *username = NULL;
  64. const char *password = NULL;
  65. const char *Mytopic = "EspLyts";
  66. int qos = 1;
  67. size_t buffer_size = 256;
  68. int command_timeout = 3000;
  69. static const char *Mypayload = "Karanja Baby";
  70. static const char *TAG = "scan";
  71.  
  72. static TaskHandle_t task = NULL;
  73.  
  74. //Function that I use to create an RTOS Task
  75. //This function doesn't seem to work at all. No SUB or PUB
  76. //Its attached to the RTOS at the very end of the code
  77. static void process()
  78. {
  79. esp_mqtt_subscribe(Mytopic, 0);
  80. while(1)
  81. {
  82. esp_mqtt_publish(Mytopic, (uint8_t *)"world", 5, 2, false);
  83. vTaskDelay(1000/portTICK_PERIOD_MS);
  84. }
  85.  
  86. }
  87.  
  88. //WIFI CONFIGS -- Ignore
  89. //Declare a |bool fucntion| to handle different events/scenarios
  90. static esp_err_t event_handler(void *ctx, system_event_t *event)
  91. {
  92. gpio_pad_select_gpio(WIFI_INDICATOR_LED);
  93. gpio_set_direction(WIFI_INDICATOR_LED, GPIO_MODE_OUTPUT);
  94. gpio_set_level(WIFI_INDICATOR_LED,0);
  95.  
  96. switch(event->event_id)
  97. {
  98. case SYSTEM_EVENT_STA_START :
  99. ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
  100. ESP_ERROR_CHECK(esp_wifi_connect());
  101. break;
  102.  
  103. case SYSTEM_EVENT_STA_GOT_IP :
  104. ESP_LOGI(TAG, "SYSTEM_EVENT_GOT_IP");
  105. ESP_LOGI(TAG, "Got IP: %s \n", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
  106. //Light the LED
  107.  
  108. gpio_set_level(WIFI_INDICATOR_LED,1);
  109. //Connect MQTT
  110. esp_mqtt_start(host, port, client_id, NULL, NULL);
  111. break;
  112.  
  113. case SYSTEM_EVENT_STA_DISCONNECTED :
  114. ESP_LOGI(TAG,"SYSTEM_EVENT_STA_DISCONNECTED");
  115. ESP_ERROR_CHECK(esp_wifi_connect());
  116. gpio_set_level(WIFI_INDICATOR_LED,0);
  117. esp_mqtt_stop();
  118. break;
  119.  
  120. default:
  121. break;
  122.  
  123. }
  124. return ESP_OK; //The bool that gets returned;
  125. };
  126.  
  127. //MQTT MESSAGE CALLBACK
  128. static void message_callback(const char *topic, uint8_t *payload, size_t len)
  129. {
  130. // printf("incoming: %s => %s (%d) \n", topic, payload, (int)len);
  131. printf("Message Received \n");
  132. }
  133.  
  134. static void status_callback(esp_mqtt_status_t status)
  135. {
  136. switch(status)
  137. {
  138. case ESP_MQTT_STATUS_CONNECTED: //I commented the code in here because I thought the callback wasnt working. The printf doesn't fire
  139. printf("Attempting to subscribe and run the process \n");
  140. //esp_mqtt_subscribe(Mytopic, 0);
  141. //xTaskCreatePinnedToCore(process, "process", 1024, NULL, 10, &task,1);
  142. break;
  143. case ESP_MQTT_STATUS_DISCONNECTED:
  144. // vTaskDelete(task);
  145. printf("MQTT Disconnected");
  146. }
  147. }
  148.  
  149. //define worker function to do establish the actual wifi connection
  150. static void wifi_scan(void)
  151. {
  152. tcpip_adapter_init();
  153. ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
  154.  
  155. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  156. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  157.  
  158. wifi_config_t wifi_config = {
  159. .sta = {
  160. .ssid = DEFAULT_SSID,
  161. .password = DEFAULT_PWD,
  162. .scan_method = DEFAULT_SCAN_METHOD,
  163. .sort_method = DEFAULT_SORT_METHOD,
  164. .threshold.rssi = DEFAULT_RSSI,
  165. .threshold.authmode = DEFAULT_AUTHMODE
  166. },
  167. };
  168. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  169. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  170. ESP_ERROR_CHECK(esp_wifi_start());
  171. }
  172.  
  173. void app_main()
  174. {
  175. // gpio_set_level(WIFI_INDICATOR_LED,0);
  176. //Initialize Non-Volatile Storage
  177. esp_err_t ret = nvs_flash_init();
  178.  
  179. if(ret == ESP_ERR_NVS_NO_FREE_PAGES)
  180. {
  181. ESP_ERROR_CHECK(nvs_flash_erase());
  182. ret = nvs_flash_init();
  183. }
  184.  
  185. ESP_ERROR_CHECK(ret);
  186.  
  187. wifi_scan();
  188. //--------------------------------------------------------------------------------------------------------------------
  189.  
  190. //Set up LEDC
  191. int ch;
  192. /*
  193. * Prepare and set configuration of timers
  194. * that will be used by LED Controller
  195. */
  196.  
  197. ledc_timer_config_t ledc_timer = {
  198. .duty_resolution = LEDC_TIMER_10_BIT, // resolution of PWM duty
  199. .freq_hz = 50, // frequency of PWM signal
  200. .speed_mode = LEDC_HS_MODE, // timer mode
  201. .timer_num = LEDC_HS_TIMER // timer index
  202. };
  203.  
  204. ledc_timer_config(&ledc_timer);
  205. /*
  206. * Prepare individual configuration
  207. * for each channel of LED Controller
  208. * by selecting:
  209. * - controller's channel number
  210. * - output duty cycle, set initially to 0 [REMEMBER THIS!!!!]
  211. * - GPIO number where LED is connected to
  212. * - speed mode, either high or low
  213. * - timer servicing selected channel
  214. * Note: if different channels use one timer,
  215. * then frequency and bit_num of these channels
  216. * will be the same
  217. */
  218.  
  219. ledc_channel_config_t ledc_channel[LEDC_TEST_CH_NUM] = {
  220. {
  221. .channel = LEDC_HS_CH0_CHANNEL,
  222. .duty = 0,
  223. .gpio_num = LEDC_HS_CH0_GPIO,
  224. .speed_mode = LEDC_HS_MODE,
  225. .timer_sel = LEDC_HS_TIMER
  226. },
  227.  
  228. {
  229. .channel = LEDC_HS_CH1_CHANNEL,
  230. .duty = 0,
  231. .gpio_num = LEDC_HS_CH1_GPIO,
  232. .speed_mode = LEDC_HS_MODE,
  233. .timer_sel = LEDC_HS_TIMER
  234. },
  235.  
  236. {
  237. .channel = LEDC_HS_CH2_CHANNEL,
  238. .duty = 0,
  239. .gpio_num = LEDC_HS_CH2_GPIO,
  240. .speed_mode = LEDC_HS_MODE,
  241. .timer_sel = LEDC_HS_TIMER
  242. }
  243. };
  244.  
  245. // Set LED Controller with previously prepared configuration. Config all PWM Channels
  246. for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) {
  247. ledc_channel_config(&ledc_channel[ch]);
  248. }
  249.  
  250. //Define struct for storing the color PWM values
  251. typedef struct
  252. {
  253. int redVal;
  254. int greenVal;
  255. int blueVal;
  256. } colors_t;
  257.  
  258. colors_t blue = {0,0,1023}; //0,0,100
  259. colors_t red = {1023,0,0}; //100,0,0
  260. colors_t green = {0, 1023, 0}; //0,100,0
  261. colors_t magenta = {700,300,1000}; //100,0,100
  262. colors_t purple = {320,0,320}; //33,0,33
  263. colors_t white = {1023,1023,1023}; //100,100,100
  264. colors_t aqua = {0, 1023,1023}; //0,100,100
  265. colors_t yellow = {1023,1023,0}; //100,100,0
  266.  
  267. //Define func for changing color. Takes struct pointer as a param.
  268. void changeColor(colors_t *colorVals)
  269. {
  270. int redVal = colorVals->redVal;
  271. int greenVal = colorVals->greenVal;
  272. int blueVal = colorVals->blueVal;
  273.  
  274. for(int a =0;a<LEDC_TEST_CH_NUM;a++)
  275. {
  276. if(a==0)
  277. {
  278. ledc_set_duty(ledc_channel[a].speed_mode, ledc_channel[a].channel,redVal);
  279. ledc_update_duty(ledc_channel[a].speed_mode, ledc_channel[a].channel);
  280. printf("Updated channel %d with %d\n", a, redVal);
  281. }
  282.  
  283. else if(a == 1)
  284. {
  285. ledc_set_duty(ledc_channel[a].speed_mode, ledc_channel[a].channel,greenVal);
  286. ledc_update_duty(ledc_channel[a].speed_mode, ledc_channel[a].channel);
  287. printf("Updated channel %d with %d\n", a, greenVal);
  288. }
  289.  
  290. else
  291. {
  292. ledc_set_duty(ledc_channel[a].speed_mode, ledc_channel[a].channel,blueVal);
  293. ledc_update_duty(ledc_channel[a].speed_mode, ledc_channel[a].channel);
  294. printf("Updated channel %d with %d\n", a, blueVal);
  295. }
  296. }
  297. printf("Changed Color \n");
  298.  
  299. }
  300.  
  301. esp_mqtt_init(message_callback,status_callback,buffer_size, command_timeout);
  302. xTaskCreate(process,"process",1024,NULL,2,&task);
  303. //configASSERT(task);
  304.  
  305. if(task != NULL){
  306. vTaskDelete(task);
  307. }
  308. }
Add Comment
Please, Sign In to add comment