├── .gitattributes ├── .gitignore └── MQTT_video └── MQTT_video.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /MQTT_video/MQTT_video.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | # define useCredentialsFile 5 | 6 | 7 | #ifdef useCredentialsFile 8 | #include 9 | #else 10 | mySSID = " "; 11 | myPASSWORD = " "; 12 | #endif 13 | 14 | 15 | AsyncMqttClient mqttClient; 16 | 17 | #define RELAY_PIN D7 18 | 19 | #define MQTT_RELAY1_TOPIC "LAB/LIGHT/MAIN/SWITCH" 20 | #define MQTT_RELAY2_TOPIC "LAB/LIGHT/BENCH" 21 | #define MQTT_FEEDBACK1_TOPIC "LAB/LIGHT/MAIN/FEEDBACK" 22 | #define MQTT_LASTWILL_TOPIC "LAB/LIGHT/lastwill" 23 | 24 | 25 | 26 | 27 | void setRelay(String command) { 28 | if (command == "ON") digitalWrite(RELAY_PIN, HIGH); 29 | else digitalWrite(RELAY_PIN, LOW); 30 | } 31 | 32 | void onMqttConnect(bool sessionPresent) { 33 | Serial.println("** Connected to the broker **"); 34 | 35 | mqttClient.subscribe(MQTT_RELAY1_TOPIC, 1); 36 | 37 | Serial.print("Subscribing : "); 38 | Serial.println(MQTT_RELAY1_TOPIC); 39 | } 40 | 41 | void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { 42 | Serial.println("** Disconnected from the broker **"); 43 | Serial.println("Reconnecting to MQTT..."); 44 | mqttClient.connect(); 45 | } 46 | 47 | void onMqttSubscribe(uint16_t packetId, uint8_t qos) { 48 | Serial.print("** Subscribe acknowledged **"); 49 | Serial.print(" packetId: "); 50 | Serial.print(packetId); 51 | Serial.print(" qos: "); 52 | Serial.println(qos); 53 | } 54 | 55 | void onMqttUnsubscribe(uint16_t packetId) { 56 | Serial.println("** Unsubscribe acknowledged **"); 57 | Serial.print(" packetId: "); 58 | Serial.println(packetId); 59 | } 60 | 61 | void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { 62 | 63 | Serial.println("** Message received **"); 64 | Serial.print(" topic: "); 65 | Serial.print(topic); 66 | Serial.print(" feedbackTopic: "); 67 | Serial.print(MQTT_RELAY1_TOPIC); 68 | Serial.print(" qos: "); 69 | Serial.print(properties.qos); 70 | Serial.print(" dup: "); 71 | Serial.print(properties.dup); 72 | Serial.print(" retain: "); 73 | Serial.print(properties.retain); 74 | Serial.print(" len: "); 75 | Serial.print(len); 76 | Serial.print(" index: "); 77 | Serial.print(index); 78 | Serial.print(" payload: "); 79 | Serial.println(payload); 80 | 81 | if (strcmp(topic, MQTT_RELAY1_TOPIC) == 0) { 82 | setRelay(payload); 83 | mqttClient.publish(MQTT_FEEDBACK1_TOPIC, 1, false, payload); 84 | Serial.println("Publishing Feedback"); 85 | } 86 | } 87 | 88 | void onMqttPublish(uint16_t packetId) { 89 | Serial.print("** Published"); 90 | Serial.print(" packetId: "); 91 | Serial.print(packetId); 92 | Serial.print("\n\n"); 93 | } 94 | 95 | void setup() { 96 | Serial.begin(115200); 97 | Serial.println(""); 98 | Serial.println("Start"); 99 | 100 | // Relay PIN 101 | pinMode(RELAY_PIN, OUTPUT); 102 | digitalWrite(RELAY_PIN, LOW); 103 | WiFi.persistent(false); 104 | 105 | WiFi.mode(WIFI_STA); 106 | Serial.print("Connecting to Wi-Fi"); 107 | WiFi.begin(mySSID, myPASSWORD); 108 | 109 | while (WiFi.status() != WL_CONNECTED) { 110 | delay(500); 111 | Serial.print("."); 112 | } 113 | 114 | Serial.println(" OK"); 115 | 116 | mqttClient.onConnect(onMqttConnect); 117 | mqttClient.onDisconnect(onMqttDisconnect); 118 | // mqttClient.onSubscribe(onMqttSubscribe); 119 | // mqttClient.onUnsubscribe(onMqttUnsubscribe); 120 | mqttClient.onMessage(onMqttMessage); 121 | mqttClient.onPublish(onMqttPublish); 122 | mqttClient.setServer(IPAddress(192, 168, 0, 201), 1883); 123 | mqttClient.setKeepAlive(5).setCleanSession(false).setWill(MQTT_LASTWILL_TOPIC, 2, true, "my wife will get all my money").setCredentials("username", "password").setClientId("m"); 124 | Serial.println("Connecting to MQTT..."); 125 | mqttClient.connect(); 126 | } 127 | 128 | void loop() { 129 | } 130 | --------------------------------------------------------------------------------