Hi! I really appreciate your help. I'm really lost about what to do about this really haha. I've been reading through the WolfSSL docs for the past hours.
I'm not sure what to make of this actually as I don't think it's a memory error(?). I got the following results from inserting these lines of code "freeRam()" as seen below in the "wolfssl_client.ino" code:
// inside void setup()
// initialize wolfSSL using callback functions
wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
wolfSSL_SetIOSend(ctx, EthernetSend);
wolfSSL_SetIORecv(ctx, EthernetReceive);
Serial.println("Setup success!");
freeRam();
return;
}
// inside the void loop()
if (reconnect) {
reconnect--;
if (client.connect(host, port)) {
Serial.print("Connected to ");
Serial.println(host);
freeRam();
ssl = wolfSSL_new(ctx);
freeRam();
if (ssl == NULL) {
Serial.println("Unable to allocate SSL object");
freeRam();
return;
}
And here are the results from each of those freeRam():
Sketch uses 141148 bytes (26%) of program storage space. Maximum is 524288 bytes.
Setup success!
Ram used (bytes):
dynamic: 368
static: 5192
stack: 72
Est. free ram: 92672
Connected to google.com
Ram used (bytes):
dynamic: 368
static: 5192
stack: 288
Est. free ram: 92456
Unable to allocate SSL object
Ram used (bytes):
dynamic: 368
static: 5192
stack: 288
Est. free ram: 92456
Here's the code of freeRam() from the freeRam.h I'm importing:
#include <Arduino.h>
#include <malloc.h>
extern char _end;
extern "C" char *sbrk(int i);
void freeRam()
{
char *ramstart = (char *) 0x20070000;
char *ramend = (char *) 0x20088000;
char *heapend = sbrk(0);
register char * stack_ptr asm( "sp" );
struct mallinfo mi = mallinfo();
Serial.println(F("Ram used (bytes): "));
Serial.print(F("dynamic: ")); Serial.println(mi.uordblks);
Serial.print(F("static: ")); Serial.println(&_end - ramstart);
Serial.print(F("stack: ")); Serial.println(ramend - stack_ptr);
Serial.print(F("Est. free ram: ")); Serial.println(stack_ptr - heapend + mi.fordblks);
}
And here's the complete sample code btw with the modifications I made while trying to make it work:
/* wolfssl_client.ino
*
* Copyright (C) 2006-2018 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <Arduino.h>
//#include <sys/time.h>
#include <wolfssl.h>
#include <wolfssl/ssl.h>
struct timeval {
long tv_sec;
long tv_usec;
};
int _gettimeofday( struct timeval *tv, void *tzvp)
{
//uint64_t t = __your_system_time_function_here__(); // get uptime in nanoseconds
//tv->tv_sec = t / 1000000000; // convert to seconds
//tv->tv_usec = ( t % 1000000000 ) / 1000; // get remaining microseconds
long mt = millis();
tv->tv_sec = mt/1000;
tv->tv_usec = mt*1000;
return 0; // return non-zero for error
} // end _gettimeofday()
#include <Ethernet2.h>
#include <freeRam.h>
/*
struct timeval {
long tv_sec;
long tv_usec;
};
*/
/*
int _gettimeofday( struct timeval *tv, void *tzvp )
{
//uint64_t t = __your_system_time_function_here__(); // get uptime in nanoseconds
//tv->tv_sec = t / 1000000000; // convert to seconds
//tv->tv_usec = ( t % 1000000000 ) / 1000; // get remaining microseconds
long mt = millis();
tv->tv_sec = mt/1000;
tv->tv_usec = mt*1000;
return 0; // return non-zero for error
} // end _gettimeofday()
*/
//const char host[] = "192.168.1.148"; // server to connect to
//const int port = 11111; // port on server to connect to
const char host[] = "google.com";
const char req[]= "GET / HTTP/1.0\r\n\r\n"; // Get the root page
const int port = 443;
int EthernetSend(WOLFSSL* ssl, char* msg, int sz, void* ctx);
int EthernetReceive(WOLFSSL* ssl, char* reply, int sz, void* ctx);
int reconnect = 10;
EthernetClient client;
WOLFSSL_CTX* ctx = NULL;
WOLFSSL* ssl = NULL;
void setup() {
WOLFSSL_METHOD* method;
Serial.begin(9600);
freeRam();
method = wolfTLSv1_2_client_method();
if (method == NULL) {
Serial.println("unable to get method");
return;
}
ctx = wolfSSL_CTX_new(method);
if (ctx == NULL) {
Serial.println("unable to get ctx");
return;
}
// initialize wolfSSL using callback functions
wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
wolfSSL_SetIOSend(ctx, EthernetSend);
wolfSSL_SetIORecv(ctx, EthernetReceive);
Serial.println("Setup success!");
freeRam();
return;
}
int EthernetSend(WOLFSSL* ssl, char* msg, int sz, void* ctx) {
int sent = 0;
sent = client.write((byte*)msg, sz);
return sent;
}
int EthernetReceive(WOLFSSL* ssl, char* reply, int sz, void* ctx) {
int ret = 0;
while (client.available() > 0 && ret < sz) {
reply[ret++] = client.read();
}
return ret;
}
void loop() {
int err = 0;
int input = 0;
int total_input = 0;
char msg[32] = "hello wolfssl!";
int msgSz = (int)strlen(msg);
char errBuf[80];
char reply[80];
const char* cipherName;
if (reconnect) {
reconnect--;
if (client.connect(host, port)) {
Serial.print("Connected to ");
Serial.println(host);
freeRam();
ssl = wolfSSL_new(ctx);
freeRam();
if (ssl == NULL) {
Serial.println("Unable to allocate SSL object");
freeRam();
return;
}
err = wolfSSL_connect(ssl);
if (err != WOLFSSL_SUCCESS) {
err = wolfSSL_get_error(ssl, 0);
wolfSSL_ERR_error_string(err, errBuf);
Serial.print("TLS Connect Error: ");
Serial.println(errBuf);
}
Serial.print("SSL version is ");
Serial.println(wolfSSL_get_version(ssl));
cipherName = wolfSSL_get_cipher(ssl);
Serial.print("SSL cipher suite is ");
Serial.println(cipherName);
if ((wolfSSL_write(ssl, msg, msgSz)) == msgSz) {
Serial.print("Server response: ");
while (client.available() || wolfSSL_pending(ssl)) {
input = wolfSSL_read(ssl, reply, sizeof(reply) - 1);
total_input += input;
if (input < 0) {
err = wolfSSL_get_error(ssl, 0);
wolfSSL_ERR_error_string(err, errBuf);
Serial.print("TLS Read Error: ");
Serial.println(errBuf);
break;
} else if (input > 0) {
reply[input] = '\0';
Serial.print(reply);
} else {
Serial.println();
}
}
} else {
err = wolfSSL_get_error(ssl, 0);
wolfSSL_ERR_error_string(err, errBuf);
Serial.print("TLS Write Error: ");
Serial.println(errBuf);
}
wolfSSL_shutdown(ssl);
wolfSSL_free(ssl);
client.stop();
Serial.println("Connection complete.");
reconnect = 0;
} else {
Serial.println("Trying to reconnect...");
}
}
delay(1000);
}