def __init__(self,
              coin=None,
              n_hours=1,
              n_time_predicts=1,
              config_train=None,
              units=None):
     self.coin = coin
     self.n_hours = n_hours
     self.n_time_predicts = n_time_predicts
     self.config_train = config_train
     self.units = units
     self.db = ConnectDB()
     self.scaler = MinMaxScaler(feature_range=(0, 1))
Esempio n. 2
0
def on_message(client, userdata, msg):
    print(client, userdata, msg)
    topic = msg.topic

    if not topic:
        return

    splited_topic = topic.split('/')

    if splited_topic[0] != '@msg':
        print(f'we not interested in topic {splited_topic[0]}')
        return
    try:
        payload = msg.payload.decode('utf-8')
    except:
        print('decode payload error')
        return

    print(datetime.now(), '#', topic, payload)

    device_type = splited_topic[1]
    value = payload

    if device_type == 'pump':
        if splited_topic[2] == "pump1":
            device_id = 1
        else:
            device_id = 2
        if (value == 'on'):
            status = 1
        else:
            status = 0
        ConnectDB.set_status(status, device_id)
    elif device_type == 'greenHouse':
        if splited_topic[3] == 'OPEN':
            status = 1
        elif splited_topic[3] == 'CLOSE':
            status = 1
        if splited_topic[2] == "OS":
            ConnectDB.set_status(status, 3)
        if splited_topic[2] == "IS":
            ConnectDB.set_status(status, 4)
    elif device_type == 'hydroponic':
        if splited_topic[2] == "light":
            if payload == 'on':
                ConnectDB.set_status(1, 5)
            if payload == 'off':
                ConnectDB.set_status(0, 5)
Esempio n. 3
0
 def process_light(cls, veget_id):
     client = mqtt.Client("P1")
     client.username_pw_set("smartfarm", "123456788")
     client.connect("192.168.31.41")
     light = ConnectDB.get_values("light", veget_id)
     print(light)
     if light < 265:  #แสงมาก
         print('ปิดไฟ')
         client.publish("@msg/hydroponic/lsight/all", "off")
         print('ปิดสแลนด้านนอก')
         client.publish("@msg/greenHouse/OS/CLOSE", "on")
         #ClosesunscreenOUT
         print('ปิดสแลนด้านใน')
         client.publish("@msg/greenHouse/IS/CLOSE", "on")
         #ClosesunscreenIN
         return True
     elif light <= 265 and light >= 275:
         client.publish("@msg/greenHouse/OS/OPEN", "on")
         #ClosesunscreenOUT
         print('ปิดสแลนด้านใน')
         client.publish("@msg/greenHouse/IS/CLOSE", "on")
     elif light > 275:  #แสงน้อย
         print('เปิดสแลนด้านนอก')
         client.publish("@msg/greenHouse/OS/OPEN", "on")
         #ClosesunscreenOUT
         print('เปิดสแลนด้านใน')
         client.publish("@msg/greenHouse/IS/OPEN", "on")
         #Closesunscreenin
         print('เปิดไฟ')
         client.publish("@msg/hydroponic/light/red", "on")
         client.publish("@msg/hydroponic/light/blue", "on")
     return True
