From 3c13ab8bc56522c18cc18e1a1ea8008a160726a1 Mon Sep 17 00:00:00 2001 From: Nicklas <145897592+nick7ass@users.noreply.github.com> Date: Thu, 7 Mar 2024 18:17:12 +0100 Subject: [PATCH] Preps for Water sensor connection --- Assets/.DS_Store | Bin 10244 -> 10244 bytes Assets/1OurScripts/ConnectUnityWithSensors.cs | 2 + .../WaterConnectUnityWithSensors.cs | 91 ++++++++++++++++++ .../WaterConnectUnityWithSensors.cs.meta | 11 +++ 4 files changed, 104 insertions(+) create mode 100644 Assets/1OurScripts/WaterConnectUnityWithSensors.cs create mode 100644 Assets/1OurScripts/WaterConnectUnityWithSensors.cs.meta diff --git a/Assets/.DS_Store b/Assets/.DS_Store index 9b88503940160ec3c7daa0160a55e7b47be44c48..312979572f7e3e740d841fc482e5f557dcb2a91a 100644 GIT binary patch delta 62 zcmZn(XbG6$&uF$WU^hRb*<>C8qsd944I)Nn209AHMkcj73e}cIK(>jQ$>eXM#+&&C RnD{rdEBs>F%q_yq3;;PL5b6K` delta 40 wcmZn(XbG6$&uF?aU^hRb>0}-Oqs=CQm-!|(eA~>f@QY=#uekDNDX|Ak03?wPHvj+t 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: