arduino.ino 2.8 KB

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