Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * IRremoteESP8266: IRrecvDemo - demonstrates receiving IR codes with IRrecv
- * This is very simple teaching code to show you how to use the library.
- * If you are trying to decode your Infra-Red remote(s) for later replay,
- * use the IRrecvDumpV2.ino example code instead of this.
- * An IR detector/demodulator must be connected to the input RECV_PIN.
- * Copyright 2009 Ken Shirriff, http://arcfn.com
- * Example circuit diagram:
- * https://github.com/markszabo/IRremoteESP8266/wiki#ir-receiving
- * Changes:
- * Version 0.2 June, 2017
- * Changed GPIO pin to the same as other examples.
- * Used our own method for printing a uint64_t.
- * Changed the baud rate to 115200.
- * Version 0.1 Sept, 2015
- * Based on Ken Shirriff's IrsendDemo Version 0.1 July, 2009
- */
- #ifndef UNIT_TEST
- #include <Arduino.h>
- #endif
- #include <IRremoteESP8266.h>
- #include <IRrecv.h>
- #include <IRutils.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #include <PubSubClient.h>
- // An IR detector/demodulator is connected to GPIO pin 14(D5 on a NodeMCU
- // board).
- uint16_t RECV_PIN = 14;
- const char* mqttServer = "192.168.1.178"; //IP address of RPi
- const int mqttPort = 1883;
- const char* mqttUser = "homeassistant";
- const char* mqttPassword = "hunter2";
- WiFiClient espClient;
- PubSubClient client(espClient);
- IRrecv irrecv(RECV_PIN);
- decode_results results;
- char test[50];
- String testString;
- void setup() {
- Serial.begin(115200);
- WiFi.begin("DebNet","luckykitty707");
- Serial.print("Connecting");
- while(WiFi.status() != WL_CONNECTED){
- delay(250);
- Serial.print(".");
- }
- Serial.println();
- //MQTT
- client.setServer(mqttServer,mqttPort);
- client.setCallback(callback);
- while(!client.connected()){
- Serial.println("Connecting to MQTT...");
- if(client.connect("ESP8266Client",mqttUser,mqttPassword)){
- Serial.println("Connected.");
- } else {
- Serial.print("Failed with state ");
- Serial.print(client.state());
- delay(2000);
- }
- }
- client.publish("test","CODE GOES HERE");
- client.subscribe("test");
- irrecv.enableIRIn(); // Start the receiver
- while (!Serial) // Wait for the serial connection to be establised.
- delay(50);
- Serial.println();
- Serial.print("IRrecvDemo is now running and waiting for IR message on Pin ");
- Serial.println(RECV_PIN);
- pinMode(2,OUTPUT);
- digitalWrite(2,HIGH);
- }
- void loop() {
- if (irrecv.decode(&results)) {
- // print() & println() can't handle printing long longs. (uint64_t)
- //serialPrintUint64(results.value, HEX);
- ltoa(results.value,test,10);
- testString = "";
- testString += test;
- testString = testString.substring(0,10);
- if(testString == "18243"){
- Serial.println("0");
- request("remote_0");
- }
- if(testString == "4931"){
- Serial.println("1");
- request("remote_1");
- }
- if(testString == "5955"){
- Serial.println("2");
- request("remote_2");
- }
- if(testString == "6979"){
- Serial.println("3");
- request("remote_3");
- }
- if(testString == "33603"){
- Serial.println("PAUSE");
- sendmqtt("pause");
- }
- if(testString == "3907"){
- Serial.println("PLAY");
- sendmqtt("play");
- }
- if(testString == "52035"){
- Serial.println("FAST FORWARD");
- sendmqtt("fast-forward");
- }
- if(testString == "51011"){
- Serial.println("FAST REWIND");
- sendmqtt("fast-rewind");
- }
- if(testString == "34627")
- {
- Serial.println("STOP");
- sendmqtt("stop");
- }
- if(testString == "57155"){
- Serial.println("SKIP FORWARD");
- sendmqtt("skip-forward");
- }
- if(testString == "56131"){
- Serial.println("SKIP BACKWARD");
- sendmqtt("skip-backward");
- }
- if(testString == "20291"){
- Serial.println("RED");
- request("light_red");
- }
- if(testString == "55107"){
- Serial.println("GREEN");
- request("light_green");
- }
- if(testString == "35651"){
- Serial.println("YELLOW");
- request("light_yellow");
- }
- if(testString == "36675"){
- Serial.println("BLUE");
- request("light_blue");
- }
- //Serial.print(testString);
- //Serial.println("");
- irrecv.resume(); // Receive the next value
- client.loop();
- }
- delay(100);
- }
- void callback(char* topic, byte* payload, unsigned int length){
- Serial.print("Message arrived on ");
- Serial.println(topic);
- Serial.print("Message: ");
- for(int i = 0; i < length; i++){
- Serial.print((char)payload[i]);
- }
- Serial.println("");
- Serial.println("---------------------");
- }
- void request(String trigger){
- HTTPClient http;
- String iftttURL = "http://maker.ifttt.com/trigger/" + trigger + "/with/key/CORRECT-KEY";
- http.begin(iftttURL);
- int httpCode = http.GET();
- http.end();
- blinkled();
- }
- void sendmqtt(char* message){
- client.publish("test",message);
- Serial.print(message);
- Serial.println(" was sent on MQTT.");
- blinkled();
- }
- void blinkled(){
- digitalWrite(2,LOW);
- delay(250);
- digitalWrite(2,HIGH);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement