1

I'm currently working with an ESP8266 NodeMCU V3 and an IR transmitter breakout board. The module receives NEC IR commands trough a HTTP request and then sends them trough the IR transmitter. Getting the command from the HTTP request and sending static (pre-programmed) IR NEC commands works perfectly. The problem however is that the NEC IR command I receive trough HTTP is in a String variable/data type (after processing). irsend.sendNEC(); however requires uint64_t as the input type, according to the error message I get in the Arduino IDE.

I don't really know my way around C data types to be honest. Searches like "Arduino String to uint64_t" have came up empty or reversed. One post came close to what I'm trying to accomplish, but irsend.sendNEC(strtoul(irrequest)); didn't do the trick either.

Arduino code;

Sketch.ino

// Configuration
const char* CONFIG_SSID      = "mywifi";
const char* CONFIG_PSK       = "wifipassword";
const int   CONFIG_SERIAL    = 115200;
const int   CONFIG_PORT      = 80;
const int   CONFIG_TIMEOUT   = 5000;
const int   CONFIG_IRTPIN    = 2;

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

// Include libraries
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <IRrecv.h>
#include <IRutils.h>

// Create webserver object
WiFiServer server(CONFIG_PORT);

// Create IR Send object
IRsend irsend(CONFIG_IRTPIN);

// Setup instructions
void setup() {

  // Start serial and LED
  Serial.begin(CONFIG_SERIAL);
  Serial.println();
  Serial.println("Starting setup");
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, 1);

  // Start WiFi connection
  Serial.print("Connecting to: ");
  Serial.println(CONFIG_SSID);
  WiFi.mode(WIFI_STA);
  WiFi.begin(CONFIG_SSID, CONFIG_PSK);

  // Wait for WiFi connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected!");

  // Print WiFi connection information
  Serial.print("  SSID: ");
  Serial.println(WiFi.SSID());
  Serial.print("  RSSI: ");
  Serial.print(WiFi.RSSI());
  Serial.println(" dBm");
  Serial.print("  Local IP: ");
  Serial.println(WiFi.localIP());

  // Start webserver
  Serial.println("Starting webserver...");
  server.begin();
  delay(1000);
  Serial.println("Webserver started!");

  // Print webserver information
  Serial.print("  Host: ");
  Serial.println(WiFi.localIP());
  Serial.print("  Port: ");
  Serial.println(CONFIG_PORT);

  // Start IR Send
  Serial.println("Starting IR Send...");
  irsend.begin();
  delay(1000);
  Serial.println("IR Send started!");

  // Setup complete
  Serial.println("Setup completed");
  digitalWrite(LED_BUILTIN, 0);
  Serial.println();

}

// Loop instructions
void loop() {

  // Wait for new client
  WiFiClient client = server.available();
  if (client) {

    // New client
    digitalWrite(LED_BUILTIN, 1);
    Serial.println("New client connected");

    // Wait nicely for the client to complete its full request
    unsigned long timeout = millis() + CONFIG_TIMEOUT;
    Serial.println("Waiting for client request to finish...");
    while (!client.available() && millis() < timeout) {
      delay(1);
    }

    // End client connection when timeout is reached to not hold up availability
    if (millis() < timeout) {
      Serial.println("Client request finished!");
    } else {
      Serial.println("Client request timeout!");
      client.flush();
      client.stop();
      digitalWrite(LED_BUILTIN, 0);
      Serial.println();
      return;
    }

    // Catch client request
    String req = client.readStringUntil('\r');
    Serial.print("Request header: ");
    Serial.println(req);
    if (req.indexOf(F("/rest/ir/nec/")) != -1) {

      // Get IR Send NEC request
      String irrequest = req;
      irrequest.remove(0, ((irrequest.lastIndexOf("/rest/ir/nec/")) + 13));
      irrequest.remove((irrequest.indexOf(" ")));
      Serial.print("IR Send NEC code: ");
      Serial.println(irrequest);

      // Send IR NEC command
      Serial.println("Sending IR NEC command");
      irsend.sendNEC(irrequest);    // <<< here lays the problem
      irsend.sendNEC(0xFB0042BD);   // This works
      irsend.sendNEC("0xFB0042BD"); // This doesn't

      // Send HTTP response
      Serial.println("Sending HTTP/1.1 200 OK");
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/plain");
      client.println("Connection: close");
      client.println();
      client.println("OK");

    } else {

      // Invalid request
      Serial.println("Invalid client request");
      Serial.println("Sending HTTP/1.1 404 response");
      client.println("HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html><body>Not found</body></html>");

    }

    // Flush output buffer
    Serial.println("Flushing output buffer");
    client.flush();
    digitalWrite(LED_BUILTIN, 0);
    Serial.println();
    return;

  }

}
1

