Skip to content

Instantly share code, notes, and snippets.

@kouya17
Created June 6, 2020 13:52
Show Gist options
  • Save kouya17/551603c7e5eb4697960ec41957fde340 to your computer and use it in GitHub Desktop.
Save kouya17/551603c7e5eb4697960ec41957fde340 to your computer and use it in GitHub Desktop.
ambientを使ってPowerCに接続されているバッテリーの情報を記録する
/**
* @file ambient_PowerC.ino
* @brief ambientを使ってPowerCに接続されているバッテリーの情報を記録する
* @date 2020.06.06
*/
#include <M5StickC.h>
#include <Wire.h>
#include "Ambient.h"
#define PERIOD 60
WiFiClient client;
Ambient ambient;
const char *ssid = "your-ssid";
const char *password = "your-password";
unsigned int channelId = 0; // AmbientのチャネルID
const char *writeKey = "hoge"; // ライトキー
void setup()
{
M5.begin();
Wire.begin(0, 26, 400000);
M5.Lcd.setRotation(1);
while (Wire.endTransmission() != 0)
{
M5.Lcd.setCursor(20, 0);
M5.Lcd.print("Not found PowerC");
Wire.beginTransmission(0x75);
}
M5.Lcd.fillRect(0, 0, 160, 80, BLACK);
WiFi.begin(ssid, password); // Wi-Fi APに接続
while (WiFi.status() != WL_CONNECTED)
{ // Wi-Fi AP接続待ち
delay(100);
}
ambient.begin(channelId, writeKey, &client); // チャネルIDとライトキーを指定してAmbientの初期化
}
uint16_t readBatV(uint8_t Reg)
{
uint8_t dataV[2] = {0, 0};
M5.I2C.readBytes(0x75, Reg, 2, dataV);
return ((dataV[1] << 8) & 0xff00) | dataV[0];
}
uint16_t readBatI(uint8_t Reg)
{
uint8_t dataI[2] = {0, 0};
M5.I2C.readBytes(0x75, Reg, 2, dataI);
return ((dataI[1] << 8) & 0xff00) | dataI[0];
}
void loop()
{
int t = millis();
float temp, humid, pressure;
// 電圧値v、電流値iの計算
float v = readBatV(0xA2);
float i = readBatI(0XA4);
v = v * 0.00026855 + 2.6;
i = i * 0.745985;
M5.Lcd.setCursor(0, 10);
M5.Lcd.printf("%1.2f,V", v);
Serial.printf("%1.2f", v);
Serial.println("");
M5.Lcd.setCursor(0, 40);
M5.Lcd.printf("%1.0f,mAh", i);
Serial.printf("%1.0f", i);
Serial.println("");
// Ambientに送信する
ambient.set(1, String(v).c_str());
ambient.set(2, String(i).c_str());
ambient.send();
t = millis() - t;
t = (t < PERIOD * 1000) ? (PERIOD * 1000 - t) : 1;
delay(t);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment