Hello,
I do not open the serial monitor on Raspberry PI. I open serial Monitor on the Arduino to make sure that it does send out the correct data before disabling the serial monitor and running the Raspberry program. There is no drawings or schematics. The Mega2560 is connected to the Raspberry PI using the USB port.
Please note that if I remove this line in the Raspberry program
ser.write(b"temps\n")
Then the Mega2560 send out the data and Raspberry PI sees it.
Here are the programs.
Here is the Arduino code;
const int LED = 8;
boolean ledON = 1;
// Initialize All the buttons to on when pushed for the first time.
boolean cabin = 1; // Cabin Light button
boolean galley = 1; // Galley light button
int tempC;
int tempF;
int sensorPin = A0; // Vout from the Analog input 0 on Ardunio
int sensorPin1 = A1; // Vout from the Analog input 0 on Ardunio
String fwdheader = "fwdtmp,"; // The string sent out to Raspberry PI so it knows next is the data (FWD temp)
String fwdtmp; // final message sent out for AFT temp including its data
String aftheader = "afttmp,"; // The string sent out to Raspberry PI so it knows next is the data (AFT temp)
String afttmp; // final message sent out for AFT temp including its data
int FWD_Temp, AFT_Temp;
String command; // Used to receive commands from Raspberry PI
void setup()
{
// Serial port is used to communicate with the Raspberry PI
Serial.begin(9600);
pinMode(22, OUTPUT);
pinMode(23, OUTPUT);
}
void loop()
{
// Read Temperature using Ardunio Analog Input 0 and 1
tempC = ((5.0 * analogRead(sensorPin) * 100.0) / 1024) + 2;
tempF = tempC * 9 / 5 + 32;
FWD_Temp = tempF;
// Send fwd temp to Raspberry PI. Lets first send the word fwdtmp word to Raspberry PI so it know what data is coming in
fwdtmp = fwdheader + tempF;
tempC = ((5.0 * analogRead(sensorPin1) * 100.0) / 1024) + 2;
tempF = tempC * 9 / 5 + 32;
AFT_Temp = tempF ;
afttmp = aftheader + tempF;
// Receive Data Commands from Raspberry Pi and execute them
if (Serial.available())
{
command = Serial.readStringUntil('\n');
command.trim();
if (command.equals("temps"))
{
Serial.print(fwdtmp);
Serial.print("\n");
delay(50);
Serial.print(afttmp);
Serial.print("\n");
}
if (command.equals("galley"))
{
digitalWrite(22, galley);
galley = !galley;
}
if (command.equals("cabin"))
{
digitalWrite(23, cabin);
cabin = !cabin;
}
delay(100);
}
}
Here is the Raspberry Code;
from tkinter import *
import tkinter as tk
from tkinter import colorchooser
from tkinter.colorchooser import askcolor
import serial
import time
LARGE_FONT= ("Verdana", 22)
# Establish Serial Communication with Raspberry PI
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyACM0',9600, timeout=1)
ser.flush()
class CMS(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.overrideredirect(True)
# Create Windows, Frames
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, minsize = 830)
container.grid_columnconfigure(0, minsize = 1150)
self.frames = {}
for F in (StartPage, PageOne, PageTwo, Page3, Page4, Page5, Page6):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
# need to defined the pointers to the images for the buttons to show their image as global variables
global home_btn
global cooling_btn
#images of the buttons
home_btn = PhotoImage(file='/home/pi/Documents/Bobby/Pictures/Home_On.png')
cooling_btn = PhotoImage(file='/home/pi/Documents/Bobby/Pictures/Cooling_Off.png')
home = tk.Button(self, image=home_btn,
command=lambda: controller.show_frame(StartPage))
home.place(x=10, y=745)
cooling = tk.Button(self, image=cooling_btn,
command=lambda: controller.show_frame(Page3))
cooling.place(x=510, y=745)
class Page3(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Cooling", font = "Times 60 bold", fg="AntiqueWhite1", bg="black")
label.place(x=15, y=40)
self.configure(bg='black') # background color
# Display The Temperature value coming from Ardunio and display it
# Function to communicate with Ardunio and get the FW and AFT temperature
def Temps():
# Send out a request to Ardunio to send out the Temperatures
ser.write(b"temps\n")
tmp_str = ser.readline().decode()
# Remove the header 'fwdtmp' or 'afttmp' seperate it from the real data
tmps = tmp_str.split(',')
print("Read input back: " + tmp_str)
#print("tmps = " + tmps[0]) # This is the header
#print("tmps1 = " + tmps[1]) # This is the data
#Remove carriage reurn from the end of tmp[2] data
#tmps2 = tmps[1].strip()
# Now display the correct data for the selected header
if tmps[0] == "fwdtmp":
FwdTemp = tk.Label(self, text=tmps2, font = "Times 25 bold", fg="black", bg="AntiqueWhite1")
FwdTemp.place(x=265, y=340)
if tmps[0] == "afttmp":
AftTemp = tk.Label(self, text=tmps2, font = "Times 25 bold", fg="black", bg="AntiqueWhite1")
AftTemp.place(x=790, y=340)
# .after command queues tkinter to run the FWD_Temp function every second (1000 msec)
self.after(1000, Temps)
Temps()
# need to defined the pointers to the images for the buttons to show their image as global variables
# need to rename them because usingthe same names wont show the button images or functions
global home_btn3
global cooling_btn3
#images of the buttons
home_btn3 = PhotoImage(file='/home/pi/Documents/Bobby/Pictures/Home_Off.png')
cooling_btn3 = PhotoImage(file='/home/pi/Documents/Bobby/Pictures/Cooling_On.png')
home = tk.Button(self, image=home_btn3,
command=lambda: controller.show_frame(StartPage))
home.place(x=10, y=745)
cooling = tk.Button(self, image=cooling_btn3,
command=lambda: controller.show_frame(Page3))
cooling.place(x=510, y=745)
app = CMS()
app.mainloop()