Black Lives Matter - Action and Equality.
0

Nina B302 + Wifinina (AWS IOT) problems
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Nina B302 + Wifinina (AWS IOT) problems

by tcpipchip on Wed May 13, 2020 8:23 am

Sir
I mounted a Circuit using the NINA B302 + NINA W102 (version 1.6) and followed steps to connect to the AWS IOT.
But i am having the following error (attached)

adafruit_minimqtt.py
line 230
proto, dummy, broker = self.broker.split("/", 2)

What am i doing wrong ? That "/" is where ?

Thanks!
Image

Here my code
Code: Select all | TOGGLE FULL SIZE
import microcontroller
import time
import json
import board
import busio
from digitalio import DigitalInOut
import neopixel
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
import adafruit_minimqtt as MQTT
from adafruit_aws_iot import MQTT_CLIENT

### WiFi ###

secrets = {
    'ssid' : 'Andreia Oi Miguel 2.4G',
    'password' : 'xxxxxxxxxxxx',
    "timezone" : "America/New_York",  # http://worldtimeapi.org/timezones
    "broker" : "xxxxxxxxxxxxxxxxxx-ats.iot.us-east-1.amazonaws.com",
    "client_id": "PyPortal"
}

# Get device certificate
try:
    with open("aws_cert.pem.crt", "rb") as f:
        DEVICE_CERT = f.read()
except ImportError:
    print("Certificate (aws_cert.pem.crt) not found on CIRCUITPY filesystem.")
    raise

# Get device private key
try:
    with open("private.pem.key", "rb") as f:
        DEVICE_KEY = f.read()
except ImportError:
    print("Certificate (private.pem.key) not found on CIRCUITPY filesystem.")
    raise

# If you are using a board with pre-defined ESP32 Pins:
esp32_cs =    DigitalInOut(microcontroller.pin.P0_13)
esp32_ready = DigitalInOut(microcontroller.pin.P0_14)
esp32_reset = DigitalInOut(microcontroller.pin.P1_12)

SCK  = microcontroller.pin.P0_07
MOSI = microcontroller.pin.P0_15
MISO = microcontroller.pin.P1_00

spi = busio.SPI(SCK, MOSI, MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

# Verify nina-fw version >= 1.4.0
assert (
    int(bytes(esp.firmware_version).decode("utf-8")[2]) >= 4
), "Please update nina-fw to >=1.4.0."

wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, None)

### Code ###

topic = "$aws/things/PyPortal/shadow/update"

# Define callback methods which are called when events occur
# pylint: disable=unused-argument, redefined-outer-name
def connect(client, userdata, flags, rc):
    # This function will be called when the client is connected
    # successfully to the broker.
    print("Connected to MQTT Broker!")
    print("Flags: {0}\n RC: {1}".format(flags, rc))

    # Subscribe to topic circuitpython/aws
    print("Subscribing to topic {}".format(topic))
    aws_iot.subscribe(topic)


def disconnect(client, userdata, rc):
    # This method is called when the client disconnects
    # from the broker.
    print("Disconnected from MQTT Broker!")


def subscribe(client, userdata, topic, granted_qos):
    # This method is called when the client subscribes to a new topic.
    print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))

    # Create a json-formatted message
    message = {"message": "Hello from AWS IoT CircuitPython"}
    # Publish message to topic
    aws_iot.publish(topic, json.dumps(message))


def unsubscribe(client, userdata, topic, pid):
    # This method is called when the client unsubscribes from a topic.
    print("Unsubscribed from {0} with PID {1}".format(topic, pid))


def publish(client, userdata, topic, pid):
    # This method is called when the client publishes data to a topic.
    print("Published to {0} with PID {1}".format(topic, pid))


def message(client, topic, msg):
    # This method is called when the client receives data from a topic.
    print("Message from {}: {}".format(topic, msg))


# Set AWS Device Certificate
esp.set_certificate(DEVICE_CERT)

# Set AWS RSA Private Key
esp.set_private_key(DEVICE_KEY)

# Connect to WiFi
print("Connecting to WiFi...")
wifi.connect()
print("Connected!")

# Initialize MQTT interface with the esp interface
MQTT.set_socket(socket, esp)

# Set up a new MiniMQTT Client
client = MQTT.MQTT(broker=secrets["broker"], client_id=secrets["client_id"])

# Initialize AWS IoT MQTT API Client
aws_iot = MQTT_CLIENT(client)

# Connect callback handlers to AWS IoT MQTT Client
aws_iot.on_connect = connect
aws_iot.on_disconnect = disconnect
aws_iot.on_subscribe = subscribe
aws_iot.on_unsubscribe = unsubscribe
aws_iot.on_publish = publish
aws_iot.on_message = message

print("Attempting to connect to %s" % client.broker)
aws_iot.connect()

# Pump the message loop forever, all events
# are handled in their callback handlers
# while True:
#   aws_iot.loop()

# Start a blocking message loop...
# NOTE: NO code below this loop will execute
# NOTE: Network reconnection is handled within this loop
while True:
    try:
        aws_iot.loop()
    except (ValueError, RuntimeError) as e:
        print("Failed to get data, retrying\n", e)
        wifi.reset()
        aws_iot.reconnect()
        continue
    time.sleep(1)
Attachments
a.png
a.png (105.29 KiB) Viewed 22 times

tcpipchip
 
Posts: 61
Joined: Sat Oct 13, 2018 8:00 pm

Re: Nina B302 + Wifinina (AWS IOT) problems

by tcpipchip on Wed May 13, 2020 4:24 pm

me again!
Why is SPLITING the broker ?
proto, dummy, broker = self.broker.split("/", 2)
The broker does not have "/"

tcpipchip
 
Posts: 61
Joined: Sat Oct 13, 2018 8:00 pm

Re: Nina B302 + Wifinina (AWS IOT) problems

by tannewt on Wed May 13, 2020 4:49 pm

It's probably expecting the broker value to start with something like "https://" or "mqtt://".

tannewt
 
Posts: 1644
Joined: Thu Oct 06, 2016 8:48 pm

Re: Nina B302 + Wifinina (AWS IOT) problems

by tcpipchip on Wed May 13, 2020 4:58 pm

Hi tannewt

Yes, i guess that is the problem too!

That is undocumented on Blog! Just now debugging the source code py

tcpipchip
 
Posts: 61
Joined: Sat Oct 13, 2018 8:00 pm

Re: Nina B302 + Wifinina (AWS IOT) problems

by tcpipchip on Wed May 13, 2020 4:59 pm

thanks to help me!

tcpipchip
 
Posts: 61
Joined: Sat Oct 13, 2018 8:00 pm

Re: Nina B302 + Wifinina (AWS IOT) problems

by tcpipchip on Wed May 13, 2020 5:26 pm

See attached!

https:// prefix was one of the problems!

Thank you!

Working!
Attachments
test.png
test.png (87.11 KiB) Viewed 18 times

tcpipchip
 
Posts: 61
Joined: Sat Oct 13, 2018 8:00 pm

Re: Nina B302 + Wifinina (AWS IOT) problems

by tcpipchip on Wed May 13, 2020 5:27 pm

Now i will control the CIRCUITPYTHON with ALEXA :)

tcpipchip
 
Posts: 61
Joined: Sat Oct 13, 2018 8:00 pm

Re: Nina B302 + Wifinina (AWS IOT) problems

by tcpipchip on Wed May 13, 2020 10:36 pm

Do you know if was implemented too the aws iot with Arduino Adafruit + Wifinina or only with circuitpython wifinina ?

tcpipchip
 
Posts: 61
Joined: Sat Oct 13, 2018 8:00 pm

Re: Nina B302 + Wifinina (AWS IOT) problems

by tcpipchip on Thu May 14, 2020 5:20 pm

Alexa now sending commands do CIRCUITPYTHON :)
Attachments
Sem título.png
Sem título.png (212.86 KiB) Viewed 11 times

tcpipchip
 
Posts: 61
Joined: Sat Oct 13, 2018 8:00 pm

Re: Nina B302 + Wifinina (AWS IOT) problems

by tcpipchip on Fri May 15, 2020 3:15 pm


tcpipchip
 
Posts: 61
Joined: Sat Oct 13, 2018 8:00 pm

Please be positive and constructive with your questions and comments.