added wifi connection and menu showing connection details

This commit is contained in:
2026-06-24 17:08:45 +03:00
parent 76606c00e3
commit 6f6ea0e0fd
9 changed files with 171 additions and 1 deletions
+1
View File
@@ -3,3 +3,4 @@
.vscode/c_cpp_properties.json .vscode/c_cpp_properties.json
.vscode/launch.json .vscode/launch.json
.vscode/ipch .vscode/ipch
include/wifi_credentials.h
+1
View File
@@ -6,6 +6,7 @@
enum class MenuAction enum class MenuAction
{ {
ShowInternetDetails,
ShowHelloWorld, ShowHelloWorld,
BackToCarousel BackToCarousel
}; };
+2
View File
@@ -4,9 +4,11 @@
#include <U8g2lib.h> #include <U8g2lib.h>
#include "am2302_sensor.h" #include "am2302_sensor.h"
#include "wifi_connection.h"
void drawScreen1(U8G2 &u8g2); void drawScreen1(U8G2 &u8g2);
void drawHelloWorldScreen(U8G2 &u8g2); void drawHelloWorldScreen(U8G2 &u8g2);
void drawInternetConnectionScreen(U8G2 &u8g2, WifiConnectionInfo wifiInfo);
void setEnvironmentReading(Am2302Reading reading); void setEnvironmentReading(Am2302Reading reading);
void drawEnvironmentScreen(U8G2 &u8g2); void drawEnvironmentScreen(U8G2 &u8g2);
void drawWeatherIconScreen(U8G2 &u8g2); void drawWeatherIconScreen(U8G2 &u8g2);
+34
View File
@@ -0,0 +1,34 @@
#ifndef WIFI_CONNECTION_H
#define WIFI_CONNECTION_H
#include <Arduino.h>
#include <IPAddress.h>
struct WifiConnectionInfo
{
bool shieldAvailable;
bool connected;
IPAddress ipAddress;
};
class WifiConnection
{
public:
WifiConnection();
void begin(unsigned long now);
void update(unsigned long now);
WifiConnectionInfo info() const;
bool consumeDirty();
private:
void connect(unsigned long now);
void recordStatus();
unsigned long nextAttempt_;
bool shieldAvailable_;
bool connected_;
IPAddress ipAddress_;
bool dirty_;
};
#endif
+7
View File
@@ -0,0 +1,7 @@
#ifndef WIFI_CREDENTIALS_H
#define WIFI_CREDENTIALS_H
const char WIFI_SSID[] = "your-wifi-name";
const char WIFI_PASSWORD[] = "your-wifi-password";
#endif
+16
View File
@@ -6,6 +6,7 @@
#include "carousel.h" #include "carousel.h"
#include "menu.h" #include "menu.h"
#include "screens.h" #include "screens.h"
#include "wifi_connection.h"
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, U8X8_PIN_NONE); U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, U8X8_PIN_NONE);
@@ -14,11 +15,13 @@ int lastButtonSelectState = LOW;
uint8_t buttonNext = A4; uint8_t buttonNext = A4;
uint8_t buttonSelect = A5; uint8_t buttonSelect = A5;
Am2302Sensor am2302(A3); Am2302Sensor am2302(A3);
WifiConnection wifiConnection;
enum class AppMode enum class AppMode
{ {
Carousel, Carousel,
Menu, Menu,
InternetDetails,
HelloWorld HelloWorld
}; };
@@ -36,6 +39,7 @@ void setup() {
pinMode(buttonNext, INPUT); pinMode(buttonNext, INPUT);
pinMode(buttonSelect, INPUT); pinMode(buttonSelect, INPUT);
am2302.begin(); am2302.begin();
wifiConnection.begin(millis());
u8g2.begin(); u8g2.begin();
u8g2.clearBuffer(); u8g2.clearBuffer();
@@ -52,6 +56,9 @@ void openMenu()
void selectMenuOption() void selectMenuOption()
{ {
switch (menu.select()) { switch (menu.select()) {
case MenuAction::ShowInternetDetails:
appMode = AppMode::InternetDetails;
break;
case MenuAction::ShowHelloWorld: case MenuAction::ShowHelloWorld:
appMode = AppMode::HelloWorld; appMode = AppMode::HelloWorld;
break; break;
@@ -73,6 +80,7 @@ void handleNextButton()
case AppMode::Menu: case AppMode::Menu:
menu.nextOption(); menu.nextOption();
break; break;
case AppMode::InternetDetails:
case AppMode::HelloWorld: case AppMode::HelloWorld:
break; break;
} }
@@ -82,6 +90,7 @@ void handleSelectButton()
{ {
switch (appMode) { switch (appMode) {
case AppMode::Carousel: case AppMode::Carousel:
case AppMode::InternetDetails:
case AppMode::HelloWorld: case AppMode::HelloWorld:
openMenu(); openMenu();
break; break;
@@ -103,6 +112,8 @@ bool displayNeedsRedraw()
return carousel.consumeDirty(); return carousel.consumeDirty();
case AppMode::Menu: case AppMode::Menu:
return menu.consumeDirty(); return menu.consumeDirty();
case AppMode::InternetDetails:
return wifiConnection.consumeDirty();
case AppMode::HelloWorld: case AppMode::HelloWorld:
return false; return false;
} }
@@ -119,6 +130,9 @@ void drawActiveView()
case AppMode::Menu: case AppMode::Menu:
menu.draw(u8g2); menu.draw(u8g2);
break; break;
case AppMode::InternetDetails:
drawInternetConnectionScreen(u8g2, wifiConnection.info());
break;
case AppMode::HelloWorld: case AppMode::HelloWorld:
drawHelloWorldScreen(u8g2); drawHelloWorldScreen(u8g2);
break; break;
@@ -127,6 +141,8 @@ void drawActiveView()
void loop(){ void loop(){
const unsigned long now = millis(); const unsigned long now = millis();
wifiConnection.update(now);
if (appMode == AppMode::Carousel) { if (appMode == AppMode::Carousel) {
carousel.update(now); carousel.update(now);
} }
+4 -1
View File
@@ -3,6 +3,7 @@
namespace namespace
{ {
const char *menuOptions[] = { const char *menuOptions[] = {
"Internet",
"Hello world", "Hello world",
"Back" "Back"
}; };
@@ -13,6 +14,8 @@ MenuAction actionForIndex(size_t index)
{ {
switch (index) { switch (index) {
case 0: case 0:
return MenuAction::ShowInternetDetails;
case 1:
return MenuAction::ShowHelloWorld; return MenuAction::ShowHelloWorld;
default: default:
return MenuAction::BackToCarousel; return MenuAction::BackToCarousel;
@@ -30,7 +33,7 @@ void Menu::draw(U8G2 &u8g2)
u8g2.drawStr(0, 12, "Menu"); u8g2.drawStr(0, 12, "Menu");
for (size_t i = 0; i < menuOptionCount; i++) { for (size_t i = 0; i < menuOptionCount; i++) {
const uint8_t y = 32 + (i * 18); const uint8_t y = 28 + (i * 14);
if (i == selectedIndex_) { if (i == selectedIndex_) {
u8g2.drawStr(0, y, ">"); u8g2.drawStr(0, y, ">");
} }
+22
View File
@@ -40,6 +40,28 @@ void drawHelloWorldScreen(U8G2 &u8g2)
u8g2.drawStr(0, 54, "world"); u8g2.drawStr(0, 54, "world");
} }
void drawInternetConnectionScreen(U8G2 &u8g2, WifiConnectionInfo wifiInfo)
{
u8g2.setFont(u8g2_font_6x12_tr);
u8g2.drawStr(0, 12, "Internet");
if (!wifiInfo.shieldAvailable) {
u8g2.drawStr(0, 34, "WiFi shield missing");
return;
}
if (!wifiInfo.connected) {
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(0, 42, "Disconnected");
return;
}
u8g2.drawStr(0, 32, "Connected");
u8g2.drawStr(0, 48, "IP address:");
u8g2.setCursor(0, 64);
u8g2.print(wifiInfo.ipAddress);
}
void setEnvironmentReading(Am2302Reading reading) void setEnvironmentReading(Am2302Reading reading)
{ {
environmentReading = reading; environmentReading = reading;
+84
View File
@@ -0,0 +1,84 @@
#include "wifi_connection.h"
#include <WiFi101.h>
#include "wifi_credentials.h"
namespace
{
const unsigned long reconnectInterval = 30000;
}
WifiConnection::WifiConnection()
: nextAttempt_(0),
shieldAvailable_(true),
connected_(false),
ipAddress_(0, 0, 0, 0),
dirty_(true) {}
void WifiConnection::begin(unsigned long now)
{
shieldAvailable_ = WiFi.status() != WL_NO_SHIELD;
connect(now);
recordStatus();
}
void WifiConnection::update(unsigned long now)
{
if (!shieldAvailable_) {
return;
}
if (WiFi.status() == WL_CONNECTED) {
recordStatus();
return;
}
if (now >= nextAttempt_) {
connect(now);
}
recordStatus();
}
WifiConnectionInfo WifiConnection::info() const
{
WifiConnectionInfo current = {
shieldAvailable_,
connected_,
ipAddress_
};
return current;
}
bool WifiConnection::consumeDirty()
{
const bool d = dirty_;
dirty_ = false;
return d;
}
void WifiConnection::connect(unsigned long now)
{
if (!shieldAvailable_) {
return;
}
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
nextAttempt_ = now + reconnectInterval;
}
void WifiConnection::recordStatus()
{
const bool currentConnected = WiFi.status() == WL_CONNECTED;
IPAddress currentIpAddress(0, 0, 0, 0);
if (currentConnected) {
currentIpAddress = WiFi.localIP();
}
if (connected_ != currentConnected || ipAddress_ != currentIpAddress) {
connected_ = currentConnected;
ipAddress_ = currentIpAddress;
dirty_ = true;
}
}