-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathThingspeakGoogeImportSmart.gs
80 lines (67 loc) · 3 KB
/
ThingspeakGoogeImportSmart.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//'CHANNEL_ID';'API_WRITE_KEY';
function sendDataToSheet() {
// Replace 'YOUR_CHANNEL_ID' and 'YOUR_API_KEY' with your own Thingspeak channel ID and API key
var channelId = '00000000';
var apiKey = 'API_WRITE_KEY';
// Replace 'SHEET_NAME' with the name of the sheet you want to send the data to
var sheetName = 'Sheet1';
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
var lastEmailTimestamp = 0;
// Get the last entry in the Thingspeak channel
var response = UrlFetchApp.fetch('https://api.thingspeak.com/channels/' + channelId + '/feeds/last.json?api_key=' + apiKey, {muteHttpExceptions: true});
var data = response.getContentText();
var dataJson = JSON.parse(data);
//get the date and time (current)
var now = new Date();
var date = Utilities.formatDate(now, "GMT+0800", "dd-MM-yyyy");
var time = Utilities.formatDate(now, "GMT+0800", "HH:mm:ss");
// Get the field values from the response, uncomment for specific field/s
var field1 = dataJson.field1;
var field2 = dataJson.field2;
var field3 = dataJson.field3;
var field4 = dataJson.field4;
var field5 = dataJson.field5;
var field6 = dataJson.field6;
var field7 = dataJson.field7;
var field8 = dataJson.field8;
// Get the latitude and longitude values from the ThingSpeak channel setup
response = UrlFetchApp.fetch("https://api.thingspeak.com/channels/" + channelId + ".json?api_key=" + apiKey, {muteHttpExceptions: true});
data = JSON.parse(response.getContentText());
//latlong
var lat = data.latitude;
Logger.log("Latitude: " + lat);
var lng = data.longitude;
Logger.log("Longitude: " + lng);
// Combine the latitude and longitude values as a single "latlong" variable
var latlong = lat + "," + lng;
Logger.log("Latitude,Longitude: " + latlong);
// Append the data to the sheet
sheet.appendRow([now, date, time, field1, field2, field3, field4, field5, field6, field7, field8, latlong]);
/*
Notice:
1) Make Thingspeak Public Channel
2) Enter the GPS (lat/ long)
3) Use the following code:
"response = UrlFetchApp.fetch("https://api.thingspeak.com/channels/" + channelId + ".json?api_key=" + apiKey, {muteHttpExceptions: true});
data = JSON.parse(response.getContentText());""
4) name your sheet - // var sheetName = 'Sheet1';
*/
/*
//===additional===\\
// Check if the temperature value is above a certain level (100 email per day)
if (field4 > 30.1) {
//Send an email notification
var recipient = "[email protected]";
var subject = "Temperature Alert";
var body = "The temperature has exceeded the threshold of -3°C.";
MailApp.sendEmail(recipient, subject, body);
}
// Check if the temperature value is above a certain level
if (field4 > 30.0) {
var botToken = "5986756358:AAG5ctpYERwrRo-ZkEjIYX-sBqvKUGA7aQA";
var chatId = "300227496";
var message = "The temperature has exceeded the threshold of -2.4°C.";
UrlFetchApp.fetch("https://api.telegram.org/bot" + botToken + "/sendMessage?chat_id=" + chatId + "&text=" + message);
}
*/
}