Php Interface Arduino Santiapps

Arduino Honduras Santiapps Marcio Valenzuela

Tutorial Arduino PHP Web JSON/XML

En este tutorial aprendemos a interfaz entre servicios web como openweather.org y lenguajes como PHP para parse JSON o XML y alimentar la Arduino.

Requisitos:

  1. Computadora (mac)
  2. Arduino UNO
  3. Servidor Linux/PHP
  4. Arduino IDE (https://www.arduino.cc/en/Main/Software)
Arduino IoT Simple Tutorial Web JSON XML Santiapps Marcio Valenzuela
Arduino IoT Simple Tutorial Web JSON XML

OpenWeather:

http://api.openweathermap.org/data/2.5/weather?q=Amsterdam,NL&units=metric&appid=suID

Resultado:

{"coord":{"lon":4.89,"lat":52.37},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"base":"cmc stations","main":{"temp":24.17,"pressure":1013.79,"humidity":39,"temp_min":24.17,"temp_max":24.17,"sea_level":1013.72,"grnd_level":1013.79},"wind":{"speed":8.56,"deg":79.5083},"clouds":{"all":12},"dt":1463065042,"sys":{"message":0.0123,"country":"NL","sunrise":1463024893,"sunset":1463081183},"id":2759794,"name":"Amsterdam","cod":200}

PHP:

[/code]
<?php
 // retrieve the current weather in Amsterdam in degrees Celcius
 $json = file_get_contents('http://api.openweathermap.org/data/2.5/weather?q=Amsterdam,NL&units=metric&appid=suID');

// parse the JSON
$data = json_decode($json);

// show the city (and the country)
echo ‘<h1>’, $data->name, ‘ (‘, $data->sys->country, ‘)</h1>’;

// the general information about the weather
echo ‘<h2>Temperature:</h2>’;
echo ‘<p><strong>Current:</strong> ‘, $data->main->temp, ‘&deg; C</p>’;
echo ‘<p><strong>Min:</strong> ‘, $data->main->temp_min, ‘&deg; C</p>’;
echo ‘<p><strong>Max:</strong> ‘, $data->main->temp_max, ‘&deg; C</p>’;

// something about the air
echo ‘<h2>Air</h2>’;
echo ‘<p><strong>Humidity:</strong> ‘, $data->main->humidity, ‘%</p>’;
echo ‘<p><strong>Pressure:</strong> ‘, $data->main->pressure, ‘ hPa</p>’;

// and about the wind
echo ‘<h2>Wind</h2>’;
echo ‘<p><strong>Speed:</strong> ‘, $data->wind->speed, ‘ m/s</p>’;
echo ‘<p><strong>Direction:</strong> ‘, $data->wind->deg, ‘&deg;</p>’;

// and what the weather is according to the API (an array)
echo ‘<h2>The weather</h2>’;
echo ‘<ul>’;
foreach ($data->weather as $weather) {
echo ‘<li>’, $weather->description, ‘</li>’;
}
echo ‘<ul>’;
?>

[/code]

 

Resultado:

Arduino IoT Simple Tutorial Web JSON XML
Arduino IoT Simple Tutorial Web JSON XML

Ahora queremos obtener el forecast de los próximos 5 días:

[/code]
<?php
// retrieve the current weather in Amsterdam in degrees Celcius
$json = file_get_contents('http://api.openweathermap.org/data/2.5/forecast?q=SanPedroSula,HN&units=metric&appid=suID');

// parse the JSON
$data = json_decode($json);

// show the city (and the country)
echo ‘<h1>’, $data->city->name, ‘ (‘, $data->city->country, ‘)</h1>’;

for ($x = 0; $x <= 39; $x++) {
// the general information about the weather
echo ‘<h2>Data for ‘,$data->list[$x]->dt_txt,’:</h2>’;
echo ‘<p><strong>Temp_range:</strong> ‘, $data->list[$x]->main->temp_min, ‘ – ‘, $data->list[$x]->main->temp_max, ‘&deg; C</p>’;
echo ‘<p><strong>Pressure:</strong> ‘, $data->list[$x]->main->pressure, ‘</p>’;
echo ‘<p><strong>Humidity:</strong> ‘, $data->list[$x]->main->humidity, ‘</p>’;
echo ‘<p><strong>Weather:</strong> ‘, $data->list[$x]->weather[0]->description, ‘</p>’;
}
?>

[/code]

Para poder servir la info en un arreglo podemos usar este código:

[code]
<?php
// retrieve the current weather in Amsterdam in degrees Celcius
$json = file_get_contents(‘http://api.openweathermap.org/data/2.5/forecast?q=SanPedroSula,HN&units=metric&appid=suID’);
// parse the JSON
$data = json_decode($json);</pre>
// show the city (and the country)
//echo ‘<h1>’, $data->city->name, ‘ (‘, $data->city->country, ‘)</h1>’;

//create master array
$masterArray = array();
for ($x = 0; $x <= 39; $x++) {
$dataArray = array(“timestamp” => $data->list[$x]->dt_txt,
“temp_min” => $data->list[$x]->main->temp_min,
“temp_max” => $data->list[$x]->main->temp_max,
“pressure” => $data->list[$x]->main->pressure,
“humidity” => $data->list[$x]->main->humidity,
“weather” => $data->list[$x]->weather[0]->description);
//echo ‘<pre>’; print_r($dataArray); echo ‘</pre>’;
array_push($masterArray, $dataArray);
} //end for loop
echo ‘<pre>’; print_r($masterArray); echo ‘</pre>’;
?>
<pre>[/code]

Es importante saber usar server scripts para poder integrarlos a nuestros proyectos de IoT.  Adelante trabajaremos con un proyecto que lee los datos que posteamos de un modulo Arduino a la web para alimentar un proyecto mas avanzado de AI residencial.

Leave a Reply