#!/bin/sh /etc/rc.common
START=97

start() {

mosquitto_sub -t '#' -v |
while read line
   do
      echo $line
         if echo $line | grep -q "ESP01/end"; then
         datum=$(date +%y-%m-%d_%H:%M:%S)
         datu=`date '+%Y-%m-%d %H:%M:%S'`
         sqlite3 /home/sqlite/mosquitto.db "insert into esp01 (date, temperature, humidity, pressure, voltage, ontime) values('${datum}', '${temperature}', '${humidity}', '${pressure}', '${voltage}', '${ontime}')"
         QUERY="insert into esp01 (date, temperature, humidity, pressure, voltage, ontime) values('${datu}', '${temperature}', '${humidity}', '${pressure}', '${voltage}', '${ontime}')"
         echo $QUERY | mysql -u weather --password='pass' -h 192.168.0.1 weather
         fi
         if echo $line | grep -q "ESP01/Temperature"; then
            temperature=$(echo $line | awk '{print $2}');
         fi
         if echo $line | grep -q "ESP01/Humidity"; then
            humidity=$(echo $line | awk '{print $2}');
         fi
         if echo $line | grep -q "ESP01/Voltage"; then
            voltage=$(echo $line | awk '{print $2}');
         fi
         if echo $line | grep -q "ESP01/OnTime"; then
            ontime=$(echo $line | awk '{print $2}');
         fi
         if echo $line | grep -q "ESP01/Pressure"; then
            pressure=$(echo $line | awk '{print $2}');
         fi
done
}

stop() {
    kill -9 $(ps | grep "mosquitto_sub" | grep -v grep | awk '{ print $1 }')
} 

