element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • 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
Arduino Projects
  • Products
  • Arduino
  • Arduino Projects
  • More
  • Cancel
Arduino Projects
Blog Arduino Nano RP2040 Connect - MQTT
  • Blog
  • Forum
  • Documents
  • Events
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Arduino Projects requires membership for participation - click to join
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: ralphjy
  • Date Created: 12 Jun 2021 7:17 PM Date Created
  • Views 1981 views
  • Likes 7 likes
  • Comments 10 comments
  • mqtt
  • arduino nano rp2040 connect
  • u-blox nina 102 module
  • mbed
  • rp2040
Related
Recommended

Arduino Nano RP2040 Connect - MQTT

ralphjy
ralphjy
12 Jun 2021

I wanted to test connecting the Nano RP2040 Connect to my RPi4 MQTT server by publishing temperature and humidity data from a BME280 sensor.  I figured that this should be fairly easy since I've already used the BME280 with another RP2040 board - the SparkFun Pro Micro RP2040 and I've used my MQTT server with multiple ESP32 devices.

 

Of course, the devil is in the details...

 

I stayed within the confines of the Arduino IDE hoping that it would makes things simpler, and it did, but new board variants are coming out an incredible pace so things are still a mess.

 

The Nano RP2040 Connect is supported as arduino mbed_nano hardware and the Pro Micro RP2040 is supported as rp2040 hardware using the board library contributed by Earle Philhower.

 

The first problem I ran into was with the Wire library.  I was using functions to set the I2C pins that are not included in the mbed Wire library (Wire.setSDA(I2C_SDA), Wire.setSCL(I2C_SCL)).  I had used these functions to designate the QWIIC connector pins on the Pro Micro RP2040.  In mbed you need to instead do that as part of the interface declaration MbedI2C I2C(I2C_SDA, I2C_SCL).  Since I'm not using a connector this time, I just used the default I2C pins (i.e. removed the function calls).

 

The second problem that I ran into is that the Pro Micro uses the WiFi library and the Nano RP2040 Connect uses the WiFiNINA library.  I was setting up a static IP addr using the WiFi.config function and the WiFiNINA version of that function uses a different parameter order.  And the WiFiNINA function is also a Void rather than Bool (i.e. doesn't return a value).

 

For the WiFi library the function is WiFi.config(ip, gateway, subnet, dns).

For WiFiNINA library the function is WiFi.config(ip, dns, gateway, subnet).

 

The final problem was that I am using the dtostrf function to convert integers to strings for MQTT and I couldn't figure out what to include for mbed (it is supposed to be "mbed/dtostrf.h").  After struggling for a while, I took the easy way out and just added the function code to the sketch.

 

After fixing all that, it is working...

You don't have permission to edit metadata of this video.
Edit media
x
Upload Preview

 

Nano_RP2040_Connct_BME280_MQTT.ino

/*
 *  Created on: 06.11.2021
 *  
 * the arduino_secrets.h file:
 * #define SECRET_SSID ""    // network name
 * #define SECRET_PASS ""    // network password
 * #define SECRET_MQTT_USER "public" // broker username
 * #define SECRET_MQTT_PASS "public" // broker password 
 *  
 *
 */

#include <Arduino.h>
#include <stdio.h>
//#include "mbed/dtostrf.h"
#include <WiFiNINA.h>
#include <PubSubClient.h>
#include "arduino_secrets.h"
#include <Wire.h>
#include "Adafruit_Sensor.h"
#include <Adafruit_BME280.h>

#define USE_SERIAL Serial

// LED Pin
const int ledPin = LED_BUILTIN;

// Add your MQTT Broker IP address, example:
//const char* mqtt_server = "192.168.1.144";
const char* mqtt_server = "10.0.0.234";

// Set your Static IP address
IPAddress local_IP(10, 0, 0, 207);
// Set your Gateway IP address
IPAddress gateway(10, 0, 0, 1);

IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8);   //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional

WiFiClient rp2040Client;
PubSubClient client(rp2040Client);
long lastMsg = 0;
char msg[50];
int value = 0;

Adafruit_BME280 bme;

float tempBME = 0.0;
float humBME = 0.0;

char *dtostrf (double val, signed char width, unsigned char prec, char *sout) {
  char fmt[20];
  sprintf(fmt, "%%%d.%df", width, prec);
  sprintf(sout, fmt, val);
  return sout;
}

void setup() {
    USE_SERIAL.begin(115200);
//    while(!USE_SERIAL);
    delay(500);

    USE_SERIAL.println();
    USE_SERIAL.println();
    USE_SERIAL.println();
    
    if (!bme.begin(0x76)){  
        USE_SERIAL.println("Could not find a valid BME280 sensor, check wiring!");
        while (1);
    }    

    setup_wifi();
    client.setServer(mqtt_server, 1883);
    client.setCallback(callback);
  
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, LOW);
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(SECRET_SSID);

  WiFi.config(local_IP);

  WiFi.begin(SECRET_SSID, SECRET_PASS);

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

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;
  
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  // Feel free to add more if statements to control more GPIOs with MQTT

  // If a message is received on the topic floor2/output, you check if the message is either "on" or "off". 
  // Changes the output state according to the message
  if (String(topic) == "rp2040connect/output") {
    Serial.print("Changing output to ");
    if(messageTemp == "on"){
      Serial.println("on");
      digitalWrite(ledPin, HIGH);
    }
    else if(messageTemp == "off"){
      Serial.println("off");
      digitalWrite(ledPin, LOW);
    }
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("RP2040Client")) {
      Serial.println("connected");
      // Subscribe
      client.subscribe("rp2040connect/output");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void loop() {
    if (!client.connected()) {
      reconnect();
    }
    client.loop();
  
    long now = millis();
    if (now - lastMsg > 30000) {
      lastMsg = now;
    
      tempBME = bme.readTemperature()*1.8 + 32 - 5;
      humBME = bme.readHumidity() + 7;
  
      // Convert the value to a char array
      char tempString[8];
      dtostrf(tempBME, 1, 2, tempString);
      Serial.print("Temperature: ");
      Serial.println(tempString);
      client.publish("rp2040connect/temperature", tempString);
  
      // Convert the value to a char array
      char humString[8];
      dtostrf(humBME, 1, 2, humString);
      Serial.print("Humidity: ");
      Serial.println(humString);
      client.publish("rp2040connect/humidity", humString);
    }
        
    delay(1000);
}

Anonymous

Top Comments

  • mp2100
    mp2100 11 months ago in reply to ralphjy +2

    From what I read (at so many places I couldn't keep track), dtostrf() is an AVR specific library (Atmel 328 etc.), and avr/dtostrf is a work-around for the M0, M4 chips.  And then we get to these mbed…

  • mp2100
    mp2100 11 months ago +1

    I appreciate the reviews of these new boards.  And: good coding tip.  I was using dtostrf on my lora board and it’s not supported everywhere, and I got stuck.  Your programming skills are better than mine…

  • cstanton
    cstanton 11 months ago +1

    Do you get much use out of MQTT?

Parents
  • cstanton
    cstanton 11 months ago

    Do you get much use out of MQTT?

    • Cancel
    • Up +1 Down
    • Reply
    • More
    • Cancel
  • ralphjy
    ralphjy 11 months ago in reply to cstanton

    I actually use MQTT a lot.  It is a great way to communicate from one device to many devices and it can use local or cloud connections.

     

    Used with Node-RED, it is very useful for home automation.

     

    I recently used it with multiple remote wifi temperature sensors in an HVAC project.

    • Cancel
    • Up +1 Down
    • Reply
    • More
    • Cancel
Comment
  • ralphjy
    ralphjy 11 months ago in reply to cstanton

    I actually use MQTT a lot.  It is a great way to communicate from one device to many devices and it can use local or cloud connections.

     

    Used with Node-RED, it is very useful for home automation.

     

    I recently used it with multiple remote wifi temperature sensors in an HVAC project.

    • Cancel
    • Up +1 Down
    • Reply
    • More
    • Cancel
Children
No Data
Element14

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 © 2022 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

  • Facebook
  • Twitter
  • linkedin
  • YouTube