Hi Guys
i am doing this tutorial (https://anvil.works/blog/raspberry-pi-4-web-ide) the live data logger and viewer.
I am not sure what is going on with the code or the pi or sensehat. the uplink works but when I run it and all works but no sensor data is being recorded in the tables. It is showing a live data but nothing in the table section
https://anvil.works/build#clone:OKTI3SL2EEVY6GP3=YX3QODMJW3NIXXML4A22UO45
Also @meredydd or @meredydd1
Could you please explain this part of the code to me:
app_tables.light_levels.add_row(
When I run my code - I get an error. I will update this post with more info on the error.
When I change that to
app_tables.atmospherics.add_row(
That seems to work. Any thoughts?
import anvil.server
from anvil.tables import app_tables
from sense_hat import SenseHat
from datetime import datetime
import time
camera = PiCamera()
sense = SenseHat()
anvil.server.connect("LK") #hidden for privacy reason. uplink works
while True:
app_tables.atmospherics.add_row( #i changed the light_levels to the name of the table. Is that right?
when=datetime.now(),
pressure=sense.get_pressure(),
humidity=sense.get_humidity(),
temperature=sense.get_temperature()
)
time.sleep(1)
Hi,
Thanks for your post. I had the same problem and had just figured out that I needed to change the table name to atmospherics before coming across this post.
I notice PiCamera() in this code, and am hoping you might help with a quick example of how to show a picture from the PiCamera in an Anvil app. (Sorry, I am a super novice high school teacher)
Hi William, welcome to the Anvil community. So this was actually part of my assignment for IT (I’m in Year 12 right now). And what the code actually does is that it stores photos locally on the Raspberry Pi. My original plan was to actual store/automatically upload the photos taken to a Dropbox, but that needed API’s etc. and I kind of gave up on that.
But I think this tutorial might be superhelpful in achieving what you. I reckon if you combine the code for the data logger and from the tutorial below you should be able to get it up and running.
If you need any help please ask the community, we’re here to help you and I am very excited to see how you go (I believe you can get up and running easily) I don’t have my Raspberry Pi on me right now, but when I head back to school in a weeks time, I will certainly try on my end to see if I can get it working,.
The tutorial can be found at:
# source code linking to Anvil was sourced from: https://anvil.works/blog/raspberry-pi-4-web-ide #
import anvil.server #import libraries i.e. the Anvil library. Mianly connects to Anvil server
from anvil.tables import app_tables #links to Anvil table
from sense_hat import SenseHat #sensehat library
from datetime import datetime #date time library
import time #time library
from picamera import PiCamera #pi camera library
camera = PiCamera() #declaring pi camera as camera
sense = SenseHat() #declaring sensehat as sense
red = (255,0,0) #red color for LED
green = (0,255,0) #green color for Led scren
id = 1 #using the ID part to name files etc. was suggested and developed by Ms Maher
camera.rotation = 180 #just flipping the camera for taking pics.
anvil.server.connect("") #unique server code for data to be sent to
def cam(id): #this is the function for the camera
camera.capture('img'+str(id)+'.jpg') #takes picture and the file name is image and as each image is taken it will be img plus a number
print('Captured image') #when image captured it will say 'captured image'
time.sleep(20) #sleep for 10 sec means take pics every 10 seconds.
def nope(): # this is a function for the LED screen. It will display real time data on the LED screen for users
nope = sense.get_humidity() #just saying when we refer to nope it means get humidity
if nope > 30: # this aspect of code is saying if the humidity is greater than 30
sense.clear(red) # the LED screen will go red
elif nope < 29 and nope > 10: # this aspect of code is saying if the humidity is greater than 10 and less than 29
sense.clear(green) # the LED screen will go green
time.sleep(0.5) #how long the color will display for
sense.show_message("Humidity: "+ str(round(nope,1))) # show message on LED
def pres(): # this is a function for the LED screen. It will display real time data on the LED screen for users
pres = sense.get_pressure() # this is to simpplify things down and say pres means to get the pressure from the sensor.
if pres > 800: #if pressure is greater than 800 the screen will be red
sense.clear(red)
elif pres < 799 and pres > 400: #if the pressure is less than 799 and greater than 400 the LED screen will be green
sense.clear(green)
time.sleep(0.5)#how long the color will display for
sense.show_message("{Pressure: "+ str(round(pres,1))) # this will do a roll of the text that shows the real time value of the atmospheric pressure of the room
def temp(): # this is a function for the LED screen. It will display real time data on the LED screen for users
temp = sense.get_temperature() # this is to simpplify things down and say temp means to get the temperature from the sensor.
if temp > 30: #if pressure is greater than 30 the screen will be red
sense.clear(red)
elif temp < 29 and temp > 10: # this aspect of code is saying if the humidity is greater than 10 and less than 29
sense.clear(green) #the LED will go green
time.sleep(0.5)#how long the color will display for
sense.show_message("Temperature: "+ str(round(temp,1))) # show message on LED
while True: #loop untill program is killed
app_tables.atmospherics.add_row( #this is the table that the data will be added to
when=datetime.now(), #get date and time and log it into the table
pressure=sense.get_pressure(), #get pressure and log it into the table
humidity=sense.get_humidity(), #get humidity and log it into the table
temperature=sense.get_temperature() #get room temp. and log it into the table
)
time.sleep(10) #sleep
cam(id)#run camera function
id = id + 1 #run id part with naming file
for event in sense.stick.get_events(): # this will be the code for when we want to display the live temperature readings. this utilises the joystick on the SenseHat
if event.action == "pressed": #if the joystick is pressed
if event.direction =="up": # in the up direction, it will run the function - nope() which will display the humidity
nope()
elif event.direction == "down": #else if the joystick is pushed down it will run the function - pres() which will show the pressure in realtime
pres()
elif event.direction == "left": #else if the joystick is pushed left it will run the function - temp() which will show the temperature currently in realtime
temp()
So this was my final code that I developed for my assignment. What would happens was that when the program was run, it would send data across to the servers. Students can also press on the joystick buttons to see live sensor data on the SenseHat and the code would take pictures every 20 seconds and save them locally to the Pi.
Thank you very much for your reply. I will read through your code and try to understand it. Thanks for sending a link to the article. It’s actually the same article that made me aware of anvil. I was not able to follow it all of the way through. I will study up a little and go back to see if I can understand it. After further study, if I still can’t get it, I will try asking specific questions in the forum about it.
Thanks again,
Bill
All good mate. I’ve did my best to comment out my code so that people can understand much of it. Sorry if it’s a bit to complicated or codey/long. I can tell you the final project works. P.S Don’t worry about the function names, i’m just a little lazy to come up with original function names