Created
November 14, 2020 21:34
-
-
Save adlerweb/a47eb917578d6affebf1814232c3a1a7 to your computer and use it in GitHub Desktop.
USB WiFi Power Logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$mysql_serv = 'localhost'; | |
$mysql_user = 'usblogger'; | |
$mysql_pass = 'usblogger'; | |
$mysql_datb = 'volkszaehler'; | |
$uuid_v = 1; | |
$uuid_a = 2; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include('ina-config.php'); | |
if(!isset($_GET['v']) || !isset($_GET['ma'])) die('Invalid request'); | |
$v = (float)$_GET['v']; | |
$mA = (float)$_GET['ma']; | |
$uuid = 0; | |
$value = 0; | |
$sql = new mysqli($mysql_serv, $mysql_user, $mysql_pass, $mysql_datb); | |
$stmt = $sql->prepare("INSERT INTO `data` (`id`, `channel_id`, `timestamp`, `value`) VALUES (NULL, ?, UNIX_TIMESTAMP()*1000, ?)"); | |
$stmt->bind_param("id", $uuid, $value); | |
$uuid = $uuid_v; | |
$value = $v; | |
$stmt->execute(); | |
$uuid = $uuid_a; | |
$value = $mA; | |
$stmt->execute(); | |
$stmt->close(); | |
$sql->close(); | |
echo 'OK'; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <Wire.h> | |
#include <Adafruit_INA219.h> | |
#include <WiFi.h> | |
#include <HTTPClient.h> | |
Adafruit_INA219 ina219; | |
void setup(void) | |
{ | |
pinMode(LED_BUILTIN, OUTPUT); | |
Wire.begin(33, 25); | |
Serial.begin(115200); | |
while (!Serial) { | |
// will pause Zero, Leonardo, etc until serial console opens | |
delay(1); | |
} | |
while(millis() < 2000) { | |
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); | |
delay(100); | |
} | |
Serial.println("Hello!"); | |
// Initialize the INA219. | |
// By default the initialization will use the largest range (32V, 2A). However | |
// you can call a setCalibration function to change this range (see comments). | |
while (! ina219.begin(&Wire)) { | |
Serial.println("Failed to find INA219 chip"); | |
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); | |
delay(1000); | |
} | |
// To use a slightly lower 32V, 1A range (higher precision on amps): | |
//ina219.setCalibration_32V_1A(); | |
// Or to use a lower 16V, 400mA range (higher precision on volts and amps): | |
//ina219.setCalibration_16V_400mA(); | |
WiFi.begin("freifunk-myk.de", ""); | |
Serial.println("Connecting"); | |
while(WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Connected to WiFi network with IP Address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.println("Measuring voltage and current with INA219 ..."); | |
} | |
bool sendHTTP(float loadvoltage, float current_mA) { | |
HTTPClient http; | |
String url = "http://example.com/ina.php?v="; | |
url += loadvoltage; | |
url += "&ma="; | |
url += current_mA; | |
Serial.println(url); | |
http.begin(url.c_str()); | |
int returncode = http.GET(); | |
http.end(); | |
if(returncode == 200) return true; | |
return false; | |
} | |
void loop(void) | |
{ | |
float shuntvoltage = 0; | |
float busvoltage = 0; | |
float current_mA = 0; | |
float loadvoltage = 0; | |
float power_mW = 0; | |
shuntvoltage = ina219.getShuntVoltage_mV(); | |
busvoltage = ina219.getBusVoltage_V(); | |
current_mA = ina219.getCurrent_mA(); | |
power_mW = ina219.getPower_mW(); | |
loadvoltage = busvoltage + (shuntvoltage / 1000); | |
Serial.print("Bus Voltage: "); Serial.print(busvoltage); Serial.println(" V"); | |
Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV"); | |
Serial.print("Load Voltage: "); Serial.print(loadvoltage); Serial.println(" V"); | |
Serial.print("Current: "); Serial.print(current_mA); Serial.println(" mA"); | |
Serial.print("Power: "); Serial.print(power_mW); Serial.println(" mW"); | |
Serial.println(""); | |
sendHTTP(loadvoltage, current_mA); | |
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); | |
delay(250); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[env:esp32doit-devkit-v1] | |
platform = espressif32 | |
board = esp32doit-devkit-v1 | |
framework = arduino | |
monitor_speed = 115200 | |
lib_deps = | |
adafruit/Adafruit INA219 @ ^1.0.9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment