• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

Load data from json file

Scheduled Pinned Locked Moved Development
7 Posts 3 Posters 5.9k Views 4 Watching
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 Offline
    mastrom90
    last edited by paviro May 13, 2016, 10:30 PM May 13, 2016, 9:57 PM

    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 Offline
      mastrom90
      last edited by May 15, 2016, 1:17 PM

      is there really noone who wants to help me?

      1 Reply Last reply Reply Quote 0
      • M Offline
        mattlugar
        last edited by paviro May 16, 2016, 9:11 PM May 16, 2016, 5:06 PM

        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
        • P Offline
          paviro Admin
          last edited by paviro May 16, 2016, 9:08 PM May 16, 2016, 9:04 PM

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

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

          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 May 16, 2016, 9:20 PM Reply Quote 0
          • M Offline
            mattlugar @paviro
            last edited by May 16, 2016, 9:20 PM

            @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?

            P 1 Reply Last reply May 16, 2016, 9:29 PM Reply Quote 0
            • P Offline
              paviro Admin @mattlugar
              last edited by paviro May 16, 2016, 9:31 PM May 16, 2016, 9:29 PM

              @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 May 16, 2016, 9:51 PM Reply Quote 0
              • M Offline
                mattlugar @paviro
                last edited by May 16, 2016, 9:51 PM

                @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
                1 / 1
                • First post
                  1/7
                  Last post
                Enjoying MagicMirror? Please consider a donation!
                MagicMirror created by Michael Teeuw.
                Forum managed by Sam, technical setup by Karsten.
                This forum is using NodeBB as its core | Contributors
                Contact | Privacy Policy