Skip to content

Instantly share code, notes, and snippets.

@jpadie
Last active April 23, 2018 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpadie/3e54dd148d1f07de8aad9466144e1fef to your computer and use it in GitHub Desktop.
Save jpadie/3e54dd148d1f07de8aad9466144e1fef to your computer and use it in GitHub Desktop.
IR Blaster code that's not getting any logging output
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "esp_wifi.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "esp_event_loop.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "freertos/event_groups.h"
#include "lwip/sockets.h"
#include "lwip/dns.h"
#include "lwip/netdb.h"
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
#include "esp_log.h"
#include "mqtt_client.h"
#include "driver/rmt.h"
#include "driver/periph_ctrl.h"
#include "soc/rmt_reg.h"
#include <rom/rtc.h>
#include "codes.h"
static const char *TAG = "IRBLASTER";
static EventGroupHandle_t wifi_event_group;
const static int CONNECTED_BIT = BIT0;
#define RMT_TX_CHANNEL RMT_CHANNEL_0
#define RMT_TX_GPIO GPIO_NUM_22
#define VERSION "0.5.0"
unsigned long n;
unsigned long lastTry;
int timeZone = 1;
static void irSend(uint32_t code, byte type, int nbits) {
int i = 0;
int t = 0;
rmt_item32_t items[nbits + 3];
switch (type) {
//format of rmt_item32_t = duration, level, duration, level
case 1:
t = 444; //usecs
items[0].duration0 = t * 6;
items[0].level0 = 1;
items[0].duration1 = t * 2;
items[0].level1 = 0;
items[1].duration0 = t;
items[1].level0 = 1;
items[1].duration1 = t;
items[1].level1 = 0;
for (unsigned long mask = 1UL << (nbits - 1); mask; mask >>= 1) {
if (code & mask) {
if (code & (1 << i)) {
items[2 + i].duration0 = t;
items[2 + i].level0 = 1;
items[2 + i].duration1 = t;
items[2 + i].level1 = 0;
} else {
items[2 + i].duration0 = t;
items[2 + i].level0 = 0;
items[2 + i].duration1 = t;
items[2 + i].level1 = 1;
}
} else {
if (code & (1 << i)) {
items[2 + i].duration0 = t * 2;
items[2 + i].level0 = 1;
items[2 + i].duration1 = t * 2;
items[2 + i].level1 = 0;
} else {
items[2 + i].duration0 = t * 2;
items[2 + i].level0 = 0;
items[2 + i].duration1 = t * 2;
items[2 + i].level1 = 1;
}
}
}
items[2 + i].duration0 = 0;
items[2 + i].level0 = 0;
items[2 + i].duration1 = 0;
items[2 + i].level1 = 0;
break;
case 2:
t = 889;
items[0].duration0 = t;
items[0].level0 = 1;
items[0].duration1 = t;
items[0].level1 = 0;
items[1].duration0 = t;
items[1].level0 = 1;
items[1].duration1 = 0;
items[1].level1 = 0;
for (unsigned long mask = 1UL << (nbits - 1); mask; mask >>= 1) {
if (code & mask) {
items[2 + i].duration0 = t;
items[2 + i].level0 = 0;
items[2 + i].duration1 = t;
items[2 + i].level1 = 1;
} else {
items[2 + i].duration0 = t;
items[2 + i].level0 = 1;
items[2 + i].duration1 = t;
items[2 + i].level1 = 0;
}
}
items[2 + i].duration0 = 0;
items[2 + i].level0 = 0;
items[2 + i].duration1 = 0;
items[2 + i].level1 = 0;
break;
}
ESP_LOGI(TAG, "Size of items: %d ; size of an item: %d", sizeof(items), sizeof(items[0]));
ESP_LOGI(TAG, "about to send IR command: [%02X]", code);
// Serial.println(items);
rmt_write_items(RMT_TX_CHANNEL, items, nbits + 3, true);
rmt_wait_tx_done(RMT_TX_CHANNEL, portMAX_DELAY);
ESP_LOGI(TAG, "finished sending IR command");
}
static void txInit( uint32_t frequency ) {
rmt_config_t config;
rmt_tx_config_t txConfig;
txConfig.loop_en = false;
txConfig.carrier_freq_hz = frequency;
txConfig.carrier_duty_percent = 50;
txConfig.carrier_level = RMT_CARRIER_LEVEL_HIGH;
txConfig.carrier_en = true;
txConfig.idle_level = RMT_IDLE_LEVEL_LOW;
txConfig.idle_output_en = true;
config.rmt_mode = RMT_MODE_TX;
config.channel = RMT_TX_CHANNEL;
config.gpio_num = RMT_TX_GPIO;
config.mem_block_num = 1;
config.tx_config = txConfig;
//clock division
//each tick is (80/80000000) secs = 1uSec.
config.clk_div = 80;
rmt_config(&config);
rmt_driver_install(config.channel, 0, 0);
}
static void sendCode(char *payload) {
ESP_LOGI(TAG, q.c_str());
int s = sizeof(code) / sizeof(_code);
ESP_LOGI(TAG, "number of items:[%d]", s);
for (int i = 0; i < s; i++) {
ESP_LOGI(TAG, "Comparing %s to %s", payload, code[i].description) ;
if (strcmp(payload, code[i].description) == 0) {
ESP_LOGI(TAG, "Found code for payload: %s", payload);
irSend(code[i].code, code[i].type, code[i].bitLength);
return;
}
}
}
static void sendMacro(char *receivedMacro) {
char sky[] = "sky";
for (int i = 0; i < sizeof(macro) / sizeof(_macro); i++) {
if (strcmp(receivedMacro, macro[i].description) == 0) {
char *code;
int j = 0;
code = macro[i].codes[j];
sendCode(sky);
while (code) {
sendCode(code);
j++;
code = macro[i].codes[j];
}
return;
}
}
}
static void callback(esp_mqtt_event_handle_t event){
char *payload = event->data;
char *topic = event->topic;
esp_mqtt_client_handle_t client = event->client;
if (strcmp("rab/irb/get/version", topic) == 0) {
esp_mqtt_client_publish(client, "rab/irb/info/version", VERSION , 0, 0, 0);
}
else if (strcmp("rab/irb/send", topic) == 0) {
sendCode(payload);
}
else if (strcmp("rab/irb/send/macro", topic) == 0) {
ESP_LOGI(TAG, "macro request received");
sendMacro(payload);
}
}
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event){
esp_mqtt_client_handle_t client = event->client;
ESP_LOGI(TAG, "In the event handler");
int msg_id;
// your_context_t *context = event->context;
switch (event->event_id) {
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
msg_id = esp_mqtt_client_subscribe(client, "rab/irb/send/#", 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
msg_id = esp_mqtt_client_subscribe(client, "rab/irb/get/version", 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
msg_id = esp_mqtt_client_subscribe(client, "rab/irb/set/#", 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
esp_mqtt_client_publish(client, "rab/irb/info", "irBlaster connected" , 0, 0, 0);
break;
case MQTT_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
break;
case MQTT_EVENT_SUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_UNSUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_PUBLISHED:
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_DATA:
ESP_LOGI(TAG, "MQTT_EVENT_DATA: %s", event->data);
callback(event);
break;
case MQTT_EVENT_ERROR:
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
break;
}
return ESP_OK;
}
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event){
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
ESP_LOGI(TAG, "Wifi got IP");
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
ESP_LOGI(TAG, "Wifi disconnected");
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}
static void wifi_init(void){
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_LOGI(TAG, "About to start wifi");
ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
wifi_config_t wifi_config = {
.sta = {
.ssid = "*****",
.password = "*****" ,
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_LOGI(TAG, "start the WIFI SSID:[%s] password:[%s]", "*****", "******");
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "Waiting for wifi");
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);
}
static void mqtt_app_start(void){
ESP_LOGI(TAG, "[APP] Connecting to MQTT");
const esp_mqtt_client_config_t mqtt_cfg = {
.uri = "******",
.event_handle = mqtt_event_handler,
.username = "******",
.password = "******",
.port = 8883
// .user_context = (void *)your_context
};
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_start(client);
}
void app_main(){
ESP_LOGI(TAG, "[APP] Startup..");
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
esp_log_level_set("*", ESP_LOG_VERBOSE);
esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE);
esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE);
esp_log_level_set(TAG, ESP_LOG_VERBOSE);
nvs_flash_init();
wifi_init();
txInit(36000);
mqtt_app_start();
}
///codes.h
#include <Arduino.h>
typedef struct {
char description[7];
char codes[5][2];
} _macro;
_macro macro[] = {
{"BBC1", {"1", "0", "1"}},
{"BBC2", {"1", "0", "2"}},
{"ITV", {"1", "0", "3"}},
{"C4", {"1", "0", "4"}},
{"C5", {"1", "0", "5"}},
{"BBC4", {"1", "0", "6"}},
{"ITV2", {"1", "1", "8"}},
{"ITV3", {"1", "1", "9"}},
{"ITV4", {"1", "2", "0"}},
{"ITV+1", {"1", "3", "1"}},
{"ITV2+1", {"1", "7", "9"}},
{"ITV3+1", {"1", "8", "0"}},
{"ITV4+1", {"1", "9", "3"}}
};
typedef struct {
char description[5];
uint32_t code;
byte type; //1 = rc6; 2 = rc5
byte bitLength;
} _code;
_code code[] = {
{"sky", 0xC00C80, 1, 24},
{"tvg", 0xC00CCC, 1, 24},
{"sel", 0xC00C5C, 1, 24},
{"pow", 0xC00C0C, 1, 24},
{"box", 0xC00C7D, 1, 24},
{"srv", 0xC00C7E, 1, 24},
{"int", 0xC00CF5, 1, 24 },
{"mute", 0x080D, 2, 12},
{"info", 0xC00CCB, 1, 24},
{"vol+", 0x0810, 2, 12},
{"vol-", 0x0811, 2, 12},
{"CH+", 0xC00C20, 1, 24},
{"CH-", 0xC00C21, 1, 24},
{"UP", 0xC00C58, 1, 24},
{"rt", 0xC00C5B, 1, 24},
{"left", 0xC00C5A, 1, 24},
{"text", 0xC00C3C, 1, 24},
{"back", 0xC00C83, 1, 24},
{"help", 0xC00C81, 1, 24},
{"<<", 0xC00C30, 1, 24},
{">>", 0xC00C28, 1, 24},
{"R", 0xC00C40, 1, 24},
{"stop", 0xC00C3F, 1, 24},
{"red", 0xC00C60, 1, 24},
{"grn", 0xC00C6E, 1, 24},
{"yell", 0xC00C6F, 1, 24},
{"blue", 0xC00C70, 1, 24},
{"1", 0xC00C01, 1, 24},
{"2", 0xC00C02, 1, 24},
{"3", 0xC00C03, 1, 24},
{"4", 0xC00C04, 1, 24},
{"5", 0xC00C05, 1, 24},
{"6", 0xC00C06, 1, 24},
{"7", 0xC00C07, 1, 24},
{"8", 0xC00C08, 1, 24},
{"9", 0xC00C09, 1, 24},
{"0", 0xC00C00, 1, 24}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment