Skip to content

Instantly share code, notes, and snippets.

@brennanMKE
Created November 26, 2023 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brennanMKE/b0629a2db59d6df3b5f2424499f20f77 to your computer and use it in GitHub Desktop.
Save brennanMKE/b0629a2db59d6df3b5f2424499f20f77 to your computer and use it in GitHub Desktop.
Networking with ESP32 Microcontroller

Networking with ESP32 Microcontroller

An ESP32 is a powerful microcontroller which includes WiFi networking. Below are a couple of Sketches that can be built and installed using Arduino IDE.

You may first need to do more setup. Add the URL below to Boards Manager in Arduino IDE.

https://dl.espressif.com/dl/package_esp32_index.json

For your current Sketch, change board selection to ESP32 Dev Board and the USB Port. You will need to install ArduinoJson library to use the code below.

You will also create 2 local libraries to hold into values which are unique to your environment. On a Mac libraries are located in ~/Documents/Arduino/libaries. Create WiFiNetwork and OpenWeatherMapKey in this directory. Then create the simple source files below in these directories, respectively.

  • WiFiNetwork.h
  • WiFiNetwork.cpp
  • OpenWeatherMapKey.h
  • OpenWeatherMapKey.cpp

The purpose of these libraries is so you can set up your unique values for WiFi and the API key for OpenWeatherMap.

Then see the Time and Weather Sketches below. Both will start up WiFi in setup and then make a request periodically. While the request is active the LED light will be turned on so you can see that activity.

#include "OpenWeatherMapKey.h"
const char* OpenWeatherMapKey::key() {
return "INSERT_HERE";
}
#ifndef OpenWeatherMapKey_h
#define OpenWeatherMapKey_h
#include "Arduino.h"
class OpenWeatherMapKey {
public:
static const char* key();
};
#endif
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <WiFiNetwork.h>
const char* ssid = WiFiNetwork::ssid();
const char* password = WiFiNetwork::password();
const int ledPin = 2;
const bool debug = false;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
delay(500);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected to WiFi");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
makeHTTPRequest();
delay(10000); // 10 secs
} else {
delay(250);
}
}
void makeHTTPRequest() {
digitalWrite(ledPin, HIGH);
HTTPClient http;
http.begin("http://worldtimeapi.org/api/ip");
int httpCode = http.GET();
if (httpCode == 200) {
String payload = http.getString();
if (debug) {
Serial.println(httpCode);
Serial.println(payload);
}
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
const char* datetime = doc["datetime"];
const char* timezone = doc["timezone"];
bool daylight_savings = doc["dst"];
Serial.println("Date and Time: " + String(datetime));
Serial.println("Timezone: " + String(timezone));
Serial.println("Daylight Savings: " + String(daylight_savings ? "Yes" : "No"));
} else {
Serial.println("There was an error with the request.");
Serial.println(httpCode);
}
http.end();
digitalWrite(ledPin, LOW);
}
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <WiFiNetwork.h>
#include <OpenWeatherMapKey.h>
const char* ssid = WiFiNetwork::ssid();
const char* password = WiFiNetwork::password();
const int ledPin = 2;
const bool debug = true;
const char* apiKey = OpenWeatherMapKey::key();
const char* weatherUrl = "https://api.openweathermap.org/data/2.5/weather?lat=37.7749&lon=-122.4194&units=imperial&appid=";
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
delay(500);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected to WiFi");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
makeHTTPRequest();
delay(60000); // 60 secs
} else {
delay(250);
}
}
void makeHTTPRequest() {
digitalWrite(ledPin, HIGH);
HTTPClient http;
String fullUrl = String(weatherUrl) + apiKey;
http.begin(fullUrl.c_str());
int httpCode = http.GET();
if (httpCode == 200) {
String payload = http.getString();
if (debug) {
Serial.println(httpCode);
Serial.println(payload);
}
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
float temp = doc["main"]["temp"];
Serial.println("Temperature in San Francisco: " + String(temp) + "°F");
} else {
Serial.println("There was an error with the request.");
Serial.println(fullUrl);
Serial.println(httpCode);
}
http.end();
digitalWrite(ledPin, LOW);
}
#include "WiFiNetwork.h"
const char* WiFiNetwork::ssid() {
return "INSERT_HERE";
}
const char* WiFiNetwork::password() {
return "INSERT_HERE";
}
#ifndef WiFiNetwork_h
#define WiFiNetwork_h
#include "Arduino.h"
class WiFiNetwork {
public:
static const char* ssid();
static const char* password();
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment