| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- /*!
- *@file getVoltageCurrentPower.ino
- *@brief Get the current, voltage, and power of electronic devices.
- *@copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
- *@license The MIT license (MIT)
- *@author [fengli](li.feng@dfrobot.com)
- *@version V1.0
- *@date 2022-3-1
- *@url https://github.com/DFRobot/DFRobot_INA219
- */
- #include <Wire.h>
- #include "DFRobot_INA219.h"
- #include <ESP8266WiFi.h>
- #include <ArduinoMqttClient.h>
- #include <ArduinoJson.h>
- #include "secrets.h"
- /**
- * @fn DFRobot_INA219_IIC
- * @brief pWire I2C controller pointer
- * @param i2caddr I2C address
- * @n INA219_I2C_ADDRESS1 0x40 A0 = 0 A1 = 0
- * @n INA219_I2C_ADDRESS2 0x41 A0 = 1 A1 = 0
- * @n INA219_I2C_ADDRESS3 0x44 A0 = 0 A1 = 1
- * @n INA219_I2C_ADDRESS4 0x45 A0 = 1 A1 = 1
- */
- DFRobot_INA219_IIC ina219(&Wire, INA219_I2C_ADDRESS1);
- // Revise the following two paramters according to actual reading of the INA219 and the multimeter
- // for linearly calibration
- float ina219Reading_mA = 1000;
- float extMeterReading_mA = 1000;
- void setup(void)
- {
- Serial.begin(115200);
- //Open the serial port
- while(!Serial);
-
- Serial.println();
- //Initialize the sensor
- while(ina219.begin() != true) {
- Serial.println("INA219 begin faild");
- delay(2000);
- }
- //Linear calibration
- ina219.linearCalibrate(/*The measured current before calibration*/ina219Reading_mA, /*The current measured by other current testers*/extMeterReading_mA);
- Serial.println();
- pinMode(15, OUTPUT);
- digitalWrite(15, LOW);
- pinMode(12, OUTPUT);
- digitalWrite(12, LOW);
- pinMode(14, OUTPUT);
- digitalWrite(14, LOW);
- WiFi.begin(SECRET_SSID, SECRET_PASS);
- Serial.print("Connecting");
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- Serial.println();
- Serial.print("Connected, IP address: ");
- Serial.println(WiFi.localIP());
-
- }
- void loop(void)
- {
- Serial.print("BusVoltage: ");
- Serial.print(ina219.getBusVoltage_V(), 2);
- Serial.println("V");
- delay(10000);
-
- }
|