I'm trying to send a button state from NodeMCU/ESP8266 to Unity via local web server, but I get some "Curl errors" and I don't know what to do about them, I'm pretty new to this stuff too. Please if you have a moment to look at it and give me a few tips it would mean the world to me!
Okay, my bad, sorry, before I list the full code, what I want to achieve is: send a string, with the button state, from nodemcu to unity and print it in the console. But I want to send a string to the web server only when the button state changes, and also when I request the string from the server in the Unity Game, I want to request only when there's a change. Like now I get the Curl Error with to many request or something, and I want to bypass that.. Thank you for your time, it really means a lot
Here's the full code of both:
NodeMCU:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System;
// UnityWebRequest.Get example
// Access a website and use UnityWebRequest.Get to download a page.
// Also try to download a non-existing page. Display the error.
public class web : MonoBehaviour
{
string webString;
void Start()
{
StartCoroutine(GetRequest("http://192.168.100.58/"));
}
IEnumerator GetRequest(string uri)
{
UnityWebRequest uwr = UnityWebRequest.Get(uri);
yield return uwr.SendWebRequest();
if (uwr.isNetworkError)
{
Debug.Log("Error While Sending: " + uwr.error);
}
else
{
webString = uwr.downloadHandler.text;
Debug.Log("Received: " + webString);
}
}
void Update()
{
StartCoroutine(GetRequest("http://192.168.100.58/"));
}
}
That's impossible in the way you are attempting to do it. In your setup, the Arduino is the web server and the Unity thing (I have no idea what that really is) is the client. The client sends a request to the server (the Arduino) which responds with the button value. The client cannot request only when the button state changes because it has no idea when that happens.
To do what you want I think you will need to have both the Arduino and Unity to act as both servers and clients. When Unity wants to request the button value, it can act as a client and send the request to the Arduino which will act as a server and return the value. This part you already have. When the button value changes, the Arduino will need to act as a client and send a request to Unity which will act as a server. The client, the Arduino, can pass the new value of the button as a parameter of the request.
My main concern about this idea is the slowness of http protocol. My experience has mostly been with remote internet servers. In your setup it looks like the Unity server is on the same local network as the Arduino, so maybe speed will be less of a problem. But with remote servers, I find that a request from an Arduino can take a few seconds to send and receive the response, and that might be too slow. By the time the request, updating the server that you pressed the button, has been performed, you will probably have let go of the button already!
So basically all I want is to get rid of the error in Unity, the rest works just fine.
The error is that Unity request data from the server too much. Since that curl error.
Maybe coroutines in the update function aren't the way to go in Unity.
Anyway thank you for your feedback and time!
Have a nice day!