ola amigos.
segue a quem interesse o sketch que escrevi para testar a internet esta online e analisar perda de pacotes e apos isso ele posta no twitter.
espero que ajude. tentei deixar o mais explicativo possivel no codigo.
Grande abraço a todos.
/*
PINGER E MONITOR
Desenvolvido para testar perda de conectividade e pacotes da internet
sao ao todo 4 teste
1 Roteador local
2 gateway de seu provedor
3 site de amostra
4 site de segunda amostro para previnir erros caso site 1 esteja fora do ar.
Tesminado testes ele calcula as medias e posta no twitter
Desenvolvido por Marcos Roberto Batista de Souza
https://www.facebook.com/marcoseanaka
marcos_anaka@hotmail.com
*/
#include <SPI.h>
#include <Ethernet.h>
#include <ICMPPing.h>
#include <Twitter.h>
//---------------------------------------------------------- configuraçoes de Ethernet.
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // max address for ethernet shield
byte ip[] = {192,168,25,142}; // Coloca aqui o seu IP
Twitter twitter("AQUI COLOQUE SEU CODIGO DE API DO TWITTER");
// ----------------------------------------------------------- configurações do pacote
#define REQ_DATASIZE 1400 // tamanho do pacote
#define PING_TIMEOUT 1000 // tempo limite do pacote
SOCKET pingSocket = 0;
ICMPPing ping(pingSocket, (uint16_t)random(0, 255));
char buffer [256];
int numerodepings = 20;
//---------------------------------------------------------------------------------------
//------------------------------------------------------------------------------Variaveis
int numping =0; // contador de Ping
int ida = 0; // contador seletor do ip
int testn =1; // conta numero de teste realizado e auxilia a evitar erro de mensagem duplicada no twitter
int perdidosRT = 0; // contador de pacotes perdidos
int perdidosGW = 0;
int perdidosST1 = 0;
int perdidosST2 = 0;
int perdidosTL = 0;
int timeRT = 0; // contadores de tempo
int timeGW = 0;
int timeST1 = 0;
int timeST2 = 0;
int timeTL = 0;
int porcRT = 0; // porcentagem de perda
int porcGW = 0;
int porcST1 = 0;
int porcST2 = 0;
int porcentagemTL=0;
//--------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------hosts a pingar
int roteador[] = {192,168,25,1}; // preferencialmente seu roteador local
String label1 = "Router"; // label do equipamento ou site
IPAddress pingAddrRT(roteador[0],roteador[1],roteador[2],roteador[3]);
int gateway[]= {179,185,128,61};// gateway do seu provedor
String label2 = "GW GVT"; // label do equipamento ou site
IPAddress pingAddrGW(gateway[0],gateway[1],gateway[2],gateway[3]);
int site1[] = {200,221,2,45};// site de teste numero 1
String label3 = "UOL"; // label do equipamento ou site
IPAddress pingAddrST1(site1[0],site1[1],site1[2],site1[3]);
int site2[] = {200,192,176,65};// site de teste numero 2
String label4 = "Terra"; // label do equipamento ou site
IPAddress pingAddrST2(site2[0],site2[1],site2[2],site2[3]);
//-------------------------------------------------------------------------------------------
void setup()
{
// start Ethernet
Ethernet.begin(mac, ip);
Serial.begin(9600);
}
void loop()
{
switch (ida) {
case 0:
Serial.println("================================================");
Serial.print("Iniciando ping ao IP :");
Serial.println(pingAddrRT);
Serial.println("------------------------------------------------");
Serial.print("Host :");
Serial.println(label1);
Serial.println("================================================");
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
while (numping < numerodepings )
{
numping++; //incremente numero do ping
ICMPEchoReply echoReply = ping(pingAddrRT, 4);
if (echoReply.status == SUCCESS)
{
sprintf(buffer,
"Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%dms TTL=%d",
echoReply.data.seq,
echoReply.addr[0],
echoReply.addr[1],
echoReply.addr[2],
echoReply.addr[3],
REQ_DATASIZE,
millis() - echoReply.data.time,
echoReply.ttl);
timeRT = timeRT + millis() - echoReply.data.time;
}
else
{
sprintf(buffer, "Echo request failed; %d", echoReply.status);
perdidosRT++;
}
Serial.println(buffer);
delay(1000);
}
timeRT = timeRT / numerodepings; // define a media de tempo dos pings
porcRT = perdidosRT * 100 / numerodepings; // porcentagem de perda
Serial.println(" ");
Serial.println("Ping encerrado!");
Serial.println(" ");
numping =0;
ida++;
break;
case 1:
if (ida==1)// pinga gateway
{
Serial.println("================================================");
Serial.print("Iniciando ping ao IP :");
Serial.println(pingAddrGW);
Serial.println("------------------------------------------------");
Serial.print("Host :");
Serial.println(label2);
Serial.println("================================================");
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
while (numping < numerodepings )
{
numping++; //incremente numero do ping
ICMPEchoReply echoReply = ping(pingAddrGW, 4);
if (echoReply.status == SUCCESS)
{
sprintf(buffer,
"Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%dms TTL=%d",
echoReply.data.seq,
echoReply.addr[0],
echoReply.addr[1],
echoReply.addr[2],
echoReply.addr[3],
REQ_DATASIZE,
millis() - echoReply.data.time,
echoReply.ttl);
timeGW = timeGW + millis() - echoReply.data.time;
}
else
{
sprintf(buffer, "Echo request failed; %d", echoReply.status);
perdidosGW++;
}
Serial.println(buffer);
delay(1000);
}
timeGW = timeGW / numerodepings; // define a media de tempo dos pings
porcGW = perdidosGW * 100 / numerodepings; // porcentagem de perda
Serial.println(" ");
Serial.println("Ping encerrado!");
Serial.println(" ");
numping =0;
ida++;
break;
case 2:
Serial.println("================================================");
Serial.print("Iniciando ping ao IP :");
Serial.println(pingAddrST1);
Serial.println("------------------------------------------------");
Serial.print("Host :");
Serial.println(label3);
Serial.println("================================================");
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
while (numping < numerodepings )
{
numping++; //incremente numero do ping
ICMPEchoReply echoReply = ping(pingAddrST1, 4);
if (echoReply.status == SUCCESS)
{
sprintf(buffer,
"Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%dms TTL=%d",
echoReply.data.seq,
echoReply.addr[0],
echoReply.addr[1],
echoReply.addr[2],
echoReply.addr[3],
REQ_DATASIZE,
millis() - echoReply.data.time,
echoReply.ttl);
timeST1 = timeST1 + millis() - echoReply.data.time;
}
else
{
sprintf(buffer, "Echo request failed; %d", echoReply.status);
perdidosST1++;
}
Serial.println(buffer);
delay(1000);
}
timeST1 = timeST1 / numerodepings; // define a media de tempo dos pings
porcST1 = perdidosST1 * 100 / numerodepings; // porcentagem de perda
Serial.println(" ");
Serial.println("Ping encerrado!");
Serial.println(" ");
numping =0;
ida++;
break;
case 3:
Serial.println("================================================");
Serial.print("Iniciando ping ao IP :");
Serial.println(pingAddrST2);
Serial.println("------------------------------------------------");
Serial.print("Host :");
Serial.println(label4);
Serial.println("================================================");
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
while (numping < numerodepings )
{
numping++; //incremente numero do ping
ICMPEchoReply echoReply = ping(pingAddrST2, 4);
if (echoReply.status == SUCCESS)
{
sprintf(buffer,
"Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%dms TTL=%d",
echoReply.data.seq,
echoReply.addr[0],
echoReply.addr[1],
echoReply.addr[2],
echoReply.addr[3],
REQ_DATASIZE,
millis() - echoReply.data.time,
echoReply.ttl);
timeST2 = timeST2 + millis() - echoReply.data.time;
}
else
{
sprintf(buffer, "Echo request failed; %d", echoReply.status);
perdidosST2++;
}
Serial.println(buffer);
delay(1000);
}
timeST2 = timeST2 / numerodepings; // define a media de tempo dos pings
porcST2 = perdidosST2 * 100 / numerodepings; // porcentagem de perda
Serial.println(" ");
Serial.println("Ping encerrado!");
Serial.println(" ");
numping =0;
ida++;
break;
default:
//------------------------------------------------calcula os totais para post em tweeter
perdidosTL = perdidosRT + perdidosGW + perdidosST1 + perdidosST2;// total de pacotes perdidos
int pingsTL = numerodepings * 4;// total de pacotes pingados
porcentagemTL = perdidosTL * 100 / pingsTL;
timeTL = (timeGW + timeST1 + timeST2) / 3; // tempo medio de todos o pings
//---------------------------------------------------------------------------------------
Serial.println("================================================");
Serial.println("RESUMO DOS TESTES");
Serial.println("================================================");
Serial.print("Host : ");
Serial.println(label1);
Serial.print("pacotes perdidos Roteador = ");
Serial.print(perdidosRT);
Serial.print(" ");
Serial.print("Tempo medio de respota :");
Serial.print(timeRT);
Serial.print(" ms");
Serial.print(" ");
Serial.print("Porcento de perda :");
Serial.print(porcRT);
Serial.println(" %");
Serial.println("------------------------------------------------");
Serial.print("Host : ");
Serial.println(label2);
Serial.print("pacotes perdidos Gateway = ");
Serial.print(perdidosGW);
Serial.print(" ");
Serial.print("Tempo medio de respota :");
Serial.print(timeGW);
Serial.print(" ms");
Serial.print(" ");
Serial.print("Porcento de perda :");
Serial.print(porcGW);
Serial.println(" %");
Serial.println("------------------------------------------------");
Serial.print("Host : ");
Serial.println(label3);
Serial.print("pacotes perdidos Site 1 = ");
Serial.print(perdidosST1);
Serial.print(" ");
Serial.print("Tempo medio de respota :");
Serial.print(timeST1);
Serial.print(" ms");
Serial.print(" ");
Serial.print("Porcento de perda :");
Serial.print(porcST1);
Serial.println(" %");
Serial.println("------------------------------------------------");
Serial.print("Host : ");
Serial.println(label4);
Serial.print("pacotes perdidos Site 2 = ");
Serial.print(perdidosST2);
Serial.print(" ");
Serial.print("Tempo medio de respota :");
Serial.print(timeST2);
Serial.print(" ms");
Serial.print(" ");
Serial.print("Porcento de perda :");
Serial.print(porcST2);
Serial.println(" %");
Serial.println("================================================");
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
delay(6000);// delay so para ficar mais fazer de visualizar mensagens na serail.
// -------------------------------------------------Message to post media geral dos testes
// Lembre-se que a mensagem nao pode ter mais que 140 caracteres
String tmsg = "#GVT #ARDUINO ";
tmsg.concat("TESTE ");
tmsg.concat(testn);
tmsg.concat(" Hosts: ");
tmsg.concat(label1);
tmsg.concat(" - ");
tmsg.concat(label2);
tmsg.concat(" - ");
tmsg.concat(label3);
tmsg.concat(" - ");
tmsg.concat(label4);
tmsg.concat(" - ");
tmsg.concat("Total perdidos : ");
tmsg.concat(perdidosTL);
tmsg.concat(" Media de respota: ");
tmsg.concat(timeTL);
tmsg.concat("ms ");
tmsg.concat(porcentagemTL);
tmsg.concat("% de perda");
tmsg.concat(" proximo em 30Min ");
int len = tmsg.length();
char msg[len];
tmsg.toCharArray(msg, len);
Serial.println("-------------------------------------------------------------------");
Serial.println("mensagem a ser enviada ao twitter");
Serial.println(" ");
Serial.println(msg);
Serial.println(" ");
Serial.println("-------------------------------------------------------------------");
//---------------------------------------------------Aqui e feito o POST o twitter
Serial.println("Enviando Twitter ...");
delay(2000);
if (twitter.post(msg)) {
// Specify &Serial to output received response to Serial.
// If no output is required, you can just omit the argument, e.g.
// int status = twitter.wait();
int status = twitter.wait(&Serial);
delay(2000);
if (status == 200) {
Serial.println(" - Mensagem twitada...");
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println("connection failed.");
}
// ----------------------------------Zerando contadores para proximo teste
delay(1800000);// delay para definir o tempo que sera repetido o teste.
perdidosRT = 0;
perdidosGW = 0;
perdidosST1 = 0;
perdidosST2 = 0;
timeRT = 0;
timeGW = 0;
timeST1 = 0;
timeST2 = 0;
testn++;
ida=0;
break;
}// case
}
}//void
Tags:
Bem-vindo a
Laboratorio de Garagem (arduino, eletrônica, robotica, hacking)
© 2024 Criado por Marcelo Rodrigues. Ativado por