INA219.ino 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*!
  2. *@file getVoltageCurrentPower.ino
  3. *@brief Get the current, voltage, and power of electronic devices.
  4. *@copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
  5. *@license The MIT license (MIT)
  6. *@author [fengli](li.feng@dfrobot.com)
  7. *@version V1.0
  8. *@date 2022-3-1
  9. *@url https://github.com/DFRobot/DFRobot_INA219
  10. */
  11. #include <Wire.h>
  12. #include "DFRobot_INA219.h"
  13. #include <ESP8266WiFi.h>
  14. #include <ArduinoMqttClient.h>
  15. #include <ArduinoJson.h>
  16. #include "secrets.h"
  17. /**
  18. * @fn DFRobot_INA219_IIC
  19. * @brief pWire I2C controller pointer
  20. * @param i2caddr I2C address
  21. * @n INA219_I2C_ADDRESS1 0x40 A0 = 0 A1 = 0
  22. * @n INA219_I2C_ADDRESS2 0x41 A0 = 1 A1 = 0
  23. * @n INA219_I2C_ADDRESS3 0x44 A0 = 0 A1 = 1
  24. * @n INA219_I2C_ADDRESS4 0x45 A0 = 1 A1 = 1
  25. */
  26. DFRobot_INA219_IIC ina219(&Wire, INA219_I2C_ADDRESS1);
  27. // Revise the following two paramters according to actual reading of the INA219 and the multimeter
  28. // for linearly calibration
  29. float ina219Reading_mA = 1000;
  30. float extMeterReading_mA = 1000;
  31. void setup(void)
  32. {
  33. Serial.begin(115200);
  34. //Open the serial port
  35. while(!Serial);
  36. Serial.println();
  37. //Initialize the sensor
  38. while(ina219.begin() != true) {
  39. Serial.println("INA219 begin faild");
  40. delay(2000);
  41. }
  42. //Linear calibration
  43. ina219.linearCalibrate(/*The measured current before calibration*/ina219Reading_mA, /*The current measured by other current testers*/extMeterReading_mA);
  44. Serial.println();
  45. pinMode(15, OUTPUT);
  46. digitalWrite(15, LOW);
  47. pinMode(12, OUTPUT);
  48. digitalWrite(12, LOW);
  49. pinMode(14, OUTPUT);
  50. digitalWrite(14, LOW);
  51. WiFi.begin(SECRET_SSID, SECRET_PASS);
  52. Serial.print("Connecting");
  53. while (WiFi.status() != WL_CONNECTED)
  54. {
  55. delay(500);
  56. Serial.print(".");
  57. }
  58. Serial.println();
  59. Serial.print("Connected, IP address: ");
  60. Serial.println(WiFi.localIP());
  61. }
  62. void loop(void)
  63. {
  64. Serial.print("BusVoltage: ");
  65. Serial.print(ina219.getBusVoltage_V(), 2);
  66. Serial.println("V");
  67. delay(10000);
  68. }