Arduino UDP Listener & TCP Post

Hi all,

I’m working on a bridge between my RAKOM RF lighting system at home, and openhab as the replacement controller.

My current gateway spits out UDP data on the network which I’m using the Arduino to parse and send item commands via TCP to the rest API on the server.

So far my results are that I get a data length of 7, which is expected, followed by my “Connected, posting data” message - but from interrogation it appears that the connection/post never seems to take place. The server does not record any connection in its log files.

Can anybody point me in the direction Im going wrong here?

Thanks!

Im running the following code - packetBuffer is an array of bytes read from UDP, 0x04 is room 04 for example, the string data to be posted is simply ‘ON’;

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEA, 0xFE, 0xAC
};
IPAddress ip(172, 16, 11, 89);

unsigned int localPort = 9761;      // local port to listen on
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  //buffer to hold incoming packet
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
EthernetClient client;
String data;

void setup() {
  Serial.begin(9600);

  // start the Ethernet and UDP:
  Serial.println("start ethernet");
  Ethernet.begin(mac, ip);
  Serial.println("starting UDP");
  Udp.begin(localPort);
  data="ON";

}

void loop() {
  // if there's rako data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.print("recieved data of size: ");
    Serial.println(packetSize);
      switch(packetSize){
        case 7:
          Serial.println("Fade command STOP / app command");
          Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
          if(packetBuffer[5]==0x0F){
            Serial.println("Fade command RX");
            //ignoring fade commands for now
          }
          else
          {
            //Not a fade command, must be a room app command
            
            if(packetBuffer[5]!=0x00){
              packetBuffer[5]=packetBuffer[5]-2;
            }

            if(client.connect("http://172.16.11.80","8080")){
            Serial.println("Connected to server, posting data");
            client.print("POST /rest/items/Room HTTP/1.0");
            client.print(packetBuffer[3],DEC);
            client.print("Scene");
            client.println(packetBuffer[5],DEC);
            client.println("Host: 172.16.11.89");
            client.println("Content-Type: text/plain");
            client.print("Content-Length: ");
            client.println(data.length());
            client.println();
            client.print(data);
            
            if(client.available())
            {
              char c = client.read();
              Serial.print("ServerResponse");
              Serial.println(c);
            }
            if(client.connected()){
  
              client.stop();
            }
          }
       
//          Serial.print("room is: ");
//          Serial.println(packetBuffer[3],DEC);
//          Serial.print("scene is: ");
//          Serial.println(packetBuffer[5],DEC);
//          Serial.println("command string will be post ON to: ");
//          Serial.print("http://172.16.11.80:8080/rest/items/Room");
//          Serial.print(packetBuffer[3],DEC);
//          Serial.print("Scene");
//          Serial.println(packetBuffer[5],DEC);

          }

          break;

        case 8:
        //Fade Commands , Ignore
        Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
          Serial.println("Fade command START");
          //byte 1 is hex 6 - FADE
          //byte 3 is room
          //
          break;

        case 9:
          Serial.println("Room Scene Data / direct dim");
          Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
          if(packetBuffer[5]==0x31){

          if(client.connect("http://172.16.11.80","8080")){
            client.print("POST /rest/items/Room");
            client.print(packetBuffer[3],DEC);
            client.print("Scene");
            client.println(packetBuffer[7],DEC);
            client.println("Host: 172.16.11.89");
            client.println("Content-Type: text/plain");
            client.print("Content-Length: ");
            client.println(data.length());
            client.println();
            client.print(data);
            if(client.connected()){
              client.stop();
            }
          }

            
          //byte 1 is hex 07 - SCENE
//          Serial.print("room is: ");
//          Serial.println(packetBuffer[3],DEC);
//          Serial.print("scene is: ");
//          Serial.println(packetBuffer[7],DEC);
//          Serial.println("command string will be post ON to: ");
//          Serial.print("http://172.16.11.80:8080/rest/items/Room");
//          Serial.print(packetBuffer[3],DEC);
//          Serial.print("Scene");
//          Serial.println(packetBuffer[7],DEC);
          //Serial.println(whatimgonnado);
          //byte 3 is room
          //byte 7 is scene
          }
          else
          {
            //Ignoring Direct Dim For The Moment
          }
          break;
        
      }


  }

}
// replace this
if(client.connect("http://172.16.11.80","8080")){


// with this
IPAddress serverIP(172,16,11,80);
int serverPort = 8080;

if(client.connect(serverIP, serverPort)){

legendary, that fixed it - thanks so much!!