Skip to content

Commit f66efa8

Browse files
committed
SocketWrapper: pure arduino examples
1 parent 9fdb174 commit f66efa8

File tree

2 files changed

+84
-128
lines changed

2 files changed

+84
-128
lines changed

libraries/SocketWrapper/examples/TestClient/TestClient.ino

Lines changed: 12 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,23 @@
1-
/*
2-
Blink
3-
4-
Turns an LED on for one second, then off for one second, repeatedly.
5-
6-
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
7-
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
8-
the correct LED pin independent of which board is used.
9-
If you want to know what pin the on-board LED is connected to on your Arduino
10-
model, check the Technical Specs of your board at:
11-
https://www.arduino.cc/en/Main/Products
12-
13-
modified 8 May 2014
14-
by Scott Fitzgerald
15-
modified 2 Sep 2016
16-
by Arturo Guadalupi
17-
modified 8 Sep 2016
18-
by Colby Newman
19-
20-
This example code is in the public domain.
21-
22-
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
23-
*/
24-
#include <zephyr/kernel.h>
25-
#include <zephyr/linker/sections.h>
26-
#include <errno.h>
27-
#include <stdio.h>
28-
29-
#include <zephyr/net/net_if.h>
30-
#include <zephyr/net/net_core.h>
31-
#include <zephyr/net/net_context.h>
32-
#include <zephyr/net/net_mgmt.h>
33-
341
#include <ZephyrClient.h>
35-
36-
#define LOG_INF printk
37-
38-
#define DHCP_OPTION_NTP (42)
39-
40-
static uint8_t ntp_server[4];
41-
42-
static struct net_mgmt_event_callback mgmt_cb;
43-
44-
static struct net_dhcpv4_option_callback dhcp_cb;
45-
46-
static void start_dhcpv4_client(struct net_if *iface, void *user_data)
47-
{
48-
ARG_UNUSED(user_data);
49-
50-
LOG_INF("Start on %s: index=%d", net_if_get_device(iface)->name,
51-
net_if_get_by_iface(iface));
52-
net_dhcpv4_start(iface);
53-
}
54-
55-
static void handler(struct net_mgmt_event_callback *cb,
56-
uint32_t mgmt_event,
57-
struct net_if *iface)
58-
{
59-
int i = 0;
60-
61-
if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
62-
return;
63-
}
64-
65-
for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
66-
char buf[NET_IPV4_ADDR_LEN];
67-
68-
if (iface->config.ip.ipv4->unicast[i].ipv4.addr_type !=
69-
NET_ADDR_DHCP) {
70-
continue;
71-
}
72-
73-
LOG_INF(" Address[%d]: %s", net_if_get_by_iface(iface),
74-
net_addr_ntop(AF_INET,
75-
&iface->config.ip.ipv4->unicast[i].ipv4.address.in_addr,
76-
buf, sizeof(buf)));
77-
LOG_INF(" Subnet[%d]: %s", net_if_get_by_iface(iface),
78-
net_addr_ntop(AF_INET,
79-
&iface->config.ip.ipv4->unicast[i].netmask,
80-
buf, sizeof(buf)));
81-
LOG_INF(" Router[%d]: %s", net_if_get_by_iface(iface),
82-
net_addr_ntop(AF_INET,
83-
&iface->config.ip.ipv4->gw,
84-
buf, sizeof(buf)));
85-
LOG_INF("Lease time[%d]: %u seconds", net_if_get_by_iface(iface),
86-
iface->config.dhcpv4.lease_time);
87-
}
88-
}
89-
90-
static void option_handler(struct net_dhcpv4_option_callback *cb,
91-
size_t length,
92-
enum net_dhcpv4_msg_type msg_type,
93-
struct net_if *iface)
94-
{
95-
char buf[NET_IPV4_ADDR_LEN];
96-
97-
LOG_INF("DHCP Option %d: %s", cb->option,
98-
net_addr_ntop(AF_INET, cb->data, buf, sizeof(buf)));
99-
}
100-
101-
int _main(void)
102-
{
103-
LOG_INF("Run dhcpv4 client");
104-
105-
//net_mgmt_init_event_callback(&mgmt_cb, handler,
106-
// NET_EVENT_IPV4_ADDR_ADD);
107-
//net_mgmt_add_event_callback(&mgmt_cb);
108-
109-
net_dhcpv4_init_option_callback(&dhcp_cb, option_handler,
110-
DHCP_OPTION_NTP, ntp_server,
111-
sizeof(ntp_server));
112-
113-
net_dhcpv4_add_option_callback(&dhcp_cb);
114-
115-
net_if_foreach(start_dhcpv4_client, NULL);
116-
return 0;
117-
}
118-
119-
#define Serial Serial1
120-
2+
#include "Ethernet.h"
1213

