Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Send data periodically as a server to my client in arduino ESP8226

Status
Not open for further replies.

matriX_1500

Junior Member level 1
Joined
May 16, 2019
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
158
Hi every One
I'm using NodeMCU board to communicate a laptop through WiFi. Basic communication is ok with a code like this:

Code:
   void loop (){
    WiFiClient client = server.available();
    
      if (client) {
        while (client.connected()){
           client.println(Data_mem[0]); 
           delay(2000);
        }
        client.stop();  // tarminates the connection with the client
       }
    }

But when i want to send data a Timer Tick, it seems the client couldn't connect to me.

Code:
    void setup(){
    ....
      //Initialize Ticker every 40ms
      Data_Rec.attach_ms(40, 40ms_Data );
    }

    void 40ms_Data (){
    WiFiClient client = server.available();
    
      Serial.println("40ms_Data A");
      if (client) {
          Serial.println("40ms_Data B");
        if (client .connected()){
           Serial.println("40ms_Data C");    
           client.println(40ms_Data [0]); 
        }
        else{
           client.stop(); 
        }
       }
    }
I arduino serial monitor, i see only this:
40ms_Data A \r\n
40ms_Data A \r\n
....
So, could any one help me? maybe it a problem of WiFiClient deceleration in a non-forever loop (like example 1). But i have no idea to fix it.
Thanks
 

Hi,

Most popular in client-server communication is that the client initiates the comunication. The server just responds.
The server usually does not send data without dedicated request by the client.

But it should be possible. But the client needs to be prepared for this.
Maybe some client side code (js, ajax ... I don't know) is necessary.
What commands do you use?

Klaus
 

From what you said, you are seemingly running the server in your laptop (first code above) and a client at the ESP8266 (second code above), not the usual approach. Anyway, at the server side, you are calling a 2 seconds delay which will make it unavailable most of the time. Another point to remark: You didn' show the Loop() content of the client side, is the 2nd code within it? If yes, the 2s delay on the server side perhaps explains why client became at unconnected state.
 

Hi,

Most popular in client-server communication is that the client initiates the comunication. The server just responds.
The server usually does not send data without dedicated request by the client.

But it should be possible. But the client needs to be prepared for this.
Maybe some client side code (js, ajax ... I don't know) is necessary.
What commands do you use?

Klaus
I don't understand what you mean by this question: What commands do you use?
The ESP8266 is server and PC is client. Now, in this test code, the client is Packet Sender application. After i connect my PC to the Node MCU access point(i configured it so) and then connect to the 192.168.1.1 that is Node MCU(i configured it so). After that PC as a client connected to Node MCU, Node MCU start to send 1KB data every 40ms to PC.
 

The ESP8266 is server and PC is client

You should have mentioned this from the 1st post. From what you have presented it did not appear to be the same program, since you implemented both Client and Server in the Arduino code.
 

Hi,

OK...

Then not the server initiates the communication, but the clinet, maybe with some client side script:
PC requests periodically information (with js) and ESP just responds..

Klaus
 

PC don't requests periodically, but Just connect once to ESP. And then ESP sends 1KB data to PC periodically.
 

Hi,

OK...then we need more explanation why ... and how you want to do this.

I can only repeat myself:
A client-server communication is not meant that a server sends data to a client without dedicated request by the client.

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top