diff --git a/Assets/.DS_Store b/Assets/.DS_Store index 9b88503..3129795 100644 Binary files a/Assets/.DS_Store and b/Assets/.DS_Store differ diff --git a/Assets/1OurScripts/ConnectUnityWithSensors.cs b/Assets/1OurScripts/ConnectUnityWithSensors.cs index a16d6fa..f2e8434 100644 --- a/Assets/1OurScripts/ConnectUnityWithSensors.cs +++ b/Assets/1OurScripts/ConnectUnityWithSensors.cs @@ -75,6 +75,8 @@ public class ConnectUnityWithSensors : MonoBehaviour } } + + } void OnDestroy() diff --git a/Assets/1OurScripts/WaterConnectUnityWithSensors.cs b/Assets/1OurScripts/WaterConnectUnityWithSensors.cs new file mode 100644 index 0000000..4fff10e --- /dev/null +++ b/Assets/1OurScripts/WaterConnectUnityWithSensors.cs @@ -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(); + } + } + +}*/ + diff --git a/Assets/1OurScripts/WaterConnectUnityWithSensors.cs.meta b/Assets/1OurScripts/WaterConnectUnityWithSensors.cs.meta new file mode 100644 index 0000000..c0141fd --- /dev/null +++ b/Assets/1OurScripts/WaterConnectUnityWithSensors.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4d772bb92ee174a46b45955222620b58 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: