mirror of
https://github.com/nick7ass/NNMDETGroupProject.git
synced 2025-04-14 09:50:35 +02:00
Preps for Water sensor connection
This commit is contained in:
parent
6a8c461f41
commit
3c13ab8bc5
BIN
Assets/.DS_Store
vendored
BIN
Assets/.DS_Store
vendored
Binary file not shown.
@ -75,6 +75,8 @@ public class ConnectUnityWithSensors : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
|
91
Assets/1OurScripts/WaterConnectUnityWithSensors.cs
Normal file
91
Assets/1OurScripts/WaterConnectUnityWithSensors.cs
Normal file
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}*/
|
||||
|
11
Assets/1OurScripts/WaterConnectUnityWithSensors.cs.meta
Normal file
11
Assets/1OurScripts/WaterConnectUnityWithSensors.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4d772bb92ee174a46b45955222620b58
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
x
Reference in New Issue
Block a user