|
| 1 | +#include <WiFi.h> |
| 2 | +#include <WireGuard-ESP32.h> |
| 3 | +#include <WiFiUdp.h> |
| 4 | + |
| 5 | +// WiFi configuration --- UPDATE this configuration for your WiFi AP |
| 6 | +char ssid[] = "ssid"; |
| 7 | +char password[] = "password"; |
| 8 | + |
| 9 | +// WireGuard configuration --- UPDATE this configuration from JSON |
| 10 | +char private_key[] = "(Private Key) "; // [Interface] PrivateKey |
| 11 | +IPAddress local_ip(1,2,3,4); // [Interface] Address |
| 12 | +char public_key[] = "(Public Key)"; // [Peer] PublicKey |
| 13 | +char endpoint_address[] = "link.arc.soracom.io"; // [Peer] Endpoint |
| 14 | +int endpoint_port = 11010; // [Peer] Endpoint |
| 15 | + |
| 16 | +static constexpr const uint32_t UPDATE_INTERVAL_MS = 5000; |
| 17 | + |
| 18 | +static WireGuard wg; |
| 19 | + |
| 20 | +void setup() |
| 21 | +{ |
| 22 | + Serial.begin(115200); |
| 23 | + Serial.println("Connecting to the AP..."); |
| 24 | + WiFi.begin(ssid, password); |
| 25 | + while( !WiFi.isConnected() ) { |
| 26 | + delay(1000); |
| 27 | + } |
| 28 | + Serial.println("Adjusting system time..."); |
| 29 | + configTime(9 * 60 * 60, 0, "ntp.jst.mfeed.ad.jp", "ntp.nict.jp", "time.google.com"); |
| 30 | + |
| 31 | + Serial.println("Connected. Initializing WireGuard..."); |
| 32 | + wg.begin( |
| 33 | + local_ip, |
| 34 | + private_key, |
| 35 | + endpoint_address, |
| 36 | + public_key, |
| 37 | + endpoint_port); |
| 38 | +} |
| 39 | + |
| 40 | +void loop() |
| 41 | +{ |
| 42 | + static uint64_t send_count = 0; |
| 43 | + WiFiUDP client; |
| 44 | + if( !client.beginPacket("uni.soracom.io", 23080) ) { |
| 45 | + Serial.println("Failed to begin packet..."); |
| 46 | + delay(5000); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + uint64_t uptime_msec = millis(); |
| 51 | + uint8_t buffer[16]; |
| 52 | + buffer[ 0] = (uptime_msec >> 0) & 0xff; |
| 53 | + buffer[ 1] = (uptime_msec >> 8) & 0xff; |
| 54 | + buffer[ 2] = (uptime_msec >> 16) & 0xff; |
| 55 | + buffer[ 3] = (uptime_msec >> 24) & 0xff; |
| 56 | + buffer[ 4] = (uptime_msec >> 32) & 0xff; |
| 57 | + buffer[ 5] = (uptime_msec >> 40) & 0xff; |
| 58 | + buffer[ 6] = (uptime_msec >> 48) & 0xff; |
| 59 | + buffer[ 7] = (uptime_msec >> 56) & 0xff; |
| 60 | + buffer[ 8] = (send_count >> 0) & 0xff; |
| 61 | + buffer[ 9] = (send_count >> 8) & 0xff; |
| 62 | + buffer[10] = (send_count >> 16) & 0xff; |
| 63 | + buffer[11] = (send_count >> 24) & 0xff; |
| 64 | + buffer[12] = (send_count >> 32) & 0xff; |
| 65 | + buffer[13] = (send_count >> 40) & 0xff; |
| 66 | + buffer[14] = (send_count >> 48) & 0xff; |
| 67 | + buffer[15] = (send_count >> 56) & 0xff; |
| 68 | + |
| 69 | + Serial.printf("Sending uptime %lu [ms], count=%d\r\n", uptime_msec, send_count); |
| 70 | + client.write(buffer, sizeof(buffer)); |
| 71 | + client.endPacket(); |
| 72 | + |
| 73 | + send_count++; |
| 74 | + delay(UPDATE_INTERVAL_MS); |
| 75 | +} |
0 commit comments