-
Notifications
You must be signed in to change notification settings - Fork 0
/
addSensor.sh
executable file
·52 lines (40 loc) · 1.48 KB
/
addSensor.sh
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
#!/bin/sh
# user API key for channel creation
userKey=USER_KEY
# write API key for sensor registration channel
registrationKey=REG_WRITE_KEY
# overwrite the above variables with actual values
source private/config.sh
bold=$(tput bold)
normal=$(tput sgr0)
# get some info for the new channel
echo "\n${bold}Channel title?${normal}"
read channelTitle
echo "${bold}Graph hex value?${normal}"
read hexCode
# spinup new channel
# @see https://www.mathworks.com/help/thingspeak/createchannel.html
createResponse=$(curl -X POST \
--data-urlencode "api_key=$userKey" \
--data-urlencode "name=ArduinoAQI / $channelTitle" \
--data-urlencode "field1=PM 1.0 (µg/m³)" \
--data-urlencode "field2=PM 2.5 (µg/m³)" \
--data-urlencode "field3=PM 10.0 (µg/m³)" \
--data-urlencode "field4=AQI" \
--data-urlencode "metadata={\"color\": \"$hexCode\"}" \
https://api.thingspeak.com/channels.json --silent)
channelId=$(echo $createResponse | jq -rc .id)
writeKey=$(echo $createResponse | jq -rc '.api_keys[] | select(.write_flag) | .api_key')
# validate channel create
if [[ $channelId == "null" || -z $writeKey ]]
then
echo "\n${bold}Channel creation failed! Quitting…${normal}"
exit 0
fi
echo "\nChannel successfully created!\n"
# add sensor to registration channel
echo "${bold}New sensor MAC address?${normal}"
read macAddress
# @todo validate
curl "https://api.thingspeak.com/update.json?api_key=$registrationKey&field1=$macAddress&field2=$channelId&field3=$writeKey"
echo "\n"