Lab_interaccio/2024/RPC_test_VS/include/main_m7.cpp

264 lines
7.4 KiB
C++
Raw Normal View History

2025-02-25 21:29:42 +01:00
//#include <SPI.h>
#include <WiFi.h>
#include <ArduinoJson.h>
#include "interface.h"
#include "constants.h"
#include <RPC.h>
#include "SerialRPC.h"
// Inicializando RTOS
using namespace rtos;
Thread sensorThread;
// Configuración de red WiFi
const char *ssid = "Analogue_Hyperlapse_Camera";
const char *password = "CraterLab";
// Crear el servidor en el puerto 80
WiFiServer server(80);
// Motor
int motorSpeedValue = 25;
bool motorIsSpeedActive = false;
int motorIntervalFrames = 1;
int motorIntervalSeconds = 1;
bool motorIsIntervalActive = false;
int motorDirection = 1;
// Shutter
int shutterFadeFrames = 50;
bool shutterSyncWithInterval = false;
bool shutterFadeInActive = false;
bool shutterFadeOutActive = false;
// Óptica
int zoomValue = 50;
int focusValue = 50;
float diaphragmValue = 1.8;
bool syncWithIntervalOptics = false;
// Dispositivo 360
int x0Degrees = 0;
int x0Duration = 0;
int x1Degrees = 0;
int x1Duration = 0;
int y0Degrees = 0;
int y0Duration = 0;
bool syncWithInterval360 = false;
int motorSpeedRead = 0;
void requestReading() {
while (true) {
delay(25);
motorSpeedRead = RPC.call("fpsRead").as<int>();
}
}
void setup()
{
Serial.begin(115200);
delay(5000);
if (!SerialRPC.begin()) {
Serial.println("RPC initialization fail");
}
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.println("Conectado a WiFi!");
Serial.print("Dirección IP: ");
Serial.println(WiFi.localIP());
server.begin();
//sensorThread.start(requestReading);
}
void handlePostRequest(String body) {
// Parseamos el cuerpo del JSON
StaticJsonDocument<500> doc;
DeserializationError error = deserializeJson(doc, body);
if (error) {
Serial.println("Error al parsear el JSON");
return;
}
Serial.println("Procesando JSON...");
String type = doc["type"];
if (type=="motor")
{
Serial.println("MOTOR");
motorSpeedValue = doc["speedValue"];
motorIsSpeedActive = doc["isSpeedActive"];
motorIntervalFrames = doc["intervalFrames"];
motorIntervalSeconds = doc["intervalSeconds"];
motorIsIntervalActive = doc["isIntervalActive"];
String Direction = doc["direction"];
if (Direction=="forward") motorDirection = FORWARD;
else if (Direction=="backward") motorDirection = BACKWARD;
if(motorIsSpeedActive)
{
SerialRPC.println("/s_motor:4," + String(motorDirection) + "," + String(motorSpeedValue));
}
else if(motorIsIntervalActive)
{
SerialRPC.println("/i_motor:4," + String(motorDirection) + "," + String(motorIntervalFrames) + "," + String(motorIntervalSeconds));
}
/*Clave: type, Valor: motor
Clave: speedValue, Valor: )25
Clave: isSpeedActive, Valor: false
Clave: intervalFrames, Valor: 1
Clave: intervalSeconds, Valor: 1
Clave: isIntervalActive, Valor: false
Clave: direction, Valor: forward*/
}
else if (type=="shutter")
{
Serial.println("Shutter");
shutterFadeFrames = doc["fadeFrames"];
shutterSyncWithInterval = doc["syncWithInterval"];
shutterFadeInActive = doc["fadeInActive"];
shutterFadeOutActive = doc["fadeOutActive"];
SerialRPC.println("/fade:" + String(shutterFadeFrames) + "," + String(shutterSyncWithInterval) + "," + String(shutterFadeInActive) + "," + String(shutterFadeOutActive));
/*Clave: type, Valor: shutter
Clave: fadeFrames, Valor: 50
Clave: syncWithInterval, Valor: false
Clave: fadeInActive, Valor: false
Clave: fadeOutActive, Valor: false*/
}
else if (type=="optics")
{
Serial.println("Optics");
zoomValue = doc["zoomValue"];
focusValue = doc["focusValue"];
diaphragmValue = doc["diaphragmValue"];
syncWithIntervalOptics = doc["syncWithInterval"];
SerialRPC.println("/optics:" + String(zoomValue) + "," + String(focusValue) + "," + String(diaphragmValue) + "," + String(syncWithIntervalOptics));
/*Clave: type, Valor: optics
Clave: zoomValue, Valor: 50
Clave: focusValue, Valor: 50
Clave: diaphragmValue, Valor: 1.9
Clave: syncWithInterval, Valor: false*/
}
else if (type=="360")
{
Serial.println("360");
String axis = doc["motor"];
if (axis=="x0")
{
x0Degrees = doc["degrees"];
x0Duration = doc["duration"];
}
else if (axis=="x1")
{
x1Degrees = doc["degrees"];
x1Duration = doc["duration"];
}
else if (axis=="y0")
{
y0Degrees = doc["degrees"];
y0Duration = doc["duration"];
}
syncWithInterval360 = doc["syncWithInterval"];
/*Clave: type, Valor: 360
Clave: motor, Valor: x0, x1, y0
Clave: degrees, Valor:
Clave: duration, Valor:
Clave: syncWithInterval, Valor: false*/
}
else if (type=="accion") Serial.println("Accion");
else if (type=="corten") Serial.println("Corten");
else Serial.println("No reconocido");
// Recorrer todos los pares clave-valor en el JSON
/*for (JsonPair kv : doc.as<JsonObject>()) {
Serial.print("Clave: ");
Serial.print(kv.key().c_str());
Serial.print(", Valor: ");
// Si el valor es un objeto, lo imprimimos como cadena
if (kv.value().is<JsonObject>()) {
Serial.println("Objeto anidado (no mostrado)");
}
else {
Serial.println(kv.value().as<String>());
}
}
Serial.println("Procesamiento de JSON completado.");*/
}
//RPC.println("/mode:"+ String(mode));
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("Nuevo cliente conectado");
String request = "";
bool isPostRequest = false;
while (client.connected()) {
if (client.available()) {
char c = client.read();
request += c;
if (request.indexOf("POST") >= 0) {
isPostRequest = true;
}
if (c == '\n' && request.endsWith("\r\n\r\n")) {
Serial.println("Solicitud recibida:");
Serial.println(request);
if (isPostRequest) {
String body = "";
while (client.available()) {
body += (char)client.read();
}
Serial.println("Cuerpo del mensaje recibido:");
Serial.println(body);
// Llamamos a la función genérica para procesar la petición POST
handlePostRequest(body);
// Respuesta al cliente
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/plain");
client.println();
client.println("Datos enviados correctamente");
} else {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.print(htmlTemplate);
}
break;
}
}
}
client.stop();
Serial.println("Cliente desconectado");
}
/*String buffer = "";
while (RPC.available()) {
buffer += (char)RPC.read(); // Fill the buffer with characters
}
if (buffer.length() > 0) {
Serial.print(buffer);
}*/
}