AWS IoT - BOOK -


#1

Sir, i am finishing a Alexa + Lambda + AWS IoT + ESP32 book and i am using the ESP32 WROOM in all examples.
The ESP WROOM is a AWS IoT client.

I successfully implemented on ESP32 WROOM access to AWS IoT using Arduino, ESP-IDF, VISUINO and AT commands.
I can succesfullly connnect using those 4 ways.

Now i intend to use the ZERYNTH.

I already created the Thing and policies on AWS IOT and i have the 3 certificates ready to use with Zerynth, i created too the thing, topic, endpoint, etc
CA, private and certificate certificates.

Where is a example to you Zerynth with those parameters ?

The controle_published_period example is not a good example to the BOOK client to understand!

Can you help me ?

I want something to much similiar to this

#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>

void connectAWSIoT();
void mqttCallback (char* topic, byte* payload, unsigned int length);

char *ssid = “Andreia e Miguel”;
char *password = “xxxxxxxxxxxxxxxxx”;

const char *endpoint = “a1dl08fmj981lr-ats.iot.us-east-1.amazonaws.com”;
// Example: xxxxxxxxxxxxxx.iot.ap-northeast-1.amazonaws.com
const int port = 8883;
char *pubTopic = “$aws/things/lampada/shadow/update”;
char *subTopic = “$aws/things/lampada/shadow/update/delta”;

const char* rootCA =
“-----BEGIN CERTIFICATE-----\n”
“-----END CERTIFICATE-----\n”;

const char* certificate =
“-----BEGIN CERTIFICATE-----\n”
“-----END CERTIFICATE-----\n”;

const char* privateKey =
“-----END RSA PRIVATE KEY-----\n”;

WiFiClientSecure httpsClient;
PubSubClient mqttClient(httpsClient);

