copercini / esp8266-aws_iot

Some examples using x.509 certificates and TLSv1.2 under Arduino IDE
117 stars 50 forks source link

Stopped working with AWS MQTT #3

Closed waghekapil closed 6 years ago

waghekapil commented 7 years ago

Hello,

It was working fine till last week. Today I found that same code is stopped working. Here is the serial monitor text:

I checked everything at AWS portal and in my code and certificated but no luck.

Please help me to solve this issue.

`Connecting to SIPLEC-IOT
scandone
....scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt 

connected with SIPLEC-IOT, channel 1
dhcp client start...
..............ip:192.168.100.3,mask:255.255.255.0,gw:192.168.100.2
.
WiFi connected
IP address: 
192.168.100.3
Heap: 37168
Success to open cert file
cert loaded
Success to open private cert file
private key loaded
Success to open ca
Error: Invalid X509 ASN.1 file (X509 not ok)
ca loaded
Heap: 30288
Attempting MQTT connection...please start sntp first !
failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...please start sntp first !
failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...please start sntp first !
failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...please start sntp first !
failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...please start sntp first !
failed, rc=-2 try again in 5 seconds.................................................
`

Full code is here. Certificates were converted to DER and uploaded to used through arduino-esp8266fs-plugin.

//This example needs https://github.com/esp8266/arduino-esp8266fs-plugin

#include "FS.h"
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "SIPLEC-IOT";
const char* password = "@w0rld";

const char* mqtt_server = "xxxxxxxxxxxx.iot.ap-southeast-1.amazonaws.com"; //MQTT broker ip 

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

}
WiFiClientSecure espClient;
PubSubClient client(mqtt_server,8883,callback,espClient); //set  MQTT port number to 8883 as per //standard
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("coisa")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {

  Serial.begin(115200);
  Serial.setDebugOutput(true);
  setup_wifi();
  delay(1000);
  if (!SPIFFS.begin()) {
    Serial.println("Failed to mount file system");
    return;
  }

Serial.print("Heap: "); Serial.println(ESP.getFreeHeap());

// Load certificate file
File cert = SPIFFS.open("/cert.der", "r"); //replace cert.crt eith your uploaded file name
  if (!cert) {
    Serial.println("Failed to open cert file");
  }
  else
  Serial.println("Success to open cert file");

  delay(1000);

if(espClient.loadCertificate(cert))
  Serial.println("cert loaded");
else
  Serial.println("cert not loaded");

// Load private key file
File private_key = SPIFFS.open("/private.der", "r"); //replace private eith your uploaded file name
  if (!private_key) {
    Serial.println("Failed to open private cert file");
  }
  else
  Serial.println("Success to open private cert file");

  delay(1000);

if(espClient.loadPrivateKey(private_key))
  Serial.println("private key loaded");
else
  Serial.println("private key not loaded");

// Load CA file
File ca = SPIFFS.open("/ca.der", "r"); //replace ca eith your uploaded file name
  if (!ca) {
    Serial.println("Failed to open ca ");
  }
  else
  Serial.println("Success to open ca");

  delay(1000);

if(espClient.loadCACert(ca, sizeof(ca)))
  Serial.println("ca loaded");
else
  Serial.println("ca failed");

Serial.print("Heap: "); Serial.println(ESP.getFreeHeap());

 // client.setServer(mqtt_server, 8883);
 // client.setCallback(callback);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 10000) {
    lastMsg = now;
    ++value;
    snprintf (msg, 75, "hello world #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("outTopic", msg);
  }
}
copercini commented 7 years ago

My ESP8266 is in use for a project, but I bought a new one and in some days will test these things and update the repository with more clean instructions

waghekapil commented 7 years ago

@copercini thanks for your reply. I'm working on client project and tomorrow is the delivery date. As the library stopped working, I'm not able to deliver it on time. If you can suggest me something to look into then it would be helpful for me. Meanwhile I found another library esp8266-awsiot from @JimmyTai. This library is also throwing error with my code (I have large code file) while connecting to the AWS. Now you can only save me. :)

copercini commented 7 years ago

Success to open ca Error: Invalid X509 ASN.1 file (X509 not ok) ca loaded

Did you changed something in your CA file? Anyway, try erase it and convert/upload again

waghekapil commented 7 years ago

No. I have not done any changes. Already tried the erase and reupload. But no luck. Also, I was getting this error message from the starting and it working with this.

copercini commented 7 years ago

So enable debug level: Core + SSL + TLS mem and Debug port: Serial on Tools menu of Arduino IDE

waghekapil commented 7 years ago

Due to JimmyTai's library I changed my Arduino configuration. I'll change it again and send it to you by Monday. If you get any esp to test then please try.

With JimmyTai's library and your code (there is some little changes like no need to upload CA certificate etc) I'm getting following on serial monitor.

state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt 
state: 5 -> 0 (0)
rm 0
del if0
mode : softAP(a2:20:a6:1b:61:f2)

AP IP address: 192.168.1.81
SPIFFSImpl: allocating 512+180+1400=2092 bytes
SPIFFSImpl: mounting fs @300000, size=fb000, block=2000, page=100
SPIFFSImpl: mount rc=0
SD card initialization done.
SPIFFS_close: fd=1
SPIFFS_close: fd=1
Saved SSID : SIPLEC-IOT
Saved Password : Sipl@w0rld
SIPLEC-IOT
Sipl@w0rld
WiFiMulti Added.
SPIFFS_close: fd=1
SPIFFS_close: fd=1
Saved SSID : 06519942-652e-11e7-907b-a
Saved Password : controllex/pub/06519942-652e-11e7-907b-a
Saved Password : controllex/sub/06519942-652e-11e7-907b-a
Connecting Wifi...
HTTP server(local) started
mode : sta(a0:20:a6:1b:61:f2) + softAP(a2:20:a6:1b:61:f2)
add if0
f r0, .Heap: 24952
Success to open cert file
cert loaded
scandone
Success to open private cert file
private key loaded
Heap: 22592
SPIFFS_close: fd=2
SPIFFS_close: fd=1
Attempting MQTT connection...failed, rc=0 try again in 5 seconds
Attempting MQTT connection...failed, rc=0 try again in 5 seconds
Attempting MQTT connection...failed, rc=0 try again in 5 seconds
Attempting MQTT connection...failed, rc=0 try again in 5 seconds
Attempting MQTT connection...failed, rc=0 try again in 5 seconds
Attempting MQTT connection...failed, rc=0 try again in 5 seconds
Attempting MQTT connection...failed, rc=0 try again in 5 seconds
Attempting MQTT connection...failed, rc=0 try again in 5 seconds
Attempting MQTT connection...failed, rc=0 try again in 5 seconds
Attempting MQTT connection...failed, rc=0 try again in 5 seconds
Attempting MQTT connection...failed, rc=0 try again in 5 seconds
Attempting MQTT connection...failed, rc=0 try again in 5 seconds
Attempting MQTT connection...failed, rc=0 try again in 5 seconds
Attempting MQTT connection...failed, rc=0 try again in 5 seconds
Attempting MQTT connection...failed, rc=0 try again in 5 seconds
Attempting MQTT connection...failed, rc=0 try again in 5 seconds

Tomorrow I'll try same on fresh system and let you know the result.

Thanks

copercini commented 7 years ago

Heap: 22592

Probably you are running out of the heap.

Try just the example, without the rest of code.

waghekapil commented 7 years ago

Hello,

I deleted all the version and did a fresh install. Here is the output after enable debug level: Core + SSL + TLS mem It is still not connecting.

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
vf6d232f1
~ld

Connecting to SIPLEC-IOT
sta config unchangedscandone
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 4
cnt 

