/* Test Sleep + Wifi Connect */
#include "device.h"
#include "sleep_ex_api.h"
#include "analogin_api.h"
#include "gpio_irq_api.h"
#include "gpio_irq_ex_api.h"
#include "sys_api.h"
#include "diag.h"
#include "main.h"
#include <platform/platform_stdlib.h>
#include <sntp/sntp.h>
#include <lwip/sockets.h>
#include <lwip_netconf.h>
#include <dhcp/dhcps.h>
#include "wifi_conf.h"
#ifndef IP4ADDR
#define IP4ADDR(a,b,c,d) (((unsigned int)((d) & 0xff) << 24) | \
((unsigned int)((c) & 0xff) << 16) | \
((unsigned int)((b) & 0xff) << 8) | \
(unsigned int)((a) & 0xff))
#endif
extern struct netif xnetif[NET_IF_NUM];
#define WiFi_SSID "APWIFISSDI"
#define WiFi_PASSWORD "Password"
#define WiFi_CHANNEL 1
#define WiFi_SECTYPE RTW_SECURITY_WPA2_AES_PSK
#define EXT_SERVER_IP "192.168.1.2"
#define EXT_SERVER_PORT 12345
#define DEF_ST_IP IP4ADDR(192,168,1,72)
#define DEF_ST_MSK IP4ADDR(255,255,255,0)
#define DEF_ST_GW IP4ADDR(192,168,1,1)
#define ADC_NULL 0x4942
analogin_t adc_vbat;
char *group_ip = EXT_SERVER_IP;
uint16_t port = EXT_SERVER_PORT;
char ssid[] = WiFi_SSID;
char password[] = WiFi_PASSWORD;
uint32_t channel = WiFi_CHANNEL;
uint8_t pscan_config = PSCAN_ENABLE | PSCAN_FAST_SURVEY;
uint16_t adc_read = ADC_NULL;
unsigned int active_tick, start_send_tick;
unsigned char packet[512];
int socket = -1;
struct sockaddr to;
struct sockaddr_in *to_sin = (struct sockaddr_in*) &to;
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
unsigned int send_id;
void cycle_main(void)
{
rtw_wifi_setting_t setting;
int flg = 0;
LwIP_Init();
while(1) {
start_send_tick = xTaskGetTickCount();
ConfigDebugClose = 1;
analogin_init(&adc_vbat, AD_2);
adc_read = analogin_read_u16(&adc_vbat) - ADC_NULL;
analogin_deinit(&adc_vbat);
if(!flg) {
wifi_on(RTW_MODE_STA);
if(wifi_connect(ssid, WiFi_SECTYPE, password, strlen(WiFi_SSID), strlen(WiFi_PASSWORD), -1, NULL) != RTW_SUCCESS)
continue;
wifi_get_setting(WLAN0_NAME, &setting);
channel = setting.channel;
if( LwIP_DHCP(0, DHCP_START) == DHCP_ADDRESS_ASSIGNED) {
ipaddr = xnetif[0].ip_addr;
netmask = xnetif[0].netmask;
gw = xnetif[0].gw;
} else {
ipaddr.addr = DEF_ST_IP;
netmask.addr = DEF_ST_MSK;
gw.addr = DEF_ST_GW;
}
flg = 1;
} else {
wifi_rf_on();
if(wifi_set_pscan_chan((uint8_t *)&channel, &pscan_config, 1) < 0) {
ConfigDebugClose = 0;
printf("Error listen beacon quickly!\n");
}
if(wifi_connect(setting.ssid, setting.security_type, setting.password, strlen(setting.ssid), strlen(setting.password), setting.key_idx, NULL) != RTW_SUCCESS)
continue;
netif_set_addr(&xnetif[0], &ipaddr , &netmask, &gw);
}
if((socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
ConfigDebugClose = 0;
printf("Error open socket!\n");
flg = 0;
} else {
to_sin->sin_family = AF_INET;
to_sin->sin_port = htons(port);
to_sin->sin_addr.s_addr = inet_addr(group_ip);
if(!active_tick) active_tick = xTaskGetTickCount();
ConfigDebugClose = 0;
sprintf(packet, "ID:0x%08x\tADC:0x%04x\tT:%d ms\n", send_id, adc_read, active_tick);
if(sendto(socket, packet, strlen(packet), 0, &to, sizeof(struct sockaddr)) < 0) {
printf("Error send!\n");
flg = 0;
} else {
printf(packet);
}
closesocket(socket);
}
wifi_rf_off();
send_id++;
active_tick = xTaskGetTickCount() - start_send_tick;
sleep_ex(SLEEP_WAKEUP_BY_STIMER, (active_tick < 2048) ? 1024*8 - 2 - active_tick : 1024*8 - 1);
// test_tmp()...
sleep_ex(SLEEP_WAKEUP_BY_STIMER, 8191);
// test_tmp()...
}
}
void main(void)
{
ConfigDebugClose = 1;
if(xTaskCreate( (TaskFunction_t)cycle_main, "main", (2048/4), NULL, (tskIDLE_PRIORITY + 1), NULL)!= pdPASS) {
ConfigDebugClose = 0;
DiagPrintf("Cannot create main task\n");
} else
vTaskStartScheduler();
}