Skip to content

Commit 89583fd

Browse files
committed
v1.1.0
1 parent 93396b9 commit 89583fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+7551
-214
lines changed

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Enjoy the "one-key" build, "one-key" upload, and work to link various other Ardu
1212

1313
Here is a [discussion](https://github.com/HomeACcessoryKid/Arduino-HomeKit/issues/1) about the RTOS is required for running Apple HomeKit, and this project is a proof of concept that Apple HomeKit can be implemented and work fine without the RTOS.
1414

15+
This library is built with ESP8266 Arduino Core 2.6.3. Lower versions may compile with errors.
16+
1517
## Preview
1618

1719
![Preview](https://raw.github.com/Mixiaoxiao/Arduino-HomeKit-ESP8266/master/extras/preview.jpg)
@@ -74,11 +76,19 @@ I tried to make WolfSSL crypto work safely on ESP8266 with better performance an
7476

7577
Here are the free heap values of running the example sketch:
7678

77-
* Boot: ~26000
78-
* Preinit over: ~22000
79-
* Pairing: ~17000 (or even low when crypto computing)
80-
* Paired and connected with one iOS device: ~21700
81-
* Paired and no iOS device connected: ~23400
79+
* ~~Boot: ~26000~~
80+
* ~~Preinit over: ~22000~~
81+
* ~~Pairing: ~17000 (or even low when crypto computing)~~
82+
* ~~Paired and connected with one iOS device: ~21700~~
83+
* ~~Paired and no iOS device connected: ~23400~~
84+
85+
After memory optimization in v1.1.0:
86+
87+
* Boot: ~46000
88+
* Preinit over: ~41000
89+
* Pairing: ~37000 (or even low when crypto computing)
90+
* Paired and connected with one iOS device: ~41700
91+
* Paired and no iOS device connected: ~43000
8292

8393

8494
## WolfSSL
@@ -138,6 +148,10 @@ Here are the free heap values of running the example sketch:
138148

139149
## Change Log
140150

151+
#### v1.1.0
152+
* Memory optimization: moved String/byte constants as much as possible to Flash. The `RODATA` section of `bin` is only 4672. Extra ~20K free-heap is available compared with v1.0.1.
153+
* Upload a [`nossl` and `noleak` version](https://raw.github.com/Mixiaoxiao/Arduino-HomeKit-ESP8266/master/extras/ESP8266WiFi_nossl_noleak) of the official `ESP8266WiFi` library of Arduino Core 2.6.3. Removed all codes of `SSL` to save memory (extra ~3K) since the HomeKit does not require SSL. Fix the memory-leak in `WiFiClinet.stop()` by adding `tcp_abandon(_pcb, 0)` in `stop()`, based on the idea of [esp8266/Arduino/pull/2767](https://github.com/esp8266/Arduino/pull/2767).
154+
141155
#### v1.0.1
142156
* Reduce `winsize` from `3` to `2`(same performance) to lower the heap required. Pairing can be done with low free-heap of ~14000.
143157
* Specify the MDNS runs on the IPAddress of STA to ensure the HomeKit can work with some SoftAP-based WiFi-Config libraries.

examples/simple_led/simple_led.ino

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
#include "ButtonDebounce.h"
2020
#include "ButtonHandler.h"
2121

22-
#define PL(s) Serial.println(s)
23-
#define P(s) Serial.print(s)
24-
2522
//D0 16 //led
2623
//D3 0 //flash button
2724
//D4 2 //led
@@ -31,6 +28,8 @@
3128
const char *ssid = "your-ssid";
3229
const char *password = "your-password";
3330

31+
#define SIMPLE_INFO(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__);
32+
3433
void blink_led(int interval, int count) {
3534
for (int i = 0; i < count; i++) {
3635
builtinledSetStatus(true);
@@ -52,27 +51,26 @@ void setup() {
5251
WiFi.setAutoReconnect(true);
5352
WiFi.begin(ssid, password);
5453

55-
printf("\n");
56-
printf("SketchSize: %d B\n", ESP.getSketchSize());
57-
printf("FreeSketchSpace: %d B\n", ESP.getFreeSketchSpace());
58-
printf("FlashChipSize: %d B\n", ESP.getFlashChipSize());
59-
printf("FlashChipRealSize: %d B\n", ESP.getFlashChipRealSize());
60-
printf("FlashChipSpeed: %d\n", ESP.getFlashChipSpeed());
61-
printf("SdkVersion: %s\n", ESP.getSdkVersion());
62-
printf("FullVersion: %s\n", ESP.getFullVersion().c_str());
63-
printf("CpuFreq: %dMHz\n", ESP.getCpuFreqMHz());
64-
printf("FreeHeap: %d B\n", ESP.getFreeHeap());
65-
printf("ResetInfo: %s\n", ESP.getResetInfo().c_str());
66-
printf("ResetReason: %s\n", ESP.getResetReason().c_str());
67-
DEBUG_HEAP();
54+
SIMPLE_INFO("");
55+
SIMPLE_INFO("SketchSize: %d", ESP.getSketchSize());
56+
SIMPLE_INFO("FreeSketchSpace: %d", ESP.getFreeSketchSpace());
57+
SIMPLE_INFO("FlashChipSize: %d", ESP.getFlashChipSize());
58+
SIMPLE_INFO("FlashChipRealSize: %d", ESP.getFlashChipRealSize());
59+
SIMPLE_INFO("FlashChipSpeed: %d", ESP.getFlashChipSpeed());
60+
SIMPLE_INFO("SdkVersion: %s", ESP.getSdkVersion());
61+
SIMPLE_INFO("FullVersion: %s", ESP.getFullVersion().c_str());
62+
SIMPLE_INFO("CpuFreq: %dMHz", ESP.getCpuFreqMHz());
63+
SIMPLE_INFO("FreeHeap: %d", ESP.getFreeHeap());
64+
SIMPLE_INFO("ResetInfo: %s", ESP.getResetInfo().c_str());
65+
SIMPLE_INFO("ResetReason: %s", ESP.getResetReason().c_str());
66+
INFO_HEAP();
6867
homekit_setup();
69-
DEBUG_HEAP();
68+
INFO_HEAP();
7069
blink_led(200, 3);
7170
}
7271

7372
void loop() {
7473
homekit_loop();
75-
delay(5);
7674
}
7775

7876
void builtinledSetStatus(bool on) {
@@ -96,55 +94,46 @@ void IRAM_ATTR btnInterrupt() {
9694
btn.update();
9795
}
9896

99-
uint32_t next_heap_millis = 0;
100-
10197
void homekit_setup() {
10298
accessory_init();
10399
uint8_t mac[WL_MAC_ADDR_LENGTH];
104100
WiFi.macAddress(mac);
105101
int name_len = snprintf(NULL, 0, "%s_%02X%02X%02X",
106102
name.value.string_value, mac[3], mac[4], mac[5]);
107-
char *name_value = (char*)malloc(name_len + 1);
103+
char *name_value = (char*) malloc(name_len + 1);
108104
snprintf(name_value, name_len + 1, "%s_%02X%02X%02X",
109105
name.value.string_value, mac[3], mac[4], mac[5]);
110106
name.value = HOMEKIT_STRING_CPP(name_value);
111107

112108
arduino_homekit_setup(&config);
113109

114-
btn.setCallback([](const bool down) {
115-
btnHandler.handleChange(down);
116-
});
110+
btn.setCallback(std::bind(&ButtonHandler::handleChange, &btnHandler,
111+
std::placeholders::_1));
117112
btn.setInterrupt(btnInterrupt);
118-
119-
btnHandler.setIsDownFunction([](void) {
120-
return btn.checkIsDown();
113+
btnHandler.setIsDownFunction(std::bind(&ButtonDebounce::checkIsDown, &btn));
114+
btnHandler.setCallback([](button_event e) {
115+
if (e == BUTTON_EVENT_SINGLECLICK) {
116+
SIMPLE_INFO("Button Event: SINGLECLICK");
117+
led_toggle();
118+
} else if (e == BUTTON_EVENT_DOUBLECLICK) {
119+
SIMPLE_INFO("Button Event: DOUBLECLICK");
120+
occupancy_toggle();
121+
} else if (e == BUTTON_EVENT_LONGCLICK) {
122+
SIMPLE_INFO("Button Event: LONGCLICK");
123+
SIMPLE_INFO("Rebooting...");
124+
homekit_storage_reset();
125+
ESP.restart(); // or system_restart();
126+
}
121127
});
122-
btnHandler.setCallback([](const button_event e) {
123-
P(F("Button Event: "));
124-
switch (e) {
125-
case BUTTON_EVENT_SINGLECLICK:
126-
PL(F("SINGLECLICK"));
127-
led_toggle();
128-
break;
129-
case BUTTON_EVENT_DOUBLECLICK:
130-
PL(F("DOUBLECLICK"));
131-
occupancy_toggle();
132-
break;
133-
case BUTTON_EVENT_LONGCLICK:
134-
PL(F("LONGCLICK"));
135-
homekit_storage_reset();
136-
system_restart();
137-
break;
138-
}
139-
} );
140128
}
141129

142130
void homekit_loop() {
143131
btnHandler.loop();
144132
arduino_homekit_loop();
133+
static uint32_t next_heap_millis = 0;
145134
uint32_t time = millis();
146135
if (time > next_heap_millis) {
147-
INFO("heap: %d, sockets: %d",
136+
SIMPLE_INFO("heap: %d, sockets: %d",
148137
ESP.getFreeHeap(), arduino_homekit_connected_clients_count());
149138
next_heap_millis = time + 5000;
150139
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
ESP8266WiFi.cpp - WiFi library for esp8266
3+
4+
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
5+
This file is part of the esp8266 core for Arduino environment.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
21+
Reworked on 28 Dec 2015 by Markus Sattler
22+
23+
*/
24+
25+
#include "ESP8266WiFi.h"
26+
27+
extern "C" {
28+
#include "c_types.h"
29+
#include "ets_sys.h"
30+
#include "os_type.h"
31+
#include "osapi.h"
32+
#include "mem.h"
33+
#include "user_interface.h"
34+
#include "smartconfig.h"
35+
#include "lwip/opt.h"
36+
#include "lwip/err.h"
37+
#include "lwip/dns.h"
38+
}
39+
40+
#include "debug.h"
41+
42+
// -----------------------------------------------------------------------------------------------------------------------
43+
// ---------------------------------------------------------- Debug ------------------------------------------------------
44+
// -----------------------------------------------------------------------------------------------------------------------
45+
46+
47+
/**
48+
* Output WiFi settings to an object derived from Print interface (like Serial).
49+
* @param p Print interface
50+
*/
51+
void ESP8266WiFiClass::printDiag(Print& p) {
52+
const char* const modes[] = { "NULL", "STA", "AP", "STA+AP" };
53+
p.print(F("Mode: "));
54+
p.println(modes[wifi_get_opmode()]);
55+
56+
const char* const phymodes[] = { "", "B", "G", "N" };
57+
p.print(F("PHY mode: "));
58+
p.println(phymodes[(int) wifi_get_phy_mode()]);
59+
60+
p.print(F("Channel: "));
61+
p.println(wifi_get_channel());
62+
63+
p.print(F("AP id: "));
64+
p.println(wifi_station_get_current_ap_id());
65+
66+
p.print(F("Status: "));
67+
p.println(wifi_station_get_connect_status());
68+
69+
p.print(F("Auto connect: "));
70+
p.println(wifi_station_get_auto_connect());
71+
72+
struct station_config conf;
73+
wifi_station_get_config(&conf);
74+
75+
char ssid[33]; //ssid can be up to 32chars, => plus null term
76+
memcpy(ssid, conf.ssid, sizeof(conf.ssid));
77+
ssid[32] = 0; //nullterm in case of 32 char ssid
78+
p.printf_P(PSTR("SSID (%d): %s\n"), strlen(ssid), ssid);
79+
80+
char passphrase[65];
81+
memcpy(passphrase, conf.password, sizeof(conf.password));
82+
passphrase[64] = 0;
83+
p.printf_P(PSTR("Passphrase (%d): %s\n"), strlen(passphrase), passphrase);
84+
85+
p.print(F("BSSID set: "));
86+
p.println(conf.bssid_set);
87+
88+
}
89+
90+
ESP8266WiFiClass WiFi;
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
ESP8266WiFi.h - esp8266 Wifi support.
3+
Based on WiFi.h from Arduino WiFi shield library.
4+
Copyright (c) 2011-2014 Arduino. All right reserved.
5+
Modified by Ivan Grokhotkov, December 2014
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
21+
22+
#ifndef WiFi_h
23+
#define WiFi_h
24+
25+
#include <stdint.h>
26+
27+
extern "C" {
28+
#include "include/wl_definitions.h"
29+
}
30+
31+
#include "IPAddress.h"
32+
33+
#include "ESP8266WiFiType.h"
34+
#include "ESP8266WiFiSTA.h"
35+
#include "ESP8266WiFiAP.h"
36+
#include "ESP8266WiFiScan.h"
37+
#include "ESP8266WiFiGeneric.h"
38+
39+
#include "WiFiClient.h"
40+
#include "WiFiServer.h"
41+
//#include "WiFiServerSecure.h"
42+
//#include "WiFiClientSecure.h"
43+
//#include "BearSSLHelpers.h"
44+
//#include "CertStoreBearSSL.h"
45+
46+
#ifdef DEBUG_ESP_WIFI
47+
#ifdef DEBUG_ESP_PORT
48+
#define DEBUG_WIFI(fmt, ...) DEBUG_ESP_PORT.printf_P( (PGM_P)PSTR(fmt), ##__VA_ARGS__ )
49+
#endif
50+
#endif
51+
52+
#ifndef DEBUG_WIFI
53+
#define DEBUG_WIFI(...) do { (void)0; } while (0)
54+
#endif
55+
56+
57+
class ESP8266WiFiClass : public ESP8266WiFiGenericClass, public ESP8266WiFiSTAClass, public ESP8266WiFiScanClass, public ESP8266WiFiAPClass {
58+
public:
59+
60+
// workaround same function name with different signature
61+
using ESP8266WiFiGenericClass::channel;
62+
63+
using ESP8266WiFiSTAClass::SSID;
64+
using ESP8266WiFiSTAClass::RSSI;
65+
using ESP8266WiFiSTAClass::BSSID;
66+
using ESP8266WiFiSTAClass::BSSIDstr;
67+
68+
using ESP8266WiFiScanClass::SSID;
69+
using ESP8266WiFiScanClass::encryptionType;
70+
using ESP8266WiFiScanClass::RSSI;
71+
using ESP8266WiFiScanClass::BSSID;
72+
using ESP8266WiFiScanClass::BSSIDstr;
73+
using ESP8266WiFiScanClass::channel;
74+
using ESP8266WiFiScanClass::isHidden;
75+
76+
// ----------------------------------------------------------------------------------------------
77+
// ------------------------------------------- Debug --------------------------------------------
78+
// ----------------------------------------------------------------------------------------------
79+
80+
public:
81+
82+
void printDiag(Print& dest);
83+
84+
friend class WiFiClient;
85+
friend class WiFiServer;
86+
87+
};
88+
89+
extern ESP8266WiFiClass WiFi;
90+
91+
#endif

0 commit comments

Comments
 (0)