四博智联产品售后

 找回密码
 立即注册
搜索
查看: 12876|回复: 0
打印 上一主题 下一主题

ESPDuino红外转发

[复制链接]

253

主题

306

帖子

1831

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1831
跳转到指定楼层
楼主
发表于 2017-3-13 14:09:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include "IRremote.h"
  2. #include <extEEPROM.h>
  3. #include <TimerOne.h>
  4. #include <SoftwareSerial.h>
  5. extEEPROM eep(kbits_4, 1, 16,0x50); //For 24c02 extEEPROM eep(kbits_2, 1, 8,0x50);For 24c04 extEEPROM eep(kbits_4, 1, 16,0x50);
  6. SoftwareSerial esp8266(9,12);
  7. int receiver = 11;
  8. char c;
  9. volatile int i=0;
  10. volatile boolean zero_cross=0;
  11. int dim = 128;
  12. //int dim2 = 0;
  13. int pas = 8;
  14. int freqStep = 75;
  15. int on1;
  16. int on2;
  17. int on3;
  18. int on4;
  19. int on5;
  20. int on6;
  21. int on7;
  22. int on8;
  23. int Relay1 =8;
  24. int Relay2 =7;
  25. int Relay3 =3;
  26. int Relay4 =4;
  27. int Relay5 =20;
  28. int Relay6 =21;
  29. int Relay7 =5;
  30. int Relay8 =6;
  31. int AC_pin = 10;
  32. int cpuled = 13;
  33. IRrecv irrecv(receiver);            
  34. decode_results results;
  35. void setup() {
  36.   // put your setup code here, to run once:

  37. pinMode(Relay1, OUTPUT);
  38. pinMode(Relay2, OUTPUT);
  39. pinMode(Relay3, OUTPUT);
  40. pinMode(Relay4, OUTPUT);
  41. pinMode(Relay5, OUTPUT);
  42. pinMode(Relay6, OUTPUT);
  43. pinMode(Relay7, OUTPUT);
  44. pinMode(Relay8, OUTPUT);
  45. pinMode(AC_pin, OUTPUT);
  46. pinMode(cpuled, OUTPUT);
  47. attachInterrupt(0, zero_cross_detect, RISING);
  48. Timer1.initialize(freqStep);
  49. Timer1.attachInterrupt(dim_check, freqStep);
  50. irrecv.enableIRIn();
  51. esp8266.begin(9600);
  52. delay(500);

  53. eep.begin(twiClock400kHz);
  54. on1 = eep.read(1);
  55. digitalWrite(Relay1, on1 ? HIGH : LOW);
  56. if (on1==1)
  57. {
  58.   esp8266.print("A");
  59. }
  60. on2 = eep.read(2);
  61. digitalWrite(Relay2, on2 ? HIGH : LOW);
  62. if (on2==1)
  63. {
  64.   esp8266.print("B");
  65. }
  66. on3 = eep.read(3);
  67. digitalWrite(Relay3, on3 ? HIGH : LOW);
  68. if (on3==1)
  69. {
  70.   esp8266.print("C");
  71. }
  72. on4 = eep.read(4);
  73. digitalWrite(Relay4, on4 ? HIGH : LOW);
  74. if (on4==1)
  75. {
  76.   esp8266.print("D");
  77. }
  78. on5 = eep.read(5);
  79. digitalWrite(Relay5, on5 ? HIGH : LOW);
  80. if (on5==1)
  81. {
  82.   esp8266.print("E");
  83. }
  84. on6 = eep.read(6);
  85. digitalWrite(Relay6, on6 ? HIGH : LOW);
  86. if (on6==1)
  87. {
  88.   esp8266.print("F");
  89. }
  90. on7 = eep.read(7);
  91. digitalWrite(Relay7, on7 ? HIGH : LOW);
  92. if (on7==1)
  93. {
  94.   esp8266.print("G");
  95. }
  96. on8 = eep.read(8);
  97. digitalWrite(Relay8, on8 ? HIGH : LOW);
  98. if (on8==1)
  99. {
  100.   esp8266.print("H");
  101. }
  102. dim = eep.read(9);

  103. delay(100);
  104. }

  105. void zero_cross_detect() {
  106. zero_cross = true;
  107. digitalWrite(AC_pin, LOW);  
  108. }
  109. void dim_check() {
  110. if(zero_cross == true) {
  111.   if(i>=dim) {
  112.     digitalWrite(AC_pin, HIGH);
  113.     i=0;
  114.     zero_cross=false;
  115.     }
  116.     else {
  117.       i++;
  118.   }
  119. }
  120. }

  121. void loop() {
  122.   // put your main code here, to run repeatedly:

  123.    if (irrecv.decode(&results))
  124.   {
  125.     translateIR();
  126.     irrecv.resume();
  127.   }
  128.   if(esp8266.available())
  129.   {
  130.     //while(esp8266.available())
  131.     //{
  132.        c = esp8266.read();
  133.      // Serial.write(c);
  134.       translateWIFI();
  135.    // }
  136.   }

  137.   digitalWrite(cpuled, HIGH);
  138. delay(100);
  139. digitalWrite(cpuled, LOW);
  140. delay(100);
  141.   
  142. }

  143. void translateWIFI()
  144. {
  145.   switch(c)
  146.   {
  147.     case 'A':  
  148.          on1 = !on1;
  149.          digitalWrite(Relay1, on1 ? HIGH : LOW);
  150.          eep.write(1, on1);
  151.          delay(100);
  152.        break;
  153.        case 'B':  
  154.          on2 = !on2;
  155.          digitalWrite(Relay2, on2 ? HIGH : LOW);
  156.          eep.write(2, on2);
  157.          delay(100);
  158.        break;
  159.       
  160. case 'C':  
  161.          on3 = !on3;
  162.          digitalWrite(Relay3, on3 ? HIGH : LOW);
  163.          eep.write(3, on3);
  164.          delay(100);
  165.        break;
  166.       
  167. case 'D':  
  168.          on4 = !on4;
  169.          digitalWrite(Relay4, on4 ? HIGH : LOW);
  170.          eep.write(4, on4);
  171.          delay(100);
  172.        break;   
  173.       
  174. case 'E':  
  175.          on5 = !on5;
  176.          digitalWrite(Relay5, on5 ? HIGH : LOW);
  177.          eep.write(5, on5);
  178.          delay(100);
  179.        break;

  180. case 'F':  
  181.          on6 = !on6;
  182.          digitalWrite(Relay6, on6 ? HIGH : LOW);
  183.          eep.write(6, on6);
  184.          delay(100);
  185.        break;

  186. case 'G':  
  187.          on7 = !on7;
  188.          digitalWrite(Relay7, on7 ? HIGH : LOW);
  189.          eep.write(7, on7);
  190.          delay(100);
  191.        break;
  192.       
  193. case 'H':  
  194.          on8 = !on8;
  195.          digitalWrite(Relay8, on8 ? HIGH : LOW);
  196.          eep.write(8, on8);
  197.          delay(100);
  198.        break;

  199. case 'N':  
  200.    if(on2==1){      
  201.   if (dim<127)  
  202.   {
  203.     dim = dim + pas;
  204.     if (dim>127)
  205.     {
  206.       dim=128;
  207.     }
  208.   }
  209.   eep.write(9, dim);
  210.    }
  211.    delay(100);
  212.     break;

  213. case 'U':
  214. if(on2==1) {
  215.     if (dim>5)  
  216.   {
  217.      dim = dim - pas;
  218.   if (dim<0)
  219.     {
  220.       dim=0;
  221.     }
  222.    }
  223.    eep.write(9, dim);
  224. }
  225.    delay(100);
  226.    break;

  227.    
  228. case 'I':  
  229.          on1 = 0;
  230.          on2 = 0;
  231.          on3 = 0;
  232.          on4 = 0;
  233.          on5 = 0;
  234.          on6 = 0;
  235.          on7 = 0;
  236.          on8 = 0;
  237.          digitalWrite(Relay1, LOW);
  238.          eep.write(1, on1);
  239.          digitalWrite(Relay2, LOW);
  240.          eep.write(2, on2);
  241.          digitalWrite(Relay3, LOW);
  242.          eep.write(3, on3);
  243.          digitalWrite(Relay4, LOW);
  244.          eep.write(4, on4);
  245.          digitalWrite(Relay5, LOW);
  246.          eep.write(5, on5);
  247.          digitalWrite(Relay6, LOW);
  248.          eep.write(6, on6);
  249.          digitalWrite(Relay7, LOW);
  250.          eep.write(7, on7);
  251.          digitalWrite(Relay8, LOW);
  252.          eep.write(8, on8);
  253.          delay(100);
  254.        break;   
  255.       
  256.    
  257.   }
  258.   
  259. }


  260. void translateIR()


  261. {

  262.   switch(results.value)

  263.   {

  264.   case 0xE0E020DF:  
  265.          on1 = !on1;
  266.          digitalWrite(Relay1, on1 ? HIGH : LOW);
  267.          eep.write(1, on1);
  268.          esp8266.print("A");
  269.          delay(100);
  270.        break;
  271.       
  272. case 0xE0E0A05F:  
  273.          on2 = !on2;
  274.          digitalWrite(Relay2, on2 ? HIGH : LOW);
  275.          eep.write(2, on2);
  276.          esp8266.print("B");
  277.          delay(100);
  278.        break;
  279.       
  280. case 0xE0E0609F:  
  281.          on3 = !on3;
  282.          digitalWrite(Relay3, on3 ? HIGH : LOW);
  283.          eep.write(3, on3);
  284.          esp8266.print("C");
  285.          delay(100);
  286.        break;
  287.       
  288. case 0xE0E010EF:  
  289.          on4 = !on4;
  290.          digitalWrite(Relay4, on4 ? HIGH : LOW);
  291.          eep.write(4, on4);
  292.          esp8266.print("D");
  293.          delay(100);
  294.        break;   
  295.       
  296. case 0xE0E0906F:  
  297.          on5 = !on5;
  298.          digitalWrite(Relay5, on5 ? HIGH : LOW);
  299.          eep.write(5, on5);
  300.          esp8266.print("E");
  301.          delay(100);
  302.        break;

  303. case 0xE0E050AF:  
  304.          on6 = !on6;
  305.          digitalWrite(Relay6, on6 ? HIGH : LOW);
  306.          eep.write(6, on6);
  307.          esp8266.print("F");
  308.          delay(100);
  309.        break;

  310. case 0xE0E030CF:  
  311.          on7 = !on7;
  312.          digitalWrite(Relay7, on7 ? HIGH : LOW);
  313.          eep.write(7, on7);
  314.          esp8266.print("G");
  315.          delay(100);
  316.        break;
  317.       
  318. case 0xE0E0B04F:  
  319.          on8 = !on8;
  320.          digitalWrite(Relay8, on8 ? HIGH : LOW);
  321.          eep.write(8, on8);
  322.          esp8266.print("H");
  323.          delay(100);
  324.        break;
  325.       
  326.         
  327. case 0xE0E0D02F:  
  328.    if(on2==1){      
  329.   if (dim<127)  
  330.   {
  331.     dim = dim + pas;
  332.     if (dim>127)
  333.     {
  334.       dim=128;
  335.     }
  336.   }
  337.   eep.write(9, dim);
  338.   esp8266.print("N");
  339.    }
  340.    delay(100);
  341.     break;

  342.     case 0xE0E0E01F:
  343. if(on2==1) {
  344.     if (dim>5)  
  345.   {
  346.      dim = dim - pas;
  347.   if (dim<0)
  348.     {
  349.       dim=0;
  350.     }
  351.    }
  352.    eep.write(9, dim);
  353.    esp8266.print("U");
  354. }
  355.    delay(100);
  356.    break;
  357.          
  358.       
  359. case 0xE0E040BF:  
  360.          on1 = 0;
  361.          on2 = 0;
  362.          on3 = 0;
  363.          on4 = 0;
  364.          on5 = 0;
  365.          on6 = 0;
  366.          on7 = 0;
  367.          on8 = 0;
  368.          digitalWrite(Relay1, LOW);
  369.          eep.write(1, on1);
  370.          digitalWrite(Relay2, LOW);
  371.          eep.write(2, on2);
  372.          digitalWrite(Relay3, LOW);
  373.          eep.write(3, on3);
  374.          digitalWrite(Relay4, LOW);
  375.          eep.write(4, on4);
  376.          digitalWrite(Relay5, LOW);
  377.          eep.write(5, on5);
  378.          digitalWrite(Relay6, LOW);
  379.          eep.write(6, on6);
  380.          digitalWrite(Relay7, LOW);
  381.          eep.write(7, on7);
  382.          digitalWrite(Relay8, LOW);
  383.          eep.write(8, on8);
  384.          esp8266.print("I");
  385.          delay(100);
  386.        break;   

  387. default:
  388. break;   
  389.   }
  390.   delay (100);
  391.   }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|四博智联 Inc. ( 粤ICP备15034758号-1

GMT+8, 2024-4-25 07:54 , Processed in 0.054824 second(s), 24 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表