#include

#define ARDUINOCC

#ifdef JNODE
#include
#endif

#ifdef ARDUINOCC
#include
#endif

#include
#include

#include “DHT.h”
#include

#define CLIENT_ID “Arduino2009MQTT”
#define TOPIC_1 “sensor/arduino/temperature”
#define TOPIC_2 “sensor/arduino/humidity”
#define TOPIC_A0 “sensor/arduino/voltage0”
#define TOPIC_A1 “sensor/arduino/voltage1”
#define PUBLISH_DELAY 30000
#define DHTGND 2
#define DHTPIN 3
#define DHTPWR 4
#define DHTTYPE DHT11

uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
uint8_t ip[] = { 192, 168, 19, 17 };
IPAddress mqttServer(192,168,8,100);
char server[] = “decdvd.noip.me”;

EthernetClient ethClient;
PubSubClient mqttClient;
//DHT dht(DHTPIN, DHTTYPE);
DHT_Unified dht(DHTPIN, DHTTYPE);

long previousMillis;

void setup() {

// setup serial communication
Serial.begin(9600);
while(!Serial) {};
Serial.println(F(“MQTT Arduino V1.2 20170907”));
Serial.println();

#ifdef ARDUINOCC
pinMode(DHTGND, OUTPUT);
digitalWrite(DHTGND,LOW);
pinMode(DHTPWR, OUTPUT);
digitalWrite(DHTPWR,HIGH);
#endif

// setup ethernet communication using DHCP
if(Ethernet.begin(mac) == 0) {
Serial.println(F(“Unable to configure Ethernet using DHCP”));
for(;;);
}
Serial.println(F(“Ethernet configured via DHCP”));
Serial.print(“IP address: “);
Serial.println(Ethernet.localIP());
Serial.println();

// setup mqtt client
mqttClient.setClient(ethClient);
// mqttClient.setServer(mqttServer, 2883);
mqttClient.setServer(server, 2883);
Serial.println(F(“MQTT client configured”));

// setup DHT sensor
dht.begin();
Serial.println(F(“DHT sensor initialized”));
sensor_t sensor;
dht.temperature().getSensor(&sensor);
Serial.println(“————————————“);
Serial.println(“Temperature”);
Serial.print (“Sensor: “); Serial.println(sensor.name);
Serial.print (“Driver Ver: “); Serial.println(sensor.version);
Serial.print (“Unique ID: “); Serial.println(sensor.sensor_id);
Serial.print (“Max Value: “); Serial.print(sensor.max_value); Serial.println(” *C”);
Serial.print (“Min Value: “); Serial.print(sensor.min_value); Serial.println(” *C”);
Serial.print (“Resolution: “); Serial.print(sensor.resolution); Serial.println(” *C”);
Serial.println(“————————————“);
// Print humidity sensor details.
dht.humidity().getSensor(&sensor);
Serial.println(“————————————“);
Serial.println(“Humidity”);
Serial.print (“Sensor: “); Serial.println(sensor.name);
Serial.print (“Driver Ver: “); Serial.println(sensor.version);
Serial.print (“Unique ID: “); Serial.println(sensor.sensor_id);
Serial.print (“Max Value: “); Serial.print(sensor.max_value); Serial.println(“%”);
Serial.print (“Min Value: “); Serial.print(sensor.min_value); Serial.println(“%”);
Serial.print (“Resolution: “); Serial.print(sensor.resolution); Serial.println(“%”);
Serial.println(“————————————“);

Serial.println();
Serial.println(F(“Ready to send data”));
previousMillis = millis();
}

void loop() {

// it’s time to send new data?
if(millis() – previousMillis > PUBLISH_DELAY) {
sendData();
previousMillis = millis();
}

mqttClient.loop();
}

void sendData() {
sensors_event_t event_t;
sensors_event_t event_h;
char msgBuffer[20];
char msgBuffer_h[20];
char msgBuffer_v0[20];
char msgBuffer_v1[20];

float V0 = analogRead(A0);
float V1 = analogRead(A1);

Serial.print(“Voltage0: “);
Serial.println(V0);
Serial.print(“Voltage1: “);
Serial.println(V1);

dht.temperature().getEvent(&event_t);
if (isnan(event_t.temperature)) {
Serial.println(“Error reading temperature!”);
}
else {
Serial.print(“Temperature: “);
Serial.print(event_t.temperature);
Serial.println(” *C”);
}

dht.humidity().getEvent(&event_h);
if (isnan(event_h.relative_humidity)) {
Serial.println(“Error reading humidity!”);
}
else {
Serial.print(“Humidity: “);
Serial.print(event_h.relative_humidity);
Serial.println(“%”);
}
Serial.println();

if(mqttClient.connect(“arduinoClient”, “root”, “viz0r3”)) {

mqttClient.publish(TOPIC_1, dtostrf(event_t.temperature, 6, 2, msgBuffer));
mqttClient.publish(TOPIC_2, dtostrf(event_h.relative_humidity, 6, 2, msgBuffer_h));
mqttClient.publish(TOPIC_A0, dtostrf(V0, 6, 2, msgBuffer));
mqttClient.publish(TOPIC_A1, dtostrf(V1, 6, 2, msgBuffer));
}
}

Annunci