just some reformatting - no code changes

This commit is contained in:
Joe Tretter
2022-07-05 14:18:19 -05:00
parent 58ab37520a
commit 4e2b454ac5

View File

@@ -1,17 +1,15 @@
#include <Arduino.h> #include <Arduino.h>
#include <IRrecv.h> #include <ESP8266WiFi.h>
#include <IRsend.h>
#include <IRremoteESP8266.h>
#include <IRac.h> #include <IRac.h>
#include <IRrecv.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <IRtext.h> #include <IRtext.h>
#include <IRutils.h> #include <IRutils.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h> #include <PubSubClient.h>
const uint16_t kIrLed = 4; // ESP8266 GPIO pin to use. Recommended: 4 (D2). const uint16_t kIrLed = 4; // ESP8266 GPIO pin to use. Recommended: 4 (D2).
IRsend irsend(kIrLed); // Set the GPIO to be used to sending the message. IRsend irsend(kIrLed); // Set the GPIO to be used to sending the message.
// IR Receiver connected to PIN D5 (14) // IR Receiver connected to PIN D5 (14)
const uint16_t kRecvPin = 14; const uint16_t kRecvPin = 14;
@@ -30,23 +28,21 @@ const uint16_t kMinUnknownSize = 12;
IRrecv irrecv(kRecvPin, kCaptureBufferSize, kTimeout, true); IRrecv irrecv(kRecvPin, kCaptureBufferSize, kTimeout, true);
decode_results results; // Somewhere to store the results decode_results results; // Somewhere to store the results
// Update these with values suitable for your network. // Update these with values suitable for your network.
const char* ssid = "JoNelsDarlings"; const char *ssid = "JoNelsDarlings";
const char* password = "Paris2016"; const char *password = "Paris2016";
const char* mqtt_server = "mqtt.ddns.kawomi.com"; const char *mqtt_server = "mqtt.ddns.kawomi.com";
//const String room_name = "IRLivingroom"; // const String room_name = "IRLivingroom";
const String room_name = "IRBedroom"; const String room_name = "IRBedroom";
WiFiClient espClient; WiFiClient espClient;
PubSubClient client(espClient); PubSubClient client(espClient);
unsigned long lastMsg = 0; unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50) #define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE]; char msg[MSG_BUFFER_SIZE];
int value = 0; int value = 0;
void setup_wifi() { void setup_wifi() {
delay(10); delay(10);
// We start by connecting to a WiFi network // We start by connecting to a WiFi network
Serial.println(); Serial.println();
@@ -69,43 +65,43 @@ void setup_wifi() {
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
} }
void callback(char* topic, byte* payload, unsigned int length) { void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived ["); Serial.print("Message arrived [");
Serial.print(topic); Serial.print(topic);
String recBuf=""; String recBuf = "";
Serial.print("] "); Serial.print("] ");
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
recBuf += (char)payload[i]; recBuf += (char)payload[i];
} }
Serial.println(recBuf); Serial.println(recBuf);
int numEle=1; int numEle = 1;
for (int i=0; i<recBuf.length() ; i++){ for (int i = 0; i < recBuf.length(); i++) {
if (recBuf.charAt(i) == ',') { if (recBuf.charAt(i) == ',') {
numEle++; numEle++;
} }
} }
recBuf+=","; recBuf += ",";
Serial.print("NumEle "); Serial.print("NumEle ");
Serial.println(numEle); Serial.println(numEle);
int rawPos=0; int rawPos = 0;
uint16_t rawData[numEle]; uint16_t rawData[numEle];
String bufSingleNum=""; String bufSingleNum = "";
for (int i=0;i<recBuf.length();i++){ for (int i = 0; i < recBuf.length(); i++) {
if (recBuf.charAt(i) != ',') { if (recBuf.charAt(i) != ',') {
bufSingleNum+=recBuf.charAt(i); bufSingleNum += recBuf.charAt(i);
} else { } else {
bufSingleNum.trim(); bufSingleNum.trim();
rawData[rawPos++]=bufSingleNum.toInt(); rawData[rawPos++] = bufSingleNum.toInt();
bufSingleNum=""; bufSingleNum = "";
} }
} }
irrecv.disableIRIn(); // Stop the receiver irrecv.disableIRIn(); // Stop the receiver
irsend.sendRaw(rawData, numEle, 38); irsend.sendRaw(rawData, numEle, 38);
irrecv.enableIRIn(); // Start the receiver irrecv.enableIRIn(); // Start the receiver
} }
@@ -115,8 +111,8 @@ void reconnect() {
Serial.print("Attempting MQTT connection..."); Serial.print("Attempting MQTT connection...");
// Create a random client ID // Create a random client ID
String clientId = String("ESP8266" + room_name); String clientId = String("ESP8266" + room_name);
//clientId += String(random(0xffff), HEX); // clientId += String(random(0xffff), HEX);
// Attempt to connect // Attempt to connect
if (client.connect(clientId.c_str())) { if (client.connect(clientId.c_str())) {
Serial.println("connected"); Serial.println("connected");
// Once connected, publish an announcement... // Once connected, publish an announcement...
@@ -134,7 +130,7 @@ void reconnect() {
} }
void setup() { void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(kBaudRate); Serial.begin(kBaudRate);
setup_wifi(); setup_wifi();
client.setBufferSize(4096); client.setBufferSize(4096);
@@ -144,9 +140,9 @@ void setup() {
// OTAwifi(); // start default wifi (previously saved on the ESP) for OTA // OTAwifi(); // start default wifi (previously saved on the ESP) for OTA
#if defined(ESP8266) #if defined(ESP8266)
Serial.begin(kBaudRate, SERIAL_8N1, SERIAL_TX_ONLY); Serial.begin(kBaudRate, SERIAL_8N1, SERIAL_TX_ONLY);
#else // ESP8266 #else // ESP8266
Serial.begin(kBaudRate, SERIAL_8N1); Serial.begin(kBaudRate, SERIAL_8N1);
#endif // ESP8266 #endif // ESP8266
while (!Serial) // Wait for the serial connection to be establised. while (!Serial) // Wait for the serial connection to be establised.
delay(50); delay(50);
Serial.printf("\n" D_STR_IRRECVDUMP_STARTUP "\n", kRecvPin); Serial.printf("\n" D_STR_IRRECVDUMP_STARTUP "\n", kRecvPin);
@@ -154,32 +150,29 @@ void setup() {
#if DECODE_HASH #if DECODE_HASH
// Ignore messages with less than minimum on or off pulses. // Ignore messages with less than minimum on or off pulses.
irrecv.setUnknownThreshold(kMinUnknownSize); irrecv.setUnknownThreshold(kMinUnknownSize);
#endif // DECODE_HASH #endif // DECODE_HASH
irrecv.enableIRIn(); // Start the receiver irrecv.enableIRIn(); // Start the receiver
irsend.begin(); irsend.begin();
} }
void loop() { void loop() {
if (!client.connected()) { if (!client.connected()) {
reconnect(); reconnect();
} }
client.loop(); client.loop();
/* unsigned long now = millis(); /* unsigned long now = millis();
if (now - lastMsg > 2000) { if (now - lastMsg > 2000) {
lastMsg = now; lastMsg = now;
++value; ++value;
snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value); snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value);
Serial.print("Publish message: "); Serial.print("Publish message: ");
Serial.println(msg); Serial.println(msg);
client.publish("IRBedroomReceivedIR", msg); client.publish("IRBedroomReceivedIR", msg);
} }
*/ */
// Check if the IR code has been received. // Check if the IR code has been received.
if (irrecv.decode(&results)) { if (irrecv.decode(&results)) {
// Display a crude timestamp. // Display a crude timestamp.
uint32_t now = millis(); uint32_t now = millis();
@@ -190,19 +183,16 @@ void loop() {
// Display the library version the message was captured with. // Display the library version the message was captured with.
Serial.println(D_STR_LIBRARY " : v" _IRREMOTEESP8266_VERSION_ "\n"); Serial.println(D_STR_LIBRARY " : v" _IRREMOTEESP8266_VERSION_ "\n");
// Display the basic output of what we found. // Display the basic output of what we found.
String theResult,theSrc; String theResult, theSrc;
theResult=resultToHumanReadableBasic(&results); theResult = resultToHumanReadableBasic(&results);
theSrc=resultToSourceCode(&results); theSrc = resultToSourceCode(&results);
Serial.println(theSrc.c_str()); Serial.println(theSrc.c_str());
bool isPub = client.publish(String(room_name + "/ReceivedIR").c_str(),
bool isPub = client.publish(String(room_name + "/ReceivedIR").c_str(), theSrc.c_str()); theSrc.c_str());
if (isPub) if (isPub)
Serial.println("PUBLISHED"); Serial.println("PUBLISHED");
else else
Serial.println("PUBLISH FAILED"); Serial.println("PUBLISH FAILED");
} }
} }