diff --git a/arduino/MQTT/MQTT.ino b/arduino/MQTT/MQTT.ino index 10100d9..49c938a 100644 --- a/arduino/MQTT/MQTT.ino +++ b/arduino/MQTT/MQTT.ino @@ -1,10 +1,17 @@ +#include "secret.h" #include + +#if isESP +#include +#else #include +#endif + #include #include #include #include "certificate.h" -#include "secret.h" + #define CERT mqtt_broker_cert #define MSG_BUFFER_SIZE (50) @@ -17,20 +24,15 @@ const char* mqttPassword = MQTT_ARDUINO_PASS; #define MAIN_CHANNEL "main/#" -WiFiClient wifiClient; WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP); -PubSubClient mqttClient(wifiClient); -#if hasRelayShield -const int RELAY_PIN_FAN1 = 1; -const int RELAY_PIN_FAN2 = 2; -const int RELAY_PIN_RADIO1 = 3; -const int RELAY_PIN_LAMP1 = 4; -const int RELAY_PIN_LAMP2 = 5; -// const int RELAY_PIN = 6; -// const int RELAY_PIN = 7; -// const int RELAY_PIN = 8; +#if isESP +WiFiClient espClient; +PubSubClient mqttClient(espClient); +#else +WiFiClient wifiClient; +PubSubClient mqttClient(wifiClient); #endif #if hasMotorshield @@ -73,13 +75,11 @@ void movePrinter(String topic, int payload) { #if hasRelayShield void togglePower(int pin, int payload) { - bool state = false; if (payload > 1) { - state = true; + digitalWrite(pin, HIGH); + } else { + digitalWrite(pin, LOW); } - - digitalWrite(pin, state); - delay(500); } #endif @@ -91,11 +91,14 @@ void callback(char* topic, uint8_t* payload, unsigned int length) { Serial.print("' with payload: "); String message = ""; + + for (unsigned int i = 0; i < length; i++) { message += (char)payload[i]; } Serial.println(message); + #if hasMotorshield if (t.indexOf("/printer/") > 0) { movePrinter(t, message.toInt()); @@ -104,20 +107,19 @@ void callback(char* topic, uint8_t* payload, unsigned int length) { #if hasRelayShield if (t.indexOf("/fan/one") > 0) { - togglePower(RELAY_PIN_FAN1, message.toInt()); + togglePower(PIN_FAN1, message.toInt()); } else if (t.indexOf("/fan/two") > 0) { - togglePower(RELAY_PIN_FAN2, message.toInt()); + togglePower(PIN_FAN2, message.toInt()); } else if (t.indexOf("/radio/one") > 0) { - togglePower(RELAY_PIN_RADIO1, message.toInt()); + togglePower(PIN_RADIO1, message.toInt()); } else if (t.indexOf("/lamp/one") > 0) { - togglePower(RELAY_PIN_LAMP1, message.toInt()); + togglePower(PIN_LAMP1, message.toInt()); } else if (t.indexOf("/lamp/two") > 0) { - togglePower(RELAY_PIN_LAMP2, message.toInt()); + togglePower(PIN_LAMP2, message.toInt()); } #endif } - void connect() { while (!mqttClient.connected()) { @@ -125,6 +127,7 @@ void connect() { String clientId = arduinoName; clientId += String(random(0xffff), HEX); + if (mqttClient.connect(clientId.c_str(), mqttUser, mqttPassword)) { Serial.println("connected"); mqttClient.subscribe(MAIN_CHANNEL); @@ -141,19 +144,16 @@ void connect() { void setup() { Serial.begin(115200); Serial.println("run setup"); - Serial.print("Connecting to "); - Serial.println(ssid); #if hasRelayShield - pinMode(RELAY_PIN_FAN1, OUTPUT); - pinMode(RELAY_PIN_FAN2, OUTPUT); - pinMode(RELAY_PIN_RADIO1, OUTPUT); - pinMode(RELAY_PIN_LAMP1, OUTPUT); - pinMode(RELAY_PIN_LAMP2, OUTPUT); + pinMode(PIN_FAN1, OUTPUT); + pinMode(PIN_FAN2, OUTPUT); + pinMode(PIN_RADIO1, OUTPUT); + pinMode(PIN_LAMP1, OUTPUT); + pinMode(PIN_LAMP2, OUTPUT); #endif #if hasMotorshield - Serial.println("i have a motorshield"); if (!AFMS.begin()) { Serial.println("Could not find Motor Shield. Check wiring."); while (1) @@ -176,21 +176,19 @@ void setup() { delay(50); Serial.print("."); } - Serial.println("----"); Serial.println("WiFi connected"); - Serial.println("----"); timeClient.begin(); - Serial.println("finish setup_wifi"); mqttClient.setServer(mqtt_server, mqtt_server_port); mqttClient.setCallback(callback); } void loop() { + Serial.println("loop"); if (!mqttClient.connected()) { connect(); } mqttClient.loop(); timeClient.update(); -} +} \ No newline at end of file diff --git a/arduino/MQTT/readme.md b/arduino/MQTT/readme.md index 216ad02..30149b4 100644 --- a/arduino/MQTT/readme.md +++ b/arduino/MQTT/readme.md @@ -10,15 +10,20 @@ The arduino listens to MQTT messages, and acts accordingly. Each arduino can ser #define VITRINE_WIFI_PASS "" #define MQTT_ARDUINO_USERNAME "" #define MQTT_ARDUINO_PASS "" +#define hasMotorshield false # Set this if the current arduino has motors attached +#define hasRelayShield false # Set this if the current arduino has power attached +#define arduinoName "motor-arduino" ``` +Here, you can also provide some information regarding the type of arduino, and which code you'd like to compile. + ### 2. Select your board to compile with. As I always forget which boards to select, here's my cheat sheet: | Arduino | Board | | ------------- | ------------- | | ArduinoWifi | LOLIN(WeMos) D1 R1 | -| ... | ... | +| ESP32 DEVKITV1 | DOIT ESP DEVKIT V1 | + + -### 3. Do something with the make files - arduino-cli compile -b arduino:avr:uno --build-property "build.extra_flags=\"-DMY_DEFINE=\"hello world\"\"" /home/user/Arduino/MySketch