|
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 |
| - |
34 | 1 | #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" |
121 | 3 |
|
122 | 4 | 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 | + |
125 | 7 | void setup() {
|
126 |
| - // initialize digital pin LED_BUILTIN as an output. |
| 8 | + |
127 | 9 | pinMode(LED_BUILTIN, OUTPUT);
|
128 | 10 | 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 | + |
133 | 19 | auto res = client.connect("example.com", 80);
|
134 |
| - Serial.println(res); |
135 | 20 | res = client.println("GET / HTTP/1.0");
|
136 |
| - Serial.println(res); |
137 | 21 | client.println("Host: example.com");
|
138 | 22 | client.println("Connection: close");
|
139 | 23 | client.println();
|
|
0 commit comments