Skip to content

Commit

Permalink
Trying to properly parse JSON string from MQTT data
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrumartin committed Apr 28, 2023
1 parent acaeba8 commit 422dd79
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 10 deletions.
6 changes: 1 addition & 5 deletions src/redux/reducers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { ADD_SENSOR_DATA } from './actions';

const initialState = {
sensorValue: {
humidity: '0',
temperature: '0',
pressure: '0',
},
sensorValue: '{"humidity": 0, "temperature": 0, "pressure": 0}',
};

const sensorReducer = (state = initialState, action) => {
Expand Down
2 changes: 1 addition & 1 deletion src/screens/Home/src/components/ReduxTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function ReduxTest() {
const handlePress = () => {
dispatch({
type: ADD_SENSOR_DATA,
payload: "{ 'temperature': '100', humidity: 7 }",
payload: '{ "temperature": 500, "humidity": 70 }',
});
};

Expand Down
5 changes: 1 addition & 4 deletions src/shared/Sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ export default function Sensor({ initial, label, dataType }) {
const value = useSelector((state) => state.sensors.sensorValue); // Get the sensor value from the MQTT sensor in the Redux state

setTimeout(() => {
// console.log(value);
// console.log(JSON.stringify(value));
// console.log(JSON.parse(value));
for (const [key, val] of Object.entries(value)) {
for (const [key, val] of Object.entries(JSON.parse(value))) {
if (key === dataType) {
setSensorValue(Number(val));
}
Expand Down

0 comments on commit 422dd79

Please sign in to comment.