MagicMirror Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • Donate
    • Discord

    Load data from json file

    Development
    3
    7
    4967
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      mastrom90 last edited by paviro

      Hi guys,
      i have a really little problem and i think it is easy to be solved. I have created a new module where i need to load a local json file which is created by an external program.
      I cant figure out how i can read this file and where to locate that one. I have created a variable with the filetext so i know i can handle it if i read it. But i cant get it to read the file so i can remove the variable because the data changes every week.

      i hope someone can help me. Sorry if im wrong here but i dont know how i can help myself 😞

      1 Reply Last reply Reply Quote 0
      • M
        mastrom90 last edited by

        is there really noone who wants to help me?

        1 Reply Last reply Reply Quote 0
        • M
          mattlugar last edited by paviro

          I’m not exactly in the same boat, but similar I guess… Maybe we can put our heads together or someone more knowledgeable will notice the thread…

          I’ve got a local weather station that I have putting plain text data (for temperature) on the SD card of MagicMirror . My goal is to put a ‘realtime’ (updated every minute or so) temperature on the display, directly from my own weather station.

          As an initial step to test this out, I went into currentweather.js and changed the following in the processWeather function:

          Removed:

          this.temperature = this.roundValue(data.main.temp);
          

          Added:

           var fs = new require('fs');
           this.temperature = fs.readFileSync('/var/www/html/wx/temp.txt');
          

          I anticipated that this would put the contents of my file in place of the temperature data from openweathermap. Unfortunately, it results in the currentweather module sticking at Loading...

          Just to check I’m not insane, I replaced my code with:
          this.temperature = '56.0';

          The module loads with 56.0 as the temperature. This leads me to believe I’m having an issue reading the file from disk. I threw together a quick js file to test I’m doing the fs.readFileSync command right:

          var sys = require("sys");
          var fs = new require('fs');
          var temp = fs.readFileSync('/var/www/html/wx/temp.txt');
          sys.puts(temp);
          

          Low and behold, my file contents show up just fine.

          As far as I can tell, this SHOULD work, but it clearly isn’t. Maybe you and I are running into similar issues reading from file? Anyone out there that can assist us?

          1 Reply Last reply Reply Quote 0
          • paviro
            paviro Admin last edited by paviro

            Hi guys,
            A simple Google search for node parse json file reveals this 😄

            For a solution without node another Google search for javascript parse local json file reveals this article 🙂

            I hope this helps!

            Please also make sure you use Markdown on code snippets so it is easier to read!

            We're all stories in the end. Just make it a good one, eh?

            – The Doctor

            M 1 Reply Last reply Reply Quote 0
            • M
              mattlugar @paviro last edited by

              @paviro Difference on mine is I’m pulling in a straight value from a text file, not JSON. The file is a text file that has current temperature. No markups tags or anything else.

              I’m no expert at Java so maybe I’m missing something?

              paviro 1 Reply Last reply Reply Quote 0
              • paviro
                paviro Admin @mattlugar last edited by paviro

                @mattlugar not sure how currentweather.js deals with data it’s @MichMich’s module he might know something. But it would probably be easier to have a look at the module development README and write a simple module for your purpose from scratch.

                We're all stories in the end. Just make it a good one, eh?

                – The Doctor

                M 1 Reply Last reply Reply Quote 0
                • M
                  mattlugar @paviro last edited by

                  @paviro Ah, well thanks for the feedback. I’m hoping to get a bit of a hybrid between that app and pulling data from my own weather station. I’d like to keep the sunrise/sunset and the current conditions coming in from openweathermap and put my own station’s temp/wind. Was thinking modifying his would be a little easier than from scratch. 🙂 Maybe I’ll get lucky and he’ll see this and point me in the right direction.

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post
                  Enjoying MagicMirror? Please consider a donation!
                  MagicMirror created by Michael Teeuw.
                  Forum managed by Paul-Vincent Roll and Rodrigo Ramírez Norambuena.
                  This forum is using NodeBB as its core | Contributors
                  Contact | Privacy Policy