Created
August 9, 2018 02:45
-
-
Save reefwing/f87764523ace1c383ab002759f6369d4 to your computer and use it in GitHub Desktop.
An IR remote using the ESP32 and controlled via the IoT hub in Azure.
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
/* | |
* IRbeacon: Continually sends IR remote control information every 5 seconds | |
* An IR LED must be connected to the ESP32 pin IR_LED via 100 ohm resistor. | |
* Uses the ESP32-IRremote library and modified functions from the | |
* Microsoft GetStarted.ino sketch in the Examples folder. | |
* | |
* 0xa90 is the Sony power on/off code. | |
* | |
* Version 0.1 August, 2018 | |
* Copyright 2018 David Such | |
* | |
* https://reefwingrobotics.blogspot.com/ | |
* | |
*/ | |
#include <IRremote.h> | |
#include <WiFi.h> | |
#include "AzureIotHub.h" | |
#include "Esp32MQTTClient.h" | |
#define INTERVAL 10000 | |
#define DEVICE_ID "ESP32_IRBeacon_1" | |
#define MESSAGE_MAX_LEN 256 | |
// SSID and password for WiFi | |
// Insert your details here and the primary connecting string below. | |
const char* ssid = ""; | |
const char* password = ""; | |
/*String containing Hostname, Device Id & Device Key in the format: */ | |
/* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */ | |
/* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>" */ | |
static const char* connectionString = "HostName="; | |
const char *messageData = "{\"deviceId\":\"%s\", \"messageId\":%d}"; | |
int messageCount = 1; | |
static bool WiFiConnected = false; | |
static bool messageSending = true; | |
static bool debugOn = true; | |
static uint64_t send_interval_ms; | |
// PIN Definitions | |
const byte IR_LED = 15; | |
const byte Tx_LED = 2; | |
const byte PWR_LED = 0; | |
const byte ARM_SWITCH = 13; | |
IRsend irsend(IR_LED); | |
// Azure IoT Hub & WiFi | |
////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// Functions | |
static void InitWiFi() | |
{ | |
Serial.println("Connecting..."); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
WiFiConnected = true; | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result) | |
{ | |
if (result == IOTHUB_CLIENT_CONFIRMATION_OK) | |
{ | |
Serial.println("Send Confirmation Callback finished."); | |
} | |
} | |
static void MessageCallback(const char* payLoad, int size) | |
{ | |
// C2D Message Handler | |
Serial.println("Message callback:"); | |
Serial.println(payLoad); | |
} | |
static void DeviceTwinCallback(DEVICE_TWIN_UPDATE_STATE updateState, const unsigned char *payLoad, int size) | |
{ | |
char *temp = (char *)malloc(size + 1); | |
if (temp == NULL) | |
{ | |
return; | |
} | |
memcpy(temp, payLoad, size); | |
temp[size] = '\0'; | |
// Display Twin message. | |
Serial.println(temp); | |
free(temp); | |
} | |
static int DeviceMethodCallback(const char *methodName, const unsigned char *payload, int size, unsigned char **response, int *response_size) | |
{ | |
LogInfo("Try to invoke method %s", methodName); | |
const char *responseMessage = "\"Successfully invoke device method\""; | |
int result = 200; | |
if (strcmp(methodName, "start") == 0) | |
{ | |
LogInfo("Start sending IR burst and heart beat"); | |
messageSending = true; | |
} | |
else if (strcmp(methodName, "stop") == 0) | |
{ | |
LogInfo("Stop sending IR burst and heart beat"); | |
messageSending = false; | |
} | |
else | |
{ | |
LogInfo("No method %s found", methodName); | |
responseMessage = "\"No method found\""; | |
result = 404; | |
} | |
*response_size = strlen(responseMessage) + 1; | |
*response = (unsigned char *)strdup(responseMessage); | |
return result; | |
} | |
////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// IR Beacon | |
////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// Functions | |
static void SendIRBurst() | |
{ | |
for (int i = 0; i < 3; i++) { | |
digitalWrite(Tx_LED, debugOn && HIGH); | |
irsend.sendSony(0xa90, 12); | |
digitalWrite(Tx_LED, LOW); | |
delay(40); | |
} | |
} | |
////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
void setup() | |
{ | |
// Debug output | |
Serial.begin(115200); | |
Serial.println("ESP32 Device"); | |
Serial.println("Initializing..."); | |
// ESP32 Init | |
pinMode(IR_LED, OUTPUT); | |
pinMode(Tx_LED, OUTPUT); | |
pinMode(PWR_LED, OUTPUT); | |
pinMode(ARM_SWITCH, INPUT_PULLUP); | |
// Initialize the WiFi module | |
Serial.println(" > WiFi"); | |
WiFiConnected = false; | |
InitWiFi(); | |
if (!WiFiConnected) | |
{ | |
return; | |
} | |
Serial.println(" > IoT Hub"); | |
Esp32MQTTClient_SetOption(OPTION_MINI_SOLUTION_NAME, "IR_BEACON"); | |
Esp32MQTTClient_Init((const uint8_t*)connectionString, true); | |
Esp32MQTTClient_SetSendConfirmationCallback(SendConfirmationCallback); | |
Esp32MQTTClient_SetMessageCallback(MessageCallback); | |
Esp32MQTTClient_SetDeviceTwinCallback(DeviceTwinCallback); | |
Esp32MQTTClient_SetDeviceMethodCallback(DeviceMethodCallback); | |
send_interval_ms = millis(); | |
} | |
void loop() | |
{ | |
if (WiFiConnected) | |
{ | |
// Check module state - Armed or Debug (LED's will operate) | |
debugOn = digitalRead(ARM_SWITCH); | |
digitalWrite(PWR_LED, debugOn); | |
if (messageSending && (int)(millis() - send_interval_ms) >= INTERVAL) { | |
SendIRBurst(); | |
// Send Heart Beat | |
char messagePayload[MESSAGE_MAX_LEN]; | |
snprintf(messagePayload, MESSAGE_MAX_LEN, messageData, DEVICE_ID, messageCount++); | |
Serial.println(messagePayload); | |
EVENT_INSTANCE* message = Esp32MQTTClient_Event_Generate(messagePayload, MESSAGE); | |
Esp32MQTTClient_Event_AddProp(message, "IRBeaconHeartbeat", "true"); | |
Esp32MQTTClient_SendEventInstance(message); | |
send_interval_ms = millis(); | |
} | |
else | |
{ | |
Esp32MQTTClient_Check(); | |
} | |
} | |
delay(10); | |
} |
Hi atlask1 - I'm not sure what your problem is. The standard IRRemote.h Arduino library only supports receiving IR signals on the ESP32 not transmitting them. Andreas Spiess has forked the standard library and added ESP32 transmission capability. You will need to download this library and add it to your IDE: https://github.com/SensorsIot/Definitive-Guide-to-IR/tree/master/ESP32-IRremote
IRsend is definitely there!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, where I can download the correct IRremote library? this 'IRsend irsend(IR_LED);' is not implemented in the original library and this one https://github.com/SensorsIot/Definitive-Guide-to-IR is the same like the original.