9 years, 9 months ago.

Websocket reconnect?

I'm using a Nucleo F401 with a cc3000 shield and websockets to make my product internet of things. I have a problem where if the internet turns off the program gets stuck instead of carrying on. My basic code template looks like this:

#include "mbed.h"
#include "cc3000.h"
#include "Websocket.h"
#include "TextLCD.h
SPI spi(PA_7, PA_6, PA_5); //SPI connection(MOSI, MISO, SCK)
cc3000 wifi(PB_3, PB_4, PB_10, SPI(PA_7, PA_6, PA_5), "SSID", "password", WPA2, false); //irq, en, cs, spi,
Serial pc(PA_11, PA_12); 
TMP36GZ temperature(PA_0); //Analog in pin
Ticker tick;
TextLCD lcd(PC_8, PC_6, PC_5, PB_15, PB_14, PB_12); // rs, e, d4-d7

Websocket ws("ws://srelog.com:49153/"); 

void disp()
{
  lcd.cls();
  lcd.locate(0, 0); 
  lcd.printf("%3.2f", temp);
}

int main()
{
  char ws_recv[22]; //Receive from webserver

  if (wifi.connect() == -1)  
  {
    lcd.locate(0, 1);
    lcd.printf("No Internet 0   ");
  } 
  else
  {
    lcd.locate(0, 1);
    lcd.printf("IP: %s", wifi.getIPAddress());
  }

  if(ws.connect() == true)
  {
    lcd.locate(0, 1);
    lcd.printf("WS");    
  }
  else
  {
    lcd.locate(0, 1);
    lcd.printf("NO WS");  
  }

  tick.attach(&ISR_DisplayTick, 1);

  while(true)
  {
    temp = temperature.sample(); 

    ws.send(data); //where data is temp etc

    if(ws.read(ws_recv))
    {
      string check = ws_recv;
      //split string and put into seperate variables
    }
  }
}

I've tried: Putting the ws send and recieve into an if(ws.is_connected()) and having an wifi and ws reconnect

Taking the ws send and recieve out seems to solve it, does anyone know what could be wrong?

Be the first to answer this question.