Automatic Weather Tweets

From Domoticz
Revision as of 12:41, 24 June 2022 by Admin (talk | contribs) (→‎Getting the Twitter script)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Use your weather sensors with Twitter

This guide is for using a Twitter bash script with Domoticz, to tweet the data of your weather sensors.

THIS GUIDE IS "WORK IN PROGRESS"

Original forum post

Example of an Domoticz Twitter weather station

Create a Twitter account

First you need to create a Twitter account here

- Name: Choose a name for your "weather station".
- E-mail: Your e-mail address.
- Password: Your password.
- Username: Use something like "WeatherInAmsterdam".

Getting the Twitter script

tweet.sh

 #!/bin/bash
 #Twitter status update bot
 #Author: Luka Pusic
 
 #REQUIRED PARAMS
 username="username"
 password="password"
 tweet="$*" #must be less than 140 chars
 
 #EXTRA OPTIONS
 uagent="Mozilla/5.0" #user agent (fake a browser)
 sleeptime=0 #add pause between requests
 
 if [ $(echo "$tweet" | wc -c) -gt 140 ]; then
 	echo "[FAIL] Tweet must not be longer than 140 chars!" && exit 1
 elif [ "$tweet" == "" ]; then
 	echo "[FAIL] Nothing to tweet. Enter your text as argument." && exit 1
 fi
 
 touch "cookie.txt" #create a temp. cookie file
 
 #GRAB LOGIN TOKENS
 echo "[+] Fetching twitter.com..." && sleep $sleeptime
 initpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" "https://mobile.twitter.com/session/new")
 token=$(echo "$initpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//')
 
 #LOGIN
 echo "[+] Submitting the login form..." && sleep $sleeptime
 loginpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$token&username=$username&password=$password" "https://mobile.twitter.com/session")
 
 #GRAB COMPOSE TWEET TOKENS
 echo "[+] Getting compose tweet page..." && sleep $sleeptime
 composepage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L -A "$uagent" "https://mobile.twitter.com/compose/tweet")
 
 #TWEET
 echo "[+] Posting a new tweet: $tweet..." && sleep $sleeptime
 tweettoken=$(echo "$composepage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//' | tail -n 1)
 update=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$tweettoken&tweet[text]=$tweet&tweet[display_coordinates]=false" "https://mobile.twitter.com/")
 
 #GRAB LOGOUT TOKENS
 logoutpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" "https://mobile.twitter.com/account")
 
 #LOGOUT
 echo "[+] Logging out..." && sleep $sleeptime
 logouttoken=$(echo "$logoutpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//' | tail -n 1)
 logout=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$logouttoken" "https://mobile.twitter.com/session/destroy")
 
 rm "cookie.txt"

Setting up the Twitter script

First you need to add some pieces of script to get temperature ect.
You don't need to create weather.txt in the scripts folder. Will be done by the script.
In this example I use an sensor with both temperature and humidity combined.

Add on top of tweet.sh
To extract temperature from Domoticz:

thermo=`curl "http://api_username:api_password@domoticz_server:domoticz_port/json.htm?type=devices&rid=your_id"`
echo $thermo > /home/pi/domoticz/scripts/weather.txt
temperature=`cat /home/pi/domoticz/scripts/weather.txt | awk -F: '{print $13, $14}' | awk '{print $3}' | sed 's/\"//g'`

If you are using separated sensors for temperature and humidity. You should edit the first "awk -F: '{print $13, $14}'" and play with the number 13 and 14.
Add under the temperature, to extract the humidity from Domoticz:

hygro=`curl "http://api_username:api_password@domoticz_server:domoticz_port/json.htm?type=devices&rid=your_id"`
echo $hygro > /home/pi/domoticz/scripts/weather.txt
hygro=`cat /home/pi/domoticz/scripts/weather.txt | awk -F: '{print $14, $15}' | awk '{print $3}' | sed 's/\"//g'`

If you are using separated sensors for temperature and humidity. You should edit the first "awk -F: '{print $13, $14}'" and play with the number 13 and 14.
Add under the humidity, to extract the rain from Domoticz:

rain=`curl "http://api_username:api_password@domoticz_server:domoticz_port/json.htm?type=devices&rid=your_id"`
echo $rain > /home/pi/domoticz/scripts/weather.txt
rain=`cat /home/pi/domoticz/scripts/weather.txt | awk -F: '{print $24, $25}' | awk '{print $3}' | sed 's/\"//g' | sed 's/,//g'`

Add under the rain, to extract the wind from Domoticz:

wind=`curl "http://api_username:api_password@domoticz_server:domoticz_port/json.htm?type=devices&rid=your_id"`
echo $wind > /home/pi/domoticz/scripts/weather.txt
dir=`cat /home/pi/domoticz/scripts/weather.txt | awk -F: '{print $11, $12}' | awk '{print $3}' | sed 's/\"//g' | sed 's/,//g'`
speed=`cat /home/pi/domoticz/scripts/weather.txt | awk -F: '{print $24, $25}' | awk '{print $3}' | sed 's/\"//g' | sed 's/,//g'`

Add this piece of script under the wind:

tweet="Temperature in #Amsterdam: $temperature°C           #change to what you want but keep "$temperature"
Humidity: $hygro%          #change to what you want but keep "$hygro"
Rain: $rain mm        #change to what you want but keep "$rain"
Wind: $dir / $speed km/h"         #change to what you want but keep "$dir and $speed"

rm /home/pi/domoticz/scripts/weather.txt            #will remove the weather.txt file

Fill in your username (e-mail address) and password at:

#REQUIRED PARAMS
username="username"
password="password"

Change this in the original script under password
tweet="$*" to tweet="$tweet"

Final Twitter script

This is what your Twitter script should look like:

 #!/bin/bash
 #Twitter status update bot by http://360percents.com
 #Author: Luka Pusic <[email protected]>
 
 thermo=`curl "http://api_username:api_password@domoticz_server:domoticz_port/json.htm?type=devices&rid=your_id"`
 echo $thermo > /home/pi/domoticz/scripts/weather.txt
 temperature=`cat /home/pi/domoticz/scripts/weather.txt | awk -F: '{print $13, $14}' | awk '{print $3}' | sed 's/\"//g'`
 
 hygro=`curl "http://api_username:api_password@domoticz_server:domoticz_port/json.htm?type=devices&rid=your_id"`
 echo $hygro > /home/pi/domoticz/scripts/weather.txt
 hygro=`cat /home/pi/domoticz/scripts/weather.txt | awk -F: '{print $14, $15}' | awk '{print $3}' | sed 's/\"//g'`
 
 rain=`curl "http://api_username:api_password@domoticz_server:domoticz_port/json.htm?type=devices&rid=your_id"`
 echo $rain > /home/pi/domoticz/scripts/weather.txt
 rain=`cat /home/pi/domoticz/scripts/weather.txt | awk -F: '{print $24, $25}' | awk '{print $3}' | sed 's/\"//g' | sed 's/,//g'`
 
 wind=`curl "http://api_username:api_password@domoticz_server:domoticz_port/json.htm?type=devices&rid=your_id"`
 echo $wind > /home/pi/domoticz/scripts/weather.txt
 dir=`cat /home/pi/domoticz/scripts/weather.txt | awk -F: '{print $11, $12}' | awk '{print $3}' | sed 's/\"//g' | sed 's/,//g'`
 speed=`cat /home/pi/domoticz/scripts/weather.txt | awk -F: '{print $24, $25}' | awk '{print $3}' | sed 's/\"//g' | sed 's/,//g'`
 
 tweet="Temperature in #Amsterdam: $temperature°C           #change to what you want but keep "$temperature"
 Humidity: $hygro%          #change to what you want but keep "$hygro"
 Rain: $rain mm        #change to what you want but keep "$rain"
 Wind: $dir / $speed km/h"         #change to what you want but keep "$dir and $speed"
 
 rm /home/pi/domoticz/scripts/weather.txt            #will remove the weather.txt file
 
 #REQUIRED PARAMS
 username="username"     #fill in your twitter e-mail address
 password="password"      #fill in your twitter password
 tweet="$tweet" #must be less than 140 chars
 
 #EXTRA OPTIONS
 uagent="Mozilla/5.0" #user agent (fake a browser)
 sleeptime=0 #add pause between requests
 
 if [ $(echo "$tweet" | wc -c) -gt 140 ]; then
 	echo "[FAIL] Tweet must not be longer than 140 chars!" && exit 1
 elif [ "$tweet" == "" ]; then
 	echo "[FAIL] Nothing to tweet. Enter your text as argument." && exit 1
 fi
 
 touch "cookie.txt" #create a temp. cookie file
 
 #GRAB LOGIN TOKENS
 echo "[+] Fetching twitter.com..." && sleep $sleeptime
 initpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" "https://mobile.twitter.com/session/new")
 token=$(echo "$initpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//')
 
 #LOGIN
 echo "[+] Submitting the login form..." && sleep $sleeptime
 loginpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$token&username=$username&password=$password" "https://mobile.twitter.com/session")
 
 #GRAB COMPOSE TWEET TOKENS
 echo "[+] Getting compose tweet page..." && sleep $sleeptime
 composepage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L -A "$uagent" "https://mobile.twitter.com/compose/tweet")
 
 #TWEET
 echo "[+] Posting a new tweet: $tweet..." && sleep $sleeptime
 tweettoken=$(echo "$composepage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//' | tail -n 1)
 update=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$tweettoken&tweet[text]=$tweet&tweet[display_coordinates]=false" "https://mobile.twitter.com/")
 
 #GRAB LOGOUT TOKENS
 logoutpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" "https://mobile.twitter.com/account")
 
 #LOGOUT
 echo "[+] Logging out..." && sleep $sleeptime
 logouttoken=$(echo "$logoutpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//' | tail -n 1)
 logout=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$logouttoken" "https://mobile.twitter.com/session/destroy")
 
 rm "cookie.txt"

Save your script at /domoticz/scripts/ as tweet.sh

Make script executable and add to crontab

Make tweet.sh executable with the following commands via SSH

cd domoticz/scripts
chmod +x tweet.sh

Check if the script is executable

./tweet.sh

Should show this in SSH:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1031  100  1031    0     0  14557      0 --:--:-- --:--:-- --:--:-- 20215
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1031  100  1031    0     0   8797      0 --:--:-- --:--:-- --:--:-- 12421
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   951  100   951    0     0   7697      0 --:--:-- --:--:-- --:--:-- 10225
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   910  100   910    0     0   8491      0 --:--:-- --:--:-- --:--:-- 12465
[+] Fetching twitter.com...
[+] Submitting the login form...
[+] Getting compose tweet page...
[+] Posting a new tweet: Temperatuur in #Hem: 1.5°C
Luchtvochtigheid: 84%
Neerslag: 0.0 mm
Wind: SSW / 3.6 km/h...
[+] Logging out...

Finally add the script to crontab in SSH

crontab -e

add this line (for every hour a tweet)

0 * * * * /home/pi/domoticz/scripts/tweet.sh

And save the crontab.
Check if the crontab is safed by:

crontab -l

The line should be present.

Discuss this here