#include <WiFi.h>
#include <WiFiClient.h>
// Configurações da rede Wi-Fi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Endereço IP e porta da câmera PTZ
const char* ptzIp = "192.168.1.100"; // Substitua pelo IP da sua câmera
const int ptzPort = 52381; // Porta padrão para VISCA over IP
WiFiClient client;
// Pinos do joystick
const int joyX = 34;
const int joyY = 35;
const int joystickButtonPin = 4;
// Pinos dos potenciômetros
const int potPan = 32;
const int potTilt = 33;
const int potZoom = 25;
// Pinos dos pushbuttons
const int pushButtons[10] = {12, 13, 14, 15, 16, 17, 18, 19, 21, 22};
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Conectando à rede Wi-Fi...");
}
Serial.println("Conectado à rede Wi-Fi");
// Configuração dos pushbuttons com resistores pull-up internos
for (int i = 0; i < 10; i++) {
pinMode(pushButtons[i], INPUT_PULLUP);
}
// Configuração do botão do joystick com resistor pull-up interno
pinMode(joystickButtonPin, INPUT_PULLUP);
// Configuração dos pinos analógicos para os potenciômetros e joystick
pinMode(joyX, INPUT);
pinMode(joyY, INPUT);
pinMode(potPan, INPUT);
pinMode(potTilt, INPUT);
pinMode(potZoom, INPUT);
}
void loop() {
// if (!client.connected()) {
// Serial.println("Conectando à câmera PTZ...");
// if (client.connect(ptzIp, ptzPort)) {
// Serial.println("Conectado à câmera PTZ");
// } else {
// Serial.println("Falha na conexão");
// delay(1000);
// return;
// }
// }
int xValue = analogRead(joyX);
int yValue = analogRead(joyY);
int panSpeed = analogRead(potPan) / 16;
int tiltSpeed = analogRead(potTilt) / 16;
int zoomSpeed = analogRead(potZoom) / 16;
// Calcular comandos VISCA
int panDirection = (xValue > 2048) ? 1 : (xValue < 2048) ? 2 : 0; // 1=direita, 2=esquerda, 0=parado
int tiltDirection = (yValue > 2048) ? 1 : (yValue < 2048) ? 2 : 0; // 1=cima, 2=baixo, 0=parado
// Comando VISCA para pan e tilt
// byte viscaCommand[9] = {0x81, 0x01, 0x06, 0x01, panSpeed, tiltSpeed, panDirection, tiltDirection, 0xFF};
// client.write(viscaCommand, 9);
// Leitura dos valores dos pushbuttons
for (int i = 0; i < 10; i++) {
int buttonState = digitalRead(pushButtons[i]);
if (buttonState == LOW) { // Botão pressionado
Serial.print("Pushbutton ");
Serial.print(i + 1);
Serial.println(" pressionado");
// Adicione aqui a lógica para cada pushbutton, como mudar cenas
}
}
// Pequeno atraso para evitar leituras excessivas
delay(100);
}