Assignment
- Control your circuit over a local wireless network.
- Send or receive data from the Internet.
- Bonus: Send a message between multiple projects.
Local Control - Connecting the ESP to WiFi
In this part of the Smart Mug Heater development, I connected the ESP8266 to Wi-Fi, allowing the device to fetch the current time from an NTP server and retrieve outside temperature. This data is then shown on the OLED display. The heater can now respond to real-world weather conditions. A simple HTTP request is used to obtain a temperature string and convert it to displayable data. Wi-Fi connection logic and status are managed on startup, ensuring the ESP boots cleanly before any network operations begin.
First, I set up the ESP8266 to connect to our home Wi-Fi. The heater's code now includes a simple WiFi.begin(ssid, password) call. If it can't connect, it blinks the OLED with a sad face until it does.
API Usage
OpenWeather API
Next, I wanted to pull some data from the internet. I chose OpenWeather API to get the current weather data. I used the HTTPClient library to make GET requests to the OpenWeather API.
Here's a simple example of how I made the request:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
WiFiClient wifiClient;
http.begin(wifiClient, "http://api.openweathermap.org/data/2.5/weather?q=London&appid=your_API_key");
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(payload);
} else {
Serial.println("Error on HTTP request");
}
http.end();
}
}
void loop() {
// Nothing to do here
}
In this code, I connect to Wi-Fi, then make a GET request to the OpenWeather API. The response is printed to the Serial Monitor.
Make sure to replace your_SSID, your_PASSWORD, and your_API_key with your actual Wi-Fi credentials and OpenWeather API key.
This was a great exercise in using APIs and handling HTTP requests in Arduino.
Local Time
Additionally, I wanted to get the local time from the internet. I used the NTPClient library to fetch the current time from an NTP server. Here's a simple example:
#include <WiFiUdp.h>
#include <NTPClient.h>
#include <ESP8266WiFi.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 0, 60000);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
timeClient.begin();
timeClient.update();
Serial.println("Current time: " + timeClient.getFormattedTime());
}
void loop() {
timeClient.update();
Serial.println("Current time: " + timeClient.getFormattedTime());
delay(1000);
}
In this code, I connect to Wi-Fi, initialize the NTP client, and print the current time to the Serial Monitor. The time is updated every second.
Again, make sure to replace your_SSID and your_PASSWORD with your actual Wi-Fi credentials.
This was a great exercise in using NTP to get the local time in Arduino.
Conclusion
I experimented a little with API calls, I was successfull and it was easy to do, didnt look into local network control from another device. So I barely scratched the surface of this weeks topics.