Advertisement
Guest User

esp8266 code

a guest
Jul 8th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. /*
  2. * IRremoteESP8266: IRrecvDemo - demonstrates receiving IR codes with IRrecv
  3. * This is very simple teaching code to show you how to use the library.
  4. * If you are trying to decode your Infra-Red remote(s) for later replay,
  5. * use the IRrecvDumpV2.ino example code instead of this.
  6. * An IR detector/demodulator must be connected to the input RECV_PIN.
  7. * Copyright 2009 Ken Shirriff, http://arcfn.com
  8. * Example circuit diagram:
  9. * https://github.com/markszabo/IRremoteESP8266/wiki#ir-receiving
  10. * Changes:
  11. * Version 0.2 June, 2017
  12. * Changed GPIO pin to the same as other examples.
  13. * Used our own method for printing a uint64_t.
  14. * Changed the baud rate to 115200.
  15. * Version 0.1 Sept, 2015
  16. * Based on Ken Shirriff's IrsendDemo Version 0.1 July, 2009
  17. */
  18.  
  19. #ifndef UNIT_TEST
  20. #include <Arduino.h>
  21. #endif
  22. #include <IRremoteESP8266.h>
  23. #include <IRrecv.h>
  24. #include <IRutils.h>
  25. #include <ESP8266WiFi.h>
  26. #include <ESP8266HTTPClient.h>
  27. #include <PubSubClient.h>
  28.  
  29. // An IR detector/demodulator is connected to GPIO pin 14(D5 on a NodeMCU
  30. // board).
  31. uint16_t RECV_PIN = 14;
  32.  
  33. const char* mqttServer = "192.168.1.178"; //IP address of RPi
  34. const int mqttPort = 1883;
  35. const char* mqttUser = "homeassistant";
  36. const char* mqttPassword = "hunter2";
  37.  
  38. WiFiClient espClient;
  39. PubSubClient client(espClient);
  40.  
  41. IRrecv irrecv(RECV_PIN);
  42.  
  43. decode_results results;
  44. char test[50];
  45. String testString;
  46.  
  47. void setup() {
  48. Serial.begin(115200);
  49. WiFi.begin("DebNet","luckykitty707");
  50. Serial.print("Connecting");
  51. while(WiFi.status() != WL_CONNECTED){
  52. delay(250);
  53. Serial.print(".");
  54. }
  55. Serial.println();
  56.  
  57. //MQTT
  58. client.setServer(mqttServer,mqttPort);
  59. client.setCallback(callback);
  60.  
  61. while(!client.connected()){
  62. Serial.println("Connecting to MQTT...");
  63. if(client.connect("ESP8266Client",mqttUser,mqttPassword)){
  64. Serial.println("Connected.");
  65. } else {
  66. Serial.print("Failed with state ");
  67. Serial.print(client.state());
  68. delay(2000);
  69. }
  70. }
  71.  
  72. client.publish("test","CODE GOES HERE");
  73. client.subscribe("test");
  74.  
  75. irrecv.enableIRIn(); // Start the receiver
  76. while (!Serial) // Wait for the serial connection to be establised.
  77. delay(50);
  78. Serial.println();
  79. Serial.print("IRrecvDemo is now running and waiting for IR message on Pin ");
  80. Serial.println(RECV_PIN);
  81. pinMode(2,OUTPUT);
  82. digitalWrite(2,HIGH);
  83. }
  84.  
  85. void loop() {
  86. if (irrecv.decode(&results)) {
  87. // print() & println() can't handle printing long longs. (uint64_t)
  88. //serialPrintUint64(results.value, HEX);
  89. ltoa(results.value,test,10);
  90. testString = "";
  91. testString += test;
  92. testString = testString.substring(0,10);
  93. if(testString == "18243"){
  94. Serial.println("0");
  95. request("remote_0");
  96. }
  97. if(testString == "4931"){
  98. Serial.println("1");
  99. request("remote_1");
  100. }
  101. if(testString == "5955"){
  102. Serial.println("2");
  103. request("remote_2");
  104. }
  105. if(testString == "6979"){
  106. Serial.println("3");
  107. request("remote_3");
  108. }
  109. if(testString == "33603"){
  110. Serial.println("PAUSE");
  111. sendmqtt("pause");
  112. }
  113. if(testString == "3907"){
  114. Serial.println("PLAY");
  115. sendmqtt("play");
  116. }
  117. if(testString == "52035"){
  118. Serial.println("FAST FORWARD");
  119. sendmqtt("fast-forward");
  120. }
  121. if(testString == "51011"){
  122. Serial.println("FAST REWIND");
  123. sendmqtt("fast-rewind");
  124. }
  125. if(testString == "34627")
  126. {
  127. Serial.println("STOP");
  128. sendmqtt("stop");
  129. }
  130. if(testString == "57155"){
  131. Serial.println("SKIP FORWARD");
  132. sendmqtt("skip-forward");
  133. }
  134. if(testString == "56131"){
  135. Serial.println("SKIP BACKWARD");
  136. sendmqtt("skip-backward");
  137. }
  138. if(testString == "20291"){
  139. Serial.println("RED");
  140. request("light_red");
  141. }
  142. if(testString == "55107"){
  143. Serial.println("GREEN");
  144. request("light_green");
  145. }
  146. if(testString == "35651"){
  147. Serial.println("YELLOW");
  148. request("light_yellow");
  149. }
  150. if(testString == "36675"){
  151. Serial.println("BLUE");
  152. request("light_blue");
  153. }
  154. //Serial.print(testString);
  155. //Serial.println("");
  156. irrecv.resume(); // Receive the next value
  157. client.loop();
  158. }
  159. delay(100);
  160. }
  161.  
  162. void callback(char* topic, byte* payload, unsigned int length){
  163. Serial.print("Message arrived on ");
  164. Serial.println(topic);
  165. Serial.print("Message: ");
  166. for(int i = 0; i < length; i++){
  167. Serial.print((char)payload[i]);
  168. }
  169. Serial.println("");
  170. Serial.println("---------------------");
  171. }
  172.  
  173. void request(String trigger){
  174. HTTPClient http;
  175. String iftttURL = "http://maker.ifttt.com/trigger/" + trigger + "/with/key/CORRECT-KEY";
  176. http.begin(iftttURL);
  177. int httpCode = http.GET();
  178. http.end();
  179. blinkled();
  180. }
  181.  
  182. void sendmqtt(char* message){
  183. client.publish("test",message);
  184. Serial.print(message);
  185. Serial.println(" was sent on MQTT.");
  186. blinkled();
  187. }
  188.  
  189. void blinkled(){
  190. digitalWrite(2,LOW);
  191. delay(250);
  192. digitalWrite(2,HIGH);
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement