add wqtt initialization

This commit is contained in:
interfisch 2023-05-10 17:44:21 +02:00
parent 1f9897d280
commit 60d75da482
4 changed files with 59 additions and 1 deletions

43
include/wifi_functions.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef _WIFI_FUNCTIONS_H_
#define _WIFI_FUNCTIONS_H_
#include <WiFi.h>
#include <MQTT.h>
#include "wifi_settings.h"
WiFiClient net;
MQTTClient client;
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("\nconnecting...");
while (!client.connect(client_id, "public", "public")) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
client.subscribe("/hello");
// client.unsubscribe("/hello");
}
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
// Note: Do not use the client in the callback to publish, subscribe or
// unsubscribe as it may cause deadlocks when other things arrive while
// sending and receiving acknowledgments. Instead, change a global variable,
// or push to a queue and handle it in the loop after calling `client.loop()`.
}
#endif

6
include/wifi_settings.h Normal file
View File

@ -0,0 +1,6 @@
const char ssid[] = "ssid";
const char pass[] = "pass";
const char mqtt_host[] = "10.0.0.1";
const char client_id[] = "hydroponic";

View File

@ -19,4 +19,5 @@ lib_deps =
https://github.com/milesburton/Arduino-Temperature-Control-Library/
d03n3rfr1tz3/HC-SR04@^1.1.2
https://github.com/emilv/ArduinoSort/
robtillaart/ADS1X15@^0.3.9
robtillaart/ADS1X15@^0.3.9
256dpi/MQTT@^2.5.1

View File

@ -1,5 +1,9 @@
#include <Arduino.h>
#include "wifi_functions.h"
bool debug=true; //print Serial information
#include "helpfunctions.h"
@ -44,6 +48,10 @@ void setup() {
pinMode(PIN_LED,OUTPUT);
digitalWrite(PIN_LED,LOW);
Serial.begin(115200);
WiFi.begin(ssid, pass);
client.begin(mqtt_host, net);
client.onMessage(messageReceived);
connect();
//init ADS1115
if (!ADS.begin()) {