Question

I have written code to display a temp reading on a RPI to an LCD screen...

I have written code to display a temp reading on a RPI to an LCD screen in python. I am required to have the data be sent to a JSON file and am having trouble modifying this to happen. I know I import JSON but then do I create a data set or can I pull it directly from the data that is being collected. Below is my code.

from grovepi import *
from grove_rgb_lcd import *
from time import sleep
from math import isnan

dht_sensor_port = 7 # connect the DHt sensor to port 7
dht_sensor_type = 0 # use 0 for the blue-colored sensor and 1 for the white-colored sensor

# set green as backlight color
# we need to do it just once
# setting the backlight color once reduces the amount of data transfer over the I2C line
setRGB(0,255,0)

while True:
try:
# get the temperature and Humidity from the DHT sensor
[ temp,hum ] = dht(dht_sensor_port,dht_sensor_type)

# change temp reading from celsius to fahrenheit
temp = ((temp/5.0)*9)+32

# round the temp to 2 decimal places so it will read on the LCD screen
new_temp = round(temp, 2)

print("temp =", temp , "F\thumidity =", hum,"%")

# check if we have nans
# if so, then raise a type error exception
if isnan(new_temp) is True or isnan(hum) is True:
raise TypeError('nan error')

t = str(new_temp)
h = str(hum)

# instead of inserting a bunch of whitespace, we can just insert a \n
# we're ensuring that if we get some strange strings on one line, the 2nd one won't be affected
setText_norefresh("Temp:" + t + "F\n" + "Humidity :" + h + "%")

except (IOError, TypeError) as e:
print(str(e))
# and since we got a type error
# then reset the LCD's text
setText("")

except KeyboardInterrupt as e:
print(str(e))
# since we're exiting the program
# it's better to leave the LCD with a blank text
setText("")
break

#

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I guess from your code that you want to dump the temp and humidity to your json file.

first of all import json at top of your code.

import json

Now for dumping into json file you need to open a jason file in write mode:

with open('temp_hum.json','w') as my_json:

here you need to dump the temp and humidity data using:

json.dump(temp_hum_details,my_json)

So before this you need to create the dataset fro temp_hum_details as:

temp_hum_details= {

'temp' : temp,

'humidity' : hum

}

So for each iteration or reading from the RPI, if you do this, the data will be dumped into the json file. Hope it helps.

The answers have been provided in accordance with the questions asked and how I have percieved the question. I have tried my best to answer upto your satisfaction. If you are satisfied with the answer, kindly like it.
Also, if you face any kind of problem regarding the solution, please feel free to comment and I shall answer to my best knowledge. Please do not dislike this answer without clarifying the doubts.
Thank you.

Add a comment
Know the answer?
Add Answer to:
I have written code to display a temp reading on a RPI to an LCD screen...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python3, write a program, specific information is in the graphs, I got 7 marks out of...

    Python3, write a program, specific information is in the graphs, I got 7 marks out of 10, you may change my codes to let it gives correct output,restrications are also on the graph, for loop, in key word, enumerate,zip,slices, with key word can't be used. All the information is on the graph, is_shakespeare_play(line) may be a function given that can check whether is Shakespeare play Question 2- 10 marks Write the function keep titles short (filename, max charactars) which takes...

  • I have a C++ code that lets me enter, display and delete a student record. I...

    I have a C++ code that lets me enter, display and delete a student record. I need to implement a function that prints the average grade score of the students I input. Below is my code and a picture of how my code looks right now. #include<iostream> #include<stdlib.h> using namespace std; //Node Declaration struct node {    string name;    string id;    int score;    node *next;   }; //List class class list {        private:        //head...

  • I am having trouble figuring out what is wrong with my code. This is the error...

    I am having trouble figuring out what is wrong with my code. This is the error I get for the last for loop. TypeError: list indices must be integers or slices, not list. This is the code: for line in fhr: ls = line.split(',') customer_list.append([ls[0], ls[1], ls[2], ls[3], ls[4], ls[5], ls[6], int(ls[7]), ls[8], ls[9], ls[10].strip('\n')]) from operator import itemgetter customer_list.sort(key=itemgetter(7), reverse=True) print(customer_list) writepath = './loan-data-output-v1.csv' fwh = open(writepath, 'w', encoding='utf-8') fwh.write('Name' +','+ 'State' +','+'Age' +','+'Annual Income'+','+   'Loan Type' +','+' Loan...

  • For my computer class I have to create a program that deals with making and searching...

    For my computer class I have to create a program that deals with making and searching tweets. I am done with the program but keep getting the error below when I try the first option in the shell. Can someone please explain this error and show me how to fix it in my code below? Thanks! twitter.py - C:/Users/Owner/AppData/Local/Programs/Python/Python38/twitter.py (3.8.3) File Edit Format Run Options Window Help import pickle as pk from Tweet import Tweet import os def show menu():...

  • Please answer this question using python programming only and provide a screen short for this code....

    Please answer this question using python programming only and provide a screen short for this code. Homework 3 - Multiples not less than a number Question 1 (out of 3) This homework is a SOLO assignment. While generally I encourage you to help one another to learn the material, you may only submit code that you have composed and that you understand. Use this template to write a Python 3 program that calculates the nearest multiple of a number that...

  • I cant get the code to work. Any help would be appreciated. Instruction.java package simmac; public...

    I cant get the code to work. Any help would be appreciated. Instruction.java package simmac; public class Instruction { public static final int DW = 0x0000; public static final int ADD = 0x0001; public static final int SUB = 0x0002; public static final int LDA = 0x0003; public static final int LDI = 0x0004; public static final int STR = 0x0005; public static final int BRH = 0x0006; public static final int CBR = 0x0007; public static final int HLT...

  • For the following task, I have written code in C and need help in determining the...

    For the following task, I have written code in C and need help in determining the cause(s) of a segmentation fault which occurs when run. **It prints the message on line 47 "printf("Reading the input file and writing data to output file simultaneously..."); then results in a segmentation fault (core dumped) I am using mobaXterm v11.0 (GNU nano 2.0.9) CSV (comma-separated values) is a popular file format to store tabular kind of data. Each record is in a separate line...

  • I will like to compare automobile producers. This assignment suppose to read data like div tags...

    I will like to compare automobile producers. This assignment suppose to read data like div tags a etc. And count occurrence of them. Reading from a URL while working with an API (using Mediawiki API as an example) Input: Will be obtained from a URL using Mediawiki API -- starter code below Output: Up to you... sort of. What to submit: Upload a report (.pdf preferred) containing screenshots of code, output, and discussion/conclusions to d2l dropbox. Please also submit your...

  • Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import...

    Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Project3 extends JFrame implements ActionListener { private static int winxpos = 0, winypos = 0; // place window here private JButton exitButton, hitButton, stayButton, dealButton, newGameButton; private CardList theDeck = null; private JPanel northPanel; private MyPanel centerPanel; private static JFrame myFrame = null; private CardList playerHand = new CardList(0); private CardList dealerHand = new CardList(0);...

  • Hey I have a task which consists of two part. Part A asks for writing a...

    Hey I have a task which consists of two part. Part A asks for writing a program of WORD & LINE CONCORDANCE APPLICATION in python which I have completed it. Now the second part has given 5 dictionary implementation codes namely as: (ChainingDict, OpenAddrHashDict with linear probing, OpenAddrHashDict with quadratic probing, and 2 tree-based dictionaries from lab 12 (BST-based dictionary implementation) and asks for my above program WORD & LINE CONCORDANCE APPLICATION  to use these implemented code and show the time...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT