#!/bin/sh /etc/rc.common
# Copyright (C) 2013 OpenWrt.org
START=90

start() {

mosquitto_sub -t ESP01/# -v |
while read line
   do
      echo $line
         if echo $line | grep -q "ESP01/end"; then
         datum=$(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}')"
         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 }')
}

