|
| 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 | +#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 | + |
| 121 | +ZephyrClient client; |
| 122 | +// the setup function runs once when you press reset or power the board |
| 123 | +void setup() { |
| 124 | + // initialize digital pin LED_BUILTIN as an output. |
| 125 | + pinMode(LED_BUILTIN, OUTPUT); |
| 126 | + Serial.begin(115200); |
| 127 | + _main(); |
| 128 | + delay(20000); |
| 129 | + while (!Serial); |
| 130 | + client.connect("93.184.215.14", 80); |
| 131 | + client.println("GET / HTTP/1.0"); |
| 132 | + client.println("Host: example.com"); |
| 133 | + client.println("Connection: close"); |
| 134 | + client.println(); |
| 135 | +} |
| 136 | + |
| 137 | +// the loop function runs over and over again forever |
| 138 | +void loop() { |
| 139 | + while (client.available()) { |
| 140 | + Serial.write(client.read()); |
| 141 | + } |
| 142 | +} |
0 commit comments