Esempio n. 4
0
    def process_fertilizer(cls, veget):
        ConnectDB.get_values("light", veget['veget_id'])
        ec = ConnectDB.get_values("ec",
                                  veget['veget_id']) + 0.1  #ec ที่วัดได้ 0.7
        ph = ConnectDB.get_values("ph", veget['veget_id'])  #pH ที่วัดได้
        valueveget = ConnectDB.get_valueveget(
            veget['veget_id'])  #ค่า ที่ต้องการ 1
        fertilizerintense = ConnectDB.get_fertilizer(
            veget['fertilizer_id'])  #ความเข้มข้นปุ๋ย 0.1 100ml/1000
        level = ConnectDB.get_values("level", veget['veget_id'])  #ระดับน้ำ
        mixer = ConnectDB.get_status(6, 0)  #สถานะถังน้ำ
        pump1 = ConnectDB.get_status(1, 0)  #สถานะถังน้ำ
        print('ค่า', ec)
        print('ค่าที่ต้องการ', valueveget['ec'])
        if (ec <= (valueveget['ec'] - 0.1)):
            fertilizersum = (valueveget['ec'] - ec) * (
                100000)  #คำนวนหาจำนวนที่ต้องใช้ปุ๋ย
            fertilizerpre = (fertilizersum + 3000) / 2
            print('ปุ๋ยที่ต้องเติม', fertilizersum)
            # client.publish("@msg/fertilizer/fertilizer1/control",fertilizerpre)#เติมปุ๋ยที่ยังไม่ผสม
            # client.publish("@msg/pump/pump2","on")

            print("น้ำที่ผสม", fertilizerpre)
            client.publish("@msg/fertilizer/water/control",
                           10000)  #เติมน้ำเพื่อผสม
            client.publish("@msg/pump/pump2", "on")
            sleep(100)
            # client.publish("@msg/htdroponic/htdroponic2","on")
            # client.publish("@msg/pump/flow2",fertilizerml)#เติมปุ๋ยที่ผสมแล้ว
            # sleep(20)
            # client.publish("@msg/pump/flow2",5000)#เติมน้ำล้างท่อ
            # client.publish("@msg/htdroponic/htdroponic2","off")
            return True

        elif (ec >= (valueveget['ec'] + 0.1)):
            water = ec - valueveget['ec']  #คำนวนปริมาณเพื่อเจือจาง
            print("เติมน้ำ", water)
            # client.publish("@msg/htdroponic/htdroponic2","on")
            # client.publish("@msg/greenHouse/water","on")
            # client.publish("@msg/pump/flow2",water)#เติมน้ำเพื่อนเจือจาง
            # client.publish("@msg/pump/pump1","on")
            # client.publish("@msg/htdroponic/htdroponic2","off")
            return True
        return False
def on_message(client, userdata, message):
    today = date.today()
    now = datetime.now()
    try:
        text = str(message.payload.decode("utf-8"))
        print("message received ", text)
        if message.topic == "@msg/hydro/sensor":
            valjs = ast.literal_eval(text)
            valsql = (valjs["ph"], valjs["ec"], valjs["lowpump"],
                      valjs["light"], valjs["temp"], valjs["level"],
                      valjs["Sensor"])
            print(ConnectDB.set_sensorvalue(valsql))
    except:
        print('error sensor')
    print("message topic=", message.topic)
    print("message qos=", message.qos)
    print("message retain flag=", message.retain)
    current_time = now.strftime("%H:%M:%S")
    current_date = today.strftime("%d/%m/%Y")
    print("Current  =", current_time, current_date)
Esempio n. 6
0
    def handler(cls, pump_id, value):  #value= เปิด-ปิด

        status = ConnectDB.get_status(pump_id, 0)

        if not status and type(status) is bool:
            return

        print(f'pump {pump_id} : status {status} : value {value}')

        if value != status:
            killer_func = cls.__WAIT_ABORTER.get(f'pump-{pump_id}', None)
            # find {'pump-1': object}

            if value == '1':
                if killer_func:
                    del cls.__WAIT_ABORTER[f'pump-{pump_id}']

                killer_func = cls.wait_with_timeout(
                    func=cls.worker,
                    pump_id=pump_id,
                    timeout=30,  #sec
                    check_interval=1,  #sec
                )
                cls.__WAIT_ABORTER[f'pump-{pump_id}'] = killer_func
                # print('thread_object', thread_object)

            elif value == '0':
                pass
                # if stopper:
                #     stopper()
                #     del cls.__WAIT_ABORTER[f'pump-{pump_id}']

            result = cls.control(pump_id=pump_id, value=value)
            print('control result', result)
            # print(cls.__DATA)
        else:
            print('device already', value)
