Advertisement
Guest User

EthGW

a guest
Sep 14th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.51 KB | None | 0 0
  1. /*
  2. * Copyright (C) 2013 Henrik Ekblad <henrik.ekblad@gmail.com>
  3. *
  4. * Contribution by a-lurker
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * DESCRIPTION
  11. * The EthernetGateway sends data received from sensors to the ethernet link.
  12. * The gateway also accepts input on ethernet interface, which is then sent out to the radio network.
  13. *
  14. * The GW code is designed for Arduino 328p / 16MHz.  ATmega168 does not have enough memory to run this program.
  15. *
  16. * COMPILING
  17. * You must make sure to disable DEBUG in Sensor.h before compiling this sketch. Othervise the sketch won't fit in program space when downloading.
  18. * For UIPEthernet(ENC28J60) usage: Note that I had to disable UDP and DHCP support in uipethernet-conf.h to reduce space. (which meas you ave to choose a static IP)
  19. * For WizNET usage: Do *not* install the provided UIPEthernet-library. Remove UIPEthernet-include below and uncomment the Ethernet.h.
  20. *
  21. * VERA CONFIGURATION:
  22. * Enter "ip-number:port" in the ip-field of the Arduino GW device. This will temporarily override any serial configuration for the Vera plugin.
  23. * E.g. If you want to use the defualt values in this sketch enter: 192.168.178.66:5003
  24. *
  25. * LED purposes:
  26. * - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
  27. * - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
  28. * - ERR (red) - fast blink on error during transmission error or recieve crc error
  29. *
  30. *  ----------- Connection guide ---------------------------------------------------------------------------
  31. *  13  Radio & Ethernet SPI SCK
  32. *  12  Radio & Ethernet SPI MISO (SO)
  33. *  11  Radio & Ethernet SPI MOSI (SI)
  34. *  10  Ethernet SPI Slave Select (CS)    Pin 10, the SS pin, must be an o/p to put SPI in master mode
  35. *  9   Radio TX LED using on board LED   (optional)  +5V -> LED -> 270-330 Ohm resistor -> pin 9.
  36. *  8   Radio RX LED                      (optional)  +5V -> LED -> 270-330 Ohm resistor -> pin 8.
  37. *  7   Radio error LED                   (optional)  +5V -> LED -> 270-330 Ohm resistor -> pin 7.
  38. *  6   Radio SPI Slave Select
  39. *  5   Radio Chip Enable
  40. *  3   Inclusion mode button             (optional), 10K pull down to GND, button to VCC)
  41. *  2   Radio IRQ pin                     (optional), W5100 Int, if linked to pin 2)
  42. * -----------------------------------------------------------------------------------------------------------
  43. * Powering: both NRF24l01 radio and Ethernet(ENC28J60) uses 3.3V
  44. */
  45.  
  46.  
  47.  
  48.  
  49. #include <SPI.h>  
  50. #include <EEPROM.h>  
  51. //#include <RF24.h>
  52. #include <MsTimer2.h>
  53. #include <PinChangeInt.h>
  54. #include <MyGateway.h>  
  55. #include <stdarg.h>
  56.  
  57. //#include <UIPEthernet.h>  // Use this if you have attached a Ethernet ENC28J60 shields  
  58. #include <Ethernet.h>   // Use this fo WizNET module and Arduino Ethernet Shield
  59.  
  60.  
  61. #define INCLUSION_MODE_TIME 1 // Number of minutes inclusion mode is enabled
  62. #define INCLUSION_MODE_PIN  3 // Digital pin used for inclusion mode button
  63.  
  64. #define RADIO_CE_PIN        5  // radio chip enable
  65. #define RADIO_SPI_SS_PIN    6  // radio SPI serial select
  66. #define RADIO_ERROR_LED_PIN 7  // Error led pin
  67. #define RADIO_RX_LED_PIN    8  // Receive led pin
  68. #define RADIO_TX_LED_PIN    9  // the PCB, on board LED
  69.  
  70. #define IP_PORT 5003        // The port you want to open
  71. IPAddress myIp(192, 168, 1, 13);  // Configure your static ip-address here
  72.  
  73. // The MAC address can be anything you want but should be unique on your network.
  74. // Newer boards have a MAC address printed on the underside of the PCB, which you can (optionally) use.
  75. // Note that most of the Ardunio examples use  "DEAD BEEF FEED" for the MAC address.
  76. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  // DEAD BEEF FEED
  77.  
  78. // a R/W server on the port
  79. EthernetServer server = EthernetServer(IP_PORT);
  80.  
  81. // No blink or button functionality. Use the vanilla constructor.
  82. //MyGateway gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN, INCLUSION_MODE_TIME);
  83.  
  84. // Uncomment this constructor if you have leds and include button attached to your gateway
  85. MyGateway gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN, INCLUSION_MODE_TIME, INCLUSION_MODE_PIN, RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN);
  86.  
  87.  
  88. char inputString[MAX_RECEIVE_LENGTH] = "";    // A string to hold incoming commands from serial/ethernet interface
  89. int inputPos = 0;
  90.  
  91. void setup()
  92. {
  93.     // Initialize gateway at maximum PA level, channel 70 and callback for write operations
  94.     gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet);
  95.  
  96.     Ethernet.begin(mac, myIp);
  97.  
  98.     // give the Ethernet interface a second to initialize
  99.     delay(1000);
  100.  
  101.     // start listening for clients
  102.     server.begin();
  103.  
  104.     // C++ classes and interrupts really sucks. Need to attach interrupt
  105.     // outside thw Gateway class due to language shortcomings! Gah!
  106.  
  107.     if (gw.isLedMode()) {
  108.         // Add led timer interrupt
  109.         MsTimer2::set(300, ledTimersInterrupt);
  110.         MsTimer2::start();
  111.         // Add interrupt for inclustion button to pin
  112.         PCintPort::attachInterrupt(INCLUSION_MODE_PIN, startInclusionInterrupt, RISING);
  113.     }
  114. }
  115.  
  116. // This will be called when data should be written to ethernet
  117. void writeEthernet(char *writeBuffer) {
  118.     server.write(writeBuffer);
  119. }
  120.  
  121.  
  122. void processEthernetMessages()
  123. {
  124.     // if an incoming client connects, there will be
  125.     // bytes available to read via the client object
  126.     EthernetClient client = server.available();
  127.  
  128.     if (client)
  129.     {
  130.         // if got 1 or more bytes
  131.         if (client.available())
  132.         {
  133.             // read the bytes incoming from the client
  134.             char inChar = client.read();
  135.  
  136.             if (inputPos<MAX_RECEIVE_LENGTH - 1) {
  137.                 // if newline then command is complete
  138.                 if (inChar == '\n')
  139.                 {  // a command was issued by the client
  140.                     // we will now try to send it to the actuator
  141.                     inputString[inputPos] = 0;
  142.  
  143.                     // echo the string to the serial port
  144.                     Serial.print(inputString);
  145.  
  146.                     gw.parseAndSend(inputString);
  147.  
  148.                     // clear the string:
  149.                     inputPos = 0;
  150.                 }
  151.                 else
  152.                 {  // add it to the inputString:
  153.                     inputString[inputPos] = inChar;
  154.                     inputPos++;
  155.                 }
  156.             }
  157.             else {
  158.                 // Incoming message too long. Throw away
  159.                 inputPos = 0;
  160.             }
  161.         }
  162.     }
  163. }
  164.  
  165.  
  166.  
  167. void loop()
  168. {
  169.     // check for incoming commands from clients
  170.     processEthernetMessages();
  171.  
  172.     gw.processRadioMessage();
  173. }
  174.  
  175. void startInclusionInterrupt() {
  176.     gw.startInclusionInterrupt();
  177. }
  178.  
  179. void ledTimersInterrupt() {
  180.     gw.ledTimersInterrupt();
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement