ESP8266 x Unity Game - Curl Error

Hi there,

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!

This is the NodeMCU code:

ESP8266WebServer server(80);
void setup() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {delay(1000); Serial.print(".");}
  server.on("/", serverHandler);
  server.begin();
}
void loop() {
  server.handleClient();
  buttonStatus = digitalRead(buttonPin);
  if (buttonStatus && !buttonLastStatus) buttonState = 1;
  else if (!buttonStatus && buttonLastStatus) buttonState = 0;
  buttonLastStatus = buttonStatus;
  delay(250);
}
void serverHandler() {
  server.send(200, "text/html", SendHTML(buttonState));
}
String SendHTML(uint8_t buttonCurrentState){
  String ptr;
  if(buttonCurrentState) ptr = "1";
  else ptr = "0";
  return ptr;
}

This is the Unity Game code:

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/"));
    }
}

No, that's only part of it. Please post all of it. Remember to "XXX" out any security credentials. Thanks for using code tags, by the way.

  buttonStatus = digitalRead(buttonPin);
  if (buttonStatus && !buttonLastStatus) buttonState = 1;
  else if (!buttonStatus && buttonLastStatus) buttonState = 0;
  buttonLastStatus = buttonStatus;

This seems rather confused and over complicated. It seems to me that it is equivalent to

  buttonState = digitalRead(buttonPin);

Can you please describe what you are trying to achieve?

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 :slight_smile:
Here's the full code of both:
NodeMCU:

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

/*Put your SSID & Password*/
const char* ssid = "xxx";  // Enter SSID here
const char* password = "xxx";  //Enter Password here

ESP8266WebServer server(80);

uint8_t ledPin = D1;
uint8_t buttonPin = D2;
uint8_t buttonStatus = 0, buttonLastStatus = 0;
uint8_t buttonState = 0;
void setup() 
{
  Serial.begin(9600);
  delay(100);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.println("Connecting to ");
  Serial.println(ssid);

  //connect to your local wi-fi network
  WiFi.begin(ssid, password);

  //check wi-fi is connected to wi-fi network
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected..!");
  Serial.print("Got IP: ");  
  Serial.println(WiFi.localIP());
  
  server.on("/", serverHandler);
  server.begin();
  Serial.println("HTTP server started");
}
void loop() 
{
  server.handleClient();
  buttonStatus = digitalRead(buttonPin);
  if (buttonStatus && !buttonLastStatus)
  {
    digitalWrite(ledPin, HIGH);
    Serial.println("Button Status: ON");
    buttonState = 1;
    
  }
  else if (!buttonStatus && buttonLastStatus)
  {
    digitalWrite(ledPin, LOW);
    Serial.println("Button Status: OFF");
    buttonState = 0;
  }
  buttonLastStatus = buttonStatus;
  delay(250);
}

void serverHandler() 
{
  server.send(200, "text/html", SendHTML(buttonState));
}

String SendHTML(uint8_t buttonCurrentState)
{
  String ptr;
  if(buttonCurrentState) ptr = "1";
  else ptr = "0";
  return ptr;
}

Unity Game code:

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!

1 Like

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!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.