• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Global Variable Micropython

Q&A Forum › Category: ESP32 › Global Variable Micropython
0 Vote Up Vote Down
self.raymond asked 5 years ago

I have been trying to setup Global Variable so they can be used in other function.  I only need the temp_f variable from this one Function.  I get an error (temp has not been define)

from ili934xnew import ILI9341, color565
from machine import Pin, SPI
import tt24
from time import sleep
import dht
import _thread as th
spi = SPI(1, baudrate=20000000, miso=Pin(19), mosi=Pin(23), sck=Pin(18))
display = ILI9341(spi, cs=Pin(2), dc=Pin(27), rst=Pin(33), w=320, h=240, r=1)
display.set_font(tt24)
sensor = dht.DHT11(Pin(5))
globals()[temp_f] = round((temp * 9 / 5) + 32, 1)
globals()[temp] = sensor.temperature()
def loop_Temp_Humid_sensor():
while True:
sleep(4)
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
# temp_f = temp * (9/5) + 32.0
temp_f = round((temp * 9 / 5) + 32, 1)
# print('Temperature: %3.1f C' %temp)
# print('Temperature: %3.1f F' %temp_f)
# print('Humidity: %3.1f %%' %hum)
# display.erase()
# display.set_font(tt24)
# display.set_pos(10, 40)
# display.print(str(temp_f))
# print(temp_f)
# print(hum)

# loop_Temp_Humid_sensor()
# th.start_new_thread(loop_Temp_Humid_sensor, ())
def loop_Text_ili9341():
while True:
display.erase()
display.set_font(tt24)
display.set_pos(60, 0)
display.print("Pool Solar Heater!!")
display.set_pos(10, 20)
display.print("Temperature")
display.set_pos(180, 20)
display.print("Humidity")
display.set_pos(20, 40)
display.print(str(temp_f))
display.set_pos(0, 80)
display.print("Pool Temp!")
display.set_pos(180, 80)
display.print("Solar Temp!")
display.set_pos(60, 140)
display.print("Solar Heater Run")
display.set_pos(60, 180)
display.print("Date and Time")
sleep(2)

loop_Text_ili9341()

# th.start_new_thread(loop_Text_ili9341, ())

The problem also stop running at

display.print(str(temp_f))

When I take the str out the lcd runs fine. Hope you can help!!

1 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi Ray.
The temp not defined error is because you’re using a variable called temp on the following line that was not declared before:

globals()[temp_f] = round((temp * 9 / 5) + 32, 1)

So, you need to swap the order of your lines like this:

globals()[temp] = sensor.temperature()
globals()[temp_f] = round((temp * 9 / 5) + 32, 1)

I hope this helps.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.