My first post on stackoverflow. I am developing a system that includes client-side javascript and backbone.js and highcharts.js for displaying real-time data from my renewable energy system at home.
It uses a small microcontroller called Arduino to process sensor data at a low level. The controller has its own ethernet equipment, and I install it as a small server.
In my client program, I use Backbone as MVC, and the model url points to Arduino IP to request a json data packet. It works well.
Now I am moving the pointer to my hosting site where I have a small php file to pull data from the SQL table and present it in the same json format.
From my browser, when I request a json data packet from an Arduino controller, it looks like this:
<head></head><body><pre>{"vT_pv":234.00,"iT_pv":5.90,"iS_pv":0.00,"vB_pv":27.51}</pre></body>
And when I point the browser to my hosted site where I call php, it looks like this:
<head></head><body><pre>{"vT_pv":230.70,"iT_pv":4.90,"iS_pv":0.00,"vB_pv":27.56}</pre></body>
Edit: included full headers as requested by Alex
Both look the same, and yet on my client javascript only the first one actually works, since the data is successfully placed in the model and then displayed on the screen.
One of the differences between them when checking the page is that my Arduino returns as HTTP / 1.0, where, since the returned json data from my hosted site is HTTP / 1.1
Will it be enough to cause such a problem?
Below is the base model for this json;
PollerModel = Backbone.Model.extend({ // url:'//xxx.xxx.xxx.xxx/poller.json', // this one works url: 'http://yyy.yyy.yyy.yyy.host_site/sql_get_live.php', // this one does not startUpdate: function() { var that = this; startUpdate(that); function startUpdate(isThis) { that.fetch({async:false}); pollerUpdateInterval = setTimeout(function(){startUpdate(that);}, 5000); }; }, });
As you can see, after calling the startUpate function, it is always called every 5 seconds, which is what I want.
When I use the first URL pointing to the microcontroller, it works like a charm. When I use the second URL, I see that it goes out to make a request, and that it receives a response, but it does nothing with the data, it does not fit into the model.
So, on my client application, the first URL I see is data in the rendered view, but not with the second URL.
Below is the php file on the host site:
<?php // The JSON standard MIME header. // header('content-type: application/json; charset=utf-8'); $con = mysql_connect('host', 'username', 'password'); if (!$con) { die('mysql connect error: ' .mysql_error()); } mysql_select_db("DataBase", $con); $sql_Live = "SELECT * FROM live"; $mysql_record = mysql_query($sql_Live,$con); $row = mysql_fetch_array($mysql_record); $jsonData = '{"vT_pv":' . $row['vT_pv'] . ',"iT_pv":' . "4.90" . ',"iS_pv":' . $row['iS_pv'] . ',"vB_pv":' . $row['vB_pv'] . '}'; echo "<pre >"; echo $jsonData; echo "</pre>"; mysql_close($con); ?>
The title that is commented out has nothing to do with the format or any result. I tried using the json encode function in different ways, all without success. actually using json code creates what I consider to be a non-json format, so maybe the format above, as I use it, is not formatted correctly? But does it work for the first URL?
I searched and feel a little stuck here, so I thought it was time to ask.
Floor