Hi everyone,
I am trying to learn the IoT Cloud. I have an Arduino MKR WiFi 1010. I followed the tutorial by DroneBotWorkshop (Getting Started with the Arduino IoT Cloud), and everything worked well with that sketch. I took a sketch I had from this silly robot (https://create.arduino.cc/projecthub/gregalli/a-robot-friend-3a3725) and tried to edit it to allow me to activate the robot's wave via an IoT Dashboard button. Normally, the robot waves with a servo if you wave in front of its distance sensor.
Here is my code:
#include "arduino_secrets.h"
#include "thingProperties.h"
#include <Servo.h>
#define trig 11
#define echo 10
#define MAX_DISTANCE 200
Servo servo;
unsigned long durata;
float distanza;
float timeOut = MAX_DISTANCE * 60;
int soundVelocity = 340;
void setup() {
Serial.begin(9600);
delay(1500);
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(4);
ArduinoCloud.printDebugInfo();
servo.attach(9);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}
void loop() {
ArduinoCloud.update();
distanza = getSonar();
if(distanza < 30 && distanza != 0) {
sayHello();
}
}
void onWaveChange() {
sayHello();
}
void sayHello() {
for(int j=0; j<2; j++) {
for(int i= 40; i<100; i++) {
servo.write(i);
delay(10);
}
for(int i= 100; i>=40; i--) {
servo.write(i);
delay(10);
}
}
}
float getSonar() {
unsigned long pingTime;
float distance;
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
pingTime = pulseIn(echo, HIGH, timeOut);
distance = (float)pingTime * soundVelocity / 2 / 10000;
return distance;
}
And the thingProperties.h part:
// Code generated by Arduino IoT Cloud, DO NOT EDIT.
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
const char THING_ID[] = XXXXXXXXXXXXXXX;
const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_PASS; // Network password (use for WPA, or use as key for WEP)
void onWaveChange();
bool wave;
void initProperties(){
ArduinoCloud.setThingId(THING_ID);
ArduinoCloud.addProperty(wave, READWRITE, ON_CHANGE, onWaveChange);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
When I use this code, my Arduino has a hard time communicating with the MQTT Broker per the Serial Monitor:
***** Arduino IoT Cloud - configuration info *****
Device ID: XXXXXXXXXXXXXXXXXXXX
Thing ID:
MQTT Broker: mqtts-sa.iot.arduino.cc:8883
WiFi.status(): 0
Current WiFi Firmware: 1.4.8
Connected to "XXXXXXXXXXXX"
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to mqtts-sa.iot.arduino.cc:8883
ArduinoIoTCloudTCP::handle_ConnectMqttBroker 1 connection attempt at tick time 17704
The board constantly restarts after encountering this MQTT Broker issue. I did not have the problem with the MQTT Broker with the DroneBotWorkshop tutorial. I pretty much copied the pertinent IoT Cloud sections from the working tutorial sketch to my robot's sketch to make it work with the cloud service. Any thoughts on what I may be doing wrong?