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

Esp8266 (Arduino) support #30

Open
Gabeki17 opened this issue Jun 21, 2020 · 15 comments
Open

Esp8266 (Arduino) support #30

Gabeki17 opened this issue Jun 21, 2020 · 15 comments

Comments

@Gabeki17
Copy link

Hello All,

I am having a bunch of esp8266 room thermostats in my flat.
I wuld also like to make them controlling my airconditionig system.

I did a quick try on this already, but to be honest, I am not experienced enough on encrypting.

Are you able to help me doing this?

Thank you very much in advance!

All the best,
Gábor

@Gabeki17 Gabeki17 changed the title Esp8266 (Arduno) support Esp8266 (Arduino) support Jun 21, 2020
@jllcunha
Copy link

jllcunha commented Jun 21, 2020 via email

@Gabeki17
Copy link
Author

Gabeki17 commented Jun 21, 2020

Thx!

I think I have found your suggestion:

https://github.com/jllcunha/openhab-greeair-binding

I feal still quite far away, but this might helps a little! :)

All the best,
Gábor

For this kind of project, perhaps Openhab would be a good option? There is a binding for gree air conditioners On 21 Jun 2020, at 19:15, Gabeki17 [email protected] wrote:  Hello All, I am having a bunch of esp8266 room thermostats in my flat. I wuld also like to make them controlling my airconditionig system. I did a quick try on this already, but to be honest, I am not experienced enough on encrypting. Are you able to help me doing this? Thank you very much in advance! All the best, Gábor — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub<#30>, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AIAM45OYS4SJWQCZE7ZTPGTRXY54TANCNFSM4OD6IGVQ.

@tomikaa87
Copy link
Owner

tomikaa87 commented Jun 21, 2020

Hello All,

I am having a bunch of esp8266 room thermostats in my flat.
I wuld also like to make them controlling my airconditionig system.

I did a quick try on this already, but to be honest, I am not experienced enough on encrypting.

Are you able to help me doing this?

Thank you very much in advance!

All the best,
Gábor

Hi Gabor,

If you want to control the AC directly from the ESP, I suggest using this library for the AES encryption: https://github.com/kakopappa/arduino-esp8266-aes-lib

Unfortunately, at the moment I don't have a lightweight C++ protocol library that can run on the ESP (the current implementation uses Qt which is too heavy for this controller).

Since the protocol doesn't need anything other than an encryption library and a JSON library (I prefer https://github.com/nlohmann/json which is header-only and lightweight), maybe you can try to write a simple program that communicates with the AC.

I have plans to simplify the current C++ implementation to be able to compile it on the ESP, but I don't have any ETA.

@jllcunha
Copy link

jllcunha commented Jun 21, 2020 via email

@Gabeki17
Copy link
Author

Hello All,
I am having a bunch of esp8266 room thermostats in my flat.
I wuld also like to make them controlling my airconditionig system.
I did a quick try on this already, but to be honest, I am not experienced enough on encrypting.
Are you able to help me doing this?
Thank you very much in advance!
All the best,
Gábor

Hi Gabor,

If you want to control the AC directly from the ESP, I suggest using this library for the AES encryption: https://github.com/kakopappa/arduino-esp8266-aes-lib

Unfortunately, at the moment I don't have a lightweight C++ protocol library that can run on the ESP (the current implementation uses Qt which is too heavy for this controller).

Since the protocol doesn't need anything other than an encryption library and a JSON library (I prefer https://github.com/nlohmann/json which is header-only and lightweight), maybe you can try to write a simple program that communicates with the AC.

I have plans to simplify the current C++ implementation to be able to compile it on the ESP, but I don't have any ETA.

Thank you!

I will give it a try soon!

So for example I just encryt the scan text:

{
"t": "scan"
}

and sending it as udp broadcast on port 7000?

@tomikaa87
Copy link
Owner

tomikaa87 commented Jun 21, 2020

So for example I just encryt the scan text:

{
"t": "scan"
}

and sending it as udp broadcast on port 7000?

The scan packet must not be encrypted, but must be sent in plain text. The answer from the device(s) will contain the encrypted response which can be decoded and decrypted with the generic encryption key.

If you want to send an encrypted packet to the device, the encryption should be done in this way:

  1. Create the JSON data you want to send
  2. Pad the data using PKCS#7 method
  3. Encrypt the data with the device-specific encryption key
  4. Encode the ciphertext into base-64

For an easy to read implementation, you can check the python CLI: https://github.com/tomikaa87/gree-remote/blob/master/PythonCLI/gree.py

PKCS-7 padding: https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS#5_and_PKCS#7

Padding example from the C++ library:

void addPKCS7Padding(QByteArray& packBase64)
{
    auto length = 16 - (packBase64.length() % 16);
    packBase64.append(QByteArray(length, length));
}

@Gabeki17
Copy link
Author

Gabeki17 commented Jun 25, 2020

So for example I just encryt the scan text:
{
"t": "scan"
}
and sending it as udp broadcast on port 7000?

The scan packet must not be encrypted, but must be sent in plain text. The answer from the device(s) will contain the encrypted response which can be decoded and decrypted with the generic encryption key.

If you want to send an encrypted packet to the device, the encryption should be done in this way:

  1. Create the JSON data you want to send
  2. Pad the data using PKCS#7 method
  3. Encrypt the data with the device-specific encryption key
  4. Encode the ciphertext into base-64

For an easy to read implementation, you can check the python CLI: https://github.com/tomikaa87/gree-remote/blob/master/PythonCLI/gree.py

PKCS-7 padding: https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS#5_and_PKCS#7

Padding example from the C++ library:

void addPKCS7Padding(QByteArray& packBase64)
{
    auto length = 16 - (packBase64.length() % 16);
    packBase64.append(QByteArray(length, length));
}

Thank you,

UDP and json part was easy. My airconditioner responds nice.

AES is a pain for me.

I have tried the suggested lib. (Dont understand how it works exactly. I think taht is the main issue.)

I am not getting the same resoult as I should.

Referrence tool I have used: https://www.devglan.com/online-tools/aes-encryption-decryption

Test code:
`#include <AESLib.h>

AESLib aesLib;

void setup() {
Serial.begin(115200);
Serial.println("\nBooting...");

String AES_Key = "a3K8Bx%2r8Y7#xDh";
byte key[AES_Key.length()];
AES_Key.getBytes(key, AES_Key.length()+1);
// byte key[] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };

// put your setup code here, to run once:
byte my_iv[N_BLOCK] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
// aesLib.gen_iv(my_iv);

Serial.println("AES Key: ");
int i;
for (i = 0; i < sizeof(my_iv); i++)
{
// my_iv[i] = key[i];
Serial.print(my_iv[i], DEC);
Serial.print(",");
}
Serial.println("");

for (i = 0; i < sizeof(key); i++)
{
Serial.print(key[i], DEC);
Serial.print(",");
}
Serial.println("");

String msg = "i hate dogs";

unsigned long ms = micros ();
String encMsg = aesLib.encrypt(msg, key, my_iv);
Serial.print("Encryption took: ");
Serial.println(micros() - ms);
Serial.println("encMsg: " + encMsg);

///aesLib.decrypt(encMsg, key, my_iv);
}

void loop() {
// put your main code here, to run repeatedly:

}`

@Gabeki17
Copy link
Author

Gabeki17 commented Jun 27, 2020

I think I will not be able to fix this myself within reasonable time and without learning for days. I might hand over the encryption part to my RPI as a workaround for now (simply throwing over the messages and receiving back the encripted or decripted text) and when done I can still do the brainstorming on this.

I really apprisiate all your help on this!

Thank you all! :)

@Gabeki17
Copy link
Author

The Raspberry PI as encrypter and decrypter server is funcioning already. Works perfect! The rest should not be hard. :)

@Gabeki17
Copy link
Author

Gabeki17 commented Jul 3, 2020

All is working now!

Thank you very much!

All the best,
Gábor

@tomikaa87
Copy link
Owner

@Gabeki17

Hi Gábor,

Thanks for the info. At the moment I have some time to test the encryption on the ESP and implement a minimal working program based on your code.

Regards,
Tamás

@tomikaa87
Copy link
Owner

tomikaa87 commented Jul 5, 2020

@Gabeki17

I've began implementing the ESP8266 version, you can find the sources here: https://github.com/tomikaa87/gree-remote/tree/master/GreeESP8266

Unfortunately it can't bind to a device, yet. I'm working on it.
Encryption and decryption works fine (I've checked the generated payloads, they match with PythonCLI), but for some strange reason, after sending the bind request, I don't get an answer on the socket. It's probably some trivial issue I can't see right now.

The project uses Platform.io and it's not a regular Arduino sketch. You can easily open it via Visual Studio Code. Install the Platform.io extension. After that you must install the ESP8266 framework by following these steps:

  1. on the Platform.io Home page, select Platforms
  2. go to the Embedded tab
  3. type "espressif 8266" in the search bar
  4. click on the search result
  5. click on the Install button

@Gabeki17
Copy link
Author

Gabeki17 commented Aug 12, 2020

@Gabeki17

I've began implementing the ESP8266 version, you can find the sources here: https://github.com/tomikaa87/gree-remote/tree/master/GreeESP8266

Unfortunately it can't bind to a device, yet. I'm working on it.
Encryption and decryption works fine (I've checked the generated payloads, they match with PythonCLI), but for some strange reason, after sending the bind request, I don't get an answer on the socket. It's probably some trivial issue I can't see right now.

The project uses Platform.io and it's not a regular Arduino sketch. You can easily open it via Visual Studio Code. Install the Platform.io extension. After that you must install the ESP8266 framework by following these steps:

  1. on the Platform.io Home page, select Platforms
  2. go to the Embedded tab
  3. type "espressif 8266" in the search bar
  4. click on the search result
  5. click on the Install button

Cool Thx!

Unfortunately my thermostates are written in Arduino (Thousands of lines.)
So I am still using my RPI as an encription/decription server.

Also I was thinking, as I already use my RPI why not try to make use of your Python script.
On the Pi I am having errors wit that. So Still my original way.... :)

Also was to mention that I am really enjoying your responsiveness!
Thank you!

All the best,
Gábor

@Gabeki17
Copy link
Author

Hi,

Long time since.

As I did not find any working AES ECB 128 PKCS capable library, I have started to make my own.
It is now ready.
Not yet a library, but it will be soon.

Also the Base64 libs I have found, on github/internet, failed on the Gree generated responses some times.

Because of this bad experience, I have also coded Base64.
This I have just changed to a library.

Let me know if you are interested.

Afer all this, the road is cleared for an Arduinio ide based ESP8266 controler.
In my case my already present room thermostats. :)

All the best,
Gábor

@tomikaa87
Copy link
Owner

@Gabeki17

That's great news! If you happen to have a repository with the working code, I can link it to the main documentation, or if you want, you can create a PR and I'll merge your solution into this repository (in that case, you should add your author info to the header of the source files).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants