Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1b42661

Browse files
authored
Merge pull request #205 from pennam/wifi-web-client-rebased
Wifi: extendig API and examples
2 parents 058595d + 839384f commit 1b42661

File tree

8 files changed

+260
-113
lines changed

8 files changed

+260
-113
lines changed

‎libraries/SocketWrapper/WiFi.cpp‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎libraries/SocketWrapper/WiFi.h‎

Lines changed: 0 additions & 110 deletions
This file was deleted.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
Web client
3+
4+
This sketch connects to a website (http://example.com) using the WiFi module.
5+
6+
This example is written for a network using WPA encryption. For
7+
WEP or WPA, change the Wifi.begin() call accordingly.
8+
9+
created 13 July 2010
10+
by dlf (Metodo2 srl)
11+
modified 31 May 2012
12+
by Tom Igoe
13+
*/
14+
15+
#include <ZephyrClient.h>
16+
#include <WiFi.h>
17+
18+
#include "arduino_secrets.h"
19+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
20+
char ssid[] = SECRET_SSID; // your network SSID (name)
21+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
22+
int keyIndex = 0; // your network key Index number (needed only for WEP)
23+
24+
int status = WL_IDLE_STATUS;
25+
// if you don't want to use DNS (and reduce your sketch size)
26+
// use the numeric IP instead of the name for the server:
27+
// IPAddress server(93,184,216,34); // IP address for example.com (no DNS)
28+
char server[] = "example.com"; // host name for example.com (using DNS)
29+
30+
ZephyrClient client;
31+
32+
void setup() {
33+
//Initialize serial and wait for port to open:
34+
Serial.begin(9600);
35+
while (!Serial) {
36+
; // wait for serial port to connect. Needed for native USB port only
37+
}
38+
39+
// check for the WiFi module:
40+
if (WiFi.status() == WL_NO_SHIELD) {
41+
Serial.println("Communication with WiFi module failed!");
42+
// don't continue
43+
while (true)
44+
;
45+
}
46+
47+
// attempt to connect to Wifi network:
48+
while (status != WL_CONNECTED) {
49+
Serial.print("Attempting to connect to SSID: ");
50+
Serial.println(ssid);
51+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
52+
status = WiFi.begin(ssid, pass);
53+
// wait 3 seconds for connection:
54+
delay(3000);
55+
}
56+
Serial.println("Connected to wifi");
57+
printWifiStatus();
58+
59+
Serial.println("\nStarting connection to server...");
60+
// if you get a connection, report back via serial:
61+
if (client.connect(server, 80)) {
62+
Serial.println("connected to server");
63+
// Make a HTTP request:
64+
client.println("GET /index.html HTTP/1.1");
65+
client.print("Host: ");
66+
client.println(server);
67+
client.println("Connection: close");
68+
client.println();
69+
}
70+
}
71+
72+
void loop() {
73+
// if there are incoming bytes available
74+
// from the server, read them and print them:
75+
while (client.available()) {
76+
char c = client.read();
77+
Serial.write(c);
78+
}
79+
80+
// if the server's disconnected, stop the client:
81+
if (!client.connected()) {
82+
Serial.println();
83+
Serial.println("disconnecting from server.");
84+
client.stop();
85+
86+
// do nothing forevermore:
87+
while (true)
88+
;
89+
}
90+
}
91+
92+
void printWifiStatus() {
93+
// print the SSID of the network you're attached to:
94+
Serial.print("SSID: ");
95+
Serial.println(WiFi.SSID());
96+
97+
// print your board's IP address:
98+
IPAddress ip = WiFi.localIP();
99+
Serial.print("IP Address: ");
100+
Serial.println(ip);
101+
102+
// print the received signal strength:
103+
long rssi = WiFi.RSSI();
104+
Serial.print("signal strength (RSSI):");
105+
Serial.print(rssi);
106+
Serial.println(" dBm");
107+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

‎libraries/WiFi/library.properties‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=WiFi
2+
version=0.1.0
3+
author=Arduino
4+
maintainer=Arduino <info@arduino.cc>
5+
sentence=Enables WiFi connection
6+
paragraph=With this library you can use the WiFi to connect to Internet.
7+
category=Communication
8+
url=https://github.com/arduino/ArduinoCore-zephyr/tree/master/libraries/WiFi
9+
architectures=zephyr_main,zephyr_contrib
10+
includes=WiFi.h

‎libraries/WiFi/src/WiFi.cpp‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include "WiFi.h"
2+
3+
WiFiClass WiFi;
4+
5+
String WiFiClass::firmwareVersion() {
6+
#if defined(ARDUINO_PORTENTA_C33)
7+
return "v1.5.0";
8+
#else
9+
return "v0.0.0";
10+
#endif
11+
}
12+
13+
int WiFiClass::begin(const char *ssid, const char *passphrase, wl_enc_type security,
14+
bool blocking) {
15+
sta_iface = net_if_get_wifi_sta();
16+
netif = sta_iface;
17+
sta_config.ssid = (const uint8_t *)ssid;
18+
sta_config.ssid_length = strlen(ssid);
19+
sta_config.psk = (const uint8_t *)passphrase;
20+
sta_config.psk_length = strlen(passphrase);
21+
// TODO: change these fields with scan() results
22+
sta_config.security = WIFI_SECURITY_TYPE_PSK;
23+
sta_config.channel = WIFI_CHANNEL_ANY;
24+
sta_config.band = WIFI_FREQ_BAND_2_4_GHZ;
25+
sta_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;
26+
int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config,
27+
sizeof(struct wifi_connect_req_params));
28+
if (ret) {
29+
return false;
30+
}
31+
NetworkInterface::begin(false, NET_EVENT_WIFI_MASK);
32+
if (blocking) {
33+
net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, NULL, NULL,
34+
K_FOREVER);
35+
}
36+
return status();
37+
}
38+
39+
bool WiFiClass::beginAP(char *ssid, char *passphrase, int channel, bool blocking) {
40+
if (ap_iface != NULL) {
41+
return false;
42+
}
43+
ap_iface = net_if_get_wifi_sap();
44+
netif = ap_iface;
45+
ap_config.ssid = (const uint8_t *)ssid;
46+
ap_config.ssid_length = strlen(ssid);
47+
ap_config.psk = (const uint8_t *)passphrase;
48+
ap_config.psk_length = strlen(passphrase);
49+
ap_config.security = WIFI_SECURITY_TYPE_PSK;
50+
ap_config.channel = channel;
51+
ap_config.band = WIFI_FREQ_BAND_2_4_GHZ;
52+
ap_config.bandwidth = WIFI_FREQ_BANDWIDTH_20MHZ;
53+
int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, ap_iface, &ap_config,
54+
sizeof(struct wifi_connect_req_params));
55+
if (ret) {
56+
return false;
57+
}
58+
enable_dhcpv4_server(ap_iface);
59+
if (blocking) {
60+
net_mgmt_event_wait_on_iface(ap_iface, NET_EVENT_WIFI_AP_ENABLE_RESULT, NULL, NULL, NULL,
61+
K_FOREVER);
62+
}
63+
return true;
64+
}
65+
66+
int WiFiClass::status() {
67+
sta_iface = net_if_get_wifi_sta();
68+
netif = sta_iface;
69+
if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &sta_state,
70+
sizeof(struct wifi_iface_status))) {
71+
return WL_NO_SHIELD;
72+
}
73+
if (sta_state.state >= WIFI_STATE_ASSOCIATED) {
74+
return WL_CONNECTED;
75+
} else {
76+
return WL_DISCONNECTED;
77+
}
78+
return WL_NO_SHIELD;
79+
}
80+
81+
int8_t WiFiClass::scanNetworks() {
82+
// TODO: borrow code from mbed core for scan results handling
83+
}
84+
85+
char *WiFiClass::SSID() {
86+
if (status() == WL_CONNECTED) {
87+
return (char *)sta_state.ssid;
88+
}
89+
return nullptr;
90+
}
91+
92+
int32_t WiFiClass::RSSI() {
93+
if (status() == WL_CONNECTED) {
94+
return sta_state.rssi;
95+
}
96+
return 0;
97+
}

