PING meter (ping mérő)

2024.02.05

Ez a videós tartalom 2024 márciusában jelenik meg a YouTube csatornámon. Ez a kód majd az akkor megjelenő tartalmat támogatja.




Az előző képen a 16x2 LCD ping mérő látható és az alábbi kódot használtam

#include <avr/pgmspace.h>

#include <EtherCard.h>

#include <LiquidCrystal.h>

#define CS_PIN 10

#define pinToToggle A2

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

// ---------- Static IP ---------------

static byte myip[] = { 192,168,0,219 }; // itt állítsd be milyen IP címet adsz a kütyünkek

static byte gwip[] = { 192,168,0,1 };

static byte mask[] = { 255,255,255,0 };

static byte dns[] = { 192,168,0,1 };

byte Ethernet::buffer[700];

static int32_t timer;

static int polling;

//------------- ping ----------------

const char srv0[] PROGMEM = "192.168.0.1";

const char srv1[] PROGMEM = "185.53.177.52";

char csrv0[10] = "router";

char csrv1[10] = "nasa.com";

const char* const servers[] PROGMEM = {srv0,srv1};

#define NUMSRVRS (sizeof(servers) / sizeof(servers[0]))

#define MAXCYCLES 3

#define PING_DELAY 2000000

static int cycles;

char sbuffer[30];

static int i;

int j=0;

int szaz_0=1;

int szaz_1=1;

float ms;

float avg_ms[42];

double T_count_0=0;

double T_count_1=0;

long interval = 500;

unsigned long currentTime = 0;

unsigned long previousTime = 0;

LiquidCrystal lcd(8, 9, 7, 6, 5, 4);

void blink() {

currentTime = millis();

if(currentTime - previousTime > interval) {

previousTime = currentTime;

digitalWrite(pinToToggle, !digitalRead(pinToToggle));

}

}

static void gotPinged (byte* ptr) {

ether.printIp(">>> ping from: ", ptr);

}

void setup () {

for (j=0; j<42; j++) avg_ms[j]=0;

lcd.begin(16, 2);

lcd.print("hello, world!");

delay(1000);

pinMode(pinToToggle,OUTPUT);

Serial.begin(57600);

if (!ether.begin(sizeof Ethernet::buffer, mymac, CS_PIN))

Serial.println(F("Failed to access Ethernet controller\n"));

strcpy_P(sbuffer, (char*)pgm_read_word(&(servers[0])));

ether.parseIp(ether.hisip,sbuffer);

if (!ether.staticSetup(myip, gwip, dns, mask))

Serial.println(F("Failed to set Static IP"));

while (ether.clientWaitingGw())

ether.packetLoop(ether.packetReceive());

ether.printIp("IP: ", ether.myip);

ether.printIp("GW: ", ether.gwip);

ether.printIp("Netmask: ", ether.netmask);

ether.printIp("DNS IP: ", ether.dnsip);

ether.registerPingCallback(gotPinged);

timer = -9999999;

polling = 0;

i = NUMSRVRS;

cycles = 3;

digitalWrite(pinToToggle, !digitalRead(pinToToggle));

delay(1000);

digitalWrite(pinToToggle, !digitalRead(pinToToggle));

}

void loop () {

if ((micros() - timer) >= PING_DELAY) {

if (polling) {

ether.printIp("\n",ether.hisip);

lcd.setCursor(9, 0);

lcd.print("Timeout");

if (cycles >= MAXCYCLES) {

cycles = 0;

if (i==0) T_count_0++;

if (i==1) T_count_1++;

blink();

lcd.setCursor(0, 1);

if (i==0)lcd.print(T_count_0*6,0);

if (i==1)lcd.print(T_count_1*6,0);

lcd.print("s");

}

}

else {

polling = 1;

lcd.clear();

lcd.setCursor(0, 0);

++i;

if (i >= NUMSRVRS) i = 0;

strcpy_P(sbuffer, (char*)pgm_read_word(&(servers[i])));

if (i==0) lcd.print(csrv0);

if (i==1) lcd.print(csrv1);

ether.parseIp(ether.hisip,sbuffer);

ether.printIp("Pinging: ", ether.hisip);

}

cycles += 1;

ether.clientIcmpRequest(ether.hisip);

timer = micros();

}

word len = ether.packetReceive();

word pos = ether.packetLoop(len);

if (len > 0 && ether.packetLoopIcmpCheckReply(ether.hisip)) {

ms = (micros() - timer) * 0.001;

lcd.setCursor(9, 0);

lcd.print(ms,1);

lcd.print("ms ");

if (i==0)

{

avg_ms[19]=ms;

for (j=0; j<20; j++) avg_ms[20]=avg_ms[20]+avg_ms[j];

avg_ms[20]=avg_ms[20]/szaz_0;

for (j=1; j<20; j++) avg_ms[j-1]=avg_ms[j];

lcd.setCursor(9, 1);

lcd.print(avg_ms[20],1);

avg_ms[20]=0;

lcd.print("ms");

szaz_0++;

if (szaz_0>20) szaz_0=20;

blink();

}

if (i==1)

{

avg_ms[40]=ms;

for (j=21; j<41; j++) avg_ms[41]=avg_ms[41]+avg_ms[j];

avg_ms[41]=avg_ms[41]/szaz_1;

for (j=22; j<41; j++) avg_ms[j-1]=avg_ms[j];

lcd.setCursor(9, 1);

lcd.print(avg_ms[41],1);

avg_ms[41]=0;

lcd.print("ms");

szaz_1++;

if (szaz_1>20) szaz_1=20;

blink();

}

lcd.setCursor(0, 1);

if (i==0) lcd.print(T_count_0*6,0);

if (i==1) lcd.print(T_count_1*6,0);

lcd.print("s");

polling = 0;

cycles = 0;

delay(2000);

}

}