connected with SIPLEC-IOT, channel 1
dhcp client start...
..............ip:192.168.100.4,mask:255.255.255.0,gw:192.168.100.2
.
WiFi connected
IP address: 
192.168.100.4
SPIFFSImpl: allocating 512+180+1400=2092 bytes
SPIFFSImpl: mounting fs @100000, size=2fb000, block=2000, page=100
SPIFFSImpl: mount rc=0
Heap: 33856
Success to open cert file
cert loaded
Success to open private cert file
private key loaded
Success to open ca
ca loaded
Heap: 22824
SPIFFS_close: fd=3
SPIFFS_close: fd=2
SPIFFS_close: fd=1
Attempting MQTT connection...:ref 1
please start sntp first !
State:  sending Client Hello (1)
:wr 134 134 0
:wrc 134 134 0
:sent 134
:rn 1360
:rd 5, 1360, 0
:rdi 1360, 5
:rd 1355, 1360, 5
:rdi 1355, 1355
:c0 1355, 1360
:rn 1360
:rch 1360, 332
:rd 1692, 1692, 0
:rdi 1360, 1360
:c 1360, 1360, 1692
:rdi 332, 332
:c0 332, 332
State:  receiving Server Hello (2)
State:  receiving Certificate (11)
State:  receiving Certificate Request (13)
State:  receiving Server Hello Done (14)
State:  sending Certificate (11)
:wr 876 876 0
:wrc 256 876 0
:wrc 256 620 0
:wrc 256 364 0
:wrc 108 108 0
:sent 876
State:  sending Client Key Exchange (16)
:wr 267 267 0
:wrc 256 267 0
:wrc 11 11 0
:sent 267
State:  sending Certificate Verify (15)
:wr 269 269 0
:wrc 256 269 0
:wrc 13 13 0
:wr 6 6 0
:wrc 6 6 0
State:  sending Finished (16)
:wr 85 85 0
:wrc 85 85 0
:wr 69 69 0
:wrc 69 69 0
Alert: close notify
failed, rc=-2 try again in 5 seconds
:sent 269
:sent 160
:rn 160
Attempting MQTT connection...:ur 1
:close
:del
:ref 1
please start sntp first !
State:  sending Client Hello (1)
:wr 134 134 0
:wrc 134 134 0
:sent 134
:rn 1360
:rch 1360, 1360
:rch 2720, 332
:rd 5, 3052, 0
:rdi 1360, 5
:rd 3047, 3052, 5
:rdi 1355, 1355
:c 1355, 1360, 3052
:rdi 1360, 1360
:c 1360, 1360, 1692
:rdi 332, 332
:c0 332, 332
State:  receiving Server Hello (2)
State:  receiving Certificate (11)
State:  receiving Certificate Request (13)
State:  receiving Server Hello Done (14)
State:  sending Certificate (11)
:wr 12 12 0
:wrc 12 12 0
:sent 12
:rn 7
:rcl
:abort
State:  sending Client Key Exchange (16)
Alert: close notify
failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...:ur 1
:del
:ref 1
please start sntp first !
State:  sending Client Hello (1)
:wr 134 134 0
:wrc 134 134 0
:sent 134
:rn 1360
:rd 5, 1360, 0
:rdi 1360, 5
:rd 1355, 1360, 5
:rdi 1355, 1355
:c0 1355, 1360
:rn 1360
:rd 1360, 1360, 0
:rdi 1360, 1360
:c0 1360, 1360
:rn 332
:rd 332, 332, 0
:rdi 332, 332
:c0 332, 332
State:  receiving Server Hello (2)
State:  receiving Certificate (11)
State:  receiving Certificate Request (13)
State:  receiving Server Hello Done (14)
State:  sending Certificate (11)
:wr 12 12 0
:wrc 12 12 0
:sent 12
:rn 7
:rcl
:abort
State:  sending Client Key Exchange (16)
Alert: close notify
failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...:ur 1
:del
:ref 1
please start sntp first !
State:  sending Client Hello (1)
:wr 134 134 0
:wrc 134 134 0
:sent 134
:rn 1360
:rd 5, 1360, 0
:rdi 1360, 5
:rd 1355, 1360, 5
:rdi 1355, 1355
:c0 1355, 1360
:rn 1360
:rch 1360, 332
:rd 1692, 1692, 0
:rdi 1360, 1360
:c 1360, 1360, 1692
:rdi 332, 332
:c0 332, 332
State:  receiving Server Hello (2)
State:  receiving Certificate (11)
State:  receiving Certificate Request (13)
State:  receiving Server Hello Done (14)
State:  sending Certificate (11)
:wr 12 12 0
:wrc 12 12 0
:sent 12
:rn 7
:rcl
:abort
State:  sending Client Key Exchange (16)
Alert: close notify
failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...:ur 1
:del
:ref 1
please start sntp first !
State:  sending Client Hello (1)
:wr 134 134 0
:wrc 134 134 0
:sent 134
:rn 1360
:rd 5, 1360, 0
:rdi 1360, 5
:rd 1355, 1360, 5
:rdi 1355, 1355
:c0 1355, 1360
:rn 1360
:rch 1360, 332
:rd 1692, 1692, 0
:rdi 1360, 1360
:c 1360, 1360, 1692
:rdi 332, 332
:c0 332, 332
State:  receiving Server Hello (2)
State:  receiving Certificate (11)
State:  receiving Certificate Request (13)
State:  receiving Server Hello Done (14)
State:  sending Certificate (11)
:wr 12 12 0
:wrc 12 12 0
:sent 12
:rn 7
:rcl
:abort
State:  sending Client Key Exchange (16)
Alert: close notify
failed, rc=-2 try again in 5 seconds
waghekapil commented 7 years ago

After sometime, I get following error in compilation. I'm not able to upload the code to the device.

Arduino: 1.8.3 (Windows 10), Board: "Generic ESP8266 Module, 80 MHz, 40MHz, DIO, 115200, 4M (3M SPIFFS), ck, Serial, Core + SSL + TLS Mem"

C:\Users\kapil\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\libraries\ESP8266WiFi\src\WiFiClientSecure.cpp: In member function 'void SSLContext::connect(ClientContext*, const char*, uint32_t)':

C:\Users\kapil\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\libraries\ESP8266WiFi\src\WiFiClientSecure.cpp:94:64: error: cannot convert 'const char*' to 'SSL_EXTENSIONS* {aka SSL_EXTENSIONS_*}' for argument '5' to 'SSL* ssl_client_new(SSL_CTX*, int, const uint8_t*, uint8_t, SSL_EXTENSIONS*)'

         _ssl = ssl_client_new(_ssl_ctx, 0, nullptr, 0, hostName);

                                                                ^

exit status 1
Error compiling for board Generic ESP8266 Module.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
waghekapil commented 7 years ago

Solved above error (my last comment) by replacing connect function with

void connect(ClientContext* ctx, const char* hostName, uint32_t timeout_ms)
    {
        SSL_EXTENSIONS* ext = ssl_ext_new();
        ssl_ext_set_host_name(ext, hostName);
        ssl_ext_set_max_fragment_size(ext, 4096);
        s_io_ctx = ctx;
        if (_ssl) {
            ssl_free(_ssl);
        }
        _ssl = ssl_client_new(_ssl_ctx, 0, nullptr, 0, ext);
        uint32_t t = millis();

        while (millis() - t < timeout_ms && ssl_handshake_status(_ssl) != SSL_OK) {
            uint8_t* data;
            int rc = ssl_read(_ssl, &data);
            if (rc < SSL_OK) {
                break;
            }
        }
    }
waghekapil commented 7 years ago

Update;

Error

Connecting to SIPLEC-IOT
sta config unchangedscandone
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 4
cnt 

connected with SIPLEC-IOT, channel 1
dhcp client start...
..............ip:192.168.100.4,mask:255.255.255.0,gw:192.168.100.2
.
WiFi connected
IP address: 
192.168.100.4
SPIFFSImpl: allocating 512+180+1400=2092 bytes
SPIFFSImpl: mounting fs @100000, size=2fb000, block=2000, page=100
SPIFFSImpl: mount rc=0
Heap: 33872
Success to open cert file
cert loaded
Success to open private cert file
private key loaded
Success to open ca
ca loaded
Heap: 23352
SPIFFS_close: fd=3
SPIFFS_close: fd=2
SPIFFS_close: fd=1
Attempting MQTT connection...:ref 1
please start sntp first !
State:  sending Client Hello (1)
:wr 134 134 0
:wrc 134 134 0
:sent 134
:rn 1360
:rd 5, 1360, 0
:rdi 1360, 5
:rd 1355, 1360, 5
:rdi 1355, 1355
:c0 1355, 1360
:rn 1360
:rch 1360, 332
:rd 1692, 1692, 0
:rdi 1360, 1360
:c 1360, 1360, 1692
:rdi 332, 332
:c0 332, 332
State:  receiving Server Hello (2)
State:  receiving Certificate (11)
State:  receiving Certificate Request (13)
State:  receiving Server Hello Done (14)
State:  sending Certificate (11)
:wr 876 876 0
:wrc 256 876 0
:wrc 256 620 0
:wrc 256 364 0
:wrc 108 108 0
State:  sending Client Key Exchange (16)
:wr 267 267 0
:wrc 256 267 0
:wrc 11 11 0
Fatal exception 29(StoreProhibitedCause):
epc1=0x4021217e, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000

Exception (29):
epc1=0x4021217e epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ctx: cont 
sp: 3fff0ff0 end: 3fff14d0 offset: 01a0

>>>stack>>>
3fff1190:  00000020 00000042 3fff651c 402201a8  
3fff11a0:  3fff5f24 ffffffe1 00000000 00000021  
3fff11b0:  52765785 0000001f 00000000 00000021  
3fff11c0:  3fffbe2c ffffffe1 3fffb81c 3fff5f04  
3fff11d0:  3fffbea8 3fffb83c 3fff4bb4 00000020  
3fff11e0:  00000020 3fff4bb4 3fff651c 3fff4bb8  
3fff11f0:  00000020 3fff4bb4 3fff651c 40220cbb  
3fff1200:  3fff515c 0000001f fffe0001 3fff4b94  
3fff1210:  3fff4bb4 3fff4b94 3fff64fc 3fff4b94  
3fff1220:  00000008 3fff4bb4 0000001c 40220e3f  
3fff1230:  3fff5fdc 3fff519c 3fff9f88 00000008  
3fff1240:  00000004 3fff5fbc 000003ff 3fff64fc  
3fff1250:  3fff6fdc 0000010b 3fff5ffc 3fff50ac  
3fff1260:  3fff515c 3fff5fbc 3fff4bb4 4022109e  
3fff1270:  3fff517c 3fff519c 3fff5fbc 4021fe68  
3fff1280:  39ef0571 66f600da 47d9a6c5 00000000  
3fff1290:  00000100 3fff4c44 3fff6fe9 40221eac  
3fff12a0:  3fff50cc 3fff4c44 3fff6fe9 40220438  
3fff12b0:  00000100 3fff4c44 3fff6fe9 402220b4  
3fff12c0:  3fff12f0 2fcab6f7 000000cd 00000033  
3fff12d0:  74190afb f175471f cb8d779d 3fff4c44  
3fff12e0:  3fff6fe1 00000006 3fff3d9c 4021d150  
3fff12f0:  0d303130 86600906 03650148 05010204  
3fff1300:  71200400 da39ef05 c566f600 2d47d9a6  
3fff1310:  fe0ffefe 967cf628 4e48d53f c3d40b5e  
3fff1320:  005930a6 00000090 3fff3e9c 402096d8  
3fff1330:  76726553 00000006 6f6c6c65 00000004  
3fff1340:  3fff7bc4 00000004 3fff3d9c 00000004  
3fff1350:  3fff7bc4 00000004 3fff3d9c 4021cb04  
3fff1360:  64475a24 3fff3ce0 00000000 0000004c  
3fff1370:  00000081 3fff6fe1 3fff3d9c 4021cee0  
3fff1380:  0000002f 3fff7014 00000200 40100704  
3fff1390:  3fff13e0 0000000e 00000010 00000000  
3fff13a0:  00000000 3fff1d94 3fff3d9c 01000000  
3fff13b0:  3fff13e0 3fff1d74 3fff3d9c 4021cc6c  
3fff13c0:  000022b3 3fff1d74 3fff2dcc 3fff5ffc  
3fff13d0:  0000c539 3fff1d74 3fff01c8 402081c1  
3fff13e0:  3fff01c8 00000000 3fff5ffc 402077bd  
3fff13f0:  3ffe8bac 000002d5 000002d5 00000000  
3fff1400:  000022b3 3fff01c8 3ffe8bac 00000000  
3fff1410:  000022b3 3fff01c8 3ffe8bac 402084cd  
3fff1420:  3ffe9798 35704c34 3ffe9798 35704c34  
3fff1430:  00000000 3fff010c 00000000 402087c1  
3fff1440:  3fff01c8 0000001c 3ffe89dc 4020ac5c  
3fff1450:  00000000 3ffe89e0 00000000 3fff04a8  
3fff1460:  3fffdad0 3fff010c 3fff03e8 3fff04a8  
3fff1470:  3fffdad0 3fff010c 3fff03e8 40208914  
3fff1480:  00000000 00000000 3fff03e8 4020912c  
3fff1490:  3fff334c 3fff010c 3fff03e8 402069c3  
3fff14a0:  feefeffe 3fff010c 3fff04a0 40206a46  
3fff14b0:  3fffdad0 00000000 3fff04a0 402096ac  
3fff14c0:  feefeffe feefeffe 3fff04b0 40100978  
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(1,7)

 ets Jan  8 2013,rst cause:4, boot mode:(1,7)

wdt reset

Decoded String

