From f61d57a60360bdfb1304b103a78a5b15b5ca5456 Mon Sep 17 00:00:00 2001 From: kou029w Date: Sat, 15 Apr 2017 23:30:50 +0900 Subject: [PATCH 1/3] =?UTF-8?q?LCD=E3=81=AB=E3=82=B9=E3=83=86=E3=83=BC?= =?UTF-8?q?=E3=82=BF=E3=82=B9=E3=82=92=E8=A1=A8=E7=A4=BA=E3=81=A7=E3=81=8D?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/k2ping.ino | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/src/k2ping.ino b/src/k2ping.ino index 3532e79..9826334 100644 --- a/src/k2ping.ino +++ b/src/k2ping.ino @@ -2,11 +2,30 @@ #include #include +extern "C" { + #include +} + // Digital pins for WeMos D1 static const uint8_t D[] = {3, 1, 16, 5, 4, 14, 12, 13, 0, 2, 15}; // LCD Keypad Shield for Arduino LiquidCrystal lcd(D[8], D[9], D[4], D[5], D[6], D[7]); +void p(char *fmt, ...){ + char buf[17]; + va_list args; + va_start (args, fmt); + vsnprintf(buf, 17, fmt, args); + va_end (args); + lcd.print(buf); +} + +uint8_t errors = 0; +uint8_t success = 0; +int min_time = 0; +int avg_time = 0; +int max_time = 0; +int total_bytes = 0; void setup() { lcd.begin(16, 2); @@ -33,8 +52,68 @@ void setup() { lcd.print("Connected"); lcd.setCursor(0, 1); lcd.print(WiFi.localIP()); + delay(1000); + + IPAddress dest_ip; + WiFi.hostByName("www.google.com", dest_ip); + lcd.clear(); + lcd.print(dest_ip); + + lcd.setCursor(15, 0); + lcd.blink(); + while (analogRead(0) >= 128) yield(); + lcd.noBlink(); + + // Let's enjoy ping! + ping(dest_ip); } void loop() { ; } + +void ping(IPAddress dest_ip) { + ping_option opt; + memset(&opt, 0, sizeof(struct ping_option)); + + opt.count = 4; + opt.coarse_time = 1; + opt.ip = dest_ip; + + opt.recv_function = reinterpret_cast(&ping_recv_cb); + opt.sent_function = NULL; + + lcd.clear(); + lcd.print(IPAddress(opt.ip)); + lcd.setCursor(15, 0); + lcd.print("*"); + + ping_start(&opt); +} + +void ping_recv_cb(void *_option, void *_resp) { + ping_resp* resp = reinterpret_cast(_resp); + ping_option* opt = reinterpret_cast(_option); + + if (resp->ping_err == -1) { + errors++; + lcd.setCursor(0, 1); + lcd.print("Transmit failed."); + } else { + success++; + avg_time += resp->resp_time; + if (min_time == 0 || resp->resp_time < min_time) min_time = resp->resp_time; + if (resp->resp_time > max_time) max_time = resp->resp_time; + total_bytes += resp->bytes; + lcd.setCursor(0, 1); + p((char*)"#%-2d%3dbyte%4dms", resp->seqno, resp->bytes, resp->resp_time); + } + + if (errors + success >= opt->count) { + avg_time /= opt->count; + lcd.clear(); + p((char*)"%4dbyte%3d%%loss", total_bytes, 100 * errors / opt->count); + lcd.setCursor(0, 2); + p((char*)"%4d/%4d/%4dms", min_time, avg_time, max_time); + } +} From 7b9c0f885869b5bdebddde6ee63fe9c4ebb11f8b Mon Sep 17 00:00:00 2001 From: kou029w Date: Fri, 21 Apr 2017 19:08:24 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=E4=B8=8D=E6=AD=A3=E3=81=AA=E5=B9=B3?= =?UTF-8?q?=E5=9D=87=E5=80=A4=E3=81=AB=E3=81=AA=E3=82=8B=E4=B8=8D=E5=85=B7?= =?UTF-8?q?=E5=90=88=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/k2ping.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/k2ping.ino b/src/k2ping.ino index 9826334..99aa307 100644 --- a/src/k2ping.ino +++ b/src/k2ping.ino @@ -110,7 +110,7 @@ void ping_recv_cb(void *_option, void *_resp) { } if (errors + success >= opt->count) { - avg_time /= opt->count; + avg_time /= success; lcd.clear(); p((char*)"%4dbyte%3d%%loss", total_bytes, 100 * errors / opt->count); lcd.setCursor(0, 2); From 42769d8c5203cd5156ed0bf0ebdde3c611cdaef0 Mon Sep 17 00:00:00 2001 From: kou029w Date: Fri, 21 Apr 2017 19:13:45 +0900 Subject: [PATCH 3/3] =?UTF-8?q?resp->resp=5Ftime=E3=81=8C0=E3=81=AE?= =?UTF-8?q?=E5=A0=B4=E5=90=88=E3=80=81=E6=AC=A1=E3=81=AE=E3=83=AC=E3=82=B9?= =?UTF-8?q?=E3=83=9D=E3=83=B3=E3=82=B9=E3=81=AE=E3=81=A8=E3=81=8D=E6=84=8F?= =?UTF-8?q?=E5=9B=B3=E3=81=97=E3=81=AA=E3=81=84=E4=B8=8A=E6=9B=B8=E3=81=8D?= =?UTF-8?q?=E3=81=82=E3=82=8B=E4=B8=8D=E5=85=B7=E5=90=88=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/k2ping.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/k2ping.ino b/src/k2ping.ino index 99aa307..0498b8e 100644 --- a/src/k2ping.ino +++ b/src/k2ping.ino @@ -102,7 +102,7 @@ void ping_recv_cb(void *_option, void *_resp) { } else { success++; avg_time += resp->resp_time; - if (min_time == 0 || resp->resp_time < min_time) min_time = resp->resp_time; + if (success == 1 || resp->resp_time < min_time) min_time = resp->resp_time; if (resp->resp_time > max_time) max_time = resp->resp_time; total_bytes += resp->bytes; lcd.setCursor(0, 1);