Hello,
I have a RPi connected to a W5100S over SPI and trying to connect it to AWS IoT (and keep it connected ofc...).
So it connects, sends a message then reconnects, not sure why.
Here's the code:
#include <Arduino.h>
#define USE_THIS_SS_PIN 5u
#include <SPI.h>
#include <Ethernet.h>
#include <SSLClient.h>
#include <PubSubClient.h>
#include "certificates.h" // This file must be regenerated at https://openslab-osu.github.io/bearssl-certificate-utility/
#include "aws_configuration.h"
//#define ETHERNET_LARGE_BUFFERS true
#define MQTT_PACKET_SIZE 1024
// Enter a MAC address and IP address for your controller below.
#define NUMBER_OF_MAC 20
byte mac[][NUMBER_OF_MAC] = {
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
};
// In case a fixed IP is needed
//IPAddress ip(192, 168, 2, 222);
// Google DNS Server IP or any other DNS server
//IPAddress myDns(8, 8, 8, 8);
void MQTTPublish(const char *topic, char *payload);
void sendState();
SSLClientParameters mTLS = SSLClientParameters::fromPEM(thing_certificate, sizeof thing_certificate, thing_key, sizeof thing_key);
char publishPayload[MQTT_PACKET_SIZE];
void blinks(int cnt) {
for (int i = 0; i < cnt; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (uint16_t i=0; i<length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
blinks(2);
}
EthernetClient ethClient;
SSLClient ethClientSSL(ethClient, TAs, (size_t)TAs_NUM, A3);
PubSubClient mqtt(AWS_IOT_ENDPOINT, 8883, callback, ethClientSSL);
void reconnect() {
while (!mqtt.connected()) {
digitalWrite(LED_BUILTIN, LOW);
Serial.print("Attempting MQTT connection...");
if ( mqtt.connect( DEVICE_NAME ) ) {
blinks(1);
Serial.println("connected!");
for (int i = 0; i < 3; i++) {
Serial.print("Subscribing to: ");
Serial.println(subscribeTopics[i]);
mqtt.subscribe(subscribeTopics[i]);
blinks(1);
delay(100);
}
sendState(); // initial
digitalWrite(LED_BUILTIN, HIGH);
} else {
blinks(3);
digitalWrite(LED_BUILTIN, LOW);
Serial.print("failed, rc=");
Serial.print(mqtt.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup() {
delay(500);
// set the onboard led pin to output
pinMode(LED_BUILTIN, OUTPUT);
//Ethernet.init(USE_THIS_SS_PIN);
// Open serial communications and wait for port to open:
Serial.begin(115200);
//while (!Serial);
// start the Ethernet connection:
uint16_t index = millis() % NUMBER_OF_MAC;
if (Ethernet.begin(mac[index]) == 0) {
delay(250);
Serial.println("Failed to configure Ethernet using DHCP");
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
blinks(3);
} else if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
blinks(3);
}
// no point in carrying on, so do nothing forevermore:
while (true) {
delay(1);
}
}
Serial.println("Ethernet connected.");
ethClientSSL.setMutualAuthParams(mTLS);
mqtt.setBufferSize(MQTT_PACKET_SIZE);
blinks(1);
Serial.println("Setup completed.");
}
void loop() {
Ethernet.maintain();
if (!mqtt.connected()) {
digitalWrite(LED_BUILTIN, LOW);
Serial.print("Not connected to MQTT: ");
Serial.println( mqtt.state() );
if (!ethClient.connected() ) {
Serial.println("Ethernet not connected!");
}
delay(5000);
reconnect();
}
mqtt.loop();
if (!mqtt.connected()){
Serial.print("X");
} else {
Serial.print(".");
}
}
void sendState(){
strcpy(publishPayload, "{\"state\": {\"reported\": {\"powerState\":\"ON\"}}}");
MQTTPublish(AWS_IOT_TOPIC, publishPayload);
}
void MQTTPublish(const char *topic, char *payload){
mqtt.publish(topic, payload);
Serial.print("Published [");
Serial.print(topic);
Serial.print("] ");
Serial.println(payload);
}
And the output:
Attempting MQTT connection...connected!
Subscribing to: $aws/things/DevBoard/shadow/name/controller/update/accepted
Subscribing to: $aws/things/DevBoard/shadow/name/controller/update/rejected
Subscribing to: $aws/things/DevBoard/shadow/name/controller/update/delta
Published [$aws/things/DevBoard/shadow/name/controller/update] {"state": {"reported": {"powerState":"ON"}}}
..XNot connected to MQTT: -3
Ethernet not connected!
... repeated indefinitely.
The client ID is unique, nothing else connects to the account. LAN connection is nice and stable. Ethernet doesn't actually get disconnected - I'm pinging it and never times out.
Any idea what might be going on? Am I doing something wrong?