Skip to content

Commit

Permalink
Merge pull request #831 from Flaykz/features/esp32
Browse files Browse the repository at this point in the history
feat(esp32): ✨ add esp32 support
  • Loading branch information
sidoh authored Jul 13, 2024
2 parents 638aa78 + d248401 commit 1aba026
Show file tree
Hide file tree
Showing 26 changed files with 286 additions and 71 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ Both modules are SPI devices and should be connected to the standard SPI pins on
<img src="https://user-images.githubusercontent.com/40266/47967518-67556f00-e05e-11e8-857d-1173a9da955c.png" align="left" width="32%" />
<img src="https://user-images.githubusercontent.com/40266/47967520-691f3280-e05e-11e8-838a-83706df2edb0.png" align="left" width="22%" />

NodeMCU | Radio | Color
-- | -- | --
GND | GND | Black
3V3 | VCC | Red
D2 (GPIO4) | CE | Orange
D8 (GPIO15) | CSN/CS | Yellow
D5 (GPIO14) | SCK | Green
D7 (GPIO13) | MOSI | Blue
D6 (GPIO12) | MISO | Violet
NodeMCU (Esp8266) | Esp32 | Radio | Color
--------- |--------------|----| --
GND | GND | GND | Black
3V3 | 3V3 | VCC | Red
D2 (GPIO4) | D4 (GPIO4) | CE | Orange
D8 (GPIO15) | D5 (GPIO5) | CSN/CS | Yellow
D5 (GPIO14) | D18 (GPIO18) | SCK | Green
D7 (GPIO13) | D23 (GPIO23) | MOSI | Blue
D6 (GPIO12) | D19 (GPIO19) | MISO | Violet