1 Answer 1

4

irsend.sendNEC(strtoul(irrequest.c_str(), NULL, 10)); did the trick with the help of Juraj. Much thanks! I only had the set 10 to 16 to get it to work.

Working code:

// Configuration
const char* CONFIG_SSID      = "mywifi";
const char* CONFIG_PSK       = "wifipassword";
const int   CONFIG_SERIAL    = 115200;
const int   CONFIG_PORT      = 80;
const int   CONFIG_TIMEOUT   = 5000;
const int   CONFIG_IRTPIN    = 2;

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

// Include libraries
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <IRrecv.h>
#include <IRutils.h>

// Create webserver object
WiFiServer server(CONFIG_PORT);

// Create IR Send object
IRsend irsend(CONFIG_IRTPIN);

// Setup instructions
void setup() {

  // Start serial and LED
  Serial.begin(CONFIG_SERIAL);
  Serial.println();
  Serial.println("Starting setup");
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, 1);

  // Start WiFi connection
  Serial.print("Connecting to: ");
  Serial.println(CONFIG_SSID);
  WiFi.mode(WIFI_STA);
  WiFi.begin(CONFIG_SSID, CONFIG_PSK);

  // Wait for WiFi connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected!");

  // Print WiFi connection information
  Serial.print("  SSID: ");
  Serial.println(WiFi.SSID());
  Serial.print("  RSSI: ");
  Serial.print(WiFi.RSSI());
  Serial.println(" dBm");
  Serial.print("  Local IP: ");
  Serial.println(WiFi.localIP());

  // Start webserver
  Serial.println("Starting webserver...");
  server.begin();
  delay(1000);
  Serial.println("Webserver started!");

  // Print webserver information
  Serial.print("  Host: ");
  Serial.println(WiFi.localIP());
  Serial.print("  Port: ");
  Serial.println(CONFIG_PORT);

  // Start IR Send
  Serial.println("Starting IR Send...");
  irsend.begin();
  delay(1000);
  Serial.println("IR Send started!");

  // Setup complete
  Serial.println("Setup completed");
  digitalWrite(LED_BUILTIN, 0);
  Serial.println();

}

// Loop instructions
void loop() {

  // Wait for new client
  WiFiClient client = server.available();
  if (client) {

    // New client
    digitalWrite(LED_BUILTIN, 1);
    Serial.println("New client connected");

    // Wait nicely for the client to complete its full request
    unsigned long timeout = millis() + CONFIG_TIMEOUT;
    Serial.println("Waiting for client request to finish...");
    while (!client.available() && millis() < timeout) {
      delay(1);
    }

    // End client connection when timeout is reached to not hold up availability
    if (millis() < timeout) {
      Serial.println("Client request finished!");
    } else {
      Serial.println("Client request timeout!");
      client.flush();
      client.stop();
      digitalWrite(LED_BUILTIN, 0);
      Serial.println();
      return;
    }

    // Catch client request
    String req = client.readStringUntil('\r');
    Serial.print("Request header: ");
    Serial.println(req);
    if (req.indexOf(F("/rest/ir/nec/")) != -1) {

      // Get IR Send NEC request
      String irrequest = req;
      irrequest.remove(0, ((irrequest.lastIndexOf("/rest/ir/nec/")) + 13));
      irrequest.remove((irrequest.indexOf(" ")));
      Serial.print("IR Send NEC code: ");
      Serial.println(irrequest);

      // Send IR NEC command
      Serial.println("Sending IR NEC command");
      irsend.sendNEC(strtoul(irrequest.c_str(), NULL, 16));

      // Send HTTP response
      Serial.println("Sending HTTP/1.1 200 OK");
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/plain");
      client.println("Connection: close");
      client.println();
      client.println("OK");

    } else {

      // Invalid request
      Serial.println("Invalid client request");
      Serial.println("Sending HTTP/1.1 404 response");
      client.println("HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html><body>Not found</body></html>");

    }

    // Flush output buffer
    Serial.println("Flushing output buffer");
    client.flush();
    digitalWrite(LED_BUILTIN, 0);
    Serial.println();
    return;

  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.