Skip to content
Permalink
Browse files
mqtt
  • Loading branch information
twiningo committed Mar 5, 2021
1 parent ac53c86 commit 773facadc789d071caaae288512e5fa1888b3f9b
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 25 deletions.
84 Mqtt.py
@@ -0,0 +1,84 @@
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn import linear_model
import statsmodels.api as sm
import statsmodels.formula.api as smf

df = pd.read_csv(r'C:\Users\ojt98\OneDrive\Desktop\Python\weather_data_24hr.csv')
#df=pd.get_dummies(df, prefix='weather', columns=['weather'])

X=df[["windspeedKPH", "humidity", "avgtempC"]]
y=df[["accidents"]]

lm = linear_model.LinearRegression()
model = lm.fit(X,y)
predictions = lm.predict(X)
#print(predictions[0:5])

print(lm.score(X,y)) ##R^2
arr=lm.coef_
print(arr)

for word in arr:
one=(word[0])

for word in arr:
two=(word[1])

for word in arr:
three=(word[2])



##pull live data into this below...

#accidents=one(windspeedKPH)+ two(humidity)+ three(avgtempC)


# python_sub.py
#mosquitto_pub -h mqtt.dioty.co -p 8883 -u <your user-id> -P <your password> -t <topic> -m <message> -r --cafile ./dioty_ca.crt

import paho.mqtt.client as mqtt
import json, os
from datetime import date, datetime
import time

def on_connect(client, userdata, flags, rc):
if rc==0:
client.connected_flag = True
print("connected OK")
client.subscribe("iot/twiningo")
else:
print("Bad connection Returned code=",rc)

def on_message( client, userdata, msg ):
print('in on_message')
""" callback function for messages received """
topic = msg.topic
data = msg.payload.decode("utf8")
print(topic)
print(data)

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set("iot/twiningo", "JLjsTIe2Ws")
client.tls_set("mqtt.crt")
client.connect("mqtt.coventry.ac.uk", 8883)
client.loop_forever()













Empty file.
@@ -13,35 +13,14 @@ df = pd.read_csv(r'C:\Users\ojt98\OneDrive\Desktop\Python\weather_data_24hr.csv'
X=df[["windspeedKPH", "humidity", "avgtempC"]]
y=df[["accidents"]]


#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 42)

#regressor = LinearRegression()

#regressor.fit(X_train, y_train)

#y_pred =regressor.predict(X_test)

#X = sm.add_constant(X)
##Train-test split
#train_X, test_X, train_y, test_y = train_test_split(X, y, train_size = 0.8, random_state = 42)
##Linear regression model
#model = sm.OLS(train_y, train_X)
#model = model.fit()
#print(model.summary2())





lm = linear_model.LinearRegression()
model = lm.fit(X,y)
predictions = lm.predict(X)
print(predictions[0:5])
#print(predictions[0:5])

print(lm.score(X,y))
print(lm.score(X,y)) ##R^2
arr=lm.coef_

print(arr)

for word in arr:
one=(word[0])
@@ -52,11 +31,15 @@ for word in arr:
for word in arr:
three=(word[2])



##pull live data into this below...

#accidents=one(windspeedKPH)+ two(humidity)+ three(avgtempC)


# python_sub.py
#mosquitto_pub -h mqtt.dioty.co -p 8883 -u <your user-id> -P <your password> -t <topic> -m <message> -r --cafile ./dioty_ca.crt

import paho.mqtt.client as mqtt
import json, os
@@ -82,7 +65,34 @@ def on_message( client, userdata, msg ):
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set("iot", "JLjsTIe2Ws")
client.username_pw_set("iot/twiningo", "JLjsTIe2Ws")
client.tls_set("mqtt.crt")
client.connect("mqtt.coventry.ac.uk", 8883)
client.loop_forever()




#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 42)

#regressor = LinearRegression()

#regressor.fit(X_train, y_train)

#y_pred =regressor.predict(X_test)

#X = sm.add_constant(X)
##Train-test split
#train_X, test_X, train_y, test_y = train_test_split(X, y, train_size = 0.8, random_state = 42)
##Linear regression model
#model = sm.OLS(train_y, train_X)
#model = model.fit()
#print(model.summary2())








0 comments on commit 773faca

Please sign in to comment.