carsonmcdonald / esp32-aws-iot

ESP32 template for connecting to the AWS IoT platform
Apache License 2.0
60 stars 21 forks source link

Bluefi + AWS IoT #2

Closed seopyoon closed 7 years ago

seopyoon commented 7 years ago

The scenario is that the user would provide WiFi connection information via BLE connection in the beginning. So I started off from the Bluefi example provided in the esp-idf/examples folder.

AWS IoT project from this works perfectly well, and the Bluefi example also works without any problem. But when merged together, I get error connecting to AWS IoT.

The error looks as follows:


DEBUG:   iot_tls_connect L#129 
  . Seeding the random number generator...
DEBUG:   iot_tls_connect L#137   . Loading the CA root certificate ...
DEBUG:   iot_tls_connect L#143  ok (0 skipped)

DEBUG:   iot_tls_connect L#145   . Loading the client cert. and key...
DEBUG:   iot_tls_connect L#158  ok

DEBUG:   iot_tls_connect L#160   . Connecting to (my_address).iot.ap-northeast-2.amazonaws.com/8883...
DEBUG:   iot_tls_connect L#179  ok

DEBUG:   iot_tls_connect L#181   . Setting up the SSL/TLS structure...
ERROR: iot_tls_connect L#206  failed
  ! mbedtls_ssl_setup returned -0x7f00

ERROR: aws_iot_mqtt_task L#221 Error connecting to (my_address).iot.ap-northeast-2.amazonaws.com:8883, err = -4

When looking at the source code, mbedtls_ssl_setup returning 0x7f00 means 'memory allocation failed'. (link)

The Bluefi example, also utilises the use of compression while making. To match the original esp32-aws-iot project, I disabled it, for I thought that may cause problem.

Please help me resolve issue. I am thinking, perhaps, it is related to memory overwriting? Since using both Bluetooth and WiFi requires too large of a stack size? Thanks a lot in advance.

carsonmcdonald commented 7 years ago

@seopyoon I'm not sure but it would help if I could understand how to get the Bluefi APK that is referenced in the docs so I can give it a try. Do you know where that is?

seopyoon commented 7 years ago

@carsonmcdonald I wasnt able to find the apk, so I had to look very deeply at the code. Basically you could use any of the ble apps out there and write to the device in the following manner.

ssid:your-ap-ssid
passwd:your-password
confirm

In three separate packets as a string. Then, it should connect to the AP. I personally used nRF connect. Made by nordic semiconductors. https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp

To add, I am using the DevC board made by Espressif.

carsonmcdonald commented 7 years ago

@seopyoon I finally got the time to look at this more and was able to reproduce the issue. After some hacking around I believe it may be related to the build config, I need to look a little more at why that would be exactly but hopefully it makes enough sense that I can git it completely fixed. If you want to try and replace the sdkconfig file with the one for blufi that may fix it for you. This has made me think about a non-blufi example as well so I may not integrate blufi but something similar so it can get both the wifi config and maybe provision the certificate as well.

seopyoon commented 7 years ago

@carsonmcdonald I am not sure if I am understanding correctly. 1) you were able to reproduce the issue 2) you were able to make it work 2-1) if I replace sdkconfig from the blufi example with your esp32-aws-iot, it works.

It would be nice to somehow integrate this library so when not provided with wifi config info, one can set it up dynamically.

Could you also be more specific in how I could get both blufi and aws iot to work together? Thank you very very much.

carsonmcdonald commented 7 years ago

@seopyoon I will work on getting an example together, I'm also interested in this and just haven't had the time to look into it. There may be more to my fix than just the config since I changed a good bit while debugging but ultimately the thing that fixed it for me was using the sdkconfig from the blufi example. My plan is to back out all the changes I made up to that point but I ran out of time last night to do that. After I figure out exactly what the issue was I want to create an example of setting up the configuration via btle but I'm not sure that I will use blufi since I don't know that much about the client side and it seems specific to a product.

seopyoon commented 7 years ago

@carsonmcdonald Ah ha, I hope you figure out what exactly is causing problem. I wish you luck.

Though you stated that using sdkconfig from blufi fixed the problem, I think otherwise. I tried starting off from esp-aws-iot and adding blufi stuff, and I have tried the otherway round too, starting from blufi, and then adding esp-aws-iot. And neither worked when I tried. So, I speculate it may have been fixed from some other reasons. Again, thank you lots for spending your time in fixing this; I shall give it a try again sometime tomorrow when I have time. The part that got me lost is when it does transfer_context stuff. No idea what that part does. (I am referring to blufi_task.c)

This may be off-topic, but do you know how large the system gets when we enable both bluetooth stack AND wifi stack? Does it leave us with only a little bit for application? Or the size is minimal and I should not be worried about it.

seopyoon commented 7 years ago

Happy new year!

Any update on this??

seopyoon commented 7 years ago

blufi seems to have been updated with many changes. Will try with that first, and the re-open the case if something isn't working.

seopyoon commented 7 years ago

Hello,

There has been some updates to the Blufi example on esp32. I have tested it working by itself. You can get the apk from here.

With the above application, one can send WiFi information via BLE, and once received, the chip can connect and get IP successfully.

But, when compiled with AWS IoT port for the chip, it gets the following error:

Connected to AP
AWS IoT SDK Version 2.1.1-

Connecting...
DEBUG:   iot_tls_connect L#129 
  . Seeding the random number generator...
DEBUG:   iot_tls_connect L#137   . Loading the CA root certificate ...
DEBUG:   iot_tls_connect L#143  ok (0 skipped)

DEBUG:   iot_tls_connect L#145   . Loading the client cert. and key...
DEBUG:   iot_tls_connect L#158  ok

DEBUG:   iot_tls_connect L#160   . Connecting to ~~~~.iot.ap-northeast-2.amazonaws.com/8883...
DEBUG:   iot_tls_connect L#179  ok

DEBUG:   iot_tls_connect L#181   . Setting up the SSL/TLS structure...
ERROR: iot_tls_connect L#206  failed
  ! mbedtls_ssl_setup returned -0x7f00

ERROR: aws_iot_mqtt_task L#529 Error(-4) connecting to ~~~~.iot.ap-northeast-2.amazonaws.com:8883
abort() was called at PC 0x40106e33
Guru Meditation Error: Core  0 panic'ed (abort)

Backtrace: 0x40178d52:0x3fff8b70 0x400826bc:0x3fff8b90

Rebooting...

The error number seems to be the same as before, when I initially posted the issue.

@carsonmcdonald can you reproduce the problem?? And possibly find out what may be causing the issue? Thanks a lot in advance.

seopyoon commented 7 years ago

I have tested https_request example and blufi together. It throws an error with a different number, but seems to be of similar reason. Created an issue here.

seopyoon commented 7 years ago

I have found a solution. If mbedTLS and BLE does not happen at the same time, the stack is large enough.

So, you can turn on BLE AFTER mbedTLS, it works.