From 55e58680ac41ed113300c3829d6d9efb7e865fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniele=20Vigan=C3=B2?= Date: Mon, 8 Jun 2020 09:51:46 +0200 Subject: [PATCH] Add MQTT support on Arduino --- arduino/tempLightSensor/tempLightSensor.ino | 44 +++++++++++++++++---- docker/docker-compose.yml | 23 +++++++++-- docker/rabbitmq/enabled_plugins | 1 + 3 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 docker/rabbitmq/enabled_plugins diff --git a/arduino/tempLightSensor/tempLightSensor.ino b/arduino/tempLightSensor/tempLightSensor.ino index 3956ccd..fd06f85 100644 --- a/arduino/tempLightSensor/tempLightSensor.ino +++ b/arduino/tempLightSensor/tempLightSensor.ino @@ -1,12 +1,16 @@ #include #include #include +#include #include #include -#define DEBUG_TO_SERIAL 1 +#define DEBUG_TO_SERIAL 1 // debug on serial port +#define USE_MQTT 0 // use mqtt protocol instead of http post #define USE_INTERNAL_NTP 0 // use default ntp server or the internal one -#define AREF_VOLTAGE 3.3 +#define AREF_VOLTAGE 3.3 // set aref voltage to 3.3v instead of default 5v + +char serial[9]; // const String serverName = "sensor.server.domain"; const size_t capacity = 2 * JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(2) + 20; @@ -17,11 +21,13 @@ JsonObject temp = payload.createNestedObject("temperature"); unsigned int counter = 0; - EthernetUDP ntpUDP; NTPClient timeClient(ntpUDP); bool NTPValid = false; +EthernetClient ethClient; +PubSubClient clientMQTT(ethClient); + struct netConfig { IPAddress address; unsigned int port; @@ -39,8 +45,6 @@ void setup(void) { StaticJsonDocument<20> api; byte mac[6]; - char serial[9]; - int eeAddress = 0; EEPROM.get(eeAddress, mac); @@ -60,10 +64,10 @@ void setup(void) { Serial.print("IoT #"); Serial.print(serial); - Serial.println(" at address:"); + Serial.print(" at address: "); Serial.println(Ethernet.localIP()); Serial.println(); - Serial.println("Connecting to:"); + Serial.print("Connecting to: "); Serial.print(config.address); Serial.print(":"); Serial.println(config.port); @@ -85,6 +89,10 @@ void setup(void) { telemetry["device"] = serial; // payload["id"] = serverName; + +#if USE_MQTT + clientMQTT.setServer(config.address, 1883); +#endif } void loop(void) { @@ -107,7 +115,11 @@ void loop(void) { temp["raw"] = tempReading; temp["volts"] = tempVoltage; +#if USE_MQTT + publishData(config, telemetry); +#else postData(config, telemetryURL, telemetry); +#endif if (counter == 6 * 120) { // Update clock every 6 times * 10 sec * 120 minutes = 2 hrs timeClient.update(); @@ -122,6 +134,22 @@ void loop(void) { delay(postDelay); } +#if USE_MQTT +void publishData(const netConfig &mqtt, const DynamicJsonDocument &json) { + if (clientMQTT.connect(serial, "freedcs", "password")) { + char buffer[256]; + serializeJson(json, buffer); + clientMQTT.publish(serial, buffer); + +#if DEBUG_TO_SERIAL + Serial.println("DEBUG: MQTT PUBLISH>>>"); + serializeJsonPretty(json, Serial); + Serial.println("\n<<<"); +#endif + } +} +#endif + void postData(const netConfig &postAPI, const String &URL, const DynamicJsonDocument &json) { if (EthernetClient client = client.connect(postAPI.address, postAPI.port)) { client.print("POST "); @@ -140,7 +168,7 @@ void postData(const netConfig &postAPI, const String &URL, const DynamicJsonDocu client.stop(); #if DEBUG_TO_SERIAL - Serial.println("DEBUG: >>>"); + Serial.println("DEBUG: HTTP POST>>>"); serializeJsonPretty(json, Serial); Serial.println("\n<<<"); #endif diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 191b27f..19a9a53 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -32,18 +32,35 @@ services: ports: - "127.0.0.1:5432:5432" + # mosquitto simple deployment + # mqtt: + # <<: *service_default + # # image: vernemq/vernemq + # # environment: + # # DOCKER_VERNEMQ_ALLOW_ANONYMOUS: "on" + # # DOCKER_VERNEMQ_ACCEPT_EULA: "yes" + # image: eclipse-mosquitto + # networks: + # - net + # ports: + # - "1883:1883" + # - "9001:9001" + rabbitmq: <<: *service_default - image: rabbitmq:3-management + image: rabbitmq:3-management-alpine environment: - RABBITMQ_DEFAULT_VHOST: "freedcs" + # RABBITMQ_DEFAULT_VHOST: "freedcs" RABBITMQ_DEFAULT_USER: "freedcs" RABBITMQ_DEFAULT_PASS: "password" + volumes: + - ./rabbitmq/enabled_plugins:/etc/rabbitmq/enabled_plugins networks: - net ports: - - "15672:15672" + - "1883:1883" - "5672:5672" + - "15672:15672" edge: <<: *service_default diff --git a/docker/rabbitmq/enabled_plugins b/docker/rabbitmq/enabled_plugins new file mode 100644 index 0000000..5358cb0 --- /dev/null +++ b/docker/rabbitmq/enabled_plugins @@ -0,0 +1 @@ +[rabbitmq_management,rabbitmq_mqtt].