Go Down

Topic: How to connect PC and Adafruit Huzzah with ESP8266 over wifi and communicate ? (Read 136 times) previous topic - next topic

ramprasadp123

I have a requirement where I need to send data from my PC to my adafruit HUZZAH with ESP8266 and acknowledge the reception of data back to my PC. I'm new to networking and arduino. I understand I need to use the client server model to establish the connection over WiFi, but I'm not sure exactly as to how. Could someone please throw some light on how to get started with this ? Thanks!

Juraj

You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

ramprasadp123

hi thanks for the help! I would also like to know if I can use the boost asio library for the tcp connection from my pc side ? Thnking you in advance!

Juraj

hi thanks for the help! I would also like to know if I can use the boost asio library for the tcp connection from my pc side ? Thnking you in advance!
I use Java on PC. But it is basic TCP. Create a socket by connecting it to IP and port and write to it, read from it.
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

ramprasadp123

Hello,

I am trying to run my tcp server on pc written in python and the esp8266 tcp client on the arduino using the ide, unfortunately I'm not able to establish the connection between the two, could someone help me if there is a mistake in the written codes below:

TCP Server Python:

'''
    Simple socket server using threads
'''
 
import socket
import sys
 
HOST = '192.168.0.105'   # Symbolic name, meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port
 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ('Socket created')
 
#Bind socket to local host and port
try:
    s.bind((HOST, PORT))
except socket.error as msg:
    print ('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
    sys.exit()
     
print ('Socket bind complete')
 
#Start listening on socket
s.listen(10)
print ('Socket now listening')
 
#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print ('Connected with ' + addr[0] + ':' + str(addr[1]))
     
s.close()


TCP Client Arduino IDE:

/*
    This sketch establishes a TCP connection to a "quote of the day" service.
    It sends a "hello" message, and then prints received data.
*/

#include <ESP8266WiFi.h>

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

const char* host = "192.168.0.105";
const uint16_t port = 8888;

void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
     would try to act as both a client and an access-point and could cause
     network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  Serial.print("connecting to ");
  Serial.print(host);
  Serial.print(':');
  Serial.println(port);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    delay(5000);
    return;
  }

  // This will send a string to the server
  Serial.println("sending data to server");
  client.println("hello from ESP8266");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      delay(60000);
      return;
    }
  }

  // Read all the lines of the reply from server and print them to Serial
  Serial.println("receiving from remote server");
  while (client.available()) {
    char ch = static_cast<char>(client.read());
    Serial.print(ch);
  }

  // Close the connection
  Serial.println();
  Serial.println("closing connection");
  client.stop();

  delay(300000); // execute once every 5 minutes, don't flood remote service
}

Thanking in advance!

Juraj

try to disable Firewall on PC to test
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

ramprasadp123


Juraj

thank you very much, it works without firewall :)
don't let the firewall disabled. add a rule to let 8888 thru
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Go Up