9 years, 7 months ago.

Websocket send not working in ticker.attach?

I have got websockets working and sending to my server and webpage but sending it in a while loop crashes my computer. I tried putting it in a ticker.attach class to be called every 3 seconds but it was called just once and then crashed the mbed program. Does anyone know whats wrong?

Here's a template of the code

websockets() { ws.send("DATA") }

int main() { tick.attach(&websockets, 3)

while(1) { Do Stuff } }

Question relating to:

1 Answer

9 years, 7 months ago.

which mbed module are you using ?? is it wifi or ethernet, I have tried both wifi and ethernet and they worked fine for me.

and in the future please use <<code>> and <</code>> on their own lines before and after any c code in order to make it readable.

I'm using the Nucleo F401 with cc3000 wifi. It seems to be sending once and then getting stuck.

posted by Marcus Parker 15 Sep 2014

I used the avnet-wigo module with cc3000 library as well. and it worked fine for me. I used the exact mbed code. Kindly check you are not importing any unnecessary libraries. Most of the programs clashes when you are using those instances which are not compatible/present in your device, for example calling a temperature sensor without any temp sensor

posted by zain aftab 15 Sep 2014

I've checked all of my libraries, everything seems fine.

posted by Marcus Parker 15 Sep 2014

kindly re-post your code, lets see what is the problem.

Ps: my code was not working I had no idea, It has started working I still have no idea :p

posted by zain aftab 15 Sep 2014

void websockets()
{
    char recv[22];
    char wstemp[20];
   
    sprintf(wstemp, "%f*%f*%f", temp, trip_level, hyst_amount);

    ws.send(wstemp);
        
    if(ws.read(recv))
    {
        pc.printf("rcv: %s\r\n", recv);
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////MAIN_PROGRAM/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() 
{       
    lcd.locate(0, 0);
    lcd.printf("Thermostat V1.8");
    pc.printf("\n\n\r********************************************************************************\n\r");
    pc.printf("*******************************Thermostat 1.8***********************************\n\r");
    pc.printf("********************************************************************************\n\r");
    
    lcd.locate(0, 0);
    lcd.printf("Thermostat 1.8");
    
    //set up wifi with failure protocall
    wifi.init();
    if (wifi.connect() == -1) 
    {
        lcd.locate(0, 1);
        lcd.printf("No Internet 1");
        pc.printf("Failed to connect to internet!\n\r");
        wait(1);
    } 
    else
    {
        lcd.locate(0, 1);
        lcd.printf("IP: %s", wifi.getIPAddress());
        pc.printf("Internet connected\n\r");
        wait(1);
    }
    
    
    if(ws.connect() == true);
    {
        pc.printf("WS connected\n\n\r");
    }
    
    ///// Calls display every 0.5 seconds /////
    tick.attach(&disp, 1);
    //pc.attach(*gui, Serial::RxIrq);
    wstick.attach(&websockets, 3);
    
    trip_off = trip_level - 2;
    
    //main program
    while(true) 
    { 
        temp = temperature.sample();  //Define temp as temp celsius reading
        hyst_amount = (trip_level - trip_off); //Lower level for hysteresis
    
    
        ///// Buttons with time buffer /////
        for(int value = TheKeyBuffer.Read(); value != -1; value = TheKeyBuffer.Read()) 
        {
            switch(value)
            {
                case KEY_UP:
                    trip_level += 0.1;
                break;
                
                case KEY_DOWN:
                    trip_level -= 0.1;
                break;
            }
        } 
    

        ///// Led will light up if temperature is above trip level /////
        if(temp >= trip_level) 
        {
            led = 1;
        }
        else if(temp <= trip_off)
        {
            led = 0;
        }  
    
        if(butPush.read() == 0) 
        {
            MiddleButton();
        }
      
    } 
}
posted by Marcus Parker 15 Sep 2014

your code looks fine, to be honest :) have you tried to send some smple texts(like hello world) in a while loop or in a ticker??

try to send simple text for example hello world in a while loop, and make it new program without any temperature or other sensor library.

posted by zain aftab 15 Sep 2014

by the way have you declare Websocket ws("ws:sockets.mbed.org:443/ws/demo/wo"); in your code ?? i think you must have included this as well.

posted by zain aftab 15 Sep 2014

I have tried to send simple text, I can get it to work in the main program just not in a tick.attach. I declared my websocket ws further up.

posted by Marcus Parker 15 Sep 2014

now try to receive the msg as well. try to go step by step, your code looks fine to me, but it might possible that calling some function for multiple times is creating a problem, try to make sure wifi.init() is called just once in a program, it is a common issue for crashing as well.

are you able to send simple text in a while loop ??

posted by zain aftab 15 Sep 2014

I can send and recieve the message both outside and inside the while loop in main, wifi.init is called just once. It happens as soon as I put websocket functions in a function to be called by a tick.attach.

posted by Marcus Parker 15 Sep 2014

Now try to send temperature

 temp = temperature.sample(); 
ws.send(temp);

I am trying to say is to debug your code one by one, some times unnecessary interrupts creates problem :/ are you using mbed webserver or you have created your own using tornado ??

posted by zain aftab 15 Sep 2014

add wait statement as well

posted by zain aftab 15 Sep 2014

Same for the temperature, I can send the temp outside and inside the while loop in main, just not in tick.attach. I'm currently using mbed webserver.

posted by Marcus Parker 16 Sep 2014