LGACSend.ino 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Copyright 2015 chaeplin
  2. // Copyright 2017 xpokor22
  3. // This is based on:
  4. // https://github.com/z3t0/Arduino-IRremote/blob/master/examples/LGACSendDemo/LGACSendDemo.ino
  5. #include <IRremoteESP8266.h>
  6. #include <IRsend.h>
  7. IRsend irsend(14); // An IR LED is controlled by GPIO pin 14 (D5)
  8. // 0 : TOWER
  9. // 1 : WALL
  10. const unsigned int kAc_Type = 1;
  11. // 0 : cooling
  12. // 1 : heating
  13. unsigned int ac_heat = 1;
  14. // 0 : off
  15. // 1 : on
  16. unsigned int ac_power_on = 0;
  17. // 0 : off
  18. // 1 : on --> power on
  19. unsigned int ac_air_clean_state = 0;
  20. // temperature : 18 ~ 30
  21. unsigned int ac_temperature = 24;
  22. // 0 : low
  23. // 1 : mid
  24. // 2 : high
  25. // if kAc_Type = 1, 3 : change
  26. unsigned int ac_flow = 0;
  27. const uint8_t kAc_Flow_Tower[3] = {0, 4, 6};
  28. const uint8_t kAc_Flow_Wall[4] = {0, 2, 4, 5};
  29. uint32_t ac_code_to_sent;
  30. void Ac_Send_Code(uint32_t code) {
  31. Serial.print("code to send : ");
  32. Serial.print(code, BIN);
  33. Serial.print(" : ");
  34. Serial.println(code, HEX);
  35. #if SEND_LG
  36. irsend.sendLG(code, 28);
  37. #else // SEND_LG
  38. Serial.println("Can't send because SEND_LG has been disabled.");
  39. #endif // SEND_LG
  40. }
  41. void Ac_Activate(unsigned int temperature, unsigned int air_flow,
  42. unsigned int heat) {
  43. ac_heat = heat;
  44. unsigned int ac_msbits1 = 8;
  45. unsigned int ac_msbits2 = 8;
  46. unsigned int ac_msbits3 = 0;
  47. unsigned int ac_msbits4;
  48. if (ac_heat == 1)
  49. ac_msbits4 = 4; // heating
  50. else
  51. ac_msbits4 = 0; // cooling
  52. unsigned int ac_msbits5 = (temperature < 15) ? 0 : temperature - 15;
  53. unsigned int ac_msbits6 = 0;
  54. if (air_flow <= 2) {
  55. if (kAc_Type == 0)
  56. ac_msbits6 = kAc_Flow_Tower[air_flow];
  57. else
  58. ac_msbits6 = kAc_Flow_Wall[air_flow];
  59. }
  60. // calculating using other values
  61. unsigned int ac_msbits7 = (ac_msbits3 + ac_msbits4 + ac_msbits5 +
  62. ac_msbits6) & B00001111;
  63. ac_code_to_sent = ac_msbits1 << 4;
  64. ac_code_to_sent = (ac_code_to_sent + ac_msbits2) << 4;
  65. ac_code_to_sent = (ac_code_to_sent + ac_msbits3) << 4;
  66. ac_code_to_sent = (ac_code_to_sent + ac_msbits4) << 4;
  67. ac_code_to_sent = (ac_code_to_sent + ac_msbits5) << 4;
  68. ac_code_to_sent = (ac_code_to_sent + ac_msbits6) << 4;
  69. ac_code_to_sent = (ac_code_to_sent + ac_msbits7);
  70. Ac_Send_Code(ac_code_to_sent);
  71. ac_power_on = 1;
  72. ac_temperature = temperature;
  73. ac_flow = air_flow;
  74. }
  75. void Ac_Change_Air_Swing(int air_swing) {
  76. if (kAc_Type == 0) {
  77. if (air_swing == 1)
  78. ac_code_to_sent = 0x881316B;
  79. else
  80. ac_code_to_sent = 0x881317C;
  81. } else {
  82. if (air_swing == 1)
  83. ac_code_to_sent = 0x8813149;
  84. else
  85. ac_code_to_sent = 0x881315A;
  86. }
  87. Ac_Send_Code(ac_code_to_sent);
  88. }
  89. void Ac_Power_Down() {
  90. ac_code_to_sent = 0x88C0051;
  91. Ac_Send_Code(ac_code_to_sent);
  92. ac_power_on = 0;
  93. }
  94. void Ac_Air_Clean(int air_clean) {
  95. if (air_clean == '1')
  96. ac_code_to_sent = 0x88C000C;
  97. else
  98. ac_code_to_sent = 0x88C0084;
  99. Ac_Send_Code(ac_code_to_sent);
  100. ac_air_clean_state = air_clean;
  101. }
  102. void setup() {
  103. Serial.begin(115200);
  104. delay(1000);
  105. irsend.begin();
  106. }
  107. void loop() {
  108. char b = ' ';
  109. Serial.println("# a : mode or temp b : air_flow, temp, swing, clean,"
  110. " cooling/heating");
  111. Serial.println("# 0 : off 0");
  112. Serial.println("# 1 : on 0");
  113. Serial.println("# 2 : air_swing 0 or 1");
  114. Serial.println("# 3 : air_clean 0 or 1");
  115. Serial.println("# 4 : air_flow 0 ~ 2 : flow");
  116. Serial.println("# + : temp + 1");
  117. Serial.println("# - : temp - 1");
  118. Serial.println("# c : cooling");
  119. Serial.println("# h : heating");
  120. Serial.println("# m : change cooling to air clean, air clean to cooling");
  121. Serial.println("a="); // Prompt User for input
  122. while (Serial.available() == 0) { // Wait for user input
  123. }
  124. char a = Serial.read(); // Read user input into a
  125. switch (a) {
  126. case '0':
  127. case '1':
  128. case '+':
  129. case '-':
  130. case 'c':
  131. case 'h':
  132. case 'm':
  133. break;
  134. default:
  135. Serial.println("b="); // Prompt User for input
  136. while (Serial.available() == 0) {}
  137. b = Serial.read();
  138. }
  139. /*
  140. # a : mode or temp b : air_flow, temp, swing, clean, cooling/heating
  141. # 18 ~ 30 : temp 0 ~ 2 : flow // on
  142. # 0 : off 0
  143. # 1 : on 0
  144. # 2 : air_swing 0 or 1
  145. # 3 : air_clean 0 or 1
  146. # 4 : air_flow 0 ~ 3 : flow
  147. # + : temp + 1
  148. # - : temp - 1
  149. # c : cooling
  150. # h : heating
  151. # m : change cooling to air clean, air clean to cooling
  152. */
  153. Serial.print("a : ");
  154. Serial.print(a);
  155. Serial.print(" b : ");
  156. Serial.println(b);
  157. switch (a) {
  158. case '0': // off
  159. Ac_Power_Down();
  160. break;
  161. case '1': // on
  162. Ac_Activate(ac_temperature, ac_flow, ac_heat);
  163. break;
  164. case '2':
  165. if (b == '0')
  166. Ac_Change_Air_Swing(0);
  167. else
  168. Ac_Change_Air_Swing(1);
  169. break;
  170. case '3': // 1 : clean on, power on
  171. if (b == '0' || b == '1')
  172. Ac_Air_Clean(b);
  173. break;
  174. case '4':
  175. switch (b) {
  176. case '1':
  177. Ac_Activate(ac_temperature, 1, ac_heat);
  178. break;
  179. case '2':
  180. Ac_Activate(ac_temperature, 2, ac_heat);
  181. break;
  182. case '3':
  183. Ac_Activate(ac_temperature, 3, ac_heat);
  184. break;
  185. default:
  186. Ac_Activate(ac_temperature, 0, ac_heat);
  187. }
  188. break;
  189. case '+':
  190. if (18 <= ac_temperature && ac_temperature <= 29)
  191. Ac_Activate((ac_temperature + 1), ac_flow, ac_heat);
  192. break;
  193. case '-':
  194. if (19 <= ac_temperature && ac_temperature <= 30)
  195. Ac_Activate((ac_temperature - 1), ac_flow, ac_heat);
  196. break;
  197. case 'c':
  198. ac_heat = 0;
  199. Ac_Activate(ac_temperature, ac_flow, ac_heat);
  200. break;
  201. case 'h':
  202. ac_heat = 1;
  203. Ac_Activate(ac_temperature, ac_flow, ac_heat);
  204. break;
  205. case 'm':
  206. /*
  207. if ac is on, 1) turn off, 2) turn on Ac_Air_Clean(1)
  208. if ac is off, 1) turn on, 2) turn off Ac_Air_Clean(0)
  209. */
  210. if (ac_power_on == 1) {
  211. Ac_Power_Down();
  212. delay(100);
  213. Ac_Air_Clean(1);
  214. } else {
  215. if (ac_air_clean_state == 1) {
  216. Ac_Air_Clean(0);
  217. delay(100);
  218. }
  219. Ac_Activate(ac_temperature, ac_flow, ac_heat);
  220. }
  221. break;
  222. }
  223. delay(100);
  224. Serial.println("ac_temperature");
  225. Serial.println(ac_temperature);
  226. Serial.println("ac_flow");
  227. Serial.println(ac_flow);
  228. Serial.println("ac_heat");
  229. Serial.println(ac_heat);
  230. Serial.println("ac_power_on");
  231. Serial.println(ac_power_on);
  232. }