Skip to content

Instantly share code, notes, and snippets.

@EEParker
Last active April 11, 2019 19:24
Show Gist options
  • Save EEParker/2025bcf45f291e03e02e486b0db8c5da to your computer and use it in GitHub Desktop.
Save EEParker/2025bcf45f291e03e02e486b0db8c5da to your computer and use it in GitHub Desktop.
ASMSA PlatformIO ESP8266 Tutorial
#include <Arduino.h>
const uint8_t PIN_PWR = 15;
float r1 = 10.0f;
float r2 = 10.0f;
float r3 = 10.0f;
float r2r3 = r2 + r3;
float rE = 1 / ((1 / r1) + (1 / r2r3));
void setup() {
Serial.begin(115200);
pinMode(A0, INPUT);
pinMode(PIN_PWR, OUTPUT);
digitalWrite(PIN_PWR, 1);
}
void loop() {
float sample = analogRead(A0) * 3.0f / 1.1f;
Serial.printf("Voltage = %.0f mV\n", sample);
float deltaV = (3.3f * 1000) - sample;
float current = deltaV / rE;
Serial.printf("Current = %.0f mA\n", current);
float power = deltaV * current / 1000;
Serial.printf("Power = %.0f mW\n", power);
delay(2000);
}
/*******************************************************************
// ASMSA PlatformIO ESP8266 Tutorial
// https://git.io/fjqKF
// https://gist.github.com/EEParker/2025bcf45f291e03e02e486b0db8c5da
******************************************************************//
#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// initialize the LCD
const uint8_t LCD_ADDR = 0x3F; // sometime the address is 0x27
const uint8_t LCD_ROWS = 2; // 2 rows of
const uint8_t LCD_COLS = 16; // 16 characters each
const uint8_t LCD_SDA = 5; // I2C Data Pin
const uint8_t LCD_SCL = 4; // I2C Clock Pin
LiquidCrystal_I2C lcd(LCD_ADDR, LCD_ROWS, LCD_COLS);
// Define the circuit
const float r1 = 30.0f;
const float r2 = 10.0f;
const float R1 = r1; // Monitored Element
const float R2 = 1 / ( (1 / r1) + (1 / r2) ); // Equivalenced Resistance formula
const float V = 3000.f; // Reference voltage analog
// define user functions
void measureVoltage();
void setup() {
// turn on the serial port for logging
Serial.begin(115200);
// set the analog port to input (read values)
pinMode(A0, INPUT);
// Initialize Grove Power
pinMode(15, OUTPUT);
digitalWrite(15, 1);
// initialize the lcd
Wire.begin(LCD_SDA, LCD_SCL);
lcd.init();
lcd.backlight();
}
void loop() {
// measure and print the values
measureVoltage();
// sleep for 1 second (1000 ms) before measuring again
delay(1000);
}
/**
* Measure the voltage using the analog port
* and estimate the current based on R values.
*/
void measureVoltage() {
// take a number of samples, then average them for more accuracy
const int samples = 100;
long sum = 0;
for(int i = 0; i < samples; i++)
{
sum += analogRead(A0);
delay(2);
}
sum = sum / samples;
// calculate the voltage
float v = sum * 3.0f / 1.1f;
// calculate the current
float i1 = 0;
float i2 = 0;
float vdelta = V - v;
if (v > 2000.f) {
i2 = (vdelta / R2);
}
i1 = (vdelta / R1);
// power calculation
float p = (v / 1000) * (i1 + i2);
// report to the serial line
Serial.printf("V=%.0fmV I=%.0fmA ", v, i1 + i2);
Serial.printf("I1=%.0f I2=%.0f P=%.0fmW\n", i1, i2, p);
// print results to the LCD
lcd.setCursor(0, 0); // first line
lcd.printf("V=%.0fmV I=%.0fmA ", v, i1 + i2);
lcd.setCursor(0, 1); // second line
lcd.printf("I1=%.0f I2=%.0f P=%.0fmW\n", i1, i2, p);
}
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:wio_link]
platform = espressif8266
board = wio_link
framework = arduino
monitor_speed = 115200
upload_speed = 921600
lib_deps =
# Using a library name
LiquidCrystal_I2C
@EEParker
Copy link
Author

This project can be completed using

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment