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
#
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.
I have written code to display a temp reading on a RPI to an LCD screen...
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 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 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 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():...
VERY URGENT*** THANK YOU IN ADVANCE: THIS IS THE CODE I HAVE GOTTEN SO FAR: PYTHON This is a code that is supposed to help someone study/ practice for jeopardy. The two functions described below are required for this assignment. You may add other functions that you think are appropriate: menu() This function, which displays all the user options to the screen, prompt the user for their choice and returns their choice. This function will verify user input and ALWAYS...
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 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 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...
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...
Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...