Skip to content

Commit 1cca42d

Browse files
committed
fist checkin
1 parent b7192cc commit 1cca42d

File tree

7 files changed

+324
-0
lines changed

7 files changed

+324
-0
lines changed

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Virtualenv
2+
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
3+
.Python
4+
[Bb]in
5+
[Ii]nclude
6+
[Ll]ib
7+
[Ll]ib64
8+
[Ll]ocal
9+
[Ss]cripts
10+
[Ss]hare
11+
pyvenv.cfg
12+
.venv
13+
pip-selfcheck.json
14+
python-code/setenvs.sh

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#docker run .. --env snmphost=<IP> --env snmpcommunity=<snmp community> --env mqtthost=<IP> --env mqttport=<port if diff than 1883>
2+
3+
FROM python:3.8-slim-buster
4+
ADD python-code/bandwidth.py /
5+
#RUN apt-get install .build-deps gcc libsmi-dev py3-cffi musl-dev libsmi libffi-dev
6+
RUN apt-get update && apt-get install gcc -y
7+
RUN apt-get install libsmi-dev -y
8+
RUN echo "deb http://ftp.us.debian.org/debian stretch main non-free" >> /etc/apt/sources.list
9+
RUN apt-get update && apt-get install snmp-mibs-downloader -y
10+
RUN pip install --upgrade pip
11+
RUN pip install snimpy
12+
RUN pip install paho-mqtt
13+
CMD [ "python", "./bandwidth.py" ]
14+

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
General:
2+
This is version 2. The code for version one was lost during the great data apocalypse (see my blog for that disaster). This contains 2 pieces.
3+
Some python code and some arduino code. The python code queries my router on a regular basis via snmp and gets my up/down bandwidth usage.
4+
It the passes the current bandwidth usage to mqtt nodes /bandwidth/up and /bandwidth/down.
5+
6+
The second piece (the arduino code) connects via the network and subscribes to those same nodes. Updating the analog meters every time new data is received.
7+
8+
Setup:
9+
10+
I have configured the python code to run on a linux box (may work on windows). I run the script in rc.local to run in background.
11+
python /scripts/bandwidth.py &
12+
13+
14+
15+
Requirements:
16+
You do need to install Snimpy (pip install snimpy)
17+
MQTT server
18+
19+
20+
# bandwidth-meter-docker

bandwidth.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from snimpy.manager import Manager as M
2+
from snimpy.manager import load
3+
import time
4+
import paho.mqtt.client as mqtt
5+
import os
6+
7+
snmphost = os.getenv('snmphost','127.0.0.1')
8+
snmpcommunity = os.getenv('snmpcommunity')
9+
mqtthost = os.getenv('mqtthost','127.0.0.1')
10+
mqttport = os.getenv('mqttport')
11+
print snmphost
12+
13+
14+
15+
16+
while True:
17+
18+
load("IF-MIB")
19+
m = M(host=snmphost, community=snmpcommunity,version = 1)
20+
ifspeed= m.ifSpeed[2]
21+
OutOctet1 = m.ifInOctets[2]
22+
InOctet1 = m.ifOutOctets[2]
23+
time.sleep(1)
24+
m = M(host=snmphost, community=snmpcommunity,version = 1)
25+
OutOctet2 = m.ifInOctets[2]
26+
InOctet2 = m.ifOutOctets[2]
27+
upload= float(float(OutOctet2) - float(OutOctet1))*8
28+
download=float(float(InOctet2) - float(InOctet1))*8
29+
mqttc = mqtt.Client("python_pub")
30+
print upload
31+
print download
32+
mqttc.connect(mqtthost,mqttport)
33+
mqttc.publish("bandwidth/up", upload)
34+
mqttc.publish("bandwidth/down",download)
35+
mqttc.loop(2)
36+
time.sleep(1)
37+
38+
39+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
void setup() {
2+
// put your setup code here, to run once:
3+
4+
}
5+
6+
void loop() {
7+
// put your main code here, to run repeatedly:
8+
9+
}

python-code/bandwidth.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from snimpy.manager import Manager as M
2+
from snimpy.manager import load
3+
import time
4+
import paho.mqtt.client as mqtt
5+
import os
6+
7+
snmphost = os.getenv('snmphost','127.0.0.1')
8+
snmpcommunity = os.getenv('snmpcommunity')
9+
print (snmpcommunity)
10+
mqtthost = os.getenv('mqtthost','127.0.0.1')
11+
print (mqtthost)
12+
mqttport = os.getenv('mqttport',1883)
13+
print (mqttport)
14+
print (snmphost)
15+
16+
17+
18+
19+
while True:
20+
21+
load("IF-MIB")
22+
m = M(host=snmphost, community=snmpcommunity,version = 1)
23+
ifspeed= m.ifSpeed[2]
24+
OutOctet1 = m.ifInOctets[2]
25+
InOctet1 = m.ifOutOctets[2]
26+
time.sleep(1)
27+
m = M(host=snmphost, community=snmpcommunity,version = 1)
28+
OutOctet2 = m.ifInOctets[2]
29+
InOctet2 = m.ifOutOctets[2]
30+
upload= float(float(OutOctet2) - float(OutOctet1))*8
31+
download=float(float(InOctet2) - float(InOctet1))*8
32+
mqttc = mqtt.Client("python_pub")
33+
print (upload)
34+
print (download)
35+
mqttc.connect(mqtthost,mqttport)
36+
mqttc.publish("bandwidth/up", upload)
37+
mqttc.publish("bandwidth/down",download)
38+
mqttc.loop(2)
39+
time.sleep(1)
40+
41+
42+
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
#include <ESP8266WiFi.h>
2+
#include <PubSubClient.h>
3+
#include "secrets.h"
4+
5+
const char* mqtt_server = "192.168.2.224";
6+
const String topic = "bandwidth/#"; // rhis is the [root topic]
7+
8+
WiFiClient espClient;
9+
PubSubClient client(espClient);
10+
11+
long target_up;
12+
long target_down;
13+
long tmp_up;
14+
long tmp_down;
15+
long dwbw_raw;
16+
long upbw_raw;
17+
long dwbw_mapped;
18+
long upbw_mapped;
19+
//bandwidth in megabit
20+
const long max_bw_down = 275.0 * 1024 * 1024;
21+
//const long max_bw_up = 25.0 * 1024 * 1024;
22+
long max_bw_up = max_bw_down;
23+
24+
unsigned long previousMillis = 0;
25+
//how long to wait between moving to the next iteration in smoothing
26+
const long interval = 1000;
27+
28+
29+
int status = WL_IDLE_STATUS; // the starting Wifi radio's status
30+
31+
void setup_wifi() {
32+
delay(10);
33+
// We start by connecting to a WiFi network
34+
Serial.println();
35+
Serial.print("Connecting to ");
36+
Serial.println(ssid);
37+
WiFi.begin(ssid, pswd);
38+
while (WiFi.status() != WL_CONNECTED) {
39+
delay(500);
40+
Serial.print(".");
41+
}
42+
Serial.println("");
43+
Serial.println("WiFi connected");
44+
Serial.println("IP address: ");
45+
Serial.println(WiFi.localIP());
46+
}
47+
48+
void callback(char* topic, byte* payload, unsigned int length) {
49+
Serial.print("Message arrived [");
50+
Serial.print(topic);
51+
Serial.print("] ");
52+
53+
char buff_p[length];
54+
for (int i = 0; i < length; i++) {
55+
//Serial.print((char)payload[i]);
56+
buff_p[i] = (char)payload[i];
57+
}
58+
buff_p[length] = '\0';
59+
String s = String(buff_p);
60+
61+
if(strcmp(topic,"bandwidth/up")==0)
62+
{
63+
// upload (lets put this on the first meter
64+
//0v - 5v
65+
//0mb - 300mb
66+
67+
upbw_raw = s.toInt();
68+
upbw_raw = constrain(upbw_raw,0,max_bw_up+1000000);
69+
70+
71+
72+
73+
upbw_mapped=mymap(upbw_raw,0,max_bw_up,0,1024);
74+
//upbw_mapped=constrain(upbw_mapped,0,max_bw+10000);
75+
Serial.print(upbw_raw);
76+
Serial.print(" ");
77+
Serial.println(upbw_mapped);
78+
analogWrite(D1,upbw_mapped);
79+
}
80+
81+
if(strcmp(topic,"bandwidth/down")==0)
82+
{
83+
// upload (lets put this on the first meter
84+
//0v - 5v
85+
//0mb - 300mb
86+
87+
dwbw_raw = s.toInt();
88+
dwbw_raw = constrain(dwbw_raw,0,max_bw_down+1000000);
89+
90+
dwbw_mapped=mymap(dwbw_raw,0,max_bw_down,0,1024);
91+
//dwbw_mapped=constrain(dwbw_mapped,0,max_bw+10000);
92+
Serial.print(dwbw_raw);
93+
Serial.print(" ");
94+
Serial.println(dwbw_mapped);
95+
analogWrite(D2,dwbw_mapped);
96+
}
97+
98+
99+
100+
}
101+
102+
String macToStr(const uint8_t* mac)
103+
{
104+
String result;
105+
for (int i = 0; i < 6; ++i) {
106+
result += String(mac[i], 16);
107+
if (i < 5)
108+
result += ':';
109+
}
110+
return result;
111+
}
112+
113+
String composeClientID() {
114+
uint8_t mac[6];
115+
WiFi.macAddress(mac);
116+
String clientId;
117+
clientId += "esp-";
118+
clientId += macToStr(mac);
119+
return clientId;
120+
}
121+
122+
void reconnect() {
123+
// Loop until we're reconnected
124+
while (!client.connected()) {
125+
Serial.print("Attempting MQTT connection...");
126+
127+
String clientId = composeClientID() ;
128+
clientId += "-";
129+
clientId += String(micros() & 0xff, 16); // to randomise. sort of
130+
131+
// Attempt to connect
132+
if (client.connect(clientId.c_str())) {
133+
Serial.println("connected");
134+
// Once connected, publish an announcement...
135+
// ... and resubscribe
136+
// topic + clientID + in
137+
client.subscribe(topic.c_str() );
138+
Serial.print("subscribed to : ");
139+
Serial.println(topic);
140+
} else {
141+
Serial.print("failed, rc=");
142+
Serial.print(client.state());
143+
Serial.print(" wifi=");
144+
Serial.print(WiFi.status());
145+
Serial.println(" try again in 5 seconds");
146+
// Wait 5 seconds before retrying
147+
delay(5000);
148+
}
149+
}
150+
}
151+
152+
long mymap(float x, float in_min, float in_max, float out_min, float out_max) {
153+
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
154+
}
155+
156+
void setup() {
157+
// put your setup code here, to run once:
158+
159+
160+
Serial.begin(115200);
161+
setup_wifi();
162+
client.setServer(mqtt_server, 1883);
163+
client.setCallback(callback);
164+
165+
Serial.print("Max Bandwidth_Up:");
166+
Serial.println(max_bw_up);
167+
Serial.print("Max Bandwidth_Down:");
168+
Serial.println(max_bw_down);
169+
170+
171+
}
172+
173+
void loop() {
174+
if (!client.connected()) {
175+
reconnect();
176+
}
177+
client.loop();
178+
//Serial.println(dwbw_mapped);
179+
//Serial.println(upbw_mapped);
180+
181+
analogWrite(D2,dwbw_mapped);
182+
analogWrite(D1,upbw_mapped);
183+
184+
185+
186+
}

0 commit comments

Comments
 (0)