add wqtt initialization
This commit is contained in:
parent
1f9897d280
commit
60d75da482
|
@ -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
|
|
@ -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";
|
|
@ -19,4 +19,5 @@ lib_deps =
|
||||||
https://github.com/milesburton/Arduino-Temperature-Control-Library/
|
https://github.com/milesburton/Arduino-Temperature-Control-Library/
|
||||||
d03n3rfr1tz3/HC-SR04@^1.1.2
|
d03n3rfr1tz3/HC-SR04@^1.1.2
|
||||||
https://github.com/emilv/ArduinoSort/
|
https://github.com/emilv/ArduinoSort/
|
||||||
robtillaart/ADS1X15@^0.3.9
|
robtillaart/ADS1X15@^0.3.9
|
||||||
|
256dpi/MQTT@^2.5.1
|
|
@ -1,5 +1,9 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
|
||||||
|
#include "wifi_functions.h"
|
||||||
|
|
||||||
|
|
||||||
bool debug=true; //print Serial information
|
bool debug=true; //print Serial information
|
||||||
|
|
||||||
#include "helpfunctions.h"
|
#include "helpfunctions.h"
|
||||||
|
@ -44,6 +48,10 @@ void setup() {
|
||||||
pinMode(PIN_LED,OUTPUT);
|
pinMode(PIN_LED,OUTPUT);
|
||||||
digitalWrite(PIN_LED,LOW);
|
digitalWrite(PIN_LED,LOW);
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
WiFi.begin(ssid, pass);
|
||||||
|
client.begin(mqtt_host, net);
|
||||||
|
client.onMessage(messageReceived);
|
||||||
|
connect();
|
||||||
|
|
||||||
//init ADS1115
|
//init ADS1115
|
||||||
if (!ADS.begin()) {
|
if (!ADS.begin()) {
|
||||||
|
|
Loading…
Reference in New Issue