2018年9月17日月曜日

STM32duino(Bluepill)とESP8266(NodeMCU)とInfluxdataとGrafana でワイヤレスロガー

STM32duino(Bluepill)の方のスケッチ
シリアルには4から5秒間隔で出力するけど、NodeMCUの方が20秒ごとの出力なのでInfluxdbは20秒ごとに更新ということになります。
電流と電圧計にしようと思っているので校正用の式になっていて妙に思うかもしれません。気にしないでね。かなりいい加減。

/*
  4ch datalogger for STM32 Blue Pill
  With NodeMCU for use InfluxDB and Grafana

  The circuit:
   analog sensors on analog ins - A0, A1, A2 and A3

  created  09 Sep 2018
  modified 09 Sep 2018
  code by knet-ore.no-ip.biz

*/

#include "SPI.h"

#include "Wire.h"

const int chipSelect = 4;

const int pinCount = 2;      //the number of pins (i.e. the length of the array)
const int sensPins[] = { 0, 1, 2, 3}; //Current sensors PA0-PA3 (if change 5ch PB0 etc)

int timecount = 0;              //counter for timer
char input[10];
int i = 0;
float sensor[] = { 0, 1, 2, 3};
float avgcur;           //current average to determine change chargeline

String dataString = "";  //string for date to log

void setup () {

  Serial1.begin(9600);

  Serial.begin(9600);
  // Wire.begin();

  pinMode(sensPins[0], INPUT_ANALOG);
  pinMode(sensPins[1], INPUT_ANALOG);
  pinMode(sensPins[2], INPUT_ANALOG);
  pinMode(sensPins[3], INPUT_ANALOG);
}

void sense() {
  int analogPin = sensPins[0];
  float average = 0;
  for (int i = 0; i < 10; i++) {
    average = average + (1 * (analogRead(analogPin) - 0) );
    delay(10);
  }
  sensor[0] = average / 10;

  analogPin = sensPins[1];
  average = 0;
  for (int i = 0; i < 10; i++) {
    average = average + (1 * (analogRead(analogPin) - 0) );
    delay(10);
  }
  sensor[1] = average / 10;

  analogPin = sensPins[2];
  average = 0;
  for (int i = 0; i < 10; i++) {
    average = average + (1 * (analogRead(analogPin) - 0) );
    delay(10);
  }
  sensor[2] = average / 10;

  analogPin = sensPins[3];
  average = 0;
  for (int i = 0; i < 10; i++) {
    average = average + (1 * (analogRead(analogPin) - 0) );
    delay(10);
  }
  sensor[3] = average / 10;
}

void loop() {
  if (Serial.available()) {      // If anything comes in Serial (USB),
    Serial1.write(Serial.read());   // read it and send it out Serial1 (pins 0 & 1)
  }

  if (Serial1.available()) {     // If anything comes in Serial1 (pins 0 & 1)
    Serial.write(Serial1.read());   // read it and send it out Serial (USB)
  }
  else {
    sense();
    Serial1.print("a1=");
    Serial1.println(sensor[0],0);
    Serial1.print("\n");
    delay(1000);
    Serial1.print("a2=");
    Serial1.println(sensor[1],0);
    Serial1.print("\n");
    delay(1000);
    Serial1.print("a3=");
    Serial1.println(sensor[2],0);
    Serial1.print("\n");
    delay(1000);
    Serial1.print("a4=");
    Serial1.println(sensor[3],0);
    Serial1.print("\n");
    delay(1000);
  }
}

0 件のコメント:

コメントを投稿