Skip to content

Instantly share code, notes, and snippets.

@PonDad
Last active June 28, 2023 04:34
Show Gist options
  • Save PonDad/2ae4dce97d114af02cbd3bcd1a668c5d to your computer and use it in GitHub Desktop.
Save PonDad/2ae4dce97d114af02cbd3bcd1a668c5d to your computer and use it in GitHub Desktop.
var wio_jp_server = '{SERVER_ADRESS}' //WioNodeの日本サーバーアドレス
var wio_access_token_1 = '{API_TOKEN_1}' // WioNode1のAPI Token
var wio_access_token_2 = '{API_TOKEN_2}' // WioNode2のAPI Token
var spreadsheetId = '{SPREADSHEET_ID}' // スプレッドシートのID
function wio() {
var moisture = getMoisture() // WioNode1から水分データを取得
var temperature = getTemperature() // WioNode2から気温データを取得
var humidity = getHumidity() // WioNode2から湿度情報を取得
var lux = getLux() // WioNode1から照度情報を取得
var lastData = getLastData() //最終data取得
setLastData(
{
mo:moisture.moisture, // 水分
te:temperature.temperature,//温度
hu:humidity.humidity, // 湿度
lu:lux.lux, // 照度
},
lastData.row + 1 //最終data追加作業
)
}
function getMoisture() {
var url = `${wio_jp_server}GroveMoistureA0/moisture?access_token=${wio_access_token_1}`
var moisture = JSON.parse(UrlFetchApp.fetch(url));
Logger.log(moisture)
return moisture
}
function getTemperature() {
var url = `${wio_jp_server}GroveTempHumiSHT35I2C0/temperature?access_token=${wio_access_token_2}`
var temperature = JSON.parse(UrlFetchApp.fetch(url));
Logger.log(temperature)
return temperature
}
function getHumidity() {
var url = `${wio_jp_server}GroveTempHumiSHT35I2C0/humidity?access_token=${wio_access_token_2}`
var humidity = JSON.parse(UrlFetchApp.fetch(url));
Logger.log(humidity)
return humidity
}
function getLux() {
var url = `${wio_jp_server}GroveDigitalLightI2C0/lux?access_token=${wio_access_token_1}`
var lux = JSON.parse(UrlFetchApp.fetch(url));
Logger.log(lux)
return lux
}
function getLastData() {
var datas = SpreadsheetApp.openById(spreadsheetId).getSheetByName('log').getDataRange().getValues()
var data = datas[datas.length - 1]
return {
totalpoint:data[1],
coupon:data[2],
row:datas.length,
}
}
function setLastData(data, row) {
SpreadsheetApp.openById(spreadsheetId).getSheetByName('log').getRange(row, 1).setValue(new Date())
SpreadsheetApp.openById(spreadsheetId).getSheetByName('log').getRange(row, 2).setValue(data.mo)
SpreadsheetApp.openById(spreadsheetId).getSheetByName('log').getRange(row, 3).setValue(data.te)
SpreadsheetApp.openById(spreadsheetId).getSheetByName('log').getRange(row, 4).setValue(data.hu)
SpreadsheetApp.openById(spreadsheetId).getSheetByName('log').getRange(row, 5).setValue(data.lu)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment