Transfer integer value from WeMos D1 Wifi to PC

I have a WeMos D1 Uno type board with builtin ESP8266 Wi-Fi . I will like to transfer an integer value from the board to a PC running a C# application with data transfer over Wi-Fi. A mobile phones hotspot is used for the Access point. For starters i need to get the correct sketch going before i can even start to work on the C# application. I am not sure if my approach is correct. Here is my sketch. It fails to connect to the pc.

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>


ESP8266WiFiMulti WiFiMulti;

//SSID and Password of your Wi-Fi router access point

const char* ssid="my ssid on mobile device";
const char* password="mobiledevicepassword";
const char* host ="xxx.xxx.xx.xxx";//IP Address of pc or laptop
const uint16_t port = 8080;


int sensorVal;
int sensorPin =4;
int numhits=0;
bool triggerOn=false;

WiFiClient client;//Use for TCP connections

void setup(){
  pinMode(sensorPin,INPUT);
  Serial.begin(115200);
  
  //Connect to a WiFi network Access Point
  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP(ssid, password);

  Serial.println();
  Serial.print("Wait for WiFi... ");
  
  //Wait for connection
  while (WiFiMulti.run() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  
  //If connection successful show IP Address in serial monitor
  Serial.println("");
  Serial.println("WiFi Connected:");
  Serial.println(ssid);
  Serial.print("IP Address:");
  Serial.println(WiFi.localIP());
  
  //Connect to our host
  if(!client.connect(host,port)){
  
    Serial.println("connection failed");
    Serial.println("wait 5 sec...");
    delay(5000);
    return;
  }

}


void loop(){

  sensorVal=digitalRead(sensorPin);

  if((sensorVal==HIGH) &&(triggerOn==false))
  {
    triggerOn=true;
    numhits++;
    Serial.println(numhits);//Write to the serial port

    if(!client.connect(host,port))
    {
      Serial.println("Connection failed cannot write data");
    }
    else{
      client.println(numhits);//Write this data to the server
    }    
  }
  else if((sensorVal==LOW) &&(triggerOn==false))
  {
    triggerOn=false;
  }

}
1 Like

Hi Chaven,

There may be more than one way to do what you are trying to do, but here are the things that I think you should look into.

Firstly, you are trying to transmit from your ESP module to your PC via your phone. Is this necessary?
Why not just connect directly to the PC from the ESP module?

Is there a reason you are using ESP8266WiFiMulti?
You are only connecting to a single access point?

There is an assumption that your PC is connected to a WiFi Router.
Connect to that WiFi router and once connected, you should be able to establish a connection to the Server on the PC (providing the PC is on the same network as the WiFi Router).

No need to get your phone involved.

Regards
Scott C

Hi Thanks for your suggestions.

NB i don't have a separate router and am using the Access point of my mobile device. I've redone the code. The ESP8266 connects to the Access point on the mobile device as expected. However i am struggling to get the connection to the PC to write the data across. The client.connect(host,port) is the issue. Now ive tried to get the IP address and port for my pc by running a command prompt ipconfig/all and netstat -a. I used the iPv4 Address under Wireless LAN Adapter Wifi. This didnt work. I also tried to create a Wireless hotspot at admin command prompt using my PC without using my mobile device.

netsh wlan set hostednetwork mode=allow ssid="myssid" key="mypassword"

netsh wlan start hostednetwork

I extracted this iPv4 Address and port. Still no success!!! .Am i doing the right thing in obtaining the host address and port number of the pc?

#include <ESP8266WiFi.h>

//SSID and Password of your Wi-Fi router access point

const char* ssid="*******";
const char* password="*******";
const char* host ="***.***.***.***";//IP Address of pc or laptop
const uint16_t port = 139;


int sensorVal;
int sensorPin =4;
int numhits=0;
bool triggerOn=false;

WiFiClient client;//Use for TCP connections

void setup(){
  pinMode(sensorPin,INPUT);
  Serial.begin(115200);
  

WiFi.begin(ssid,password);

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


//If connection successful show IP Address in serial monitor on Arduino
Serial.println("");
Serial.print("Connected, IP Address");
Serial.println(WiFi.localIP());

  //Connect to our host
  if(!client.connect(host,port)){
  
    Serial.println("connection failed");
    Serial.println("wait 5 sec...");
    delay(5000);
    return;
  }

}


void loop(){

  sensorVal=digitalRead(sensorPin);

  if((sensorVal==HIGH) &&(triggerOn==false))
  {
    triggerOn=true;
    numhits++;
    Serial.println(numhits);//Write to the serial port

    if(!client.connect(host,port))
    {
      Serial.println("Connection failed cannot write data");
    }
    else{
      client.println(numhits);//Write this data to the server
    }    
  }
  else if((sensorVal==LOW) &&(triggerOn==false))
  {
    triggerOn=false;
  }

}

Chaven,

Just to be clear - from what you have said, it seems like you are trying to transmit data from your ESP8266 to your PC via a mobile hotspot. So you have the ESP8266 and your PC both connected to your mobile hotspot. I am assuming that your PC has WiFi capabilities ??

This is what you are attempting to do:

ESP8266 ------- Mobile -----------PC

==============================================

Here is my suggestion:

ESP8266 ----------PC

Connect your PC directly to the ESP8266 and eliminate your mobile phone completely from the equation.
Set up your ESP8266 as an access point, and then get your computer to connect to that access point.

Once your computer is connected, you should be able to establish server-client communication between the two.

No need for your mobile phone to get involved.

Hi

My laptop has Wi-Fi capabilities for connecting to my mobile phone which i use to connect to the internet. It does not have a visible hot-spot feature (Using Windows Eight). I have configured a hot-spot manually as i've illustrated which shows up on my mobile phone when doing a Wi-Fi scan. However it wouldn't release its IP address.

I am aware that the ESP8266 can be used as an Access Point. However i have found the connection is not stable and breaks up more frequently. I prefer to rather have it connect to an access point like my mobile device which is more stable. In principle if two devices are connected to a network then the ESP8266 should be able to connect to the PC and vice versa. I am clearly missing out something here and i suspect it either has to do with

a) The port and IP address i am using is incorrect.
b) My interpretation of using the client.connect(host,port) is incorrect as host may only apply to website URLs.
c) In order for the ESP8266 to connect it also requires acceptance from the server side. So the server needs to be running first before i run the client

Is their a standard port number commonly assigned for Wi-Fi communication?

What is the correct procedure to obtain the host address for a PC and not a website ?

NB in C# if i run the following code

IPHostEntry ipHostEntry = new IPHostEntry();
            ipHostEntry = Dns.GetHostEntry("MYLAPTOPNAME");

IPAddress[] ipaddress = ipHostEntry.AddressList;

            

            foreach (IPAddress address in ipaddress)
            {
                Console.WriteLine(address.ToString());
            }

I get two different IP addresses generated if the code is executed with an without Wi-Fi connected via my mobile device access point. My understanding is that each machine should have a unique IP address.

My guess is that your ESP8266 and your computer are on two different networks.
Your WiFi hotspot on your phone should be indicating 2 connections - one from the ESP8266 and the other from your computer. Is that the case ?

Is your computer accessing any other networks (ie. via LAN ??).. which might be over-riding your attempts to use WiFi ??

I don't understand why the connection between the ESP8266 and your PC would be better by introducing an extra point of access, unless the physical distance between your PC and the ESP8266 is large. The phone could be used a stepping stone I guess... but if the physical distance is close, then that does not make sense to me.

There are only two connections registered on my phone (The Laptop & the ESP8266).

I've tried troubleshooting by focusing on two devices the ESP8266 and the mobile device as i already have a Wi-Fi connection. This time i use the mobile device IP Address as host obtained as follows:

For a mobile Android device
Go to "Settings"
Scroll down to "About device"
Select "Status" and scroll down to the IP Address

However if i try client.connect(host,port) it fails. I tried using port 80. Clearly this is not the right port no.

I haven't the faintest idea what is the correct port on the Android device nor how to obtain this.

Any suggestions?

After connecting your PC and ESP8266 to the WiFi hotspot:

  • What IP address are you getting on your ESP8266?
  • What IP address are you getting on your PC?
  • What server are you using on your PC or phone ?
  • What port have you set up to listen for Clients on that server[\li]

-When i connect the ESP8266 and laptop to the mobile device hotspot it shows both connected devices.

-The hotspot gives info for the MAC address and IP address for each connected device but no port number. Both IP Addresses are different.

-The IP address print out from the Arduino serial monitor matches the IP address on the mobile device.

-The IP address for the PC matches the iPv4 address i pick up in Windows Network Connection details.

-If the mobile phone was the server side i used its IP Address i pulled off the phone.

-If the pc was the server side i used the iPv4 address.

So in total there are three unique IP address for ESP8266--->Mobile Phone---> Laptop , configuration

Or two IP Address for ESP8266 ----->Mobile Phone, configuration

For the port number i opened a command prompt typed netstat -a and pulled up the matching IP address for the laptop as above. There were six TCP "local address" connections with the same IP but each having different port numbers. I had used 139 its "State" was set on listening and its "Foreign Address" was the name of my laptop.

Currently there is no server side script running as per my original question.

Ok - let me ask this a different way.

Let us assume that the ESP8266 has an IP address of AAA.BBB.CCC.1
Does the PC have an address that is like: AAA.BBB.CCC.2 ? Or is it completely different from the ESP8266?
eg. XXX.YYY.ZZZ.45

Secondly,
There are two different types of connections happening on your ESP8266.
The first type is a WiFi connection. And it seems like you have managed to get that sorted.

The next type of connection is a Client - Server connection.
For the client to connect to a server, you need to have a server running.
The server will open a port for clients to connect to.

The client cannot connect to the server unless it knows where it is and what port to connect to.
When you run a server on the PC (which has an IP address of AAA.BBB.CCC.2), you will need to tell your ESP8266's Client where the server is located.

So you tell the Client to first find the PC at AAA.BBB.CCC.2, and to connect to whatever port you set up on your server.

The port does not come from the PC, the phone or the ESP8266. It is determined by the server running on that device. If you don't have a server running, then there is nothing the client can connect to.

If you want to send data from the ESP8266 to the PC.
It probably would be better to set up a server on the ESP8266, and then use a client on the PC to retrieve the data from the server.

It does not really matter which way you choose to do it.
There are pros and cons for either way.

The IP addresses are the same in the first three sequence of numbers and only differ in the last.

As i suspected the server also needs to running. So i have to write the server side software first.

NB Just some general info for other readers. I was able to run a ping command from the laptop to the ESP8266 over Wi-Fi using its IP address and it succeeded. So the laptop can see the ESP8266 over the network.