Preps for Water sensor connection

This commit is contained in:
Nicklas 2024-03-07 18:17:12 +01:00
parent 6a8c461f41
commit 3c13ab8bc5
4 changed files with 104 additions and 0 deletions

BIN
Assets/.DS_Store vendored

Binary file not shown.

@ -75,6 +75,8 @@ public class ConnectUnityWithSensors : MonoBehaviour
}
}
}
void OnDestroy()

@ -0,0 +1,91 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using WebSocketSharp; // Ensure this matches the WebSocket library you're using
public class WaterConnectUnityWithSensors : MonoBehaviour
{
// Websocket Service
WebSocket ws;
//public AudioSource audioSource; // Assign in inspector
//public AudioClip narrationClip; // Assign in inspector
//
public string esp32IPAddress = "10.204.0.249"; // Assign your ESP32 IP Address
public string esp32WebsocketPort = "81"; // Assign your ESP32 WebSocket port, typically "81"
private bool distanceDataReceived = false;
private int receivedDistanceValue = 0;
public static bool isDistanceDetected = false;
//Change to waterpublic BoundEarthScript earthScript = new BoundEarthScript();
void Start()
{
ConnectWithESP32();
}
public void ConnectWithESP32()
{
Debug.Log("Connecting Unity with ESP32 via Websockets...");
ws = new WebSocket($"ws://{esp32IPAddress}:{esp32WebsocketPort}");
ws.OnOpen += (sender, e) =>
{
Debug.Log("WebSocket connected");
ws.Send("Hello from Unity Water Script!");
};
ws.OnMessage += (sender, e) =>
{
Debug.Log("Received message: " + e.Data);
int parsedValue;
bool isNumeric = int.TryParse(e.Data, out parsedValue);
if (isNumeric)
{
receivedDistanceValue = parsedValue;
distanceDataReceived = true; // Indicate that new data has been received
}
};
ws.Connect();
Debug.Log("Websocket state - " + ws.ReadyState);
}
void Update()
{//Change to Water script
/*if (earthScript.narrationHasFinished && !earthScript.seedHasAppeared)
{
Debug.Log("Asking for distance.");
ws.Send("Need Distance");
if (distanceDataReceived)
{
if (receivedDistanceValue > 900)
{
Debug.Log("Distance threshold exceeded, action triggered.");
isDistanceDetected = true;
//earthScript.collectForce();
}
distanceDataReceived = false; // Reset for the next message
}*/
}
}
/* void OnDestroy()
{
if (ws != null && ws.IsAlive)
{
ws.Close();
}
}
}*/

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4d772bb92ee174a46b45955222620b58
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: