Skip to content

Instantly share code, notes, and snippets.

@ericoporto
Last active January 10, 2023 20:26
Show Gist options
  • Save ericoporto/fb9c975a290a822edca9 to your computer and use it in GitHub Desktop.
Save ericoporto/fb9c975a290a822edca9 to your computer and use it in GitHub Desktop.
An Arduino + Ethernet shield example on how to get data from Open WeatherMap
/*
Web Client to consume Open WeatherMap web service
This sketch connects to a website (http://api.openweathermap.org)
using an Arduino Ethernet shield and get data from site.
Circuit:
* Arduino MEGA 2560 R3 Board
* Ethernet shield attached to pins 10, 11, 12, 13
created 24 May 2015
by Erico Porto
Based on the WeatherUnderground version from Afonso C. Turcato
*/
#include <ArduinoJson.h>
#include <SPI.h>
#include <Ethernet.h>
#define RBUFFSIZE 600
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
const char server[] = "api.openweathermap.org";
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,1,202);
EthernetClient client;
char responseBuffer[RBUFFSIZE];
int rbindex = 0;
boolean startCapture;
void setup() {
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a half-second to initialize:
delay(500);
Serial.print("My IP Address is: ");
Serial.println(Ethernet.localIP());
Serial.println("Connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80))
{
Serial.println("Connected!");
const String html_cmd1 = "GET /data/2.5/weather?q=SaoPaulo,BR HTTP/1.1";
const String html_cmd2 = "Host: api.openweathermap.org";
const String html_cmd3 = "Connection: close";
//You can comment the following 3 lines
Serial.println(" " + html_cmd1);
Serial.println(" " + html_cmd2);
Serial.println(" " + html_cmd3);
// Make a HTTP request:
client.println(html_cmd1);
client.println(html_cmd2);
client.println(html_cmd3);
client.println();
responseBuffer[0] = '\0';
rbindex = 0;
startCapture = false;
}
else
{
// if you didn't get a connection to the server:
Serial.println("Connection failed!");
}
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and buffer:
if (client.available())
{
char c = client.read();
if(c == '{') {
startCapture=true;
}
if(startCapture && rbindex < RBUFFSIZE) {
responseBuffer[rbindex] = c;
rbindex++;
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.print("Received bytes");
Serial.print(strlen(responseBuffer));
Serial.println("Disconnecting.");
client.stop();
client.flush();
Serial.println(responseBuffer);
Serial.println();
StaticJsonBuffer<500> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(responseBuffer);
if (!root.success()) {
Serial.println("parseObject() failed");
} else {
//Now, some examples of how to use it!
Serial.print("Current Temperature: ");
Serial.print((double)root["main"]["temp"]);
Serial.println(" K\n");
Serial.print("Rain: ");
Serial.println((double)root["rain"]["3h"]);
Serial.println();
Serial.print("Wind: ");
Serial.println((double)root["wind"]["speed"]);
Serial.println();
}
// do nothing forevermore:
while(true);
}
}
@moonisshah
Copy link

hello eric,
Thank you for sharing code.. I was stuck on how to get the uno to communicate with an api. I want to ask you that is it possible to give the weather data to run a motor. say the wind data from the api runs a dc motor?... can u please help with that ...

@ericoporto
Copy link
Author

@moonisshah yes, it's. Damn, Github doesn't have notifications. The reason I did this was to rotate a motor (that one where you pass the angle and it turns to that angle) so I could build one of those wooden devices that use pressure difference to detect rain - except I planned to use the Arduino getting data instead. My wooden habilities failed me so I couldn't do. I will try to find some code to do just this and get back to you.

@INE1
Copy link

INE1 commented Feb 26, 2017

@ericoporto thank you for sharing code. i have a project which use this code which is a smart sprinkler :
If soil moisture is below a specific threshold and it will not rain in 2 coming days the sprinkler turn on. please can you help me doing this using your code?

@AnumSheraz
Copy link

Thanks for sharing the code. I guess it won't work as it is, unless you provide your APPID at the end of GET request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment