// ArduinoMqttClient - Version: Latest
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include <ArduinoMqttClient.h>
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the Wifi radio's status
// ---------------------------------------------------------------- //
// Arduino Ultrasoninc Sensor HC-SR04
// Re-writed by Arbi Abdul Jabbaar
// Using Arduino IDE 1.8.7
// Using HC-SR04 Module
// Tested on 17 September 2019
// ---------------------------------------------------------------- //
#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "ip";
int port = port;
const char topic[] = "avstand_i_garasje";
//const char topic2[] = "real_unique_topic_2";
//const char topic3[] = "real_unique_topic_3";
//set interval for sending messages (milliseconds)
const long interval = 8000;
unsigned long previousMillis = 0;
int count = 0;
void setup() {
//Initialize serial and wait for port to open:
// Serial.begin(9600);
// while (!Serial);
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to network: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.println("You're connected to the network");
Serial.println("----------------------------------------");
printData();
Serial.println("----------------------------------------");
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
Serial.println("with Arduino UNO R3");
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
while (1);
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
}
void loop() {
// check the network connection once every 10 seconds:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to network: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
delay(5000);
if (!mqttClient.connected()) {
Serial.println("mqtt client not connected");
//
mqttClient.connect(broker, port);
delay(5000);
}
delay(1000);
printData();
Serial.println("----------------------------------------");
// wait 10 seconds for connection:
delay(10000);
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
}
void printData() { /*
Serial.println("Board Information:");
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
Serial.println();
Serial.println("Network Information:");
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
*/
// call poll() regularly to allow the library to send MQTT keep alive which
// avoids being disconnected by the broker
mqttClient.poll();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time a message was sent
previousMillis = currentMillis;
//record random value from A0, A1 and A2
int Rvalue = (distance) ;//analogRead(A0);
// int Rvalue2 = analogRead(A1);
// int Rvalue3 = analogRead(A2);
Serial.print("Sending message to topic: ");
Serial.println(topic);
Serial.println(Rvalue);
mqttClient.beginMessage(topic);
mqttClient.print(Rvalue);
mqttClient.endMessage();
Serial.println();
}
}
/*
void connectMQTTClientIfNeeded() {
if (!mqttClient.connected()) {
Serial.println("mqtt client not connected");
//
mqttClient.connect(broker, port);
}
//
// tasks.after(30000, connectMQTTClientIfNeeded); // after 30 seconds call connectMQTTClientIfNeeded() again
}
*/
(Så skal jeg naturligvis rydde opp litt i koden når jeg har fått det til å virke som jeg vil. Får skylde på at jeg er litt i tåka etter Corona...
Hei, kopierte linjene fra void setup():
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to network: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
delay(5000);
)
if (!mqttClient.connected()) {
Serial.println("mqtt client not connected");
//
mqttClient.connect(broker, port);
delay(5000);
}
Der sjekkes det om WiFi er koblet til og det blir ikke gjort noe i koden før du har tilkobling til WiFi. Det sjekkes så om MQTT er koblet og hvis det ikke er det så kobles det til igjen. Denne kan kanskje legges i en While loop slik at den ikke går videre før MQTT er koblet til igjen.
Dette er ikke en god løsning hvis det skal styres noe lokalt feks uavhengig av WiFi og MQTT, men i dette tilfellet går det greit