Exception 28: LoadProhibited: A load referenced a page mapped with an attribute that does not permit loads
Decoding 14 results
0x40207f65: SSLContext::connect(ClientContext*, char const*, unsigned int) at C:\Users\kapil\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\libraries\ESP8266WiFi\src/WiFiClientSecure.cpp line 692
:  (inlined by) WiFiClientSecure::_connectSSL(char const*) at C:\Users\kapil\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\libraries\ESP8266WiFi\src/WiFiClientSecure.cpp line 323
0x40207f65: SSLContext::connect(ClientContext*, char const*, unsigned int) at C:\Users\kapil\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\libraries\ESP8266WiFi\src/WiFiClientSecure.cpp line 692
:  (inlined by) WiFiClientSecure::_connectSSL(char const*) at C:\Users\kapil\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\libraries\ESP8266WiFi\src/WiFiClientSecure.cpp line 323
0x4020131e: delay at C:\Users\kapil\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\cores\esp8266/core_esp8266_wiring.c line 54
0x40207f57: SSLContext::connect(ClientContext*, char const*, unsigned int) at C:\Users\kapil\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\libraries\ESP8266WiFi\src/WiFiClientSecure.cpp line 692
:  (inlined by) WiFiClientSecure::_connectSSL(char const*) at C:\Users\kapil\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\libraries\ESP8266WiFi\src/WiFiClientSecure.cpp line 323
0x402076a6: WiFiClient::connect(IPAddress, unsigned short) at C:\Users\kapil\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\libraries\ESP8266WiFi\src/WiFiClient.cpp line 341
0x402082a5: WiFiClientSecure::connect(char const*, unsigned short) at C:\Users\kapil\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\libraries\ESP8266WiFi\src/WiFiClientSecure.cpp line 312
0x40208599: PubSubClient::connect(char const*, char const*, char const*, char const*, unsigned char, unsigned char, char const*) at C:\Users\kapil\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.cpp line 586
0x4020a734: Print::write(unsigned char const*, unsigned int) at C:\Users\kapil\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\cores\esp8266/Print.cpp line 38
0x402086ec: PubSubClient::connect(char const*) at C:\Users\kapil\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.cpp line 586
0x40208ee8: Print::print(char const*) at C:\Users\kapil\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\cores\esp8266/Print.cpp line 123
0x402069c3: reconnect() at C:\Users\kapil\Documents\Arduino\MQTT_AWS/MQTT_AWS.ino line 58
0x40206a46: loop at C:\Users\kapil\Documents\Arduino\MQTT_AWS/MQTT_AWS.ino line 146
0x40209460: loop_wrapper at C:\Users\kapil\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\cores\esp8266/core_esp8266_main.cpp line 57
0x4010070c: cont_norm at C:\Users\kapil\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.0-rc1\cores\esp8266/cont.S line 109
waghekapil commented 7 years ago

Hello,

Did you get any change to check it with ESP? Till now I've tried many ways but not working, I read almost all threads but no luch.

Now my job is in risk :(

Still getting errors. Can you please help me?

copercini commented 7 years ago

I tried but it doesn't work in my board.

You can try https://github.com/odelot/aws-mqtt-websockets it's working for me in the last year

JimmyTai commented 7 years ago

Hi @waghekapil ,

Could you try not to load CA. In my case, I only load certification and private key and the code works fine.

waghekapil commented 7 years ago

Hi @JimmyTai ,

I tried without uploading CA, it is still not connecting. Can you please share more details of your code and configuration like esp board version, your library or @copercini library? Please check my code:

I'm using Esp8266-2.4.0-rc1 version with Arduino 1.8.3.

//This example needs https://github.com/esp8266/arduino-esp8266fs-plugin

#include "FS.h"
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "SIPLEC-IOT";
const char* password = "Sipl@w0rld";

const char* mqtt_server = "dddddddddddd.iot.ap-southeast-1.amazonaws.com"; //MQTT broker ip 

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

}
WiFiClientSecure espClient;
PubSubClient client(mqtt_server, 8883, callback, espClient); //set  MQTT port number to 8883 as per //standard
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  if (!SPIFFS.begin()) {
    Serial.println("FS not loaded.");
  }

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("sairam")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    }
    else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying   
      delay(5000);
    }
  }

}