void setup() {
delay(1000);
Serial.begin(115200);

// Start WiFi
Serial.println("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);

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

// Configure MQTT Client
httpsClient.setCACert(rootCA);
httpsClient.setCertificate(certificate);
httpsClient.setPrivateKey(privateKey);
mqttClient.setServer(endpoint, port);
mqttClient.setCallback(mqttCallback);

connectAWSIoT();

}

void connectAWSIoT() {
while (!mqttClient.connected()) {
if (mqttClient.connect(“ESP32_device”)) {
Serial.println(“Connected.”);
int qos = 0;
mqttClient.subscribe(subTopic, qos);
Serial.println(“Subscribed.”);
} else {
Serial.print(“Failed. Error state=”);
Serial.print(mqttClient.state());
// Wait 5 seconds before retrying
delay(5000);
}
}
}

long messageSentAt = 0;
int dummyValue = 0;
char pubMessage[128];

void mqttCallback (char* topic, byte* payload, unsigned int length) {
Serial.print(“Received. topic=”);
Serial.println(topic);
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.print("\n");
}

void mqttLoop() {
if (!mqttClient.connected()) {
connectAWSIoT();
}
mqttClient.loop();

long now = millis();
if (now - messageSentAt > 5000) {
    messageSentAt = now;
    sprintf(pubMessage, "{\"state\": {\"desired\":{\"foo\":\"%d\"}}}", dummyValue++);
    Serial.print("Publishing message to topic ");
    Serial.println(pubTopic);
    Serial.println(pubMessage);
    mqttClient.publish(pubTopic, pubMessage);
    Serial.println("Published.");
}

}

void loop() {
mqttLoop();
}

I dont to use ARN

Thanks!


#2

Hello,
In the controlled publish period example, When you clone the example, You will find a file in the project folder called “thing.config.json”
This file contains the parameters that you refer to such as : mqttid, policyname,thingname…

Let me know if you have further questions.


#3

Hello,
Yes, i see, but you can that there is no option to CA CERTIFICATE, right ?

Then i am looking a example that the client dont neeed to use the ZERYNTH command line to create the THING.

I just have the thing, certificates, topic, mqtt broker address…just want to open a mqtt using those parameters, wich includes the CA


#4

Btw, did you get this error ?


#5

Hello @Miguel_Wisintainer,

the CA Certificate is already included inside the Zerynth AWS IoT library.
Of course you can try the Controlled Publish Period example with credentials not generated using the command line toolchain: you just need to set the "endpoint", "mqttid", and "thingname" fields inside the thing.conf.json file and place private key and certificate inside the project folder renaming them to private.pem.key and certificate.cert.crt respectively.

Let me know :slight_smile:


#6

Hi Lorenzo

Getting the following error now!

Can me contact you directly ? I can send you the project to analyse!

my e-mail is tcpipchip@hotmail.com

This is what i want to do with Zerynth too


#9

99% ready
thing.mqtt.publish("$aws/things/lamp/shadow/update", json.dumps({“state”: {“reported”:{“temp”:random(0,70),“umidade”:random(0,100)}}}))
I can published on
$aws/things/lampada/shadow/update

I need help how subscribe to
$aws/things/lampada/shadow/update/accepted
to get callback ?

I am already publishing good on
$aws/things/lampada/shadow/update


#10

Hi @Miguel_Wisintainer,

great, to subscribe and set a callback you just need to work directly on the mqtt client attribute of your AWS IoT Client Instance:

import mqtt
client.mqtt.subscribe([['$aws/things/lampada/shadow/update/accepted',0]])
client.mqtt.on(mqtt.PUBLISH, my_callback, check_topic)

as described here.

Let me know if you need further assistance :slight_smile:


#11

Hi Lorenzo
Are you sure that i have to use MQTT lib ? Why not the AWS IOT lib ?
Dont make sense for me!

if i do
thing.mqtt.publish("$aws/things/lamp/shadow/update", json.dumps({“state”: {“reported”:{“temp”:random(0,70),“umidade”:random(0,100)}}}))
i successfull publish on $aws/things/lamp/shadow/update and then the callback returns:
{lampada:1}
That is the last value published in
$aws/things/lamp/shadow/update
by my Lambda

if (lightState === “off”) {
paramsUpdate = {
“thingName” : “lamp”,
“payload” : ‘{“state”: {“desired”: {“lampada”:0}}}’
};
}

	//Update Device Shadow
	**iotData.updateThingShadow**(paramsUpdate, function(err, data) { ....

My callback routine

define a callback for shadow updates

def shadow_callback(requested):
print(requested)
return

# AWS IoT Controlled publish period 
# Created at 2017-10-03 08:49:48.182639

import streams
import json
from wireless import wifi

# choose a wifi chip supporting secure sockets and client certificates
from espressif.esp32net import esp32wifi as wifi_driver

# import aws iot module
from aws.iot import iot

# import helpers functions to easily load keys, certificates and thing configuration
import helpers

# THING KEY AND CERTIFICATE FILE MUST BE PLACED INSIDE PROJECT FOLDER 
new_resource('private.pem.key')
new_resource('certificate.pem.crt')
# SET THING CONFIGURATION INSIDE THE FOLLOWING JSON FILE
new_resource('thing.conf.json')

# define a callback for shadow updates
def shadow_callback(requested):
    print(requested)
    return

streams.serial()
wifi_driver.auto_init()

print('connecting to wifi...')
# place here your wifi configuration
wifi.link("Andreia e Miguel",wifi.WIFI_WPA2,"xxxx")

pkey, clicert = helpers.load_key_cert('private.pem.key', 'certificate.pem.crt')
thing_conf = helpers.load_thing_conf()
publish_period = 30000

# create aws iot thing instance, connect to mqtt broker, set shadow update callback and start mqtt reception loop
thing = iot.Thing(thing_conf['endpoint'], thing_conf['mqttid'], clicert, pkey, thingname=thing_conf['thingname'])
print('connecting to mqtt broker...')
thing.mqtt.connect()
thing.on_shadow_request(shadow_callback)
thing.mqtt.loop()

while True:
    print('publish Temperature and Humidity...')
    #{"state": {"reported":{"temp":"%0","umidade":"%1"}}}
    thing.mqtt.publish("$aws/things/lamp/shadow/update", json.dumps({"state": {"reported":{"temp":random(0,70),"umidade":random(0,100)}}}))
    sleep(publish_period)

#12

Hi @Miguel_Wisintainer,

you are still using the AWS IoT library, the import mqtt line is only needed for the mqtt.PUBLISH constant used in client.mqtt.on(mqtt.PUBLISH, my_callback, check_topic).

Moreover, what I wanted to say is that inside the AWS IoT object you have the mqtt attribute that is a complete MQTT Client which makes available for use all the functions of an MQTT Client as described inside the Zerynth MQTT Documentation :slight_smile:


#13

Lorenzo

I did the modifications and the call backs not occurs! I am spending too much time!

i will publish in the book that original code above that really works and gives a idea that is possible to use
too the Zerynth to work as a end device to Alexa!