arduino.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <RCSwitch.h>
  4. byte mac[] = {0xDE, 0xAD, 0xBE, 0xAA, 0xDE, 0x01};
  5. IPAddress ip(192,168,2,114);
  6. const int serverPort = 2313;
  7. const int senderPin = 6;
  8. const int requestBufferLength = 16;
  9. const int attemptsCount = 3;
  10. EthernetServer server(serverPort);
  11. RCSwitch sender = RCSwitch();
  12. char requestBuffer[requestBufferLength];
  13. void setup()
  14. {
  15. Serial.begin(9600);
  16. sender.enableTransmit(senderPin);
  17. sender.switchOn('b', 3, 2);
  18. Serial.println("transmission enabled");
  19. Ethernet.begin(mac, ip);
  20. server.begin();
  21. Serial.print("started ardvindo server at ");
  22. Serial.print(Ethernet.localIP());
  23. Serial.print(":");
  24. Serial.println(serverPort);
  25. }
  26. void errorResponse(EthernetClient& client, const char* cmd, const char* msg)
  27. {
  28. Serial.print("error: ");
  29. Serial.println(msg);
  30. client.print("cmd: ");
  31. client.println(cmd);
  32. client.print("error: ");
  33. client.println(msg);
  34. }
  35. void processCmd(EthernetClient& client, const char* cmd)
  36. {
  37. if(cmd[0] == 'e' || cmd[0] == 'd') {
  38. char family = requestBuffer[2];
  39. int group = requestBuffer[4] - '0';
  40. int device = requestBuffer[6] - '0';
  41. if(family < 'a' || family > 'f') {
  42. errorResponse(client, cmd, "error: family < 'a' || family > 'f'");
  43. } else if(group < 0) {
  44. errorResponse(client, cmd, "group < 0");
  45. } else if(device < 0) {
  46. errorResponse(client, cmd, "device < 0");
  47. } else {
  48. bool on = (requestBuffer[0] == 'e');
  49. for(int i = 0; i < attemptsCount; i++) {
  50. if(on) {
  51. sender.switchOn(family, group, device);
  52. } else {
  53. sender.switchOff(family, group, device);
  54. }
  55. delay(1);
  56. }
  57. client.print(family);
  58. client.print(" ");
  59. client.print(group);
  60. client.print(" ");
  61. client.print(device);
  62. client.print(" ");
  63. if(on) {
  64. client.println("switched on");
  65. Serial.println("switched on");
  66. } else {
  67. client.println("switched off");
  68. Serial.println("switched off");
  69. }
  70. //sender.switchOn('b', 3, 2);
  71. //delay(1500);
  72. //sender.switchOff('b', 3, 2);
  73. }
  74. } else {
  75. errorResponse(client, cmd, "unknown command");
  76. }
  77. }
  78. void loop()
  79. {
  80. EthernetClient client = server.available();
  81. if (client) {
  82. Serial.println("client connected");
  83. client.println("ardvindo");
  84. while(client.connected()) {
  85. if(client.available()) {
  86. for(int i=0; i<requestBufferLength && client.available(); i++) {
  87. char c = client.read();
  88. if(c == '\n') {
  89. break;
  90. }
  91. requestBuffer[i] = c;
  92. requestBuffer[i+1] = '\0';
  93. }
  94. Serial.print("> ");
  95. Serial.println(requestBuffer);
  96. processCmd(client, requestBuffer);
  97. }
  98. }
  99. delay(1);
  100. client.stop();
  101. Serial.println("client disonnected");
  102. }
  103. }