void setup() {

  Serial.begin(115200);
  Serial.setDebugOutput(true);

  configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");

  setup_wifi();
  delay(1000);
  if (!SPIFFS.begin()) {
    Serial.println("Failed to mount file system");
    return;
  }

  Serial.print("Heap: "); Serial.println(ESP.getFreeHeap());

  // Load certificate file
  File cert = SPIFFS.open("/cert.der", "r"); //replace cert.crt eith your uploaded file name
  if (!cert) {
    Serial.println("Failed to open cert file");
  }
  else
    Serial.println("Success to open cert file");

  delay(1000);

  if (espClient.loadCertificate(cert))
    Serial.println("cert loaded");
  else
    Serial.println("cert not loaded");

  delay(1000);

  // Load private key file
  File private_key = SPIFFS.open("/private.der", "r"); //replace private eith your uploaded file name
  if (!private_key) {
    Serial.println("Failed to open private cert file");
  }
  else
    Serial.println("Success to open private cert file");

  delay(1000);

  if (espClient.loadPrivateKey(private_key))
    Serial.println("private key loaded");
  else
    Serial.println("private key not loaded");

  delay(1000);

  //// Load CA file
  //File ca = SPIFFS.open("/ca.der", "r"); //replace ca eith your uploaded file name
  //if (!ca) {
  //  Serial.println("Failed to open ca ");
  //}
  //else
  //  Serial.println("Success to open ca");

  //

  //if (espClient.loadCACert(ca, sizeof(ca)))
  //  Serial.println("ca loaded");
  //else
  //  Serial.println("ca failed");
  //delay(1000);

  Serial.print("Heap: "); Serial.println(ESP.getFreeHeap());

  // client.setServer(mqtt_server, 8883);
  // client.setCallback(callback);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 10000) {
    lastMsg = now;
    ++value;
    snprintf(msg, 75, "hello world #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("outTopic", msg);
  }
}

current output:

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
vf6d232f1
~ld

Connecting to SIPLEC-IOT
SPIFFSImpl: allocating 512+180+1400=2092 bytes
SPIFFSImpl: mounting fs @100000, size=2fb000, block=2000, page=100
SPIFFSImpl: mount rc=0
sta config unchangedscandone
....scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 4
cnt 

connected with SIPLEC-IOT, channel 2
dhcp client start...
.............ip:192.168.100.7,mask:255.255.255.0,gw:192.168.100.2
.
WiFi connected
IP address: 
192.168.100.7
Heap: 33760
Success to open cert file
cert loaded
Success to open private cert file
private key loaded
Heap: 27048
SPIFFS_close: fd=2
SPIFFS_close: fd=1
Attempting MQTT connection...:ref 1
please start sntp first !
State:  sending Client Hello (1)
:wr 134 134 0
:wrc 134 134 0
:sent 134
:rn 1360
:rd 5, 1360, 0
:rdi 1360, 5
:rd 1355, 1360, 5
:rdi 1355, 1355
:c0 1355, 1360
:rn 1360
:rd 1360, 1360, 0
:rdi 1360, 1360
:c0 1360, 1360
:rn 332
:rd 332, 332, 0
:rdi 332, 332
:c0 332, 332
State:  receiving Server Hello (2)
State:  receiving Certificate (11)
State:  receiving Certificate Request (13)
State:  receiving Server Hello Done (14)
State:  sending Certificate (11)
:wr 876 876 0
:wrc 256 876 0
:wrc 256 620 0
:wrc 256 364 0
:wrc 108 108 0
:sent 876
State:  sending Client Key Exchange (16)
:wr 267 267 0
:wrc 256 267 0
:wrc 11 11 0
:sent 267
State:  sending Certificate Verify (15)
:wr 269 269 0
:wrc 256 269 0
:wrc 13 13 0
:wr 6 6 0
:wrc 6 6 0
State:  sending Finished (16)
:wr 85 85 0
:wrc 85 85 0
:wr 69 69 0
:wrc 69 69 0
Alert: close notify
failed, rc=-2 try again in 5 seconds
:sent 269
:sent 160
:rn 160
Attempting MQTT connection...:ur 1
:close
:del
:ref 1
please start sntp first !
State:  sending Client Hello (1)
:wr 134 134 0
:wrc 134 134 0
:sent 134
:rn 1360
:rd 5, 1360, 0
:rdi 1360, 5
:rd 1355, 1360, 5
:rdi 1355, 1355
:c0 1355, 1360
:rn 1360
:rch 1360, 332
:rd 1692, 1692, 0
:rdi 1360, 1360
:c 1360, 1360, 1692
:rdi 332, 332
:c0 332, 332
State:  receiving Server Hello (2)
State:  receiving Certificate (11)
State:  receiving Certificate Request (13)
State:  receiving Server Hello Done (14)
State:  sending Certificate (11)
:wr 12 12 0
:wrc 12 12 0
:sent 12
:rn 7
:rcl
:abort
State:  sending Client Key Exchange (16)
Alert: close notify
failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...:ur 1
:del
:ref 1
please start sntp first !
State:  sending Client Hello (1)
:wr 134 134 0
:wrc 134 134 0
:sent 134
:rn 1360
:rd 5, 1360, 0
:rdi 1360, 5
:rd 1355, 1360, 5
:rdi 1355, 1355
:c0 1355, 1360
:rn 1360
:rd 1360, 1360, 0
:rdi 1360, 1360
:c0 1360, 1360
:rn 332
:rd 332, 332, 0
:rdi 332, 332
:c0 332, 332
State:  receiving Server Hello (2)
State:  receiving Certificate (11)
State:  receiving Certificate Request (13)
State:  receiving Server Hello Done (14)
State:  sending Certificate (11)
:wr 12 12 0
:wrc 12 12 0
:sent 12
:rn 7
:rcl
:abort
State:  sending Client Key Exchange (16)
Alert: close notify
failed, rc=-2 try again in 5 seconds
waghekapil commented 7 years ago

@copercini Thanks! and @JimmyTai Thanks! you too for the help

Finally https://github.com/odelot/aws-mqtt-websockets worked for me. Ithink it is doing MQTT Over the WebSocket Protocol. Is it true?

I have a doubt: Is aws-mqtt-websockets secure as aws-iot with certificates upload? I mean line TLS 1.2? My client needs secure communication.

Well thanks! again :)

You both saved me 👍

JimmyTai commented 7 years ago

@waghekapil

According the official document, AWS IoT Websocket use Sigv4 as the authorization. And there is some description from Amozong.


When using AWS Signature Version 4 with AWS IoT, clients must support the following in their TLS implementation:

  1. TLS 1.2, TLS 1.1, TLS 1.0.
  2. SHA-256 RSA certificate signature validation.
  3. One of the cipher suites from the TLS cipher suite support section.

I think it also use TLS1.2 too, because I saw the same key exchange step when I turn on debug level with TLS. The difference between MQTT and Websocket in AWS IoT is the permission control. In MQTT, you are able to attach a certification and policy on a Thing. But in Websocket, you have to use IAM user to control the permission.

According my experiment, AWS IoT through MQTT or Websocket spend around 8 second during connecting process. I am puzzled by this problem for a long time.

copercini commented 7 years ago

I made some changes in the repo today, now the things seems work fine

earmero commented 7 years ago

Hi All, I started make some tests today, but I seeing the same error, I have a doubt, I need to create CA and also convert it to a DER format and upload ??? Thanks for Your Help

Here the errors

connected with test, channel 1 dhcp client start... .ip:192.168.1.123,mask:255.255.255.0,gw:192.168.1.1 . WiFi connected IP address: 192.168.1.123 Heap: 36288 Success to open cert file cert loaded Success to open private cert file private key loaded Heap: 34040 Attempting MQTT connection...please start sntp first ! failed, rc=-2 try again in 5 seconds Attempting MQTT connection...failed, rc=-2 try again in 5 seconds Attempting MQTT connection...failed, rc=-2 try again in 5 seconds Attempting MQTT connection...please start sntp first ! failed, rc=-2 try again in 5 seconds Attempting MQTT connection...please start sntp first ! failed, rc=-2 try again in 5 seconds

waghekapil commented 7 years ago

@copercini thanks for update. Currently I'm working with WebSocket option. Project is under development. I'll check it and let you know the outcome.

@earmero I think yes. You have to upload the CA in DER formate.

iotechFabio commented 7 years ago

Hi Copercini,

I used your last code and I am getting the same error.

WiFi connected
IP address: 192.168.0.13
Heap: 36416
Success to open cert file
cert loaded
Success to open private cert file
private key loaded
Heap: 34168
Attempting MQTT connection...please start sntp first !
failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...please start sntp first !
failed, rc=-2 try again in 5 seconds
...

Does your code still work with you?

My board is a WeMOS D1

waghekapil commented 6 years ago

Hi Copercini,

I'm also getting same error. Can you please look into this? I'm using your suggested library https://github.com/odelot/aws-mqtt-websockets . But, it is taking too much Heap Memory and I'm not able to run IR operations.

With Websocket library img_06112017_121537_0

Your library is NOT eating too much memory. If you findout the issue and solve it asap then it will be very helpful for us. We are already running late.

Thanks :)

ztittle commented 6 years ago

I was able to connect after modifying a hard coded timeout in the WifiClientSecure class. See https://github.com/esp8266/Arduino/issues/3944