1224
ZephyrClient client;
123-
IPAddress addr(93,184,215,14);
124-
// the setup function runs once when you press reset or power the board
5+
IPAddress addr(93, 184, 215, 14);
6+
1257
void setup() {
126-
// initialize digital pin LED_BUILTIN as an output.
8+
1279
pinMode(LED_BUILTIN, OUTPUT);
12810
Serial.begin(115200);
129-
_main();
130-
delay(5000);
131-
while (!Serial);
132-
//auto res = client.connect(addr, 80);
11+
12+
while (Ethernet.linkStatus() != LinkON) {
13+
Serial.println("waiting for link on");
14+
delay(100);
15+
}
16+
Ethernet.begin();
17+
Serial.println(Ethernet.localIP());
18+
13319
auto res = client.connect("example.com", 80);
134-
Serial.println(res);
13520
res = client.println("GET / HTTP/1.0");
136-
Serial.println(res);
13721
client.println("Host: example.com");
13822
client.println("Connection: close");
13923
client.println();
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "ZephyrServer.h"
2+
#include "Ethernet.h"
3+
4+
ZephyrServer server(80);
5+
6+
void setup() {
7+
Serial.begin(115200);
8+
9+
while (Ethernet.linkStatus() != LinkON) {
10+
Serial.println("waiting for link on");
11+
delay(100);
12+
}
13+
Ethernet.begin();
14+
Serial.println(Ethernet.localIP());
15+
server.begin();
16+
}
17+
18+
void loop() {
19+
20+
ZephyrClient client = server.available();
21+
if (client) {
22+
Serial.println("new client");
23+
// an http request ends with a blank line
24+
bool currentLineIsBlank = true;
25+
while (client.connected()) {
26+
if (client.available()) {
27+
char c = client.read();
28+
Serial.write(c);
29+
// if you've gotten to the end of the line (received a newline
30+
// character) and the line is blank, the http request has ended,
31+
// so you can send a reply
32+
if (c == '\n' && currentLineIsBlank) {
33+
// send a standard http response header
34+
client.println("HTTP/1.1 200 OK");
35+
client.println("Content-Type: text/html");
36+
client.println("Connection: close"); // the connection will be closed after completion of the response
37+
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
38+
client.println();
39+
client.println("<!DOCTYPE HTML>");
40+
client.println("<html>");
41+
// output the value of each analog input pin
42+
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
43+
int sensorReading = random(analogChannel, 100);
44+
client.print("analog input ");
45+
client.print(analogChannel);
46+
client.print(" is ");
47+
client.print(sensorReading);
48+
client.println("<br />");
49+
}
50+
client.println("</html>");
51+
break;
52+
}
53+
54+
if (c == '\n') {
55+
// you're starting a new line
56+
currentLineIsBlank = true;
57+
58+
} else if (c != '\r') {
59+
// you've gotten a character on the current line
60+
currentLineIsBlank = false;
61+
}
62+
}
63+
}
64+
// give the web browser time to receive the data
65+
delay(1);
66+
// close the connection:
67+
client.stop();
68+
Serial.println("client disconnected");
69+
} else {
70+
delay(100);
71+
}
72+
}

0 commit comments

Comments
 (0)