Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Esp32 #725

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Esp32 #725

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# TODO's for ESP32

- Implement handleFirmwarePost() and handleFirmwareUpload() in MiLightHttpServer.cpp
- Erase config in MiLightHttpServer.cpp
- Wifi set hostname in main.cpp
- Set Wifi physical (setPhyMode) in main.cpp
- SSDP (Service discovery) support in main.cpp
- LED callback in WiFiManager loop in main.cpp
- Implement other TODO's in main.cpp
- Reset reason in AboutHelper.cpp

# esp8266_milight_hub [![Build Status](https://travis-ci.org/sidoh/esp8266_milight_hub.svg?branch=master)](https://travis-ci.org/sidoh/esp8266_milight_hub) [![License][shield-license]][info-license]

This is a replacement for a Milight/LimitlessLED remote/gateway hosted on an ESP8266. Leverages [Henryk Plötz's awesome reverse-engineering work](https://hackaday.io/project/5888-reverse-engineering-the-milight-on-air-protocol).
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
7 changes: 4 additions & 3 deletions lib/MQTT/HomeAssistantDiscoveryClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void HomeAssistantDiscoveryClient::addConfig(const char* alias, const BulbId& bu
DynamicJsonDocument config(1024);

char uniqidBuffer[30];
sprintf_P(uniqidBuffer, PSTR("%X-%s"), ESP.getChipId(), alias);
sprintf_P(uniqidBuffer, PSTR("%X-%s"), getESPId(), alias);

config[F("schema")] = F("json");
config[F("name")] = alias;
Expand All @@ -50,7 +50,8 @@ void HomeAssistantDiscoveryClient::addConfig(const char* alias, const BulbId& bu
deviceMetadata[F("sw_version")] = QUOTE(MILIGHT_HUB_VERSION);

JsonArray identifiers = deviceMetadata.createNestedArray(F("identifiers"));
identifiers.add(ESP.getChipId());
identifiers.add(getESPId());

bulbId.serialize(identifiers);

// HomeAssistant only supports simple client availability
Expand Down Expand Up @@ -142,7 +143,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 @@ -56,7 +56,8 @@ 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
4 changes: 4 additions & 0 deletions lib/MiLightState/GroupStatePersistence.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include <GroupStatePersistence.h>
#include <FS.h>

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

static const char FILE_PREFIX[] = "group_states/";

void GroupStatePersistence::get(const BulbId &id, GroupState& state) {
Expand Down
15 changes: 14 additions & 1 deletion lib/Settings/AboutHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#include <AboutHelper.h>
#include <ArduinoJson.h>
#include <Settings.h>
#include <ESP8266WiFi.h>

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

String AboutHelper::generateAboutString(bool abbreviated) {
DynamicJsonDocument buffer(1024);
Expand All @@ -18,11 +23,19 @@ void AboutHelper::generateAboutObject(JsonDocument& obj, bool abbreviated) {
obj["firmware"] = QUOTE(FIRMWARE_NAME);
obj["version"] = QUOTE(MILIGHT_HUB_VERSION);
obj["ip_address"] = WiFi.localIP().toString();
#ifdef ESP8266
obj["reset_reason"] = ESP.getResetReason();
#elif ESP32
// TODO get reset reason
#endif

if (! abbreviated) {
obj["variant"] = QUOTE(FIRMWARE_VARIANT);
obj["free_heap"] = ESP.getFreeHeap();
#ifdef ESP8266
obj["arduino_version"] = ESP.getCoreVersion();
#elif ESP32
obj["arduino_version"] = ESP.getSdkVersion();
#endif
}
}
9 changes: 7 additions & 2 deletions lib/Settings/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include <algorithm>
#include <JsonHelpers.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 Expand Up @@ -208,10 +212,11 @@ void Settings::dumpGroupIdAliases(JsonObject json) {
}

void Settings::load(Settings& settings) {

if (SPIFFS.exists(SETTINGS_FILE)) {
// Clear in-memory settings
settings = Settings();

File f = SPIFFS.open(SETTINGS_FILE, "r");

DynamicJsonDocument json(MILIGHT_HUB_SETTINGS_BUFFER_SIZE);
Expand All @@ -220,7 +225,7 @@ void Settings::load(Settings& settings) {

if (! error) {
JsonObject parsedSettings = json.as<JsonObject>();
settings.patch(parsedSettings);
settings.patch(parsedSettings);
} else {
Serial.print(F("Error parsing saved settings file: "));
Serial.println(error.c_str());
Expand Down
11 changes: 10 additions & 1 deletion lib/Udp/MiLightDiscoveryServer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#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 +90,10 @@ void MiLightDiscoveryServer::sendResponse(char* buffer) {
#endif

socket.beginPacket(socket.remoteIP(), socket.remotePort());
#ifdef ESP8266
socket.write(buffer);
#elif ESP32
socket.write(*buffer);
#endif
socket.endPacket();
}
7 changes: 6 additions & 1 deletion lib/Udp/MiLightUdpServer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#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
7 changes: 6 additions & 1 deletion lib/Udp/V6MiLightUdpServer.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#include <V6MiLightUdpServer.h>
#include <ESP8266WiFi.h>
#include <Arduino.h>
#include <Size.h>
#include <V6CommandHandler.h>

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

#define MATCHES_PACKET(packet1) ( \
matchesPacket(packet1, size(packet1), packet, packetSize) \
)
Expand Down
18 changes: 17 additions & 1 deletion lib/WebServer/MiLightHttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include <AboutHelper.h>
#include <index.html.gz.h>

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

using namespace std::placeholders;

void MiLightHttpServer::begin() {
Expand Down Expand Up @@ -101,7 +105,7 @@ WiFiClient MiLightHttpServer::client() {
return server.client();
}

void MiLightHttpServer::on(const char* path, HTTPMethod method, ESP8266WebServer::THandlerFunction handler) {
void MiLightHttpServer::on(const char* path, HTTPMethod method, THandlerFunction handler) {
server.on(path, method, handler);
}

Expand All @@ -125,7 +129,11 @@ void MiLightHttpServer::handleSystemPost(RequestContext& request) {
server.send_P(200, TEXT_PLAIN, PSTR("true"));

delay(100);
#ifdef ESP8266
ESP.eraseConfig();
#elif ESP32
// TODO erase config
#endif
delay(100);
ESP.restart();

Expand Down Expand Up @@ -225,6 +233,7 @@ void MiLightHttpServer::handleUpdateSettingsPost(RequestContext& request) {
}

void MiLightHttpServer::handleFirmwarePost() {
#ifdef ESP8266
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");

Expand All @@ -245,9 +254,13 @@ void MiLightHttpServer::handleFirmwarePost() {
delay(1000);

ESP.restart();
#elif ESP32
// TODO implement firmware post
#endif
}

void MiLightHttpServer::handleFirmwareUpload() {
#ifdef ESP8266
HTTPUpload& upload = server.upload();
if(upload.status == UPLOAD_FILE_START){
WiFiUDP::stopAll();
Expand All @@ -266,6 +279,9 @@ void MiLightHttpServer::handleFirmwareUpload() {
}
}
yield();
#elif ESP32
// TODO implement firmware upload
#endif
}


Expand Down
5 changes: 3 additions & 2 deletions lib/WebServer/MiLightHttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

typedef std::function<void(void)> SettingsSavedHandler;
typedef std::function<void(const BulbId& id)> GroupDeletedHandler;
typedef std::function<void(void)> THandlerFunction;

using RichHttpConfig = RichHttp::Generics::Configs::EspressifBuiltin;
using RequestContext = RichHttpConfig::RequestContextType;
Expand Down Expand Up @@ -47,7 +48,7 @@ class MiLightHttpServer {
void handleClient();
void onSettingsSaved(SettingsSavedHandler handler);
void onGroupDeleted(GroupDeletedHandler handler);
void on(const char* path, HTTPMethod method, ESP8266WebServer::THandlerFunction handler);
void on(const char* path, HTTPMethod method, THandlerFunction handler);
void handlePacketSent(uint8_t* packet, const MiLightRemoteConfig& config);
WiFiClient client();

Expand Down Expand Up @@ -101,7 +102,7 @@ class MiLightHttpServer {
GroupStateStore*& stateStore;
SettingsSavedHandler settingsSavedHandler;
GroupDeletedHandler groupDeletedHandler;
ESP8266WebServer::THandlerFunction _handleRootPage;
THandlerFunction _handleRootPage;
PacketSender*& packetSender;
RadioSwitchboard*& radios;
TransitionController& transitions;
Expand Down
Loading