Esempio n. 7
0
from subprocess import Popen, PIPE, STDOUT
from connectDB import ConnectDB
from crawler_data_binance import crawlerDataBinance
import time

if __name__ == '__main__':
    start_time = time.time()
    # get data from Binance
    crawler = crawlerDataBinance()
    crawler.insert_coin_info_to_db()
    crawler.insert_symbols_candlestick_data()
    print("Total time get data: %f" % (time.time() - start_time))
    start_time = time.time()
    # predict data
    DB = ConnectDB()
    list_coin = DB.get_list_coin_info("ETH")
    for coin in list_coin:
        print(coin)
        try:
            p = Popen('python3 predict.py -id %s -symbol %s' %
                      (coin[1], coin[0]),
                      shell=True,
                      stdout=PIPE,
                      stderr=STDOUT)
            retval = p.wait()
        except Exception as e:
            print(e)
    print("Total time predict: %f" % (time.time() - start_time))
Esempio n. 8
0
from datetime import datetime, timedelta
from threading import Event, Thread
from connectDB import ConnectDB
ConnectDB.connect()


class Pump:
    __WAIT_ABORTER = {}

    @classmethod
    def worker(cls, **kwargs):
        pump_id = kwargs.get('pump_id')
        status = cls.get_status(pump_id=pump_id)

        print(f'wait for pump {pump_id} off')
        if int(status) == 0:
            # print('yes status is 0')
            killer_func = cls.__WAIT_ABORTER.get(f'pump-{pump_id}', None)
            # print('killer_func', killer_func)
            if killer_func:
                killer_func()  # real kill

            print(f'now pump {pump_id} off, done')
            print(cls.__DATA)

    @classmethod
    def wait_with_timeout(cls, func, **kwargs):
        stopped = Event()
        check_interval = kwargs.get('check_interval')
        timeout = kwargs.get('timeout')
        wait_since = datetime.now()
Esempio n. 9
0
#!/usr/bin/env python
import MySQLdb
import requests
from time import sleep
#from fertilizer import Fertilizer
from light import Light
from connectDB import ConnectDB
ConnectDB.connect()
ConnectDB._cursor.execute("SELECT * FROM veget")
myresult = ConnectDB._cursor.fetchall()
for value in myresult:
    print('main')
    veget_id = value['veget_id']
    print(veget_id)
    light = ConnectDB.get_values("light", veget_id)
    print(light)
print('this is main process from Pi')
Esempio n. 10
0
 def __init__(self):
     self.db = ConnectDB()
     self.scaler = MinMaxScaler(feature_range=(0, 1))
     self.n_hours = 1
Esempio n. 11
0
class Predict(object):
    SYMBOL = 0
    ID_COIN = 1

    def __init__(self):
        self.db = ConnectDB()
        self.scaler = MinMaxScaler(feature_range=(0, 1))
        self.n_hours = 1

    def get_y(self, values):
        y = delete(values, s_[1:], 1).reshape(-1)
        return y

    def load_model(self):
        json_file = open('models/model_%s.json' % coin[self.SYMBOL], 'r')
        loaded_model_json = json_file.read()
        json_file.close()
        model = model_from_json(loaded_model_json)
        model.load_weights("weights/weight_%s.h5" % coin[self.SYMBOL])
        return model

    def save_to_db(self, inv_yhat, inv_y, id_coin):
        time_create = int(time.time())
        price_predict = array2string(inv_yhat, separator=',')
        price_actual = array2string(inv_y, separator=',')
        price_preidct_last = inv_yhat[-1]
        price_predict_previous = inv_yhat[-2]
        price_actual_last = inv_y[-1]
        price_actual_previous = inv_y[-2]
        self.db.insert_history_prediction(id_coin, time_create, price_actual,
                                          price_predict, price_preidct_last,
                                          price_predict_previous,
                                          price_actual_last,
                                          price_actual_previous)

    def normalize_data(self,
                       dataset,
                       n_time_predicts=1,
                       n_features=1,
                       dropnan=True):
        values = dataset.values
        values = values.astype('float32')
        values = self.scaler.fit_transform(values)
        values = values.reshape((n_time_predicts, 1, n_features))
        return values

    def make_predict(self, model, test_X, n_features=1):
        yhat = model.predict(test_X)
        test_X = test_X.reshape((test_X.shape[0], self.n_hours * n_features))
        inv_yhat = self.invert_scaling(yhat, test_X, n_features)
        return inv_yhat

    def invert_scaling(self, test_y, test_X_reshape, n_features):
        inv_y = concatenate((test_y, test_X_reshape[:, -(n_features - 1):]),
                            axis=1)
        inv_y = self.scaler.inverse_transform(inv_y)
        inv_y = inv_y[:, 0]
        return inv_y

    def make_predict_price_coin(self, coin):
        dataset = self.db.get_data_predict_by_id(coin[self.ID_COIN])
        inv_y = self.get_y(dataset.values)
        n_features = len(dataset.columns)
        n_time_predicts = len(dataset)
        test_X = self.normalize_data(dataset,
                                     n_time_predicts=n_time_predicts,
                                     n_features=n_features)
        model = self.load_model()
        inv_yhat = self.make_predict(model, test_X, n_features=n_features)
        self.save_to_db(inv_yhat, inv_y, coin[self.ID_COIN])
Esempio n. 12
0
    def process_fertilizer(cls, veget):
        ConnectDB.get_values("light", veget['veget_id'])
        ec = ConnectDB.get_values("ec",
                                  veget['veget_id']) + 0.1  #ec ที่วัดได้ 0.7
        ph = ConnectDB.get_values("ph", veget['veget_id'])  #pH ที่วัดได้
        valueveget = ConnectDB.get_valueveget(
            veget['veget_id'])  #ค่า ที่ต้องการ 1
        fertilizerintense = ConnectDB.get_fertilizer(
            veget['fertilizer_id'])  #ความเข้มข้นปุ๋ย 0.1 100ml/1000
        level = ConnectDB.get_values("level", veget['veget_id'])  #ระดับน้ำ
        mixer = ConnectDB.get_status(6, 0)  #สถานะถังน้ำ
        pump1 = ConnectDB.get_status(1, 0)  #สถานะถังน้ำ
        print('ค่า', ec)
        print('ค่าที่ต้องการ', valueveget['ec'])
        if (ec <= (valueveget['ec'] - 0.1)):
            client = mqtt.Client("P1")
            client.username_pw_set("smartfarm", "123456788")
            client.connect("192.168.31.41")
            #fertilizersum = (valueveget['ec']-ec)*(100000) #คำนวนหาจำนวนที่ต้องใช้ปุ๋ย
            fertilizersum = 10000
            fertilizerpre = (fertilizersum + 2000) / 2
            print('ปุ๋ยที่ต้องเติม', fertilizersum)
            client.publish("@msg/fertilizer/fertilizer2/control",
                           fertilizerpre)  #เติมปุ๋ยที่ยังไม่ผสม
            client.publish("@msg/pump/pump2", "on")
            print("้เวลาเติมปุ๋ย", 9 * (fertilizerpre / 1000))
            sleep(9 * (fertilizerpre / 1000))
            client = mqtt.Client("P1")
            client.username_pw_set("smartfarm", "123456788")
            client.connect("192.168.31.41")
            client.publish("@msg/fertilizer/fertilizer2/control", 'off')
            client.publish("@msg/fertilizer/water/control",
                           fertilizerpre)  #เติมน้ำเพื่อผสม
            client.publish("@msg/pump/pump2", "on")
            print("เวลาเติมน้ำ", 9 * (fertilizerpre / 1000))
            sleep(9 * (fertilizerpre / 1000))
            client = mqtt.Client("P1")
            client.username_pw_set("smartfarm", "123456788")
            client.connect("192.168.31.41")
            print("น้ำที่ผสม", fertilizerpre)
            client.publish("@msg/pump/pump2", "off")
            print("เติมน้ำเสร็จ")
            client.publish("@msg/fertilizer", "on")
            sleep(1)
            client.publish("@msg/hydroponic/hydroponic2", "on")
            sleep(1)
            client.publish("@msg/pump/flow2", 1000)  #เติมปุ๋ยที่ผสมแล้ว
            sleep(1)
            client.publish("@msg/pump/pump1", "on")
            print("เวลาเติมน้ำที่ผสม", 3 + (1.5 * (fertilizerpre / 1000)))
            sleep(3 + (2 * (fertilizerpre / 1000)))
            client = mqtt.Client("P1")
            client.username_pw_set("smartfarm", "123456788")
            client.connect("192.168.31.41")
            client.publish("@msg/pump/pump1", "off")
            client.publish("@msg/fertilizer", "off")
            client.publish("@msg/hydroponic/hydroponic2", "off")
            return True

        elif (ec >= (valueveget['ec'] + 0.1)):
            water = ec - valueveget['ec']  #คำนวนปริมาณเพื่อเจือจาง
            print("เติมน้ำ", water)
            # client.publish("@msg/htdroponic/htdroponic2","on")
            # client.publish("@msg/greenHouse/water","on")
            # client.publish("@msg/pump/flow2",water)#เติมน้ำเพื่อนเจือจาง
            # client.publish("@msg/pump/pump1","on")
            # client.publish("@msg/htdroponic/htdroponic2","off")
            return True
        return False
