Hex packets over Wifi : Send Receive Question

Hi,

I have a project creating a PTZ Joystick to control a PTZOptics camera.

Wifi is working fine using wifi.h, control HTTP-CGI commands work, camera moves.

However.....

In order to enquire where on the zoom scale the camera currently is, I can't use HTTP-CGI, I have to use VISCA over IP.

I need to send a hex packet 81 09 04 47 FF over TCP or UDP which will return a position packet of 90 50 0p 0q 0r 0s FF. For example 90 50 04 00 00 00 FF is fully zoomed in.

I've gone through the Wifi example code and examples on the net but I think I need pointing in the right direction as there are so many paths to do this and I'm getting confused...

Is there an obvious library to do this?

TIA

BSG

how this is done depends on the type of micro-controller that you are using.
So please post the exact name of microcontroller / your microcontroller-board.

best regards Stefan

Hi Stefan,

The board is an ESP32 based Lolin32 V1.0.0

Thanks

I did a quoogling
https://www.google.de/search?as_q=github+arduino+VISCA
best regards Stefan

Thanks Stefan,

Yes, I've seen a number of those before. That's the issue, that they are using the Arduino has a converter between serial PTZ connections and UDP, as a converter for their non IP enabled cameras.

I don't need that step as our camera is IP enabled. I can write a function and pass the hex values / command needed. It's the sending and listening for TCP / UDP hex that I'm stuck on.

Kind regards

BSG

sending UDP is a huge difference compared to sending TCP

https://www.google.de/search?q=arduino+esp32+send+%2F+receive+udp+democode

True, given the joystick would be sending a request to move 1 unit at a time, if one request was delivered out of order, or not delivered, the camera would only move if it received a good packet and not move if it didn't. I can also listen for a success VISCA over IP packet (if it receives a valid UDP packet) from the camera before sending another UDP packet.

Of the codebases that look promising, the logic of this one.
[visca-ip-proxy-arduino/visca-proxy-rs232.ino at main · utopiantools/visca-ip-proxy-arduino · GitHub]

He's creating a proxy for UDP to RS232 though. I don't need one.

There is a WifiUdp.h library that has written for ESP32 boards.
github com espressif arduino-esp32 blob master libraries WiFi src WiFiUdp.h
(I can't send more than two URLs.)

Of the WifiUdp.h library, in this example has a sender and listener. I'd need to make sure my routine pulls the hex from the holding array into the string.
[https://www.arduino.cc/en/Tutorial/WiFiSendReceiveUDPString]

Sounds feasible?

Best regards

BSG

me myself I was only using code similar to this simple udp-send-receive-demo

#include <DebugTxtVar.h>

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
  
// Set WiFi credentials
#define WIFI_SSID "TheOtherESP"
#define WIFI_PASS "flashmeifyoucan"

char buttonState = 1;
int packetsize  = 0;

// UDP Buffer
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
 
// UDP
WiFiUDP UDP;
IPAddress remote_IP(192,168,4,1);
#define UDP_PORT 4210

void PrintFileNameDateTime() {
  Serial.println("Code running comes from file ");
  Serial.println(__FILE__);
  Serial.print("  compiled ");
  Serial.print(__DATE__);
  Serial.print(" ");
  Serial.println(__TIME__);  
}


boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();  
  if ( currentMillis - periodStartTime >= TimePeriod )
  {
    periodStartTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  } 
  else return false;            // not expired
}

unsigned long MyTestTimer = 0;                   // variables MUST be of type unsigned long
const byte    OnBoard_LED = 2;


void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);
  
  if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
    digitalWrite(IO_Pin,!digitalRead(IO_Pin) ); 
  }
}

 
void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  PrintFileNameDateTime();
 
  // Begin WiFi
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  WiFi.mode(WIFI_STA);
  
  // Connecting to WiFi...
  Serial.print("Connecting to ");
  Serial.print(WIFI_SSID);
  // Loop continuously while WiFi is not connected
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(100);
    Serial.print(".");
  }
  
  // Connected to WiFi
  Serial.println();
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());
 
  // Begin UDP port
  UDP.begin(UDP_PORT);
  Serial.print("Opening UDP port ");
  Serial.println(UDP_PORT);
  
}
  
void loop() {
  BlinkHeartBeatLED(OnBoard_LED,100);

  if ( TimePeriodIsOver(MyTestTimer,2000) ) {
    if (buttonState == 1) {
      buttonState = 0;
      Serial.print("buttonState =");
      Serial.println(buttonState); 
    }
    else {
      buttonState = 1;      
      Serial.print("buttonState =");
      Serial.println(buttonState); 
    }
  // Send Packet
  UDP.beginPacket(remote_IP, UDP_PORT);
  UDP.write(buttonState);
  UDP.endPacket();
  Serial.println("UDP packed sended"); 
  delay(100);
  }  

  // Receive packet
  packetsize = UDP.parsePacket();
  UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
  
  if (packetsize){
    Serial.print("UDP-packet #");
    Serial.print(packetBuffer);
    Serial.print("# from IP");
    Serial.println(UDP.remoteIP() );
  }  
   
}

it is written for ESP8266 but the udp-part will be the same for ESP32

these are the commands for sending a string

  Udp.beginPacket(remoteIP, remotePort);
  Udp.write((const uint8_t*)UDP_Msg_SS.c_str(), UDP_Msg_SS.length() );
  Udp.endPacket();    

as you can see for using the udp.write-function the string has to be type-casted into uint8_t anyway. So sending a few bytes with hexadecimal values should be pretty straight forward

best regards Stefan