So gehts mit Wlan und Http Client:
Code:
#include <iostream>
#include "mbed.h"
#include "wifi.h"
/*------------------------------------------------------------------------------
Hyperterminal settings: 115200 bauds, 8-bit data, no parity
This example
- connects to a wifi network (SSID & PWD)
- displays the IP address and get webpage html code
This example uses SPI3 ( PE_0 PC_10 PC_12 PC_11), wifi_wakeup pin (PB_13),
wifi_dataready pin (PE_1), wifi reset pin (PE_8)
------------------------------------------------------------------------------*/
#define MBED_CONF_APP_WIFI_SSID "xyz"
#define MBED_CONF_APP_WIFI_PASSWORD "geheim"
Serial pc (SERIAL_TX, SERIAL_RX);
DigitalOut led(LED2);
AnalogIn adc_temp(ADC_TEMP);
//
// WLAN mit dem Hotspot verbinden.
//
int wifi_sample_run(void)
{
uint8_t IP_Addr[4];
uint8_t MAC_Addr[6];
// WLAN initialisieren
if (WIFI_Init() == WIFI_STATUS_OK)
{
std::cout << "WiFi initialisieren." << std::endl;
if (WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK)
{
std::cout << "Wifi Modul MAC Address: ";
std::cout << unsigned (MAC_Addr[0]) << ":";
std::cout << unsigned (MAC_Addr[1]) << ":";
std::cout << unsigned (MAC_Addr[2]) << ":";
std::cout << unsigned (MAC_Addr[3]) << ":";
std::cout << unsigned (MAC_Addr[4]) << ":";
std::cout << unsigned (MAC_Addr[5]) << std::endl;
}
else
{
std::cout << "ERROR: MAC-Adresse konnte nicht gelesen werden." << std::endl;
}
if (WIFI_Connect (MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK)
{
std::cout << "WiFi Modul mit Hotspot verbunden." << std::endl;
if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK)
{
std::cout << "WiFi Modul IP-Adresse: ";
std::cout << unsigned (IP_Addr[0]) << ".";
std::cout << unsigned (IP_Addr[1]) << ".";
std::cout << unsigned (IP_Addr[2]) << ".";
std::cout << unsigned (IP_Addr[3]) << std::endl;
}
else
{
std::cout << "ERROR: WiFi Modul IP-Adresse konnte nicht gelesen werden." << std::endl;
return -1;
}
}
else
{
std::cout << "ERROR: WiFi Modul konnte nicht verbunden werden." << std::endl;
return -1;
}
}
else
{
std::cout << "ERROR: WiFi Modul konnte nicht initialisiert werden" << std::endl;
return -1;
}
return 0;
}
//
// Sende einen HTTP Get an den Server und lese die Server Antwort.
//
void WebClient (void)
{
// Parameter zu welchem Server verbunden werden soll.
uint16_t iPort = 80;
uint16_t iLocalPort = 3000;
uint8_t ipAddr[4] = { 10, 0, 0, 3 }; // Muss man mit der Server IP ersetzen.
// Mit dem Serve verbinden.
WIFI_Status_t wifiRc;
int32_t Socket = 0;
wifiRc = WIFI_OpenClientConnection (Socket, WIFI_TCP_PROTOCOL, "Name", ipAddr, iPort, iLocalPort);
if (wifiRc != WIFI_STATUS_OK)
{
if (wifiRc == ES_WIFI_STATUS_ERROR)
std::cout << "Connect zum HTTP-Server Fehler: ES_WIFI_STATUS_ERROR" << std::endl;
std::cout << "Conntect zum HTTP-Server fehlgeschlagen" << std::endl;
exit (0);
}
else
std::cout << "HTTP-Client Connect erfolgreich ..." << std::endl;
// Sende HTTP Request
static uint8_t http[1024];
strcpy ((char *) http, (char *) "GET / HTTP/1.1\r\n");
strcat ((char *) http, (char *) "Host: domain.de\r\n");
strcat ((char *) http, (char *) "\r\n");
uint16_t SentDataLength;
const int wifiWriteTimeout = 10000;
wifiRc = WIFI_SendData(0, (uint8_t *)http, strlen((char *)http), &SentDataLength, wifiWriteTimeout);
if ((wifiRc == WIFI_STATUS_OK) && (SentDataLength != strlen((char *)http)))
{
std::cout << "HTTP Request senden fehlgeschlagen" << std::endl;
wifiRc = WIFI_STATUS_ERROR;
exit (0);
}
else
std::cout << "HTTP Request '" << http << "' gesendet: " << SentDataLength << std::endl;
// Empfange Server Antwort
static uint8_t resp[1024];
uint16_t respLen;
const int wifiReadTimeout = 10000;
WIFI_ReceiveData (Socket, resp, 1200, &respLen, wifiReadTimeout);
if( respLen > 0)
{
std::cout << "HTTP-Client Server Antwort: " << resp << std::endl;
}
else
{
std::cout << "HTTP-Client: Server Antwort konnte nicht Empfangen werden." << std::endl;
}
}
int main()
{
int ret = 0;
led = 0;
pc.baud(115200);
std::cout << "Programm gestartet ..." << std::endl;
// Verbindung zum WLAN Hotspot herstellem
std::cout << "WLAN: connect" << std::endl;
ret = wifi_sample_run();
if (ret != 0)
{
std::cout << "WLAN: connect fehlgeschlagen" << std::endl;
return -1;
}
WebClient ();
}
Die platformio Ini ist wie oben angegeben.
Ich habe dazu das Paket benutzt:
Code:
# Lib: DISCO_L475VG_IOT01A_wifi installieren
# ID : 3438
pio lib install 3438
Lesezeichen