Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
sorriso93 authored Jun 9, 2019
1 parent a05c271 commit 6ffc228
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 38 deletions.
44 changes: 18 additions & 26 deletions IOTroutertraffic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ extern "C" { // needed to set hostname

#include <ArduinoJson.h>
#include "preferences.h"
#include "display_mgm_espi.h"
#include <ESP8266WiFi.h>
#include <ESP8266Ping.h>
#include <WiFiClient.h>
Expand All @@ -38,14 +37,12 @@ extern "C" { // needed to set hostname
void setup()
{
Serial.begin(115200);

#ifdef MQTT_Y
//DEBUG_PRINTLN ("SETUP MQTT");
client.setServer(mqtt_server, 1883); //per MQTT
//#ifndef publisher_MQTT
client.setCallback(callback); //per MQTT
#endif //per MQTT

setup_display(); // DISPLAY
cancella_display();
accendi_backlight();
Expand Down Expand Up @@ -74,7 +71,7 @@ void setup()
{
//No time via NTP I reset
cancella_display();
DEBUG_PRINTLN("Non ho orario, resetto");
DEBUG_PRINTLN("No time, reset");
scrivi_display_riga_colore ("NO\r\nNETWORK\r\nTIME",16, TFT_GREEN);
delay(2000);
ESP.reset(); // resetto se non riceve l'orario da server NTP
Expand Down Expand Up @@ -161,9 +158,9 @@ void loop()
{
String temp_str;
double bytes_r1, bytes_s1, bytes_r2, bytes_s2;
long int kbytes_r_xfreq, kbytes_s_xfreq;
// long int kbytes_r_xfreq, kbytes_s_xfreq;
// int avg_time_ms = 0;
int readings = 0;
int avg_time_ms = 0;

ArduinoOTA.handle(); // per OTA

Expand Down Expand Up @@ -197,7 +194,6 @@ void loop()
{
Serial.println("Error in WiFi connection");
}

// converts in kbytes per second data collected
kbytes_r_xfreq = (long int) (bytes_r2 - bytes_r1)/1024; //non 1024 boh!
kbytes_s_xfreq = (long int) (bytes_s2 - bytes_s1)/1024;
Expand All @@ -215,27 +211,23 @@ void loop()
//Calls update routine to publish MQTT data on queue
MQTT_update();
#endif
// max & min update
if (max_speed_r < kbytes_r_xfreq)
max_speed_r = kbytes_r_xfreq;
if (max_speed_s < kbytes_s_xfreq)
max_speed_s = kbytes_s_xfreq;
disegna (kbytes_r_xfreq, kbytes_s_xfreq, avg_time_ms); //kbytes per second on lcd screen
#else
#ifdef MQTT_Y
//Reads JSON data received from MQTT queue
/*kbytes_r_xfreq = (stampa_MQTT[1].substring(stampa_MQTT[1].indexOf("Kb-received: "),stampa_MQTT[1].length+1)).toInt;
kbytes_s_xfreq = (stampa_MQTT[2].substring(stampa_MQTT[2].indexOf("Kb-sent: "),stampa_MQTT[2].length+1)).toInt; */
kbytes_r_xfreq = stampa_MQTT["Kbsec_rec"];
kbytes_s_xfreq = stampa_MQTT["Kbsec_sent"];
avg_time_ms = stampa_MQTT["ping"];
#endif
//DEBUG_PRINTLN("subscriber MQTT");
if (!client.connected())
{
reconnect(); // connects to MQTT queue
}
client.loop();
#endif

// max & min update
if (max_speed_r < kbytes_r_xfreq)
max_speed_r = kbytes_r_xfreq;
if (max_speed_s < kbytes_s_xfreq)
max_speed_s = kbytes_s_xfreq;

disegna (kbytes_r_xfreq, kbytes_s_xfreq, avg_time_ms); //kbytes per second on lcd screen
//scrivi_ora();

DEBUG_PRINTLN("kbytes received per "+ String(freq_request) + " millisecond >>> " + String(kbytes_r_xfreq));
DEBUG_PRINTLN("kbytes sent per "+ String(freq_request) + " millisecond >>> " + String(kbytes_s_xfreq));
DEBUG_PRINTLN("------ Ping "+String(ping_site)+": "+String(avg_time_ms));
//DEBUG_PRINTLN("kbytes received per "+ String(freq_request) + " millisecond >>> " + String(kbytes_r_xfreq));
//DEBUG_PRINTLN("kbytes sent per "+ String(freq_request) + " millisecond >>> " + String(kbytes_s_xfreq));
//DEBUG_PRINTLN("------ Ping "+String(ping_site)+": "+String(avg_time_ms));
}
8 changes: 4 additions & 4 deletions display_mgm_espi.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,24 @@ int scan = 1;

void drawWifiQuality()
{
tft.fillRect(285,0,320,8,BLACK);
tft.fillRect(265,0,320,8,BLACK);

int8_t quality = getWifiQuality();
tft.setTextSize(0);
tft.setCursor(285,0,1);
tft.setCursor(280,0,1);
tft.setTextColor(TFT_WHITE);
tft.print(String(quality) + "%"); // wifi signal quality

tft.setTextColor(TFT_WHITE);
#ifdef MQTT_Y
if (collegato_MQTT == 1)
{
tft.setCursor(270,0);
tft.setCursor(265,0);
tft.print("Q");
}
else
{
tft.setCursor(270,0);
tft.setCursor(265,0);
tft.print("X");
}
#endif
Expand Down
35 changes: 27 additions & 8 deletions preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// http://192.168.0.1:49152/gateicfgSCPD.xml

#define debug_serial //comment out if not debugging
#define name_sensor_default "RTraffic" // BOARD NAME -- HOSTNAME
#define name_sensor_default "RTrafficSub" // BOARD NAME -- HOSTNAME
#define MQTT_Y //use MQTT (publisher or subscriber), comment it out if no MQTT is used
#define publisher // publisher or subscriber of MQTT, comment it out if no MQTT is used or if you want it as subscriber
//#define publisher // publisher or subscriber of MQTT, comment it out if no MQTT is used or if you want it as subscriber

//BOARD NAME AND WIF-OTA CONFIGURATION by default
#define password_AP "passwordota"
Expand All @@ -26,6 +26,10 @@ int max_upload_rate = 800; // max upload rate of my internet connection
#define MQTT_QUEUE "router_stats" // MQTT queue name
#endif

long int kbytes_r_xfreq, kbytes_s_xfreq = 0; //global variables with data to bee displayed
int avg_time_ms = 0; //global variables with data to bee displayed
unsigned long max_speed_r, max_speed_s = 0;

//---------- UPNP IDG configuration --- Internet gateway
//actions&services available on my router
String gettotalbytesreceived = "GetTotalBytesReceived";
Expand All @@ -40,8 +44,6 @@ String getcommonlinkproperties = "GetCommonLinkProperties";
String linkpropertiesresult = "NewPhysicalLinkStatus";
String _service = "\"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1\"";

unsigned long max_speed_r, max_speed_s = 0;

// DAYLIGHT saving routine
#include <NTPClient.h>
#include <Time.h>
Expand Down Expand Up @@ -101,27 +103,44 @@ String twoDigits(int digits);
void callback(char* topic, byte* payload, unsigned int length);
void MQTT_update (); //MQTT Publish
void reconnect(); //MQTT reconnect
#endif

#include "display_mgm_espi.h"

#ifdef MQTT_Y
void callback(char* topic, byte* payload, unsigned int length)
{

#ifndef publisher
char messaggio[1024];
String messaggio;
char msg[1024];
int i;

for (int i = 0; i < length; i++)
{
messaggio += (char)payload[i];
}
DEBUG_PRINT("MQTT Message arrived [");
DEBUG_PRINT(messaggio);
DEBUG_PRINTLN("] ");
stampa_MQTT = buffer_MQTT.parseObject(messaggio); //only deserialization, assignement to variables to graph data is made in main code
messaggio.toCharArray(msg,1024);
JsonVariant read_MQTT = buffer_MQTT.parseObject(msg); //only deserialization, assignement to variables to graph data is made in main code
if (!stampa_MQTT.success())
{
DEBUG_PRINT("Json parsing error"); //additional things?
return;
}
else
{
kbytes_r_xfreq = read_MQTT[String("Kbsec_rec")];
kbytes_s_xfreq = read_MQTT[String("Kbsec_sent")];
avg_time_ms = read_MQTT[String("ping")];
DEBUG_PRINTLN("kbsec_r = "+String(kbytes_r_xfreq)+"kbsec_s = "+String(kbytes_s_xfreq)+"avg_ping = "+String(avg_time_ms));
}
// max & min update
if (max_speed_r < kbytes_r_xfreq)
max_speed_r = kbytes_r_xfreq;
if (max_speed_s < kbytes_s_xfreq)
max_speed_s = kbytes_s_xfreq;
disegna (kbytes_r_xfreq, kbytes_s_xfreq, avg_time_ms); //kbytes per second on lcd screen
#endif
}

Expand Down

0 comments on commit 6ffc228

Please sign in to comment.