Instantly share code, notes, and snippets.

Embed
What would you like to do?
iot_ofuro_sensor
#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial
#include <BlynkSimpleSerialBLE.h>
#include <Adafruit_BLE.h>
#include <Adafruit_BluefruitLE_SPI.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// DS18B20温度センサーの設定
#define ONE_WIRE_BUS 6 // データ(黄)で使用するポート番号
#define SENSER_BIT 9 // 精度の設定bit
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// フロートスイッチの設定
int b_pin = 5; // デジタルデータ出力用ピン番号
int state = 0; // ピンより取得したデータ格納用
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "MYAUTHTOKEN";
// Using Timer
BlynkTimer timer; // Create a Timer object called "timer"!
// SHARED SPI SETTINGS (see adafruit webpages for details)
#define BLUEFRUIT_SPI_CS 8
#define BLUEFRUIT_SPI_IRQ 7
#define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused
#define BLUEFRUIT_VERBOSE_MODE true
// Create ble instance, see pinouts above
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
void setup() {
Serial.begin(9600);
pinMode(b_pin, INPUT); // ボタンスイッチ用に入力に設定
pinMode(13, OUTPUT);
sensors.setResolution(SENSER_BIT);
ble.begin(BLUEFRUIT_VERBOSE_MODE);
ble.factoryReset(); // Optional
ble.setMode(BLUEFRUIT_MODE_DATA);
Blynk.begin(auth, ble);
timer.setInterval(1000L, sendUptime); // Here you set interval (1sec) and which function to call
}
void sendUptime()
{
// This function sends Arduino up time every 1 second to Virtual Pin (V5)
// In the app, Widget's reading frequency should be set to PUSH
// You can send anything with any interval using this construction
// Don't send more that 10 values per second
sensors.requestTemperatures(); // 温度取得要求
Serial.println(sensors.getTempCByIndex(0)); //温度の取得&シリアル送信
Blynk.virtualWrite(V1, sensors.getTempCByIndex(0));
// フロートスイッチ用の検知用
state = digitalRead(b_pin); // ピンよりデータ取得
Serial.print("switch:");
// フロートスイッチの状態をblinkさせる, モニタにも状況を出力
if (state == 1) {
Serial.println("on"); // シリアルモニタに出力
digitalWrite(13, HIGH); // turn the LED off by making the voltage LOW
Blynk.virtualWrite(V2, 255);
} else {
Serial.println("off"); // シリアルモニタに出力
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
Blynk.virtualWrite(V2, 0);
}
//Blynk.virtualWrite(V5, millis() / 1000);
}
void loop()
{
Blynk.run(); // all the Blynk magic happens here
timer.run(); // BlynkTimer is working...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment