element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Design For A Cause 2021
  • Challenges & Projects
  • Design Challenges
  • Design For A Cause 2021
  • More
  • Cancel
Design For A Cause 2021
Blog Connecting the Arduino Nano 33 IoT with Local MQTT Broker (2)
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: ahmadradhy
  • Date Created: 5 Apr 2021 4:47 PM Date Created
  • Views 5513 views
  • Likes 4 likes
  • Comments 0 comments
Related
Recommended

Connecting the Arduino Nano 33 IoT with Local MQTT Broker (2)

ahmadradhy
ahmadradhy
5 Apr 2021

Arduino Nano 33 IoT is advanced Arduino dev kit designed for IoT implementation. It has a network module such as WiFi and BLE, allowing us to send the data to Arduino cloud over MQTT and HTTP by using internet connectivity. This project is to aim to how we could integrate LSM6DS3 (accelerometer and gyroscope) module sensor embedded on Arduino nano 33 IoT, so this dev kit is very easy to do an experiment.  We will implement it to connect with the cloud. so then data sent to cloud, accelerometer, and gyroscope data will be visualized on the web dashboard.

 

image

 

MQTT is an OASIS standard messaging protocol for the Internet of Things (IoT). It is designed as an extremely lightweight publish/subscribe messaging transport that is ideal for connecting remote devices with a small code footprint and minimal network bandwidth. MQTT today is used in a wide variety of industries, such as automotive, manufacturing, telecommunications, oil and gas, etc.  https://mqtt.org/

 

You can copy this source code for running arduino nano 33 IoT

 

 

#include <Arduino.h>

 

#include <SPI.h>

#include <Wire.h>

#include <Arduino_LSM6DS3.h>

#include <ArduinoJson.h>

#include <PubSubClient.h>

#include <WiFiNINA.h>


#define CONVERT_G_TO_MS2 9.80665f

#define FREQUENCY_HZ 104

#define INTERVAL_MS (1000 / (FREQUENCY_HZ + 1))


struct Acc_senseData{


  float acc_x = 0.0F;

  float acc_y = 0.0F;

  float acc_z = 0.0F;


};


struct Gyr_senseData

{

  float gyr_x = 0.0F;

  float gyr_y = 0.0F;

  float gyr_z = 0.0F;

};



void setup_wifi();

void reconnect();


static char payload[256];

static Acc_senseData acc_data;

static Gyr_senseData gyr_data;

StaticJsonDocument<256> doc;


#define TOKEN ""

#define DEVICEID ""


const char* ssid = "";

const char* password = "";

const char mqtt_server[] = "";

const char publishTopic[] = "v1/devices/me/telemetry";


WiFiClient wifiClient;

PubSubClient mqtt(wifiClient);


void setup_wifi(){


  delay(10);

  Serial.println();

  Serial.print("Connecting to ");

  Serial.println(ssid);

  WiFi.begin(ssid, password);


  while( WiFi.status() != WL_CONNECTED){


    delay(500);

    Serial.print("."); 

  }


  randomSeed(micros());

  Serial.println("");

  Serial.println("WiFi connected");

  Serial.println("IP address: ");

  Serial.println(WiFi.localIP());

 

}


void reconnect(){


  while(!mqtt.connected()){

  

    //Serial.print("Attempting MQTT connection ....");

    //String clientID = "nano33_accelerometer-";

    //clientID += String(random(0xffff), HEX);

  

    if (mqtt.connect(DEVICEID, TOKEN, NULL)) {

    

      Serial.println("Connected to MQTT broker");

      digitalWrite(LED_BUILTIN, HIGH);

    }


    else

    {

      Serial.print("failed to connect to MQTT broker, rc=");

      Serial.print(mqtt.state());

      Serial.println("try again in 5 second");

      digitalWrite(LED_BUILTIN, LOW);

      delay(5000);


    }

     

  }

 

}


void setup() {


  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);

  while (!Serial);

 

  if (!IMU.begin())

  {

    Serial.println("Failed to initialize IMU!");

    while(1);

  }


  setup_wifi();

  mqtt.setServer(mqtt_server, 1883);


}


void loop() {


  if (!mqtt.connected())

  {

    reconnect();

  }


  mqtt.loop();


  static unsigned long last_interval_ms = 0;


  float a_x, a_y, a_z;

  float g_x, g_y, g_z;


  if (millis() > last_interval_ms + INTERVAL_MS)

  {


    last_interval_ms = millis();


    IMU.readAcceleration(a_x, a_y, a_z);

    acc_data.acc_x = a_x;

    acc_data.acc_y = a_y;

    acc_data.acc_z = a_z;

    doc["ACC_X"] = acc_data.acc_x * CONVERT_G_TO_MS2;

    doc["ACC_Y"] = acc_data.acc_y * CONVERT_G_TO_MS2;

    doc["ACC_Z"] = acc_data.acc_z * CONVERT_G_TO_MS2;


    IMU.readGyroscope(g_x, g_y, g_z);

    gyr_data.gyr_x = g_x;

    gyr_data.gyr_y = g_y;

    gyr_data.gyr_z = g_z;

    doc["GYR_X"] = gyr_data.gyr_x;

    doc["GYR_Y"] = gyr_data.gyr_y;

    doc["GYR_Z"] = gyr_data.gyr_z;


    serializeJsonPretty(doc, payload);

    mqtt.publish(publishTopic, payload);

    Serial.println(payload);

 

  }


}

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube