ひかりん班
https://hackmd.io/t_ZHPH96RpeQwtUL5LiN_Q
# 村瀬ソース
上に行くほど新しいソースです
#### ※注 ラズパイのusbポートは繋ぐ順番で名前が変わるので注意
## firestoreに移行したプログラム
```
from time import sleep
import serial
import firebase_admin
from firebase_admin import firestore
from firebase_admin import credentials
cred = credentials.Certificate("/home/pi/Desktop/my-first-project-7e725-firebase-adminsdk-cpqgy-61a391587a.json")
firebase_admin.initialize_app(cred)
db = firestore.client()
lsw_new="n"
p_flg=1
t_flg=1
l_flg=1
if(p_flg):
arduino_ph=serial.Serial('/dev/ttyUSB0',9600)
if(t_flg):
arduino_temp=serial.Serial('/dev/ttyUSB1',9600)
if(l_flg):
arduino_led=serial.Serial('/dev/ttyUSB2',9600)
aquarium_ref=db.collection(u'aquarium')
while 1:
#ph
if(p_flg):
ph_read=arduino_ph.readline()
ph_utf8=ph_read.decode()
ph_val=ph_utf8.strip()
if(ph_val!='0' and ph_val!='1'):
doc_ref = db.collection(u'aquarium').document(u'sensor_val')
doc_ref.update({u'ph': ph_val})
elif(ph_val=='0'):
send_ph=b'0'
arduino_led.write(send_ph)
elif(ph_val=='1'):
send_ph=b'1'
arduino_led.write(send_ph)
if(t_flg):
temp_read=arduino_temp.readline()
temp_utf8=temp_read.decode()
temp_val=temp_utf8.strip()
if(temp_val!='a' and temp_val!='b'):
doc_ref = db.collection(u'aquarium').document(u'sensor_val')
doc_ref.update({u'temp': temp_val})
elif(temp_val=='a'):
send_temp=b'a'
arduino_led.write(send_temp)
elif(temp_utf8=='b'):
send_temp=b'b'
arduino_led.write(send_temp)
#led
if(l_flg):
lsw_old=lsw_new
doc_ref = db.collection(u'aquarium').document(u'led_sw')
doc = doc_ref.get()
lsw_new=doc.to_dict()['val']
if(lsw_old!=lsw_new):
if(lsw_new=='+'):
send_led=b'+'
arduino_led.write(send_led)
if(lsw_new=='-'):
send_led=b'-'
arduino_led.write(send_led)
sleep(1)
```
## スマホ(firebase)から+-を送るプログラム
```
from time import sleep
import serial
import pyrebase
#// Your web app's Firebase configuration
#// For Firebase JS SDK v7.20.0 and later, measurementId is optional
firebaseConfig = {
"apiKey": "AIzaSyA-DfJFfWMYFH46MYg-TOdA9YtHOB75qTg",
"authDomain": "my-first-project-7e725.firebaseapp.com",
"projectId": "my-first-project-7e725",
"storageBucket": "my-first-project-7e725.appspot.com",
"messagingSenderId": "666881609941",
"appId": "1:666881609941:web:b6bb69e6b22f71dbef525c",
"measurementId": "G-EVNRCJEJD1",
#下記村追加
"databaseURL": "https://my-first-project-7e725-default-rtdb.firebaseio.com/"
};
#// Initialize Firebase
#firebase.initializeApp(firebaseConfig);
#firebase.analytics();
firebase=pyrebase.initialize_app(firebaseConfig)
m_db=firebase.database()
lsw_new="n"
p_flg=1
t_flg=1
l_flg=1
if(p_flg):
arduino_ph=serial.Serial('/dev/ttyUSB0',9600)
if(t_flg):
arduino_temp=serial.Serial('/dev/ttyUSB1',9600)
#if(l_flg):
#arduino_led=serial.Serial('/dev/ttyUSB2',9600)
while(1):
#ph
if(p_flg):
ph_read=arduino_ph.readline()
ph_utf8=ph_read.decode()
ph_val=ph_utf8.strip()
if(ph_val!='0' and ph_val!='1'):
m_db.child("aquarium").update({"ph":ph_val})
elif(ph_val=='0'):
send_ph=b'0'
#arduino_ph.write(send_ph)
#arduino_led.write(send_ph)
elif(ph_val=='1'):
send_ph=b'1'
#arduino_ph.write(send_ph)
#arduino_led.write(send_ph)
#temp
if(t_flg):
temp_read=arduino_temp.readline()
temp_utf8=temp_read.decode()
temp_val=temp_utf8.strip()
if(temp_val!='a' and temp_val!='b'):
m_db.child("aquarium").update({"temp":temp_val})
elif(temp_val=='a'):
send_temp=b'a'
#arduino_temp.write(send_temp)
#arduino_led.write(send_ph)
elif(temp_val=='b'):
send_temp=b'b'
#arduino_temp.write(send_temp)
#arduino_led.write(send_ph)
#led
if(l_flg):
lsw_old=lsw_new
lsw_new=m_db.child("input/led_sw").get().val()
if(lsw_old!=lsw_new):
if(lsw_new=='+'):
send_ph=b'+'
arduino_ph.write(send_ph)
#arduino_led.write(send_ph)
if(lsw_new=='-'):
send_ph=b'-'
arduino_ph.write(send_ph)
#arduino_led.write(send_ph)
```
## ラズパイからnano2台に温度とphの正常異常(0,1,a,b)を送るプログラム
```
from time import sleep
import serial
import pyrebase
arduino_ph=serial.Serial('/dev/ttyUSB0',9600)
arduino_temp=serial.Serial('/dev/ttyUSB1',9600)
#// Your web app's Firebase configuration
#// For Firebase JS SDK v7.20.0 and later, measurementId is optional
firebaseConfig = {
"apiKey": "AIzaSyA-DfJFfWMYFH46MYg-TOdA9YtHOB75qTg",
"authDomain": "my-first-project-7e725.firebaseapp.com",
"projectId": "my-first-project-7e725",
"storageBucket": "my-first-project-7e725.appspot.com",
"messagingSenderId": "666881609941",
"appId": "1:666881609941:web:b6bb69e6b22f71dbef525c",
"measurementId": "G-EVNRCJEJD1",
#以下村追記
"databaseURL": "https://my-first-project-7e725-default-rtdb.firebaseio.com/"
};
#// Initialize Firebase
#firebase.initializeApp(firebaseConfig);
#firebase.analytics();
firebase=pyrebase.initialize_app(firebaseConfig)
m_db=firebase.database()
while(1):
#ph
ph=arduino_ph.readline()
ph_utf8=ph.decode()
if(ph_utf8!="0\r\n" and ph_utf8!="1\r\n"):
m_db.child("aquarium").update({"ph":ph_utf8})
elif(ph_utf8=="0\r\n"):
send_ph=b'0'
arduino_ph.write(send_ph)
elif(ph_utf8=="1\r\n"):
send_ph=b'1'
arduino_ph.write(send_ph)
#temp
temp=arduino_temp.readline()
temp_utf8=temp.decode()
if(temp_utf8!="a\r\n" and temp_utf8!="b\r\n"):
m_db.child("aquarium").update({"temp":temp_utf8})
elif(temp_utf8=="a\r\n"):
send_temp=b'a'
arduino_temp.write(send_temp)
elif(temp_utf8=="b\r\n"):
send_temp=b'b'
arduino_temp.write(send_temp)
sleep(1)
```
## nano2台から温度とphの正常異常(0,1,a,b)を受け取るプログラム
```
from time import sleep
import serial
import pyrebase
arduino_ph=serial.Serial('/dev/ttyUSB0',9600)
arduino_temp=serial.Serial('/dev/ttyUSB1',9600)
#// Your web app's Firebase configuration
#// For Firebase JS SDK v7.20.0 and later, measurementId is optional
firebaseConfig = {
"apiKey": "AIzaSyA-DfJFfWMYFH46MYg-TOdA9YtHOB75qTg",
"authDomain": "my-first-project-7e725.firebaseapp.com",
"projectId": "my-first-project-7e725",
"storageBucket": "my-first-project-7e725.appspot.com",
"messagingSenderId": "666881609941",
"appId": "1:666881609941:web:b6bb69e6b22f71dbef525c",
"measurementId": "G-EVNRCJEJD1",
#以下村追記
"databaseURL": "https://my-first-project-7e725-default-rtdb.firebaseio.com/"
};
#// Initialize Firebase
#firebase.initializeApp(firebaseConfig);
#firebase.analytics();
firebase=pyrebase.initialize_app(firebaseConfig)
m_db=firebase.database()
while(1):
#ph
ph=arduino_ph.readline()
ph_utf8=ph.decode()
if(ph!=str.encode("0\r\n") and ph!=str.encode("1\r\n")):
m_db.child("aquarium").update({"ph":ph_utf8})
elif(ph==str.encode("0\r\n")):
print("0_ph")
#arduino_ph.write(str.encode("0"))
elif(ph==str.encode("1\r\n")):
print("1_ph")
#arduino_ph.write(str.encode("1"))
#temp
temp=arduino_temp.readline()
temp_utf8=temp.decode()
if(temp!=str.encode("a\r\n") and temp!=str.encode("b\r\n")):
m_db.child("aquarium").update({"temp":temp_utf8})
elif(temp==str.encode("a\r\n")):
print("a_temp")
arduino_temp.write(str.encode("a"))
elif(temp==str.encode("b\r\n")):
print("b_temp")
#rduino_temp.write(str.encode("b"))
sleep(3)
```
## nano2台からデータを受け取りfirebaseのデータベースを更新するプログラム(python)
```
from time import sleep
import serial
import pyrebase
arduino_ph=serial.Serial('/dev/ttyUSB0',9600)
arduino_temp=serial.Serial('/dev/ttyUSB1',9600)
#// Your web app's Firebase configuration
#// For Firebase JS SDK v7.20.0 and later, measurementId is optional
firebaseConfig = {
"apiKey": "AIzaSyA-DfJFfWMYFH46MYg-TOdA9YtHOB75qTg",
"authDomain": "my-first-project-7e725.firebaseapp.com",
"projectId": "my-first-project-7e725",
"storageBucket": "my-first-project-7e725.appspot.com",
"messagingSenderId": "666881609941",
"appId": "1:666881609941:web:b6bb69e6b22f71dbef525c",
"measurementId": "G-EVNRCJEJD1",
#以下村追記
"databaseURL": "https://my-first-project-7e725-default-rtdb.firebaseio.com/"
};
#// Initialize Firebase
#firebase.initializeApp(firebaseConfig);
#firebase.analytics();
firebase=pyrebase.initialize_app(firebaseConfig)
m_db=firebase.database()
while(1):
ph=arduino_ph.readline()
ph_utf8=ph.decode()
m_db.child("aquarium").update({"ph":ph_utf8})
temp=arduino_temp.readline()
temp_utf8=temp.decode()
m_db.child("aquarium").update({"temp":temp_utf8})
sleep(3)
```
## nano1台からデータを受け取りfirebaseのデータベースを更新するプログラム(python)
```
from time import sleep
import serial
import pyrebase
arduino=serial.Serial('/dev/ttyUSB0',9600)
#// Your web app's Firebase configuration
#// For Firebase JS SDK v7.20.0 and later, measurementId is optional
firebaseConfig = {
"apiKey": "AIzaSyA-DfJFfWMYFH46MYg-TOdA9YtHOB75qTg",
"authDomain": "my-first-project-7e725.firebaseapp.com",
"projectId": "my-first-project-7e725",
"storageBucket": "my-first-project-7e725.appspot.com",
"messagingSenderId": "666881609941",
"appId": "1:666881609941:web:b6bb69e6b22f71dbef525c",
"measurementId": "G-EVNRCJEJD1",
#以下村追記
"databaseURL": "https://my-first-project-7e725-default-rtdb.firebaseio.com/"
};
#// Initialize Firebase
#firebase.initializeApp(firebaseConfig);
#firebase.analytics();
firebase=pyrebase.initialize_app(firebaseConfig)
m_db=firebase.database()
while(1):
ph=arduino.readline()
ph_utf8=ph[0:4].decode()
m_db.child("aquarium").update({"ph":ph_utf8})
sleep(3)
```