_Image source: [MySensors.org](https://mysensors.org)_

Expand Down
17 changes: 17 additions & 0 deletions lib/ESP/ESPId.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <ESPId.h>

#ifdef ESP8266
uint32_t getESPId()
{
return ESP.getChipId();
}
#elif ESP32
uint32_t getESPId()
{
uint32_t id = 0;
for(int i=0; i<17; i=i+8) {
id |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
}
return id;
}
#endif
8 changes: 8 additions & 0 deletions lib/ESP/ESPId.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef _ESPID_H
#define _ESPID_H

#include <Esp.h>

uint32_t getESPId();

#endif
12 changes: 8 additions & 4 deletions lib/MQTT/HomeAssistantDiscoveryClient.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include <HomeAssistantDiscoveryClient.h>
#include <MiLightCommands.h>
#include <Units.h>
#include <ESP8266WiFi.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#elif ESP32
#include <WiFi.h>
#endif

HomeAssistantDiscoveryClient::HomeAssistantDiscoveryClient(Settings& settings, MqttClient* mqttClient)
: settings(settings)
Expand Down Expand Up @@ -40,7 +44,7 @@ void HomeAssistantDiscoveryClient::addConfig(const char* alias, const BulbId& bu

// Unique ID for this device + alias combo
char uniqueIdBuffer[30];
snprintf_P(uniqueIdBuffer, sizeof(uniqueIdBuffer), PSTR("%X-%s"), ESP.getChipId(), alias);
snprintf_P(uniqueIdBuffer, sizeof(uniqueIdBuffer), PSTR("%X-%s"), getESPId(), alias);

// String to ID the firmware version
char fwVersion[100];
Expand All @@ -64,7 +68,7 @@ void HomeAssistantDiscoveryClient::addConfig(const char* alias, const BulbId& bu
deviceMetadata[F("sw")] = fwVersion;
deviceMetadata[F("mf")] = F("espressif");
deviceMetadata[F("mdl")] = QUOTE(FIRMWARE_VARIANT);
deviceMetadata[F("identifiers")] = String(ESP.getChipId());
deviceMetadata[F("identifiers")] = String(getESPId());
deviceMetadata[F("cu")] = deviceUrl;

// HomeAssistant only supports simple client availability
Expand Down Expand Up @@ -148,7 +152,7 @@ String HomeAssistantDiscoveryClient::buildTopic(const BulbId& bulbId) {

topic += "light/";
// Use a static ID that doesn't depend on configuration.
topic += "milight_hub_" + String(ESP.getChipId());
topic += "milight_hub_" + String(getESPId());

// make the object ID based on the actual parameters rather than the alias.
topic += "/";
Expand Down
1 change: 1 addition & 0 deletions lib/MQTT/HomeAssistantDiscoveryClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <BulbId.h>
#include <MqttClient.h>
#include <ESPId.h>
#include <map>

class HomeAssistantDiscoveryClient {
Expand Down
3 changes: 2 additions & 1 deletion lib/MQTT/MqttClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <MiLightRadioConfig.h>
#include <AboutHelper.h>


static const char* STATUS_CONNECTED = "connected";
static const char* STATUS_DISCONNECTED = "disconnected_clean";
static const char* STATUS_LWT_DISCONNECTED = "disconnected_unclean";
Expand Down Expand Up @@ -56,7 +57,7 @@ void MqttClient::begin() {

bool MqttClient::connect() {
char nameBuffer[30];
sprintf_P(nameBuffer, PSTR("milight-hub-%u"), ESP.getChipId());
sprintf_P(nameBuffer, PSTR("milight-hub-%u"), getESPId());

#ifdef MQTT_DEBUG
Serial.println(F("MqttClient - connecting using name"));
Expand Down
1 change: 1 addition & 0 deletions lib/MQTT/MqttClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <PubSubClient.h>
#include <WiFiClient.h>
#include <MiLightRadioConfig.h>
#include <ESPId.h>

#ifndef MQTT_CONNECTION_ATTEMPT_FREQUENCY
#define MQTT_CONNECTION_ATTEMPT_FREQUENCY 5000
Expand Down
1 change: 1 addition & 0 deletions lib/MiLight/MiLightClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <MiLightCommands.h>
#include <functional>


using namespace std::placeholders;

static const uint8_t STATUS_UNDEFINED = 255;
Expand Down
1 change: 1 addition & 0 deletions lib/MiLight/V2PacketFormatter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <V2PacketFormatter.h>
#include <V2RFEncoding.h>


#define GROUP_COMMAND_ARG(status, groupId, numGroups) ( groupId + (status == OFF ? (numGroups + 1) : 0) )

V2PacketFormatter::V2PacketFormatter(const MiLightRemoteType deviceType, uint8_t protocolId, uint8_t numGroups)
Expand Down
1 change: 1 addition & 0 deletions lib/MiLightState/GroupState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <BulbId.h>
#include <MiLightCommands.h>


static const char* BULB_MODE_NAMES[] = {
"white",
"color",
Expand Down
1 change: 1 addition & 0 deletions lib/MiLightState/GroupState.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <ArduinoJson.h>
#include <BulbId.h>
#include <ParsedColor.h>
#include <vector>

#ifndef _GROUP_STATE_H
#define _GROUP_STATE_H
Expand Down
9 changes: 8 additions & 1 deletion lib/MiLightState/GroupStatePersistence.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#include <GroupStatePersistence.h>
#include <FS.h>
#ifdef ESP32
#include <SPIFFS.h>
#endif
#include "ProjectFS.h"

static const char FILE_PREFIX[] = "group_states/";
#ifdef ESP8266
static const char FILE_PREFIX[] = "group_states/";
#elif ESP32
static const char FILE_PREFIX[] = "/group_states/";
#endif

void GroupStatePersistence::get(const BulbId &id, GroupState& state) {
char path[30];
Expand Down
6 changes: 5 additions & 1 deletion lib/SSDP/New_ESP8266SSDP.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ License (MIT license):
#define ESP8266SSDP_H

#include <Arduino.h>
#include <ESP8266WiFi.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#elif ESP32
#include <WiFi.h>
#endif
#include <WiFiUdp.h>

class UdpContext;
Expand Down
49 changes: 40 additions & 9 deletions lib/Settings/AboutHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
#include <AboutHelper.h>
#include <ArduinoJson.h>
#include <Settings.h>

#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <cont.h>
#elif ESP32

#include <WiFi.h>
#include <SPIFFS.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

#endif

#include <ProjectFS.h>

#ifdef ESP8266
extern "C" {
#include <cont.h>
extern cont_t* g_pcont;
};
extern cont_t* g_pcont;
}
#endif

String AboutHelper::generateAboutString(bool abbreviated) {
DynamicJsonDocument buffer(1024);
Expand All @@ -20,22 +33,40 @@ String AboutHelper::generateAboutString(bool abbreviated) {
return body;
}

void AboutHelper::generateAboutObject(JsonDocument& obj, bool abbreviated) {
void AboutHelper::generateAboutObject(JsonDocument &obj, bool abbreviated) {
obj[FPSTR("firmware")] = QUOTE(FIRMWARE_NAME);
obj[FPSTR("version")] = QUOTE(MILIGHT_HUB_VERSION);
obj[FPSTR("ip_address")] = WiFi.localIP().toString();
#ifdef ESP8266
obj[FPSTR("reset_reason")] = ESP.getResetReason();
#elif ESP32
obj[FPSTR("reset_reason")] = String(esp_reset_reason());
#endif

if (! abbreviated) {
if (!abbreviated) {
obj[FPSTR("variant")] = QUOTE(FIRMWARE_VARIANT);
obj[FPSTR("free_heap")] = ESP.getFreeHeap();
#ifdef ESP8266
obj[FPSTR("arduino_version")] = ESP.getCoreVersion();
obj[FPSTR("free_stack")] = cont_get_free_stack(g_pcont);
#elif ESP32
obj[FPSTR("arduino_version")] = ESP.getSdkVersion();
obj[FPSTR("free_stack")] = uxTaskGetStackHighWaterMark(nullptr);
#endif

#ifdef ESP8266
FSInfo fsInfo;
ProjectFS.info(fsInfo);
obj[FPSTR("flash_used")] = fsInfo.usedBytes;
obj[FPSTR("flash_total")] = fsInfo.totalBytes;
obj[FPSTR("flash_pct_free")] = fsInfo.totalBytes == 0 ? 0 : (fsInfo.totalBytes - fsInfo.usedBytes) * 100 / fsInfo.totalBytes;
ProjectFS.info(fsInfo);
obj[FPSTR("flash_used")] = fsInfo.usedBytes;
obj[FPSTR("flash_total")] = fsInfo.totalBytes;
obj[FPSTR("flash_pct_free")] = fsInfo.totalBytes == 0 ? 0 : (fsInfo.totalBytes - fsInfo.usedBytes) * 100 / fsInfo.totalBytes;
#elif ESP32

obj[FPSTR("flash_used")] = ProjectFS.usedBytes();
obj[FPSTR("flash_total")] = ProjectFS.totalBytes();
obj[FPSTR("flash_pct_free")] =
ProjectFS.totalBytes() == 0 ? 0 : (ProjectFS.totalBytes() - ProjectFS.usedBytes()) * 100 /
ProjectFS.totalBytes();
#endif
}
}
4 changes: 4 additions & 0 deletions lib/Settings/BackupManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
//

#include <BackupManager.h>
#ifdef ESP32
#include <SPIFFS.h>
#endif
#include <ProjectFS.h>
#include <StreamUtils.h>


const uint8_t BackupManager::SETTINGS_BACKUP_VERSION = 1;
const uint32_t BackupManager::SETTINGS_MAGIC_HEADER = 0x92A7C300 | SETTINGS_BACKUP_VERSION;

Expand Down
4 changes: 4 additions & 0 deletions lib/Settings/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include <ProjectFS.h>
#include <StreamUtils.h>

#ifdef ESP32
#include <SPIFFS.h>
#endif

#define PORT_POSITION(s) ( s.indexOf(':') )

GatewayConfig::GatewayConfig(uint16_t deviceId, uint16_t port, uint8_t protocolVersion)
Expand Down
30 changes: 27 additions & 3 deletions lib/Settings/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
#define MILIGHT_HUB_VERSION unknown
#endif

#ifdef ESP8266
#define CSN_DEFAULT_PIN 15
#elif ESP32
#define CSN_DEFAULT_PIN 5
#endif

#ifndef MILIGHT_MAX_STATE_ITEMS
#define MILIGHT_MAX_STATE_ITEMS 100
#endif
Expand Down Expand Up @@ -146,7 +152,7 @@ class Settings {
adminPassword(""),
// CE and CSN pins from nrf24l01
cePin(4),
csnPin(15),
csnPin(CSN_DEFAULT_PIN),
resetPin(0),
ledPin(-2),
radioInterfaceType(nRF24),
Expand Down Expand Up @@ -273,8 +279,10 @@ class Settings {
JsonVariant val = obj[key];

// For booleans, parse string/int
if constexpr (std::is_same_v<bool, T>) {
if (val.is<bool>()) {

#ifdef ESP8266
if (std::is_same_v<bool, T>) {
if (val.is<bool>()) {
var = val.as<bool>();
} else if (val.is<const char*>()) {
var = strcmp(val.as<const char*>(), "true") == 0;
Expand All @@ -286,6 +294,22 @@ class Settings {
} else {
var = val.as<T>();
}
#elif ESP32
if (std::is_same<bool, T>::value) {
if (val.is<bool>()) {
var = val.as<bool>();
} else if (val.is<const char*>()) {
var = strcmp(val.as<const char*>(), "true") == 0;
} else if (val.is<int>()) {
var = val.as<int>() == 1;
} else {
var = false;
}
} else {
var = val.as<T>();
}
#endif

}
}
};
Expand Down
2 changes: 1 addition & 1 deletion lib/Types/GroupAlias.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void GroupAlias::loadAliases(Stream &stream, std::map<String, GroupAlias> &alias
void GroupAlias::saveAliases(Stream &stream, const std::map<String, GroupAlias> &aliases) {
// Write number of aliases
stream.print(aliases.size());
stream.write(0);
stream.write((uint8_t)0);

Serial.printf_P(PSTR("Saving %d aliases\n"), aliases.size());

Expand Down
12 changes: 10 additions & 2 deletions lib/Udp/MiLightDiscoveryServer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include <MiLightDiscoveryServer.h>
#include <Size.h>
#include <ESP8266WiFi.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#elif ESP32
#include <WiFi.h>
#endif

const char V3_SEARCH_STRING[] = "Link_Wi-Fi";
const char V6_SEARCH_STRING[] = "HF-A11ASSISTHREAD";
Expand Down Expand Up @@ -85,6 +89,10 @@ void MiLightDiscoveryServer::sendResponse(char* buffer) {
#endif

socket.beginPacket(socket.remoteIP(), socket.remotePort());
socket.write(buffer);
#ifdef ESP8266
socket.write(buffer);
#elif ESP32
socket.write(*buffer);
#endif
socket.endPacket();
}
6 changes: 5 additions & 1 deletion lib/Udp/MiLightUdpServer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include <MiLightUdpServer.h>
#include <V5MiLightUdpServer.h>
#include <V6MiLightUdpServer.h>
#include <ESP8266WiFi.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#elif ESP32
#include <WiFi.h>
#endif

MiLightUdpServer::MiLightUdpServer(MiLightClient*& client, uint16_t port, uint16_t deviceId)
: client(client),
Expand Down
Loading

0 comments on commit 1aba026

Please sign in to comment.