Skip to content

Wifi library: extending apis and improving library #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions libraries/SocketWrapper/WiFi.cpp

This file was deleted.

105 changes: 0 additions & 105 deletions libraries/SocketWrapper/WiFi.h

This file was deleted.

106 changes: 106 additions & 0 deletions libraries/WiFi/examples/WiFiWebClient/WiFiWebClient.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Web client

This sketch connects to a website (http://example.com) using the WiFi module.

This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.

created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
*/

#include <ZephyrClient.h>
#include <WiFi.h>

#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
// IPAddress server(93,184,216,34); // IP address for example.com (no DNS)
char server[] = "example.com"; // host name for example.com (using DNS)

ZephyrClient client;

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// check for the WiFi module:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
Serial.println(status);
// wait 3 seconds for connection:
delay(3000);
}
Serial.println("Connected to wifi");
printWifiStatus();

Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected to server");
// Make a HTTP request:
client.println("GET /index.html HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
}
}

void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial.write(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();

// do nothing forevermore:
while (true);
}
}

void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
2 changes: 2 additions & 0 deletions libraries/WiFi/examples/WiFiWebClient/arduino_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define SECRET_SSID ""
#define SECRET_PASS ""
10 changes: 10 additions & 0 deletions libraries/WiFi/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=WiFi
version=1.0.0
author=Arduino
maintainer=Arduino <info@arduino.cc>
sentence=Enables WIFI connection
paragraph=With this library you can use the WiFi to connect to Internet.
category=Communication
url=https://github.com/arduino/ArduinoCore-zephyr/tree/master/libraries/WiFi
architectures=renesas,renesas_portenta
includes=WiFi.h
104 changes: 104 additions & 0 deletions libraries/WiFi/src/WiFi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "WiFi.h"

WiFiClass WiFi;

String WiFiClass::firmwareVersion() { // TODO integrate fw version detection
#if defined(ARDUINO_PORTENTA_C33)
return "v1.5.0";
#elif defined(ARDUINO_PORTENTA_H7) || defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA) ||\
defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_NICLA_SENSE_ME)
return "v0.0.0";
#else
return "v0.0.0";
#endif
}


int WiFiClass::begin(const char* ssid, const char* passphrase, wl_enc_type security, bool blocking) {
sta_iface = net_if_get_wifi_sta();
netif = sta_iface;
sta_config.ssid = (const uint8_t *)ssid;
sta_config.ssid_length = strlen(ssid);
sta_config.psk = (const uint8_t *)passphrase;
sta_config.psk_length = strlen(passphrase);
// TODO: change these fields with scan() results
sta_config.security = WIFI_SECURITY_TYPE_PSK;
sta_config.channel = WIFI_CHANNEL_ANY;
sta_config.band = WIFI_FREQ_BAND_2_4_GHZ;
sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;

int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config,
sizeof(struct wifi_connect_req_params));
if (ret) {
return false;
}

if (blocking) {
net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, NULL, NULL, K_FOREVER);
}
// FIXME verify that non in blocking version dhcp can be called even if connect is not completed
NetworkInterface::begin(blocking, NET_EVENT_WIFI_MASK);

return status();
}

int WiFiClass::beginAP(char* ssid, char* passphrase, int channel, bool blocking) {
if (ap_iface != NULL) {
return false;
}
ap_iface = net_if_get_wifi_sap();
netif = ap_iface;
ap_config.ssid = (const uint8_t *)ssid;
ap_config.ssid_length = strlen(ssid);
ap_config.psk = (const uint8_t *)passphrase;
ap_config.psk_length = strlen(passphrase);
ap_config.security = WIFI_SECURITY_TYPE_PSK;
ap_config.channel = channel;
ap_config.band = WIFI_FREQ_BAND_2_4_GHZ;
ap_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;

int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, ap_iface, &ap_config,
sizeof(struct wifi_connect_req_params));

if (ret) {
return false;
}

enable_dhcpv4_server(ap_iface);

if (blocking) {
net_mgmt_event_wait_on_iface(ap_iface, NET_EVENT_WIFI_AP_ENABLE_RESULT, NULL, NULL, NULL, K_FOREVER);
}

return status();
}

int WiFiClass::status() {
sta_iface = net_if_get_wifi_sta();
netif = sta_iface;
if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &sta_state,
sizeof(struct wifi_iface_status))) {
return WL_NO_SHIELD;
}

if (sta_state.state >= WIFI_STATE_ASSOCIATED) {
return WL_CONNECTED;
} else {
return WL_DISCONNECTED;
}
return WL_NO_SHIELD;
}

char* WiFiClass::SSID() {
if (status() == WL_CONNECTED) {
return (char *)sta_state.ssid;
}
return nullptr;
}

int32_t WiFiClass::RSSI() {
if (status() == WL_CONNECTED) {
return sta_state.rssi;
}
return 0;
}
44 changes: 44 additions & 0 deletions libraries/WiFi/src/WiFi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "SocketHelpers.h"

#include "utility/wl_definitions.h"
#include <zephyr/net/wifi_mgmt.h>

#define NET_EVENT_WIFI_MASK \
(NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT | \
NET_EVENT_WIFI_AP_ENABLE_RESULT | NET_EVENT_WIFI_AP_DISABLE_RESULT | \
NET_EVENT_WIFI_AP_STA_CONNECTED | NET_EVENT_WIFI_AP_STA_DISCONNECTED | \
NET_EVENT_WIFI_SCAN_RESULT)


class WiFiClass: public NetworkInterface
{
public:
WiFiClass() {}
~WiFiClass() {}

int begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN, bool blocking = true);

int beginAP(char* ssid, char* passphrase, int channel = WIFI_CHANNEL_ANY, bool blocking = true);

int status();

int8_t scanNetworks() {
// TODO: borrow code from mbed core for scan results handling
}

char* SSID();
int32_t RSSI();

String firmwareVersion();

private:
struct net_if *sta_iface = nullptr;
struct net_if *ap_iface = nullptr;

struct wifi_connect_req_params ap_config;
struct wifi_connect_req_params sta_config;

struct wifi_iface_status sta_state = { 0 };
};

extern WiFiClass WiFi;
Loading