JayV
Published

OpenWeatherMap to SQL-Database Demo

Demo to show online OpenWeatherMaps.org and send data to your SQL server.

BeginnerWork in progress1 hour7,807
OpenWeatherMap to SQL-Database Demo

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

simple diagram

Code

OpenWeather_Arduino_Lib.zip

C/C++
OpenWeather library for Arduino
No preview (download only).

weather_Php_files.zip

PHP
Php Script files for OpenWeather example
No preview (download only).

WeatherTest.ino

C/C++
Arduino Script file
/*
Open Weather Demo for MKR1010 (Wifi with UBLox NINA module / Espressif = wifi)
J.Vos 2019

Stream OpenweatherMaps data to your SQL server, and make PHP Graphics or lists.

Open Weather API : https://openweathermap.org
SQL example : https://icreateproject.info/2014/12/14/arduino-save-data-to-database/

*************************************************************/
#include <WiFiNINA.h>         // change to your Wifi or Ethernet library - also change this in the openweather.h library !
#include <WiFiUdp.h>          // for UDP NTP Server - get time
#include <RTCZero.h>          // for clock ticks
#include <OpenWeather.h>
#include "arduino_secrets.h"  // Secret info on Pass qwords and API keys etc.
#define DEBUG_X 1             // Debug info via Serial Print

//
//Define ServerSettings
//
#define VERSION "1.1.0"              // Software verson 
#define SSERVER_PORT 80              // SQL Server Port
char apiKey[]= SECRET_APIKEY; 
char ssid[] = SECRET_SSID;           // your network SSID (name) - arduino_secrets.h
char pass[] = SECRET_PASS;           // your network password - arduino_secrets.h
IPAddress SQLServer(192,168,200,11); //  SQL server for dAta logging (local IP adress of my Synlogy server)


//
// Define RTC timers
//
#define LIFETICK 300                 // Life tick in seconds 
RTCZero rtc;                         // RTC Clock
long LogCheckTime;
unsigned long epoch= 1541062800UL;   // epoch time global variable


OpenWeather MyWeather;               // OpenWeathermaps Object
char Loc[]= "Amsterdam,NL";          // Location string  - see openweathermaps.org how to use the API interface
   
void setup()
{ 

/*********** Serial SETUP  **********/
int t=10;  //Initialize serial and wait for port to open, max 10 second waiting
  Serial.begin(115200);
  while (!Serial) {
    ; delay(1000);
    if ( (t--)== 0 ) break;
  }
/*********** RTC SETUP  **********/
rtc.begin();
rtc.setEpoch(epoch);                             // Set Time RTC to base time
/*********** WIFI SETUP  **********/
if (WiFi.status() == WL_NO_SHIELD) {             // check for the presence of the shield:
  #if DEBUG_X
    Serial.println("WiFi shield not present");
  #endif
  while (true);                                  // don't continue if no shield
  }
delay(1000);
  #if DEBUG_X
   Serial.println("WiFi starting...");
  #endif  
StartMyWifi();                                   // Start Wifi login 
/*********** Weather Object SETUP  **********/
MyWeather.setup(apiKey);
MyWeather.getweather(Loc);
Serial.print("\n# Main Weather : ");Serial.print(MyWeather.Main);
Serial.print(" / ");Serial.println(MyWeather.Description);
LogCheckTime = rtc.getEpoch() + LIFETICK;        // Set initial Log Check timer
} // endSetup




//
// Main SUPER Loop
//
void loop()
{
  Serial.print("."); delay(5000);
  ManageTimeKeeper(); 
} // end Main loop



//
// Time Keeper , do the house keeping : check log time, send data to SQL server
//
void ManageTimeKeeper()
{

if ( rtc.getEpoch()>= LogCheckTime ){                                 // if log Timer: check weather and log SQL
#if DEBUG_X  
   Serial.print("\n# Log SQL");
#endif
  MyWeather.getweather(Loc);
  LogSQLServer();
  LogCheckTime = rtc.getEpoch()+LIFETICK;
  if ( (WiFi.status() != WL_CONNECTED) || (WiFi.RSSI() <= -90) || (WiFi.RSSI() ==0) ) StartMyWifi();  // check wifi in case not connected : re-connect
   }
}



/**** SQLroutine  *****/
void  LogSQLServer(){

WiFiClient clients;
clients.connect(SQLServer,SSERVER_PORT);

if (clients.connected() ) {                                                   // check connected
  clients.print("GET /weather/write_data.php?");
  clients.print("description=");clients.print(MyWeather.Description);         // write PhP command
  clients.print("&temp=");clients.print(MyWeather.Temperature);
  clients.print("&hum=");clients.print(MyWeather.Humidity);
  clients.print("&press=");clients.print(MyWeather.Pressure);
  clients.print("&speed=");clients.print(MyWeather.Windspeed);
  clients.print("&dir=");clients.print(MyWeather.Winddirection);
  clients.println(" HTTP/1.1");
  clients.print("Host: ");clients.println(SQLServer);
  clients.println("Connection: close");
  clients.println();
  clients.flush();
while ( clients.connected() ) clients.stop();
#if DEBUG_X
    Serial.print("*SQL data logged");           // print a message out the serial port
#endif

  }
 else
 {
 #if DEBUG_X
    Serial.println("*SQL not connected");           // print a message out the serial port
#endif
 }
}



/**** WIFI ROUTINES  *****/
// Login to local network  //
void StartMyWifi(){
int t=0;
int status = WiFi.status();

WiFi.disconnect(); delay(3000); printWiFiStatus();

if ( (status != WL_CONNECTED) || (WiFi.RSSI() <= -90) ||(WiFi.RSSI() ==0) ) {                  // only try if not yet connected
  while ( ((status != WL_CONNECTED) || (WiFi.RSSI() <= -90) || (WiFi.RSSI() ==0) ) && t<5) {   // attempt to connect to WiFi network, max 5 times
    
#if DEBUG_X
       Serial.print("Attempting to connect to Network named: ");
       Serial.print(ssid);                                                                     // print the network name (SSID);
#endif
       status = WiFi.begin(ssid, pass);                                                        // Connect to WPA/WPA2 network.
       delay(2000);                                                                            // wait 2 seconds for connection:
       t=t+1;                                                                                  // try-counter
  }
  if ( status == WL_CONNECTED ) {
       //server.begin();                                                                       // start the web server outside this loop!
#if DEBUG_X
       printWiFiStatus();                                                                      // you're connected now, so print out the status
#endif
  }
  else{                                                                                        // no connection possible : exit without server started
#if DEBUG_X
    Serial.println("Connection not possible after several retries.");                   
#endif
  }
}
else{
#if DEBUG_X
    Serial.println("Already connected.");                                                     // you're already connected
    printWiFiStatus(); 
#endif
    }
}


// SERIALPRINT Wifi Status - only for debug
void printWiFiStatus() {
#if DEBUG_X
  if (Serial) {
    // print the SSID of the network you're attached to:
    Serial.print("\nSSID: ");
    Serial.print(WiFi.SSID());

    // print your WiFi shield's IP address:
    IPAddress ip = WiFi.localIP();
    Serial.print(" - IP Address: ");
    Serial.print(ip);

    // print the received signal strength:
    long rssi = WiFi.RSSI();
    Serial.print("- Rssi: ");
    Serial.print(rssi);
    Serial.println(" dBm");
  }
#endif
}

Credits

JayV

JayV

28 projects • 32 followers
Silicon crazy for profession, silicon do-it-yourselves at Home.

Comments