Ejemplo n.º 1
0
void WifiClass::handleEvent(System_Event_t *e)
{
    int event = e->event;

    if (event == EVENT_STAMODE_GOT_IP)
    {
	Debug.printf("Wifi client got IP\n");
        if (!haveIp && changeDlg)
            changeDlg(true);
        haveIp = true;

        if (!AppSettings.portalUrl.equals(""))
        {
            String mac;
            uint8 hwaddr[6] = {0};
            wifi_get_macaddr(STATION_IF, hwaddr);
            for (int i = 0; i < 6; i++)
            {
                if (hwaddr[i] < 0x10) mac += "0";
                    mac += String(hwaddr[i], HEX);
                if (i < 5) mac += ":";
            }

            String body = AppSettings.portalData;
            body.replace("{ip}", WifiStation.getIP().toString());
            body.replace("{mac}", mac);
            portalLogin.setPostBody(body.c_str());
            String url = AppSettings.portalUrl;
            url.replace("{ip}", WifiStation.getIP().toString());
            url.replace("{mac}", mac);

            portalLogin.downloadString(
                url, HttpClientCompletedDelegate(&WifiClass::portalLoginHandler, this));
        }
    }
    else if (event == EVENT_STAMODE_CONNECTED)
    {
	if (!connected)
            Debug.printf("Wifi client got connected\n");
        connected = true;
    }
    else if (event == EVENT_STAMODE_DISCONNECTED)
    {
	if (connected)
            Debug.printf("Wifi client got disconnected\n");
        connected = false;
        if (changeDlg)
            changeDlg(false);
    }
    else
    {
	Debug.printf("Unknown wifi event %d !\n", event);
    }
}
Ejemplo n.º 2
0
void SwitchHttp::setState(uint8_t state)
{
	Switch::setState(state);
	if (_httpClient.isProcessing())
	{
			Serial.printf("IS PROCESSING\n");
			return; // We need to wait while request processing was completed
	}
	else
	{
		String postBody = "{state: \"";
		postBody += (String)state;
		postBody += "\"}";
		Serial.printf("postBody: %s\n", postBody.c_str());
		_httpClient.setRequestContentType("Content-Type: application/json; charset=utf-8");
		_httpClient.setPostBody(postBody);
		_httpClient.downloadString(_url, HttpClientCompletedDelegate(&SwitchHttp::_httpGetResponse, this));
	}
}