If you like to add weather info to your connected Arduino, openweathermap.org is a free and easy to use service. It supplies weather info, but you can also use it to look up GPS position of a city, or receive the exact time (i.e. in case you don't want to use an NTP-server routine). Register on the site, generate an ApiKey and your are free to go. API calls look like this:
and it returns a string of data in JSON-format. For this project we use the weather by city, but you can modify for other services like forecast info. Try this in your browser to verify the results. The JSON string can be parsed to pick out the data you need, and there is an excellent library by to have this done:
ArduinoJsonThe library at https://arduinojson.org/ is very easy to use, its well developed and very flexible - although its wise to dig a bit deeper into the use of strings and pointers. Install the library in your Arduino IDE, and take a look at some examples how JSON strings are parsed: link
As I like to avoid the use of the 'String' type (as its not always so stable out of my experience), I use Char* to store string data, and in the library I use array-copy functions. All this is combined in a library:
OpenWeather Arduino Library
This library creates a new object type called OpenWeather, by calling the object function .setup(char* key) the APi key can be attached. by calling .getweather(char* Location) the weather data is retrieved of the Location, and stored in the object-variables. easy, simple, straight forward - see example code.
Install the library in your Arduino Library-directory (unzip). As the library is build with the MKR1010 WiFi library (WifiNINA.h), you can - if required - change this for your own wifi-board by changing the wifi-lib in OpenWeather.h:
ifndef OPENWEATHER_H
#define OPENWEATHER_H
//#define DEBUG_ON
#include "Arduino.h"
#include <ArduinoJson.h>
#include <WiFiNINA.h> // Replace with your own Wifi Library
#define MAXBUFFER 2000 // MAXBUFFER DATA
class OpenWeather
{
public:
OpenWeather();
void setup(char *key);
void setupserver(char *srvr);
char* raw();
void getweather(char *Loc);
char WeatherApiKey[64];
char WeatherServer[128];
char WeatherBuffer[MAXBUFFER];
float Longitude;
float Latitude;
char Main[24];
char Description[64];
float Temperature;
long Pressure;
long Humidity;
float Windspeed;
long Winddirection;
private: //
int _cstring(char *c1,char *c2);
int _cstringc(const char *c1,char *c2);
};
#endif
MySQL-Server UploadSecond part in this demo is storing the weather data in an MySQL database. Its not really practical (as this info is on OpenWeather.org all the time anyway), but for IoT demo purposes its nice :) For this demo I'm using a local MySQL-server on my Synology: enabled Web-Server (Nginx back end), installed Mariadb (MySQL) and PhPMyAdmin (SQL-Manager)
Tricky parts to check for setting up the MySQL:
- Create a new user (ie Arduino) with read/write allowance on your server-directory, give this user database Priviliges in PhPMyAdmin.
- Install PhP (5.6 in this case), enable PHP - use phpinfo(); to check enables features
- Enable PHP extensions -mysqli for MySQL access, and -gd (Graphics)
To setup the Database follow this example:
Example icreateproject - setup your database with PhPMyAdmin
To move the data from the Arduino to the SQL database, we call PHP-scripts. in this case I use the HTTP-Get method (POST method is also possible, especially if you have larger sets of data - future post). I don't use any encryption of secure-call (SSL), as this runs on my local network. Calls look like this :
http://192.168.200.11/weather/write_data.php?temp=15&press=1012&hum=43
My local web-server IP is 19.168.200.11, I made a web directory 'weather' and call the script write_data.php, and pass variable temp, press and hum. This is the php script that passes the code to the MyQSL database:
<?php
include ('connect.php');
// Prepare the SQL statement
$sql_insert = "INSERT INTO data (description,temp,hum,press,speed,dir) VALUES ('".$_GET["description"]."','".$_GET["temp"]."' ,'".$_GET["hum"]."','".$_GET["press"]."' ,'".$_GET["speed"]."' ,'".$_GET["dir"]."' )";
// Execute SQL statement
if(mysqli_query($con,$sql_insert))
{
echo "Done";
mysqli_close($con);
}
else
{
echo "error is ".mysqli_error($con );
}
?>
The connect.php include file creates the database connection-string ($con) - see zip-file. Edit thjis file with the Web-Server user and password name (ie arduino). We can use a second php-script to call the database info, and put it in a table: display.php, result looks like this :
Additional it's possible to set the information gathered over time in a graphics form. There are many graphics libraries available, for this example I'm using pchart, an class-oriented library withe many options.
Install the library in the webs-erver path, and be sure you have the -gd extension enabled in PHP. See graphics.php as an example :
Comments
Please log in or sign up to comment.