int main()
{
  wifi_start(9, 8, 115200, USB_PGM_TERM);

  print("Join a network:\r");
  wifi_join("SSID", "passphrase");

  int ip[] = {0, 0, 0, 0};
  memset(ip, 0, 16);
  do
  {
    wifi_ip(STA, ip);
  }while(ip[0] == 0 && ip[1] == 0 && ip[2] == 0 && ip[3] == 0);
  print("\rip=%d.%d.%d.%d\r", ip[0], ip[1], ip[2], ip[3]);
    
  int mode = wifi_mode(CHECK);
  switch(mode)
  {
    case STA:    //0xf4: 
      print("mode=STA\r");
      break;
    case AP:     //0xf3  
      print("mode=AP\r");
      break;
    case STA_AP: //0xf2
      print("mode=STA+AP");
      break;
  }     
}
Example #2
0
static int
wifi_cli(int argc, char **argv)
{
    struct wifi_if *wi;
    int i;

    if (argc < 2) {
        goto usage;
    }
    wi = wifi_if_lookup(0);

    if (!strcmp(argv[1], "start")) {
        wifi_start(wi);
    } else if (!strcmp(argv[1], "stop")) {
        wifi_stop(wi);
    } else if (!strcmp(argv[1], "scan")) {
        wifi_scan_start(wi);
    } else if (!strcmp(argv[1], "aps")) {
        int i;
        struct wifi_ap *ap;

        console_printf("   %32s %4s %4s %s\n", "SSID", "RSSI", "chan", "sec");
        for (i = 0; i < wi->wi_scan_cnt; i++) {
            ap = (struct wifi_ap *)&wi->wi_scan[i];
            console_printf("%2d:%32s %4d %4d %s\n",
              i, ap->wa_ssid, ap->wa_rssi, ap->wa_channel,
              ap->wa_key_type ? "X" : "");
        }
    } else if (!strcmp(argv[1], "connect")) {
        if (argc < 2) {
            goto conn_usage;
        }
        i = strlen(argv[2]);
        if (i >= sizeof(wi->wi_ssid)) {
            goto conn_usage;
        }
        if (argc > 2) {
            i = strlen(argv[2]);
            if (i >= sizeof(wi->wi_key)) {
                goto conn_usage;
            }
            strcpy(wi->wi_key, argv[3]);
        }
        strcpy(wi->wi_ssid, argv[2]);
        if (wifi_connect(wi)) {
conn_usage:
            console_printf("%s %s <ssid> [<key>]\n",
              argv[0], argv[1]);
        }
    } else {
usage:
        console_printf("start|stop|scan|aps|connect <ssid> [<key>]\n");
    }
    return 0;
}
int main()
{
  wifi_start(9, 8, 115200, USB_PGM_TERM);
    
  int mode = wifi_mode(STA_AP);
  switch(mode)
  {
    case STA:    //0xf4: 
      print("mode=STA\r");
      break;
    case AP:     //0xf3  
      print("mode=AP\r");
      break;
    case STA_AP: //0xf2
      print("mode=STA+AP");
      break;
  }     
}
int main()
{
  wifi_start(31, 30, 115200, WX_ALL_COM);
  wifi_setBuffer(str, sizeof(str));

  int tcpHandle = wifi_connect("api.openweathermap.org", 80);
  
  print("tcpHandle = %d\r", tcpHandle);
  
  pause(2000);
  
  // IMPORTANT: Replace YourKeyYourKey... with the API key you 
  // obtain from openweathermap.com when you create a free 
  // account. 

  char request[] = 
  "GET /data/2.5/weather?zip=95677,us"\
  "&appid=YourKeyYourKeyYourKeyYourKeyYour"\
  " HTTP/1.1\r\n"\
  "Host: api.openweathermap.org\r\n"\
  "Connection: keep-alive\r\n"\
  "Accept: *" "/" "*\r\n\r\n";

  int size = strlen(request);
  
  print("GET req size: %d\r", size);
  
  pause(2000);

  wifi_print(TCP, tcpHandle, "%s", request);
  event = wifi_event;
  
  pause(2000);
  size = strlen(str);
  print("size = %d", size);
  
  pause(2000);
  wifi_scan(TCP, tcpHandle, "%s", str); 
  for(int n = 0; n < sizeof(str); n++)
  {
    if(str[n] <= 'z' && str[n] >= ' ')
    {
      print("%c", str[n]);
    }      
    else if(str[n] == 0)
    {
      print("[%d]", str[n]);
      break;
    }      
    else if(str[n] == '\n')
    {
      print("\r", str[n]);
    }      
    else
    {
      print("[%d]", str[n]);
    }      
  }
  char *loc = strstr(str, "temp");
  print("\rloc = %d\r", loc);
  float temp = 0;
  sscan(loc+5, "%f", &temp);
  float degC = temp -273.15;
  print("temp = %6.2f deg C\r", degC); 
  float degF = degC * 9.0 / 5.0 + 32.0;
  print("temp = %6.2f deg C\r", degF); 
}