Create a gist now

Instantly share code, notes, and snippets.

anonymous /sessyu_iot
Created Dec 17, 2017

What would you like to do?
#include <HX711_ADC.h>
#include <Nefry.h>
#include <NefryDisplay.h>
#include <NefryIFTTT.h>
#define DRINK_EVENT "drink201712" //IFTTTの飲んだ時用のイベント名
#define RESET_EVENT "drinkreset201712" //IFTTTのリセットした時用のイベント名
#define SECRET_KEY "************************"//IFTTTのシークレットキー
#define DT_PIN D2
#define SCK_PIN D4
#define SW_PIN D3
HX711_ADC LoadCell(DT_PIN, SCK_PIN);
int weight_now = 0; //現在の重さ
int weight_temp = 0; //前回の重さ
int drink = 0; //飲んだ量
int total_drink = 0; //飲んだ量合計
int switch_temp;
void setup() {
LoadCell.begin();
long stabilisingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilising time
LoadCell.start(stabilisingtime);
LoadCell.setCalFactor(1800.0); // user set calibration factor (float)
pinMode(SW_PIN, INPUT_PULLUP);
Nefry.ndelay(2000);
NefryDisplay.clear();
NefryDisplay.setAutoScrollFlg(true);
NefryDisplay.autoScrollFunc(displayPrint);
Nefry.enableSW();
}
void loop() {
LoadCell.update();
weight_now = (int)LoadCell.getData();
if (digitalRead(SW_PIN) == LOW) {
if(switch_temp == HIGH){
Nefry.setLed(255, 0, 0); //LED:赤色
int i = 0;
int change_weight = weight_now;
while(i < 10){
Nefry.ndelay(200);
LoadCell.update();
weight_now = (int)LoadCell.getData();
if(change_weight != weight_now){
i = 0;
}
change_weight = weight_now;
i++;
}
if(weight_temp > weight_now){
drink = weight_temp - weight_now;
total_drink = total_drink + weight_temp - weight_now;
IFTTT.send(DRINK_EVENT,SECRET_KEY,String(drink),String(total_drink));
}
weight_temp = weight_now;
Nefry.setLed(0, 255, 0); //LED:緑色
}
switch_temp = LOW;
}else{
if(switch_temp == LOW){
Nefry.setLed(0, 0, 255); //LED:青色
}
switch_temp = HIGH;
}
if (Nefry.readSW()) {
drink = 0;
total_drink = 0;
IFTTT.send(RESET_EVENT,SECRET_KEY,String(drink),String(total_drink));
}
}
void displayPrint(){
NefryDisplay.drawString(0, 10, "Weight :");
NefryDisplay.drawString(80, 10, String(weight_now) + " g");
NefryDisplay.drawString(0, 30, "Drank :");
NefryDisplay.drawString(80, 30, String(drink) + " ml");
NefryDisplay.drawString(0, 50, "Total drank :");
NefryDisplay.drawString(80, 50, String(total_drink) + " ml");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment