-->
Page 2 of 2

Re: How ESP8266 send and receive data with Winsock

PostPosted: Sun Jun 06, 2021 4:58 am
by shiningthing
JurajA wrote:does the other side act as HTTP server?

https://arduino.stackexchange.com/quest ... 6899#66899



I don't think it works with HTTP. I've tried this code and winsock managed to send data to ESP8266. Do you have any idea to send data from ESP8266 to winsock (reverse direction)?

Code: Select all#include <ESP8266WiFi.h>
#define RX 3

const char* ssid = "XXX"; //SSID Access Point
const char* password = "123"; //Password Access Point
const char* host = "192.168.100.10"; //IP PC

//Client-ESP8266 IP Address
IPAddress IP(192,168,100,220);
IPAddress NETMASK(255,255,255,0);
IPAddress NETWORK(192,168,100,1);
IPAddress DNS(192,168,100,1);

WiFiServer server(180);
String sEncoded="";
String line="";
int bytes[256];

void setup() {
  Serial.begin(9600);
  delay(1);
 
   // Connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 

  server.begin();
  Serial.println("Server started");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
   
   WiFiClient client = server.available();
   Serial.printf("\n[Connecting to %s ...", host);
   if (client.connect(host, 180)){   
      Serial.println("connected]");
      while (client.connected() || client.available()){
        if (client.available()){
          line = client.readStringUntil('\n');
          Serial.println(line);
          }
         }
       //convert ascii to binary (not succeeded)
        for (int i=0; i<line.length(); i++){
          char character = line.charAt(i);

          for (int j=7; j>=0; j--){
            bytes[i]=bitRead(character,i);
            Serial.print(bytes[i]);
            if(bytes[i]==1){
              digitalWrite(RX, HIGH);
            }
            else{
              digitalWrite(RX,LOW);
            }
            delay(50);
       }
   }
      client.stop();
      Serial.println("\n[Disconnected]");
   }
   else {
          Serial.println("connection failed!]");
          client.stop();
        }
    delay(1000);
}

Re: How ESP8266 send and receive data with Winsock

PostPosted: Tue Jun 08, 2021 12:34 am
by JurajA