WIFI Suckets and timer1 Interrupts

Hi guys,

I'm writing a program that would collect sensor readings every 0.1 second and send the results to an Android phone via wifi Suckets.

The problem I'm encountering is that, in the data sending via wifi sucket gets interrupted repeatedly, sometimes for seconds, or minutes or hours before been re-established. I'm not sure what the issue is, if someone can shed some light on this matter that will greatly be appreciated.

Hello Catalyst0001,
Without any code it is hard to help you. Please share it here so we can try to understand what is happening.

p.s: do you mean wifi sockets?

@ Angelo9999, Yes I meant wifi sockets

below is the code used so far.

#include <TimerOne.h>

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

#define PORT1 4444

YunServer server(PORT1);
YunClient client;

double i, voltOp;
int n, d;  //'n' handles the number before the decimal and 'd' handles the number after the decimal
volatile double RollR;
volatile byte set = 0;

void setup() {
  Serial.begin(9600);
  Bridge.begin();
  ///Timer1 setup
  Timer1.initialize(100000);  //Iniitializes the timer
  //Timer1.stop();  //stop the counter
  Timer1.attachInterrupt(Timer1ISR);  //Attach interrupt to Timer1 Interrupt Service routine
  //Timer1.stop();  //stop the counter
  ////Timer1 setup end
  
  server.noListenOnLocalhost();
  server.begin();
}

void Timer1ISR(){
  set = 0;
  voltOp = (((analogRead(A1))/1.1335)*5.0)/1024.0;
  RollR = ((voltOp - 2.5)/0.02)+10;  //collects Roll Angle rate
  set = 1;//allows data to be sent to the Android devices
}//end Timer1ISR

void loop() {
  double RollRf;
  byte collect; //data collection flag
  
  while(!client.connected()){
    Serial.println("attempting to connect");
    client = server.accept();
  }
  //interrupts();
  //YunClient client = server.accept();
  while(client.connected()){
    Serial.println("CLIENT CONNECTED!");
    String string = "";
    char received;
    int index;

    noInterrupts();
    RollRf = RollR;
    collect = set;
    interrupts();
    
    if(collect == 1){
      if(RollRf < 0){
        i = RollRf * -1;
        n = i;
        d = (i - n)*100;
        //noInterrupts();
        Serial.println("Send Roll Rate-");
        //Timer1.stop();
        client.write('r');
        client.write('-');
        client.write(n);
        client.write(d);
        //Timer1.resume();
        Serial.println("Sent Roll Rate-");
        //interrupts();
      } else{
        n = RollRf;
        d = (RollRf - n)*100;
        //noInterrupts();
        Serial.println("Send Roll Rate+");
        //Timer1.stop();
        client.write('r');
        client.write('+');
        client.write(n);
        client.write(d);
        //Timer1.resume();
        Serial.println("Sent Roll Rate+");
        //interrupts();
      }
    }//end collect =1
    Serial.println("In the loop");
  }//end while connected
  client.stop();
  Serial.println("stopped client");
}//end loop

Catalyst0001:
@ Angelo9999, Yes I meant wifi sockets

::::SNIP::::
while(!client.connected()){
Serial.println(“attempting to connect”);
client = server.accept();
}
//interrupts();
//YunClient client = server.accept();
while(client.connected()){
Serial.println(“CLIENT CONNECTED!”);
String string = “”;
::::SNIP::::

@Catalyst0001,

Okay, classic error. Please do NOT put the two (2) while loops inside void loop(). This is conflicting logic. Instead use:

if (client.connected()) {
    // do what I do when CONNECTED.
} else {
    // do what I do when NOT connected.
}

As a matter of fact, it is not good practice to put a possibly infinite loop inside of loop().

Please ask questions, I image you have a few.

Jesse

Thank you Jesse!!!