4- Connect Arduino Ethernet over MQTT

The following guide shows how to publish data to the AskSensors IoT platform using Arduino Ethernet shield and MQTT.

Connect Arduino Ethernet To AskSensors Over MQTT

a) Prerequisites :

b) Required Hardwares :

  • Computer running Arduino software (version 1.8.7 or higher).
  • Arduino Uno.
  • Arduino Ethernet Shield
  • USB micro cable to connect the ESP8266 board to the computer.
  • RJ45 Ethernet Cable

c) Software:

The provided code includes the libraries for both connecting to a Ethernet network and to publish data to AskSensors using MQTT.
You need to fill the following:

    • The Api Key In and username to set the MQTT Topic:publish/username/apiKeyIn
    • A fixed IP address.

const char* username = "................."; // my AskSensors username
const char* pubTopic = "publish/..../....."; // publish/username/apiKeyIn
IPAddress ip(..., ..., ..., ...); // IP address

d) Run the code:

  • Connect your Arduino board to the computer via serial USB and upload the code using the Arduino IDE.
  • Connect your Arduino Ethernet shield to the network through Ethernet cable
  • Reset the board
  • Return back to your sensor page on AskSensors, Click on’Show Graph’ and select the chart type (such as Line, Bar, Binary..) to visualize your sensor data stream in graph.
  • Open a serial terminal. You can cross-check the AskSensors graph readings with the values being printed on your Arduino Terminal.

e) Source Code:

A basic source code is shown below. Please refer to the AskSensors Github page to get the latest version and updates.


/*
* MQTT and AskSensors IoT Platform
* Description: Arduino Ethernet publishes data to AskSensors using MQTT
* Author: https://asksensors.com, 2020
* github: https://github.com/asksensors
*/
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// TODO: AskSensors MQTT user config
const char* username = "................."; // my AskSensors username
const char* pubTopic = "publish/..../....."; // publish/username/apiKeyIn
const unsigned int writeInterval = 25000; // write interval (in ms)
// MQTT host config
const char* mqtt_server = "mqtt.asksensors.com";
unsigned int mqtt_port = 1883;
// ETHERNET config.
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(..., ..., ..., ...); // TODO: Add the IP address
EthernetClient askClient;
PubSubClient client(askClient);
void setup() {
Serial.begin(115200);
Serial.println("*****************************************************");
Serial.println("********** Program Start : Arduino Ethernet publishes data to AskSensors over MQTT");
Serial.print("********** connecting to Ethernet : ");
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
}

void loop() {
if (!client.connected())
reconnect();
client.loop();
Serial.println("********** Publish MQTT data to ASKSENSORS");
char mqtt_payload[30] = "";
snprintf (mqtt_payload, 100, "m1=%ld&m2=%ld", random(10,100), random(10,100) );
Serial.print("Publish message: ");
Serial.println(mqtt_payload);
client.publish(pubTopic, mqtt_payload);
Serial.println("> MQTT data published");
Serial.println("********** End ");
Serial.println("*****************************************************");
delay(writeInterval);// delay
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("********** Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ethClient",username, "")) {
Serial.println("-> MQTT client connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println("-> try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

Was this article helpful to you? Yes No 1

How can we help?