Software Setup

The software for this project consists of 2 parts. There is an Arduino sketch that you upload to the device. There is also a webpage that you can either open on a PC to view in a browser or if you have a web hosting service available or a Web server on your PC or perhaps a raspberry pi you can host the webpage that way.

The necessary files can be found in the IRLib2/examples/IoT_IR folder. Or you can copy and paste from the source code listings below.

Configuring the sketch

The Arduino sketch consists of 3 files.

Here is IoT_IR.ino which is the source code in C++,

Download: file
/*
 * IR IOT demo program
 * by Chris Young
 * use with Adafruit M0 Wi-Fi
 */

#include <SPI.h>
#include <WiFi101.h>
#include "WiFi101_Util.h"

#include <IRLibSendBase.h>            // First include the send base
#include <IRLib_P01_NEC.h>            // Now include only the protocols you wish
#include <IRLib_P02_Sony.h>           // to actually use. The lowest numbered
#include <IRLib_P05_Panasonic_Old.h>  // must be first but others can be any order.
#include <IRLib_P07_NECx.h>  
#include <IRLibCombo.h>               // After all protocols, include this

IRsend My_Sender;

void processIR(WiFiClient client) {
  int protocol = client.parseInt();
  if (client.read() != '/') return; //Need more. If not there then ignore
  unsigned long code= client.parseInt();
  //If next character is a '/' then we will parse number of bits
  //otherwise assume bits are zero
  int bits =0; 
  if (client.read() == '/') {
    bits = client.parseInt();
  }
  client.print("{\"command\":\"irsend\"");
  client.print(",\"protocol\":"); client.print(protocol);
  client.print(",\"code\":");  client.print(code);
  client.print(",\"bits\":");  client.print(bits);
  client.println('}');
  My_Sender.send(protocol, code, bits);  
}

void setup() {
  WiFi101_Setup(); //moved all the setup code to a separate tab for clarity
}

void loop() {
  // listen for incoming clients
  WiFiClient client = server.available();
  if(client.available()) {
    //char c = client.read();//skip initial "/"
    #if(0) //use for debugging
      String command=client.readString ();
      Serial.print('"');Serial.print(command);Serial.print('"');
    #else 
      String command= client.readStringUntil('/');//Skips over "Put" to the start of the commands
      command= client.readStringUntil('/');
      if(command == "irsend") { //is this IR send command
        processIR(client);
      } //add an else if there are other kinds of commands
    #endif
    client.stop();
  }
  delay(50);//Poll every 50ms
}

WiFi101_Util.h is an include file containing all of the Wi-Fi setup code. Make sure that SERIAL_DEBUG is set to 1 so that you can ensure that your board is working properly and you can obtain your IP address. Once you have everything configured and are using the board under normal circumstances you would set this value to 0.

Download: file
/*
 * Moved all of the Wi-Fi initialization and debugging code here for clarity
 */
#include "arduino_secrets.h" 
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;          // your network key Index number (needed only for WEP)

//Set to 1 if you are going to open the serial monitor
#define SERIAL_DEBUG 1
IPAddress ip;
int status = WL_IDLE_STATUS;
// Initialize the WiFi server library
WiFiServer server(80);

#if SERIAL_DEBUG
  void printWifiStatus() {
    // print the SSID of the network you're attached to:
    Serial.print("SSID: ");
    Serial.println(WiFi.SSID());
  
    // print your WiFi shield's IP address:
    Serial.print("IP Address: ");
    Serial.println(ip);
  
    // print the received signal strength:
    long rssi = WiFi.RSSI();
    Serial.print("signal strength (RSSI):");
    Serial.print(rssi);
    Serial.println(" dBm");
  }
#endif

void WiFi101_Setup(void) {
  #if defined(ARDUINO_SAMD_FEATHER_M0)
    //Configure pins for Adafruit ATWINC1500 Feather
    WiFi.setPins(8,7,4,2);
  #endif
  
  #if SERIAL_DEBUG
    // Start Serial
    Serial.begin(115200);
  
    // check for the presence of the shield:
    if (WiFi.status() == WL_NO_SHIELD) {
      Serial.println("WiFi shield not present");
      // don't continue:
      while (true);
    }
  
    String fv = WiFi.firmwareVersion();
    if ( fv != "1.1.0" )
      Serial.println("Please upgrade the firmware");
  #endif
  // Attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    #if SERIAL_DEBUG
        Serial.print("Attempting to connect to SSID: ");
        Serial.println(ssid);
    #endif
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // Wait 10 seconds for connection
    delay(10000);
  }
  // Start the server
  server.begin();
  ip = WiFi.localIP();
  #if SERIAL_DEBUG
    // Print out the status
    printWifiStatus();
  #endif
}

The file arduino_secrets.h where you will enter your Wi-Fi SSID and password. You will have to edit this file to include your values.

Download: file
#define SECRET_SSID ""
#define SECRET_PASS ""

Upload the sketch and open your serial monitor. Take note of the IP address that is printed. You'll need that information later.

Configuring the HTML file

Open the file adafruit_remote.html using a text editor. At approximately line 21 edit in the URL value that you got from serial monitor. Then save the file and open it in any browser. If you use Internet Explorer it will ask you to enable active X controls. Other browsers we have tested such as Google Chrome, Mozilla Firefox, and Opera browser do not have this requirement. Also Internet Explorer will automatically resize the window and dock it in the upper left corner of your screen. The other browsers do not allow this to be down under JavaScript control.

You will see a screen that looks like this.

This remote has been preconfigured to emulate the Adafruit Mini-Remote product number 389. When you click on any of the buttons it will transmit the proper code via your IR LED. If you have another Arduino set up with a receiver you can detect the transmitting codes. In addition to mouse clicks, this webpage also responds to keyboard presses. If you want to see which keystroke activates which button, You should press the "escape" key on your keyboard and the display will toggle to reveal this…

Customizing your remote

It is unlikely that you have a device that responds to these codes unless you have built one yourself designed to use this remote. So you will want to customize the webpage to create your own custom remote . Open the file in a text editor and and save as a different filename. Then we will edit the button definitions which begin at line number 26. This is a JavaScript definition that defines the buttons. Here is a portion of the existing code.

Download: file
var Button= [//object containing all buttons
	[//object containing row 0
		[1,0xfd00ff,0, "Vol-",189],
		[1,0xfd807f,0, "►||",32],//play pause
		[1,0xfd40bf,0, "Vol-",187]
	],
	[//row 1
		[1,0xfd20df,0, "SETUP",83],
		[1,0xfda05f,0, "<span class='Tiny'>PREV</span><br /><span class='Big'>⇑</span>",38],//up arrow
		[1,0xfd609f,0, "STOP/<br />MODE",77]
	],

Although we have only included 3 buttons per row, you can have as many buttons in each row as you want and will fit on your webpage. Each entry of the button consists of five values. The first value is the protocol number used by IRLib2. In this example we are using protocol 1 which is NEC protocol. The second number is a hex value that is the actual data transmitted. The third value is the number of bits. Note that some of protocols do not require this value so you can enter zero as we have in this example. The third value is a string enclosed in quotes that will be displayed. Note that this can include HTML formatting such as linebreaks or span definitions to change the style such as size and color. The fifth value is the keyboard scan code for the keypress that you want to assign to that particular button. If you do not know the proper value you can open up the webpage and press a key and it will display the value for you.

To determine the proper protocol number and code for your particular remote you will need to set up an Arduino with a receiver and use the dump.ino sample sketch from IRLib2. Point your remote at the receiver and record the protocol number, the data value, and the number of bits. For further details on how to determine the proper protocols and data values for your remote, we recommend you read our other tutorial "Using an Infrared Library on Arduino"

For further examples of how you might configure your buttons see our other example file "cable_and_tv.html" which has been configured for my own cable box which is a Scientific-Atlanta/Cisco cable box from BrightHouse/Spectrum cable company which uses protocol number 5 Panasonic_Old and a Samsung TV which uses protocol 7 NECx.

NOTE: This sketch will only work if the device is connected to your computer with the serial monitor open. Once you have everything working, you can change the SERIAL_DEBUG to 0 in the WiFi101_Util.h file. Then the device will no longer be dependent upon serial output. It can be disconnected from your computer and operated by battery or USB power supply.
This guide was first published on Mar 12, 2018. It was last updated on Mar 12, 2018. This page (Software Setup) was last updated on Jul 20, 2019.