Az előbbi képe pedig a 240x240 pixeles kiejlzőt használja a ping jelzésére. A kód 1:1 ben működik kijelző nélkül és a D3 portra mutatós műszert kötve (kb. 3.2Volt felel meg 60ms ping értének)

#include <ESP8266WiFi.h>

#include <ESP8266Ping.h>

#include <Adafruit_ST7789.h>

#define TFT_DC D1 // TFT DC pin is connected to NodeMCU pin D1 (GPIO5)

#define TFT_RST D2 // TFT RST pin is connected to NodeMCU pin D2 (GPIO4)

#define TFT_CS D8 // TFT CS pin is connected to NodeMCU pin D8 (GPIO15)

// initialize ST7789 TFT library with hardware SPI module

// SCK (CLK) ---> NodeMCU pin D5 (GPIO14)

// MOSI(DIN) ---> NodeMCU pin D7 (GPIO13)

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);

const char* ssid = "SSID";

const char* password = "JELSZÓ";

const int ledPin = D3;

//const char* remote_host = "www.google.com";

const char* remote_host = "www.nasa.com";

String avg_time_ms;

int i;

int k=0;

int dutyCycle;

void setup() {

Serial.begin(115200);

delay(10);

tft.init(240, 240, SPI_MODE2); // Init ST7789 display 240x240 pixel

tft.setRotation(2);

tft.fillScreen(ST77XX_BLACK);

tft.setCursor(0, 30);

tft.setTextColor(ST77XX_YELLOW);

tft.setTextSize(3);

tft.println("Hello World!");

for(int dutyCycle = 0; dutyCycle < 255; dutyCycle++){

analogWrite(ledPin, dutyCycle);

delay(20);

}

for(int dutyCycle = 255; dutyCycle > 0; dutyCycle--){

analogWrite(ledPin, dutyCycle);

delay(20);

}

Serial.println();

Serial.println("Connecting to WiFi");

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(100);

Serial.print(".");

}

Serial.println();

Serial.print("WiFi connected with ip ");

Serial.println(WiFi.localIP());

Serial.print("Pinging host ");

Serial.println(remote_host);

}

void loop() {

if(Ping.ping(remote_host))

{

Ping.ping(remote_host, 10); //10 time ping

i= Ping.averageTime();

avg_time_ms = Ping.averageTime();

Serial.println(i);

if (i < 31) // good ping value (ms)

{

tft.fillScreen(ST77XX_BLACK);

tft.setTextColor(ST77XX_GREEN);

tft.setTextSize(3);

tft.setCursor(50, 0);

tft.print("INTERNET");

tft.setTextColor(ST77XX_YELLOW);

tft.setTextSize(3);

tft.setCursor(50, 100);

tft.setTextColor(ST77XX_WHITE);

tft.print("Pingtime ");

tft.setTextColor(ST77XX_YELLOW);

tft.setTextSize(3);

tft.setCursor(80, 150);

tft.print(avg_time_ms);

tft.print(" ms");

tft.setCursor(0, 200);

tft.println("Allapot: JO"); // 60m-ig jó

analogWrite(ledPin, i*4);

}

if (i > 30 && i < 62) // not so good ping value (ms)

{

tft.fillScreen(ST77XX_BLACK);

tft.setTextColor(ST77XX_GREEN);

tft.setTextSize(3);

tft.setCursor(50, 0);

tft.print("INTERNET");

tft.setTextColor(ST77XX_YELLOW);

tft.setTextSize(3);

tft.setCursor(50, 100);

tft.setTextColor(ST77XX_WHITE);

tft.print("Pingtime ");

tft.setTextColor(ST77XX_YELLOW);

tft.setTextSize(3);

tft.setCursor(80, 150);

tft.print(avg_time_ms);

tft.print(" ms");

tft.setCursor(0, 200);

tft.println("Allapot: ROSSZ");

analogWrite(ledPin, i*4);

}

}

else // KO ping

{

Serial.println("Error :(");

tft.fillScreen(ST77XX_BLACK);

tft.setTextColor(ST77XX_GREEN);

tft.setTextSize(3);

tft.setCursor(50, 0);

tft.print("INTERNET");

tft.setTextColor(ST77XX_YELLOW);

tft.setTextSize(3);

tft.setCursor(50, 100);

tft.setTextColor(ST77XX_WHITE);

tft.print("Pingtime ");

tft.setTextColor(ST77XX_YELLOW);

tft.setTextSize(3);

tft.setCursor(80, 150);

tft.print("ERROR");

tft.setCursor(0, 200);

tft.print("Allapot:");

tft.setTextColor(ST77XX_RED);

tft.println("KO");

if (k==0) {k=1; analogWrite(ledPin, 253); }

if (k==1) {k=0; analogWrite(ledPin, 10); }

}

delay(10000); // 10 sec

}


Készítsd el weboldaladat ingyen! Ez a weboldal a Webnode segítségével készült. Készítsd el a sajátodat ingyenesen még ma! Kezdd el