‎libraries/WiFi/src/WiFi.h‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "SocketHelpers.h"
2+
3+
#include "utility/wl_definitions.h"
4+
#include <zephyr/net/wifi_mgmt.h>
5+
6+
#define NET_EVENT_WIFI_MASK \
7+
(NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT | \
8+
NET_EVENT_WIFI_AP_ENABLE_RESULT | NET_EVENT_WIFI_AP_DISABLE_RESULT | \
9+
NET_EVENT_WIFI_AP_STA_CONNECTED | NET_EVENT_WIFI_AP_STA_DISCONNECTED | \
10+
NET_EVENT_WIFI_SCAN_RESULT)
11+
12+
class WiFiClass : public NetworkInterface {
13+
public:
14+
WiFiClass() {
15+
}
16+
17+
~WiFiClass() {
18+
}
19+
20+
int begin(const char *ssid, const char *passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN,
21+
bool blocking = true);
22+
bool beginAP(char *ssid, char *passphrase, int channel = WIFI_CHANNEL_ANY,
23+
bool blocking = false);
24+
25+
int status();
26+
27+
int8_t scanNetworks();
28+
29+
char *SSID();
30+
int32_t RSSI();
31+
32+
String firmwareVersion();
33+
34+
private:
35+
struct net_if *sta_iface = nullptr;
36+
struct net_if *ap_iface = nullptr;
37+
38+
struct wifi_connect_req_params ap_config;
39+
struct wifi_connect_req_params sta_config;
40+
41+
struct wifi_iface_status sta_state = {0};
42+
};
43+
44+
extern WiFiClass WiFi;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /