Advertisement
Guest User

LightwaveRF Arduino and UDP

a guest
Jan 7th, 2013
1,291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1.  
  2. #include <SPI.h>        
  3. #include <Ethernet.h>
  4. #include <EthernetUdp.h>
  5.  
  6. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  7. IPAddress arduinoIP(192, 168, 1, 120);  /* Set this to the IP address you wish to give to the arduino */
  8. IPAddress wifiLinkIP(255,255,255,255);
  9. unsigned int lwrfUDPTxPort = 9760;    
  10. EthernetUDP Udp;  
  11.  
  12. void setup()
  13. {
  14.   Ethernet.begin(mac,arduinoIP);
  15.   Udp.begin(lwrfUDPTxPort);
  16.   Serial.begin(57600);
  17. }
  18.  
  19. void loop()
  20. {
  21.   if (Serial.available())
  22.   {
  23.     char c = Serial.read();
  24.  
  25.     if ((c == '0') || (c == '1'))
  26.     {
  27.       lw_udp_sendOnOff_text(6,0,c-'0', (char*)"Arduino set", &c);
  28.       Serial.println("Switching Device State");
  29.     }
  30.     else if((c > '1') && (c <= '9'))
  31.     {
  32.       lw_udp_sendDIM(6 , 0, c-'0');
  33.     }
  34.   }
  35. }
  36.  
  37.  
  38. /* This is only necessary the first time the arduino is used. Once the
  39.  arduino is registered with the wifi link, the mac/ip will be remembered*/
  40. void lw_udp_register()
  41. {
  42.   char buffer [64];
  43.   Udp.beginPacket(wifiLinkIP, lwrfUDPTxPort);
  44.   (void)sprintf (buffer, "533, !R0D0F0|");
  45.  
  46.   Udp.write(buffer);
  47.  
  48.   Udp.endPacket();
  49.  
  50. }
  51.  
  52. /* This sends the On/Off message to the specified device */
  53. void lw_udp_sendOnOff(int room , int device, bool switchOn)
  54. {
  55.   char buffer [64];
  56.   Udp.beginPacket(wifiLinkIP, lwrfUDPTxPort);
  57.   (void)sprintf (buffer, "000, !R%uD%uF%u|", room, device, switchOn);
  58.   Udp.write(buffer);
  59.   Udp.endPacket();
  60. }
  61.  
  62. /* This sends the On/Off message to the specified device ,with two lines of text printed on the wifilink screen*/
  63. void lw_udp_sendOnOff_text(int room , int device, bool switchOn, char* line1, char* line2)
  64. {
  65.   char buffer [64];
  66.   Udp.beginPacket(wifiLinkIP, lwrfUDPTxPort);
  67.   (void)sprintf (buffer, "000, !R%uD%uF%u|%s|%s", room, device, switchOn,line1, line2 );
  68.   Udp.write(buffer);
  69.   Udp.endPacket();
  70. }
  71.  
  72. /* This sends the On/Off message to the specified device */
  73. void lw_udp_sendDIM(uint8_t room , uint8_t device, uint8_t dim)
  74. {
  75.   if(dim <= 32)
  76.   {
  77.     char buffer [64];
  78.     Udp.beginPacket(wifiLinkIP, lwrfUDPTxPort);
  79.  
  80.     Serial.println("Dimming light");
  81.     (void)sprintf (buffer, "000, !R%uD%uFdP%u|", room, device, dim);
  82.     Udp.write(buffer);
  83.     Udp.endPacket();
  84.   }
  85.   else
  86.   {
  87.     Serial.println("Invalid dim value");
  88.   }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement