+ Post New Thread
Results 1 to 8 of 8
  1. #1
    Newbie level 6
    Points: 89, Level: 1

    Join Date
    May 2019
    Posts
    12
    Helped
    0 / 0
    Points
    89
    Level
    1

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

    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

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 74,927, Level: 66
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,192
    Helped
    3461 / 3461
    Points
    74,927
    Level
    66

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

    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
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  3. #3
    Super Moderator
    Points: 29,198, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,691
    Helped
    1102 / 1102
    Points
    29,198
    Level
    41
    Blog Entries
    9

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

    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.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   AltAdvertisement

        
       

  4. #4
    Newbie level 6
    Points: 89, Level: 1

    Join Date
    May 2019
    Posts
    12
    Helped
    0 / 0
    Points
    89
    Level
    1

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

    Quote Originally Posted by KlausST View Post
    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.



  5. #5
    Super Moderator
    Points: 29,198, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,691
    Helped
    1102 / 1102
    Points
    29,198
    Level
    41
    Blog Entries
    9

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

    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.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



    •   AltAdvertisement

        
       

  6. #6
    Super Moderator
    Points: 74,927, Level: 66
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,192
    Helped
    3461 / 3461
    Points
    74,927
    Level
    66

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

    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
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  7. #7
    Newbie level 6
    Points: 89, Level: 1

    Join Date
    May 2019
    Posts
    12
    Helped
    0 / 0
    Points
    89
    Level
    1

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

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



  8. #8
    Super Moderator
    Points: 74,927, Level: 66
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,192
    Helped
    3461 / 3461
    Points
    74,927
    Level
    66

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

    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
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



--[[ ]]--