class trainModel(object):
    SYMBOL = 0
    ID_COIN = 1

    def __init__(self,
                 coin=None,
                 n_hours=1,
                 n_time_predicts=1,
                 config_train=None,
                 units=None):
        self.coin = coin
        self.n_hours = n_hours
        self.n_time_predicts = n_time_predicts
        self.config_train = config_train
        self.units = units
        self.db = ConnectDB()
        self.scaler = MinMaxScaler(feature_range=(0, 1))

    def series_to_supervised(self, data, n_in=1, n_out=1, dropnan=True):
        n_vars = 1 if type(data) is list else data.shape[1]
        df = DataFrame(data)
        cols, names = list(), list()
        for i in range(0, n_out):
            cols.append(df.shift(i))
            if i == 0:
                names += [('var%d(t)' % (j + 1)) for j in range(n_vars)]
            else:
                names += [('var%d(t+%d)' % (j + 1, i)) for j in range(n_vars)]
        for i in range(n_in, 0, -1):
            cols.append(df.shift(-i))
            names += [('var%d(t+%d)' % (j + 1, i)) for j in range(n_vars)]
        agg = concat(cols, axis=1)
        agg.columns = names
        if dropnan:
            agg.dropna(inplace=True)
        agg = agg.fillna(0)
        return agg

    def build_model(self, units, train_X, loss='mse', optimizer='adam'):
        model = Sequential()
        model.add(
            LSTM(units,
                 input_shape=(train_X.shape[1], train_X.shape[2]),
                 return_sequences=True))
        model.add(LSTM(units))
        model.add(Dense(1))
        model.compile(loss=loss, optimizer=optimizer)
        return model

    def save_img_predict_test(self, inv_yhat, inv_y, symbol):
        pyplot.plot(inv_yhat, label='predict')
        pyplot.plot(inv_y, label='test')
        pyplot.legend()
        pyplot.savefig("img/chart_%s.png" % symbol)
        pyplot.close()

    def make_predict(self, model, test_X, n_features=1):
        yhat = model.predict(test_X)
        test_X = test_X.reshape((test_X.shape[0], self.n_hours * n_features))
        inv_yhat = self.invert_scaling(yhat, test_X, n_features)
        return inv_yhat

    def make_actual(self, model, test_X, test_y, n_features=1):
        test_X = test_X.reshape((test_X.shape[0], self.n_hours * n_features))
        test_y = test_y.reshape((len(test_y), 1))
        inv_y = self.invert_scaling(test_y, test_X, n_features)
        return inv_y

    def invert_scaling(self, test_y, test_X_reshape, n_features):
        inv_y = concatenate((test_y, test_X_reshape[:, -(n_features - 1):]),
                            axis=1)
        inv_y = self.scaler.inverse_transform(inv_y)
        inv_y = inv_y[:, 0]
        return inv_y

    def normalize_data(self, dataset, dropnan=True):
        values = dataset.values
        values = values.astype('float32')
        scaled = self.scaler.fit_transform(values)
        reframed = self.series_to_supervised(scaled, self.n_hours, 1, dropnan)
        values = reframed.values
        return values

    def evaluate_model(self, inv_y, inv_yhat):
        max_error = 0
        for i in range(0, len(inv_y)):
            err = abs(inv_y[i] - inv_yhat[i])
            if err > max_error:
                max_error = err
        rmse = sqrt(mean_squared_error(inv_y, inv_yhat))
        return max_error, rmse

    def split_train_test(self, values, n_time_predicts):
        n_train_hours = len(values) - n_time_predicts
        train = values[:n_train_hours, :]
        test = values[n_train_hours:, :]
        return train, test

    def split_into_inputs_and_outputs(self, values, n_features=10):
        n_time_predicts = len(values)
        n_obs = self.n_hours * n_features
        test_X, test_y = values[:, :n_obs], values[:, -n_features]
        test_X = test_X.reshape((n_time_predicts, self.n_hours, n_features))
        return test_X, test_y

    def fit_model(self, model, train_X, train_y, test_X, test_y, symbol,
                  config):
        epochs, batch_size, verbose, min_delta, patience, monitor = config
        model.fit(train_X,
                  train_y,
                  epochs=epochs,
                  batch_size=batch_size,
                  verbose=verbose,
                  shuffle=False,
                  validation_data=(test_X, test_y),
                  callbacks=[
                      EarlyStopping(monitor=monitor,
                                    min_delta=min_delta,
                                    patience=patience)
                  ])
        return model

    def save_model(self, model, symbol):
        model.save_weights("weights/weight_%s.h5" % symbol)
        model_json = model.to_json()
        with open("models/model_%s.json" % symbol, "w") as json_file:
            json_file.write(model_json)

    def save_to_db(self, inv_y, inv_yhat, id_coin):
        max_openTime = self.db.get_max_open_time(id_coin)
        max_error, RMSE = self.evaluate_model(inv_y, inv_yhat)
        openTime_last = max_openTime - self.n_time_predicts * 60 * 60 * 1000
        time_create = int(time.time())
        price_predict = array2string(inv_yhat, separator=',')
        price_test = array2string(inv_y, separator=',')
        self.db.insert_history_train(id_coin, time_create, price_test,
                                     price_predict, RMSE, max_error,
                                     openTime_last)

    def train_model(self):
        dataset = self.db.get_data_train_by_id(coin[self.ID_COIN])
        n_features = len(dataset.columns)
        values = self.normalize_data(dataset)
        train, test = self.split_train_test(values, self.n_time_predicts)
        train_X, train_y = self.split_into_inputs_and_outputs(
            train, n_features=n_features)
        test_X, test_y = self.split_into_inputs_and_outputs(
            test, n_features=n_features)
        model = self.build_model(units=self.units, train_X=train_X)
        model = self.fit_model(model, train_X, train_y, test_X, test_y,
                               coin[self.SYMBOL], self.config_train)
        self.save_model(model, coin[self.SYMBOL])
        inv_yhat = self.make_predict(model, test_X, n_features)
        inv_y = self.make_actual(model, test_X, test_y, n_features)
        inv_yhat = inv_yhat[:-1]
        inv_y = inv_y[1:]
        self.save_to_db(inv_y, inv_yhat, coin[self.ID_COIN])
        self.save_img_predict_test(inv_y, inv_yhat, coin[self.SYMBOL])