wifi.connect_non_blocking need to be called twice

A 40 Pin Dip packaged development board enabling you to create internet connected products

wifi.connect_non_blocking need to be called twice

Postby peter247 » Sat Jun 14, 2014 10:06 pm

I`m trying to make a web socket server with the wifi dipcortex , and found it I could not get it to connect if you just use "wifi.connect_non_blocking((uint8_t *)SSID, (uint8_t *)AP_KEY, AP_SECURITY);" once , but you use the command twice it works .

eg

wifi.connect_non_blocking((uint8_t *)SSID, (uint8_t *)AP_KEY, AP_SECURITY);
wifi.connect_non_blocking((uint8_t *)SSID, (uint8_t *)AP_KEY, AP_SECURITY);

it connect a couple of seconds later !!!!

any documentation on how to make a socket server , eg now to listen to a port , receive to incoming tcp connection ?
peter247
 
Posts: 5
Joined: Thu Feb 06, 2014 1:44 am

Re: wifi.connect_non_blocking need to be called twice

Postby Carl-SolderSplash » Sun Jun 15, 2014 10:49 am

Hi,

Thats a bit strange, I use wifi.connect_non_blockingin the kitchensink project.

http://mbed.org/teams/Solder-Splash/cod ... tchenSink/

The connection can take a few seconds to get setup. To check the connection once you have called connect :
Code: Select all
if (( wifi.is_enabled() ) && ( wifi.is_dhcp_configured() ))
{
   wifi.get_ip_config(&ipinfo);
}

The Kitchensink demo has both websocket transmit and receieve ( WebSocketReadTest() in tcpTest.cpp )
User avatar
Carl-SolderSplash
Site Admin
 
Posts: 223
Joined: Sun Mar 17, 2013 11:15 pm

Re: wifi.connect_non_blocking need to be called twice

Postby peter247 » Sun Jun 15, 2014 12:53 pm

I`m using the kitchensink project , but I`ve removed the menu system and cut it down to just main.cpp.

Code: Select all
#include "mbed.h"
#include "cc3000.h"
#include "TCPSocketConnection.h"
#include "TCPSocketServer.h"
#include "wifi.h"
#include "UDPSocket.h"
#include "main.h"
#include "NTPClient.h"
#include <time.h>

#define slave_address 0x44

using namespace mbed_cc3000;

/* cc3000 module declaration specific for user's board. Check also init() */
#if (MY_BOARD == WIGO)
cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), PORTA_IRQn);
#elif (MY_BOARD == WIFI_DIPCORTEX)
cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37));
//Serial uart(p19, p20);

I2CSlave slave(p23, p18); // sda , sclk
Serial RFID( p25 , p26 ); // tx , rx
DigitalOut rfid_rst(p24);


#endif

bool Connected = false;
bool UsingSmartConfig = false;
char _deviceName[] = "CC3000";


tNetappIpconfigRetArgs ipinfo;
extern char tmpBuffer[512];


char fob_status = 0 ; // 0 = clear , 1 = new fob , 2 = old fob
char last_fob[20];
int cnt = 0;
int reg = 0;
int data = 0;
int i;
char tmp_char;


// ------------------------------------------------------------------------------------------------------------
/*!
    @brief main loop
*/
// ------------------------------------------------------------------------------------------------------------
int main( void )
{
    init();
    RFID.baud( 9600 );
    wifi.start(0);
    rfid_rst = 0;
    wait ( 1 );
    rfid_rst = 1;
    setup_wifi();
    slave.address( slave_address );
    while (1)
    {   
        check_rfid();
        check_wifi();
        i = slave.receive();
        switch (i)
        {
            case I2CSlave::ReadAddressed:
                if ( reg == 0 )
                {
                    slave.write( fob_status );
                }
                 else if ( reg == 1 )
                {
                   slave.write( last_fob , 13 );
                }
                else if ( reg == 3 )
                {
                    slave.write( Connected );
                }
                else if ( reg == 123 )
                {
                    slave.write( "v1.1c" , 6 );                     
                }     
                slave.stop();
                break;
               
            case I2CSlave::WriteAddressed:
                reg = slave.read();
                if ( reg == 2 )
                {
                    fob_status = 0;
                }
       
                wait_us(10);             
                slave.stop();
                break;
        }
     }
}

void init()
{
    NVIC_SetPriority(SSP1_IRQn, 0x0);
    NVIC_SetPriority(PIN_INT0_IRQn, 0x1);
   
    // SysTick set to lower priority than Wi-Fi SPI bus interrupt
    NVIC_SetPriority(SysTick_IRQn, 0x2);
   
   
    // Enable RAM1
    LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26);
}

void check_rfid( void )
{
    while ( RFID.readable() > 0 )
    {
        tmp_char = RFID.getc();
        if ( tmp_char == 2 )
        {
            cnt = 0;    // start number = 2
        }
        else if ( tmp_char == 3 )
        {
            fob_status = 1;  // end number = 3
        }
        else
        {
            last_fob[cnt] = tmp_char;
            cnt++;
        }
    }
}

void check_wifi(void)
{
   
  if ( wifi.is_connected() )
    {
       Connected = true; 
    }
    else
    {
       Connected = false;
    }   
   
}

void setup_wifi(void)
{
   
uint32_t ip = 0;
         
            // Enable DHCP
wifi._netapp.dhcp(&ip, &ip, &ip, &ip);
RFID.printf("\r\n Connecting to : %s key : %s", SSID, AP_KEY );
wifi.connect_non_blocking((uint8_t *)SSID, (uint8_t *)AP_KEY, AP_SECURITY);
wait( .5 );
wifi.connect_non_blocking((uint8_t *)SSID, (uint8_t *)AP_KEY, AP_SECURITY);

while ( !wifi.is_connected() )
{
wait(0.1);
}


print_cc3000_info();

   

TCPSocketServer server;
TCPSocketConnection client;
   
server.bind(80);
server.listen();

while ( server.accept(client) == 0 )
{
    wait(0.1);
}   
//
RFID.printf("waiting");
client.set_blocking(true); // Timeout after (1.5)s
RFID.printf("Connection from: %s \r\n", client.get_address());
client.close();


        if ( wifi.is_connected() )
        {
            server.close();
        }
}
   
void print_cc3000_info() {
uint8_t myMAC[8];
uint8_t buffer[2];
int32_t status = 0;
tNetappIpconfigRetArgs ipinfo2;
tUserFS cc_user_info;
const char * WIFI_STATUS[] = {"Disconnected", "Scanning", "Connecting", "Connected"};

    wifi.get_user_file_info((uint8_t *)&cc_user_info, sizeof(cc_user_info));
    wifi.get_mac_address(myMAC);
    RFID.printf(" MAC address : %02x:%02x:%02x:%02x:%02x:%02x\r\n", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]);
   
    if (! wifi._nvmem.read_sp_version( (unsigned char*)&buffer ) )
    {
        RFID.printf(" CC3000 Firmware Version : %u.%u \r\n", buffer[0], buffer[1]);
    }
    else
    {
        RFID.printf(" CC3000 Read nvmem failed!");
    }
   
    status = wifi._wlan.ioctl_statusget();
    if (( status > -1 ) && ( status < 4 ))
    {
        RFID.printf(" Wifi Status    : %s\r\n", WIFI_STATUS[status]);
    }
    else
    {
        RFID.printf(" Wifi Status    : %d\r\n", status);
    }
   
    if ( wifi.is_dhcp_configured() )
    {
        wifi.get_ip_config(&ipinfo2);
        RFID.printf(" Connected to   : %s \r\n", ipinfo2.uaSSID);
        RFID.printf(" IP             : %d.%d.%d.%d \r\n", ipinfo2.aucIP[3], ipinfo2.aucIP[2], ipinfo2.aucIP[1], ipinfo2.aucIP[0]);   
        RFID.printf(" Gateway        : %d.%d.%d.%d \r\n", ipinfo2.aucDefaultGateway[3], ipinfo2.aucDefaultGateway[2], ipinfo2.aucDefaultGateway[1], ipinfo2.aucDefaultGateway[0]); 
        RFID.printf(" Subnet         : %d.%d.%d.%d \r\n", ipinfo2.aucSubnetMask[3], ipinfo2.aucSubnetMask[2], ipinfo2.aucSubnetMask[1], ipinfo2.aucSubnetMask[0]); 
        RFID.printf(" DNS            : %d.%d.%d.%d \r\n", ipinfo2.aucDNSServer[3], ipinfo2.aucDNSServer[2], ipinfo2.aucDNSServer[1], ipinfo2.aucDNSServer[0]); 
       
        RFID.printf(" Cached IP      : %s \r\n", wifi.getIPAddress());   
        RFID.printf(" Cached Gateway : %s \r\n", wifi.getGateway());   
        RFID.printf(" Cached Subnet  : %s \r\n", wifi.getNetworkMask());   

    }
    else
    {
         RFID.printf(" Not connected \r\n");
    }
}


Looks like I may have to start again and work on just getting the wifi and tcp socket server working and add the other bit after.
I copied the rough ideas from your code , but for me it`s not waiting for a connection .
peter247
 
Posts: 5
Joined: Thu Feb 06, 2014 1:44 am

re - tcp server problems

Postby peter247 » Mon Jun 16, 2014 2:01 pm

FIXED !!!!!

I had a misunderstanding of how server.accept(client) and client.set_blocking(false, 1500); work.

I had the idea that the code should wait after server.accept() for a incoming connection , but looking at it that would be catch 22 , because you would have to accept a incoming connection to use make it blocking with client.set_blocking.

So what I need to do is say server.accept() = -1 no connection and server.accept() = 0 , someone has connected ..
peter247
 
Posts: 5
Joined: Thu Feb 06, 2014 1:44 am

Re: wifi.connect_non_blocking need to be called twice

Postby Carl-SolderSplash » Tue Jun 17, 2014 11:38 pm

Awesome, thanks for letting us know. I'm sure it will help others too.
User avatar
Carl-SolderSplash
Site Admin
 
Posts: 223
Joined: Sun Mar 17, 2013 11:15 pm


Return to WiFi DipCortex

Who is online

Users browsing this forum: No registered users and 3 guests

cron