0

I am using React Native with socket.io to send data to Arduino using sockets. I'm just getting confused because my arduino prints this string as output (instead of "hello world")

GET /socket.io/?EIO=3&transport=polling&t=N3MDU9z HTTP/1.1
accept: */*
Host: 192.168.1.109
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.12.1

I have this code running on my ESP8266 to receive and print a string from the client

#include "ESP8266WiFi.h"

const char* ssid = "SSID";
const char* password =  "PASSWORD";

WiFiServer wifiServer(80);

void setup() {

  Serial.begin(115200);

  delay(1000);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting..");
  }

  Serial.print("Connected to WiFi. IP:");
  Serial.println(WiFi.localIP());

  wifiServer.begin();
}

void loop() {

  WiFiClient client = wifiServer.available();

  if (client) {

    while (client.connected()) {

      while (client.available()>0) {
        char c = client.read();
        Serial.write(c);
      }

      delay(10);
    }

    client.stop();
    Serial.println("Client disconnected");

  }
}

and here is the code for the client (js)

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import io from 'socket.io-client';


export default class App extends React.Component{

  constructor(props) {
    super(props);
  }
  componentDidMount() {
    const socket = io("http://192.168.1.109:80");
    socket.emit("message","hello world");
  }

  render(){
    return(
      <View style={styles.container}>
        <Text>Hello</Text>
      </View>
    )
  }
}

Does anyone have any advice?

2 Answers2

1

Your React code is using the HTTP protocol to open a connection to the ESP8266.

The ESP8266 WiFiClient class is a raw TCP client, not an HTTP server. So when you print what it receives, you're printing the HTTP protocol lines that your React code sent. That's why you see what you're seeing.

In other words, socket.io uses a protocol built on top of HTTP, which is built on top of TCP. Your code is just using TCP, so it's just seeing HTTP. It's not responding to the HTTP messages and will never see the data you're trying to send because you're not speaking the protocol.

If you really want to use socket.io (why?) then you'll need to find a socket.io library for the ESP8266 and use that. Or you'll need to implement the socket.io protocol yourself. There's one here but I'm not sure it will do what you need.

socket.io is an unfortunately named piece of software. In other areas of computing a "socket" would more often refer to a raw TCP connection; they chose to confusingly reuse the name for a protocol built a couple of layers above TCP. A TCP "socket" under Linux is not the same thing as a socket.io "socket". A TCP "socket" under Linux is the same thing as an ESP8266 WiFiClient (which is also unfortunately named, as it is not specific to WiFi).

romkey
  • 6,218
  • 3
  • 16
  • 12
0
componentDidMount() {
    const socket = io("http://192.168.1.109:80");
    socket.emit("message","hello world");
}

well you're trying to open a ws connection using HTTP

componentDidMount() {
    const socket = io("ws://192.168.1.109:80");
    socket.emit("message","hello world");
}

this should work

LucidMach
  • 1
  • 1