Page 1 of 1

(Solved) LCD 16x2 Display IP Date Time and CPU Temp

Posted: Fri Mar 23, 2018 11:04 pm
by wildcat2083
Here is my code, essentially what I am trying to achieve is I have three basic functions
Get ip address send it to a lcd display
Same with cpu temp and displaying date and time

The functions are written with a loop that is supposed to run for about a minute then what it's supposed to do is go to the next function

I want the LCD to display the temp for a minute then display the date and time again for a minute then the ip address for a minute then I'd like it to loop back to the start, the way I have it now each function called runs for a minute then it stops the script

Refer to the while true part at the bottom
curtime ()
tempcpu()
getip ()

Each one works on their own if I comment out 2 of the 3 definitions above but not as I intended as described, any help would be greatly appreciated as I'm new to python

Code: Select all

#!/usr/bin/env python
#import RPi.GPIO as GPIO
import lcd_driver
import socket
import struct
import fcntl
import time
import os

from time import sleep

disp = lcd_driver.lcd()
t_time = time.time() + 60 * 1

def tempcpu():
   while time.time() < t_time:
      def measure_temp():
         temp = os.popen("vcgencmd measure_temp").readline(
         return (temp.replace("temp=","").replace("'C\n",""
)

      temp1 = int(float(measure_temp()))
      temp2 = int(float(9.0/5.0*temp1+32))
      disp.lcdstring("Temp: " + str(temp1) + " C", 1, 0)
      disp.lcdstring("Conv: " + str(temp2) + " F", 2, 0)

      if time.time() => t_time:
         break

def curtime():
   while time.time() < t_time:
      disp.lcdstring("Time: %s" %time.strftime("%H:%M:%S"),
,0)
      disp.lcdstring("Date: %s" %time.strftime("%m:%d:%Y"),
,0)
      sleep(1)

def getip():
   while time.time() < t_time:
      def get_ip_address(ifname):
         s = socket.socket(socket.AF_INET, socket.SOCK_DGRA
)
         return socket.inet_ntoa(fcntl.ioctl(
            s.fileno(),
            0x8915,
            struct.pack('256s', ifname[:15]),
         ) [20:24])

      disp.lcdstring("IP Address:",1)
      disp.lcdstring(get_ip_address('wlan0'),2)
      sleep(1)


try:
   while True:
      tempcpu()
      curtime()
      getip()

except KeyboardInterrupt:
   disp.clear()
   sleep(1)
   disp.backlight(0)

Re: Help with breaking out of while loops and continue script

Posted: Sat Mar 24, 2018 3:10 pm
by pcmanbob
Hi.

So this is an example of how to do it.

each loop runs for 10 seconds and prints 10 lines telling you which loop is running, when it gets to the end of each loop it starts the next and just goes on repeating forever.

Code: Select all

#!/usr/bin/env python
#import RPi.GPIO as GPIO

import time

def tempcpu():
    t_time = time.time() + 10 * 1
    while time.time() < t_time:
        print "temp cpu loop running"
        time.sleep (1)

def curtime():
    t_time = time.time() + 10 * 1
    while time.time() < t_time:
        print "time cpu loop running"
        time.sleep(1)

def getip(): 
    t_time = time.time() + 10 * 1
    while time.time() < t_time:
        print "IP loop running"
        time.sleep (1)       
        
try:
   while True:
      tempcpu()
      print ""
      curtime()
      print""
      getip()
      print ""

except KeyboardInterrupt:
   
   time.sleep(1)
   
you see in the example that with each call of the function you need to set a new end time ( t_time), in your code you set the end time just once at program start so after the time was up your functions would never run. no need for any break commands as once the time is up the loop exits and the function executes the inbuilt return command returning you to the main loop and the next function call.

Re: Help with breaking out of while loops and continue script

Posted: Sat Mar 24, 2018 5:59 pm
by wildcat2083
Thanks for the example, that is what I needed

Solved!!! :D

Posted: Sat Mar 24, 2018 8:18 pm
by wildcat2083
I ended up writing the code out like this
I am sharing for anyone interested

Code: Select all

#!/usr/bin/env python


#Written By Cyrus Wolf
#Can be modified and is under the Open Source Standard Rules
#Please pay attention to indents and spacing as it matters with python


#import RPi.GPIO as GPIO #Imports RPi for GPIO Use (Comment/Uncomment to use)
import lcd_driver # Imports Custom LCD Display Driver
import socket # Imports socket function
import struct # Imports structure function
import fcntl # Imports networking function
import time # Imports time function
import os # Imports Operating System function
import re # Imports Reg Ex function
from time import sleep # Imports sleep function from time module

disp = lcd_driver.lcd() # Initializes LCD Display

# Sends the Temp of the cpu to the lcd display (for 10 seconds)
def tempcpu(): #Defines "tempcpu"
   for _ in range(10): # Sets up timer

        cputemp = os.popen("vcgencmd measure_temp").readline() # Gets temp reading (shows as "temp=xx.x'C")
        celsius = re.sub("[^0123456789\.]", "", cputemp) # Removes everything but numbers and "."
        fahrenheit = int(9.0/5.0*int(float(celsius)+32)) # Math Function Fahrenheit (celsius * 9 / 5 + 32) as interger

        disp.lcdstring("Cpu : {} C".format(celsius), 1) # Prints Temp as Celsius to the LCD Display line 1
        disp.lcdstring("Temp: {}  F".format(fahrenheit), 2) # Prints Temp as Fahrenheit to the LCD Display line 2

        sleep(1) # Sleeps for one second before restarting loop

# Sends the Time and Date to the lcd display (for 10 seconds)
def curtime(): # Defines "curtime"
   for _ in range(10): # Sets up timer

        disp.lcdstring("Time: {}".format(time.strftime("%H:%M:%S")), 1) # Prints time to the LCD Display line 1
        disp.lcdstring("Date: {}".format(time.strftime("%m:%d:%Y")), 2) # Prints date to the LCD Display line 2

        sleep(1) # Sleeps for one second before restarting loop

# Gets the IP Address
def getaddr(ifname): # Defines "getaddr" as well as ifname arguement later

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,
        struct.pack('256s', ifname[:15])
    )[20:24]) # Not sure how this block works just yet, but it does dig up the ip address

# Sends the IP Address to the lcd display (for 10 seconds) as wlan0, eth0 can also be used
def getip(): # Defines "getip"

    ip = getaddr('wlan0') # Grabs the address from "wlan0" and assigns it to "ip"
    for _ in range(10): # Sets up timer

        disp.lcdstring("IP Address:", 1) # Prints string to LCD Display line 1
        disp.lcdstring(ip, 2) # Prints "ip" to LCD Display line 2

        sleep(1) # Sleeps for one second before restarting loop

# runs a forever loop calling the defs above
try: # Gives way to exception later

    while True: # Forever loop

        tempcpu() # Calls "tempcpu"
        disp.clear() # Clears the LCD Display

        curtime() # Calls "curtime"
        disp.clear() # Clears the LCD Display

        getip() # Calls "getip"
        disp.clear() # Again Clears the LCD Display

# Allows for clean exit
except KeyboardInterrupt: # If interrupted by the keyboard ("Control" + "C")

   disp.clear() #clear the lcd display
   sleep(1) #sleeps 1 second
   disp.backlight(0) #Turn Off Backlight

# Exits the python interperter
exit()


Re: (Solved) LCD 16x2 Display IP Date Time and CPU Temp

Posted: Sun Oct 14, 2018 10:08 pm
by aislanmds
Thank you for sharing this! where I find the 16x2 lcd connection scheme for this program?

Re: (Solved) LCD 16x2 Display IP Date Time and CPU Temp

Posted: Wed Oct 17, 2018 10:30 pm
by wildcat2083
aislanmds wrote:
Sun Oct 14, 2018 10:08 pm
Thank you for sharing this! where I find the 16x2 lcd connection scheme for this program?
The needed LCD driver was one i modified, it combines the I2C module and the code to get the lcd itself working, if you dont have a lcd with the i2c interface then the driver won't work, i origionally wrote this for the raspberry pi you need smbus from their repository and you can install the driver with python, ill link it so you can download the driver, as a bonus in the examples folder i have some code that i used to build this, including a newer button controlled lcd function scrolling through the ip date and time, and more

here is the link

https://drive.google.com/file/d/1AUpEQt ... p=drivesdk