├── .gitignore ├── Devices ├── airConditioner │ └── airConditioner.ino ├── analogSensor │ └── analogSensor.ino ├── blinds │ └── blinds.ino ├── camera │ └── camera.ino ├── contactSensor │ └── contactSensor.ino ├── door │ └── door.ino ├── doorbell │ └── doorbell.ino ├── electrometer │ └── electrometer.ino ├── fan │ └── fan.ino ├── gasmeter │ └── gasmeter.ino ├── light │ └── light.ino ├── lightGroup │ └── lightGroup.ino ├── lightRgb │ └── lightRgb.ino ├── lock │ └── lock.ino ├── motionSensor │ └── motionSensor.ino ├── sprinkler │ └── sprinkler.ino ├── switch │ └── switch.ino ├── switchGroup │ └── switchGroup.ino ├── temperatureSensor │ └── temperatureSensor.ino ├── thermostat │ └── thermostat.ino ├── tv │ └── tv.ino └── watermeter │ └── watermeter.ino ├── README.md └── Tutorials ├── Blink ├── Blink.ino └── README.md ├── Doorbell-Door ├── Doorbell-Door.ino └── README.md ├── Doorbell ├── Doorbell.ino └── README.md ├── Plant-Watering ├── Plant-Watering.ino └── README.md ├── Raspberry-Blink ├── README.md ├── app.js ├── package-lock.json └── package.json ├── Raspberry-Doorbell ├── README.md ├── app.js ├── package-lock.json └── package.json ├── Raspberry-DoorbellLock ├── README.md ├── app.js ├── package-lock.json └── package.json ├── Raspberry-MultipleDevices ├── README.md ├── app.js ├── package-lock.json └── package.json ├── Raspberry-switchGroup ├── app.js └── package.json ├── Relay-Module ├── README.md └── Relay-Module.ino ├── Smart-Button ├── 3DButton.stl ├── README.md └── Smart-Button.ino └── Tower-Fan ├── README.md └── Tower-Fan.ino /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .DS_Store 3 | .vscode/ 4 | node_modules/ -------------------------------------------------------------------------------- /Devices/airConditioner/airConditioner.ino: -------------------------------------------------------------------------------- 1 | //#include "WiFi.h" //Uncomment for ESP32 2 | #include //Comment for ESP-32 3 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 4 | #include 5 | 6 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 7 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 8 | #define MQTT_BROKER "smartnest.cz" // Broker host 9 | #define MQTT_PORT 1883 // Broker port 10 | #define MQTT_USERNAME "username" // Username from Smartnest 11 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 12 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 13 | #define FIRMWARE_VERSION "Example-airConditioner" // Custom name for this program 14 | 15 | WiFiClient espClient; 16 | PubSubClient client(espClient); 17 | int airConditionerPin = 2; 18 | int sensorpin = A0; 19 | 20 | char msg[5]; 21 | double temp = 25; 22 | double setpoint = 25; 23 | double lastTimeSent = 0; 24 | 25 | void startWifi(); 26 | void startMqtt(); 27 | void checkMqtt(); 28 | void mqttLoop(); 29 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 30 | void callback(char* topic, byte* payload, unsigned int length); 31 | 32 | void turnOn(); 33 | void turnOff(); 34 | void setSetpoint(double temp); 35 | void reportTemperature(double temp); 36 | void sendToBroker(char* topic, char* message); 37 | 38 | void setup() { 39 | pinMode(airConditionerPin, OUTPUT); 40 | pinMode(sensorpin, INPUT); 41 | Serial.begin(115200); 42 | startWifi(); 43 | startMqtt(); 44 | } 45 | 46 | void loop() { 47 | client.loop(); 48 | checkMqtt(); 49 | reportTemperature(temp); 50 | } 51 | 52 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 53 | 54 | int tokensNumber = 10; 55 | char message[length + 1]; 56 | char* tokens[tokensNumber]; 57 | 58 | Serial.print("MQTT Message Received. Topic:"); 59 | Serial.println(topic); 60 | Serial.print("Message:"); 61 | Serial.println(message); 62 | 63 | splitTopic(topic, tokens, tokensNumber); 64 | 65 | sprintf(message, "%c", (char)payload[0]); 66 | for (int i = 1; i < length; i++) { 67 | sprintf(message, "%s%c", message, (char)payload[i]); 68 | } 69 | 70 | //------------------Decide what to do depending on the topic and message--------------------------------- 71 | 72 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { // Turn On or OFF 73 | 74 | if (strcmp(message, "ON") == 0) { 75 | turnOn(); 76 | } else if (strcmp(message, "OFF") == 0) { 77 | turnOff(); 78 | } 79 | 80 | } else if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "setpoint") == 0) { // Set Setpoint Temperature 81 | 82 | double value = atof(message); 83 | if (isnan(value)) { 84 | setpoint = 0; 85 | } 86 | 87 | setSetpoint(value); 88 | 89 | sprintf(topic, "%s/report/setpoint", MQTT_CLIENT); 90 | client.publish(topic, message); 91 | } 92 | } 93 | 94 | void startWifi() { 95 | WiFi.mode(WIFI_STA); 96 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 97 | Serial.println("Connecting ..."); 98 | 99 | while (WiFi.status() != WL_CONNECTED) { 100 | delay(500); 101 | Serial.print("."); 102 | } 103 | 104 | Serial.println('\n'); 105 | Serial.print("Connected to "); 106 | Serial.println(WiFi.SSID()); 107 | Serial.print("IP address:\t"); 108 | Serial.println(WiFi.localIP()); 109 | delay(500); 110 | } 111 | 112 | void startMqtt() { 113 | client.setServer(MQTT_BROKER, MQTT_PORT); 114 | client.setCallback(callback); 115 | 116 | while (!client.connected()) { 117 | Serial.println("Connecting to MQTT..."); 118 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 119 | Serial.println("connected"); 120 | } else { 121 | if (client.state() == 5) { 122 | Serial.println("Connection not allowed by broker, possible reasons:"); 123 | Serial.println("- Device is already online. Wait some seconds until it appears offline"); 124 | Serial.println("- Wrong Username or password. Check credentials"); 125 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 126 | 127 | } else { 128 | Serial.println("Not possible to connect to Broker Error code:"); 129 | Serial.print(client.state()); 130 | } 131 | 132 | delay(0x7530); 133 | } 134 | } 135 | 136 | char subscibeTopic[100]; 137 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 138 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 139 | 140 | sendToBroker("report/online", "true"); // Reports that the device is online 141 | delay(100); 142 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 143 | delay(100); 144 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 145 | delay(100); 146 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 147 | delay(100); 148 | 149 | char signal[5]; 150 | sprintf(signal, "%d", WiFi.RSSI()); 151 | sendToBroker("report/signal", signal); // Reports the signal strength 152 | delay(100); 153 | } 154 | 155 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 156 | const char s[2] = "/"; 157 | int pos = 0; 158 | tokens[0] = strtok(topic, s); 159 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 160 | pos++; 161 | tokens[pos] = strtok(NULL, s); 162 | } 163 | return pos; 164 | } 165 | 166 | void checkMqtt() { 167 | if (!client.connected()) { 168 | startMqtt(); 169 | } 170 | } 171 | 172 | void turnOff() { 173 | digitalWrite(airConditionerPin, HIGH); 174 | sendToBroker("report/powerState", "OFF"); 175 | } 176 | 177 | void turnOn() { 178 | digitalWrite(airConditionerPin, LOW); 179 | sendToBroker("report/powerState", "ON"); 180 | } 181 | 182 | void setSetpoint(double temp) { 183 | Serial.printf("setpoint changed to %3.2f", temp); 184 | //Do something with the value, turn on AC? 185 | } 186 | 187 | void reportTemperature(double temp) { 188 | if (millis() - lastTimeSent > 30000 && client.connected()) { //Makes sure not to send reports too often 189 | sprintf(msg, "%d", temp); 190 | sendToBroker("report/temperature", msg); 191 | lastTimeSent = millis(); 192 | } 193 | } 194 | 195 | void sendToBroker(char* topic, char* message) { 196 | if (client.connected()) { 197 | char topicArr[100]; 198 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 199 | client.publish(topicArr, message); 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /Devices/analogSensor/analogSensor.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | #include 5 | 6 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 7 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 8 | #define MQTT_BROKER "smartnest.cz" // Broker host 9 | #define MQTT_PORT 1883 // Broker port 10 | #define MQTT_USERNAME "username" // Username from Smartnest 11 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 12 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 13 | #define FIRMWARE_VERSION "Example-analogSensor" // Custom name for this program 14 | 15 | WiFiClient espClient; 16 | PubSubClient client(espClient); 17 | int sensorPin = 0; 18 | int value = 10; 19 | int interval = 20; 20 | Ticker sendUpdate; 21 | 22 | void startWifi(); 23 | void startMqtt(); 24 | void checkMqtt(); 25 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 26 | void callback(char* topic, byte* payload, unsigned int length); 27 | void sendToBroker(char* topic, char* message); 28 | 29 | void turnOff(); 30 | void turnOn(); 31 | void sendValue(); 32 | 33 | void setup() { 34 | pinMode(sensorPin, INPUT); 35 | Serial.begin(115200); 36 | startWifi(); 37 | startMqtt(); 38 | } 39 | 40 | void loop() { 41 | client.loop(); 42 | checkMqtt(); 43 | } 44 | 45 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 46 | Serial.print("Topic:"); 47 | Serial.println(topic); 48 | int tokensNumber = 10; 49 | char* tokens[tokensNumber]; 50 | char message[length + 1]; 51 | splitTopic(topic, tokens, tokensNumber); 52 | sprintf(message, "%c", (char)payload[0]); 53 | for (int i = 1; i < length; i++) { 54 | sprintf(message, "%s%c", message, (char)payload[i]); 55 | } 56 | Serial.print("Message:"); 57 | Serial.println(message); 58 | 59 | //------------------ACTIONS HERE--------------------------------- 60 | 61 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 62 | if (strcmp(message, "ON") == 0) { 63 | turnOn(); 64 | } else if (strcmp(message, "OFF") == 0) { 65 | turnOff(); 66 | } 67 | } 68 | } 69 | 70 | void startWifi() { 71 | WiFi.mode(WIFI_STA); 72 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 73 | Serial.println("Connecting ..."); 74 | int attempts = 0; 75 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 76 | attempts++; 77 | delay(500); 78 | Serial.print("."); 79 | } 80 | 81 | if (WiFi.status() == WL_CONNECTED) { 82 | Serial.println('\n'); 83 | Serial.print("Connected to "); 84 | Serial.println(WiFi.SSID()); 85 | Serial.print("IP address:\t"); 86 | Serial.println(WiFi.localIP()); 87 | 88 | } else { 89 | Serial.println('\n'); 90 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 91 | } 92 | 93 | delay(500); 94 | } 95 | 96 | void startMqtt() { 97 | client.setServer(MQTT_BROKER, MQTT_PORT); 98 | client.setCallback(callback); 99 | 100 | while (!client.connected()) { 101 | Serial.println("Connecting to MQTT..."); 102 | 103 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 104 | Serial.println("connected"); 105 | sendUpdate.attach(interval, sendValue); 106 | 107 | } else { 108 | if (client.state() == 5) { 109 | Serial.println("Connection not allowed by broker, possible reasons:"); 110 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 111 | Serial.println("- Wrong Username or password. Check credentials"); 112 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 113 | 114 | } else { 115 | Serial.println("Not possible to connect to Broker Error code:"); 116 | Serial.print(client.state()); 117 | } 118 | 119 | delay(0x7530); 120 | } 121 | } 122 | 123 | char subscibeTopic[100]; 124 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 125 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 126 | 127 | sendToBroker("report/online", "true"); // Reports that the device is online 128 | delay(100); 129 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 130 | delay(100); 131 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 132 | delay(100); 133 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 134 | delay(100); 135 | 136 | char signal[5]; 137 | sprintf(signal, "%d", WiFi.RSSI()); 138 | sendToBroker("report/signal", signal); // Reports the signal strength 139 | delay(100); 140 | } 141 | 142 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 143 | const char s[2] = "/"; 144 | int pos = 0; 145 | tokens[0] = strtok(topic, s); 146 | 147 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 148 | pos++; 149 | tokens[pos] = strtok(NULL, s); 150 | } 151 | 152 | return pos; 153 | } 154 | 155 | void checkMqtt() { 156 | if (!client.connected()) { 157 | Serial.printf("Device lost connection with broker status %, reconnecting...\n", client.connected()); 158 | 159 | if (WiFi.status() != WL_CONNECTED) { 160 | startWifi(); 161 | } 162 | 163 | sendUpdate.detach(); 164 | startMqtt(); 165 | } 166 | } 167 | 168 | void sendToBroker(char* topic, char* message) { 169 | if (client.connected()) { 170 | char topicArr[100]; 171 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 172 | client.publish(topicArr, message); 173 | } 174 | } 175 | 176 | void turnOff() { 177 | Serial.printf("Turning off...\n"); 178 | sendToBroker("report/powerState", "OFF"); 179 | } 180 | 181 | void turnOn() { 182 | Serial.printf("Turning on...\n"); 183 | sendToBroker("report/powerState", "ON"); 184 | } 185 | 186 | void sendValue() { 187 | value = analogRead(sensorPin); 188 | char message[5]; 189 | sprintf(message, "%d", value); 190 | sendToBroker("report/value", message); 191 | } 192 | 193 | -------------------------------------------------------------------------------- /Devices/blinds/blinds.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Example-blinds" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int blindsPin = 0; 17 | int percentage = 0; 18 | 19 | void startWifi(); 20 | void startMqtt(); 21 | void checkMqtt(); 22 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 23 | void callback(char* topic, byte* payload, unsigned int length); 24 | void sendToBroker(char* topic, char* message); 25 | 26 | void setPercentage(int value); 27 | void turnOff(); 28 | void turnOn(); 29 | 30 | void setup() { 31 | pinMode(blindsPin, OUTPUT); 32 | Serial.begin(115200); 33 | startWifi(); 34 | startMqtt(); 35 | } 36 | 37 | void loop() { 38 | client.loop(); 39 | checkMqtt(); 40 | } 41 | 42 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 43 | Serial.print("Topic:"); 44 | Serial.println(topic); 45 | int tokensNumber = 10; 46 | char* tokens[tokensNumber]; 47 | char message[length + 1]; 48 | splitTopic(topic, tokens, tokensNumber); 49 | sprintf(message, "%c", (char)payload[0]); 50 | for (int i = 1; i < length; i++) { 51 | sprintf(message, "%s%c", message, (char)payload[i]); 52 | } 53 | Serial.print("Message:"); 54 | Serial.println(message); 55 | 56 | //------------------ACTIONS HERE--------------------------------- 57 | 58 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 59 | if (strcmp(message, "ON") == 0) { 60 | turnOn(); 61 | } else if (strcmp(message, "OFF") == 0) { 62 | turnOff(); 63 | } 64 | } else if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "percentage") == 0) { 65 | int val = atoi(message); 66 | if (val >= 0 && val <= 100) { 67 | setPercentage(val); 68 | } 69 | } 70 | } 71 | 72 | void startWifi() { 73 | WiFi.mode(WIFI_STA); 74 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 75 | Serial.println("Connecting ..."); 76 | int attempts = 0; 77 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 78 | attempts++; 79 | delay(500); 80 | Serial.print("."); 81 | } 82 | 83 | if (WiFi.status() == WL_CONNECTED) { 84 | Serial.println('\n'); 85 | Serial.print("Connected to "); 86 | Serial.println(WiFi.SSID()); 87 | Serial.print("IP address:\t"); 88 | Serial.println(WiFi.localIP()); 89 | 90 | } else { 91 | Serial.println('\n'); 92 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 93 | } 94 | 95 | delay(500); 96 | } 97 | 98 | void startMqtt() { 99 | client.setServer(MQTT_BROKER, MQTT_PORT); 100 | client.setCallback(callback); 101 | 102 | while (!client.connected()) { 103 | Serial.println("Connecting to MQTT..."); 104 | 105 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 106 | Serial.println("connected"); 107 | } else { 108 | if (client.state() == 5) { 109 | Serial.println("Connection not allowed by broker, possible reasons:"); 110 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 111 | Serial.println("- Wrong Username or password. Check credentials"); 112 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 113 | 114 | } else { 115 | Serial.println("Not possible to connect to Broker Error code:"); 116 | Serial.print(client.state()); 117 | } 118 | 119 | delay(0x7530); 120 | } 121 | } 122 | 123 | char subscibeTopic[100]; 124 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 125 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 126 | 127 | sendToBroker("report/online", "true"); // Reports that the device is online 128 | delay(100); 129 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 130 | delay(100); 131 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 132 | delay(100); 133 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 134 | delay(100); 135 | 136 | char signal[5]; 137 | sprintf(signal, "%d", WiFi.RSSI()); 138 | sendToBroker("report/signal", signal); // Reports the signal strength 139 | delay(100); 140 | } 141 | 142 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 143 | const char s[2] = "/"; 144 | int pos = 0; 145 | tokens[0] = strtok(topic, s); 146 | 147 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 148 | pos++; 149 | tokens[pos] = strtok(NULL, s); 150 | } 151 | 152 | return pos; 153 | } 154 | 155 | void checkMqtt() { 156 | if (!client.connected()) { 157 | startMqtt(); 158 | } 159 | } 160 | 161 | void sendToBroker(char* topic, char* message) { 162 | if (client.connected()) { 163 | char topicArr[100]; 164 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 165 | client.publish(topicArr, message); 166 | } 167 | } 168 | 169 | void turnOff() { 170 | Serial.printf("Turning off...\n"); 171 | digitalWrite(blindsPin, HIGH); 172 | sendToBroker("report/powerState", "OFF"); 173 | } 174 | 175 | void turnOn() { 176 | Serial.printf("Turning on...\n"); 177 | digitalWrite(blindsPin, LOW); 178 | sendToBroker("report/powerState", "ON"); 179 | } 180 | 181 | void setPercentage(int value) { 182 | percentage = value; 183 | Serial.printf("Percentage set to %d\n", percentage); 184 | } -------------------------------------------------------------------------------- /Devices/camera/camera.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Example-camera" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int cameraPin = 0; 17 | 18 | void startWifi(); 19 | void startMqtt(); 20 | void checkMqtt(); 21 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 22 | void callback(char* topic, byte* payload, unsigned int length); 23 | void sendToBroker(char* topic, char* message); 24 | 25 | void turnOff(); 26 | void turnOn(); 27 | 28 | void setup() { 29 | pinMode(cameraPin, OUTPUT); 30 | Serial.begin(115200); 31 | startWifi(); 32 | startMqtt(); 33 | } 34 | 35 | void loop() { 36 | client.loop(); 37 | checkMqtt(); 38 | } 39 | 40 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 41 | Serial.print("Topic:"); 42 | Serial.println(topic); 43 | int tokensNumber = 10; 44 | char* tokens[tokensNumber]; 45 | char message[length + 1]; 46 | splitTopic(topic, tokens, tokensNumber); 47 | sprintf(message, "%c", (char)payload[0]); 48 | for (int i = 1; i < length; i++) { 49 | sprintf(message, "%s%c", message, (char)payload[i]); 50 | } 51 | Serial.print("Message:"); 52 | Serial.println(message); 53 | 54 | //------------------ACTIONS HERE--------------------------------- 55 | 56 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 57 | if (strcmp(message, "ON") == 0) { 58 | turnOn(); 59 | } else if (strcmp(message, "OFF") == 0) { 60 | turnOff(); 61 | } 62 | } 63 | } 64 | 65 | void startWifi() { 66 | WiFi.mode(WIFI_STA); 67 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 68 | Serial.println("Connecting ..."); 69 | int attempts = 0; 70 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 71 | attempts++; 72 | delay(500); 73 | Serial.print("."); 74 | } 75 | 76 | if (WiFi.status() == WL_CONNECTED) { 77 | Serial.println('\n'); 78 | Serial.print("Connected to "); 79 | Serial.println(WiFi.SSID()); 80 | Serial.print("IP address:\t"); 81 | Serial.println(WiFi.localIP()); 82 | 83 | } else { 84 | Serial.println('\n'); 85 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 86 | } 87 | 88 | delay(500); 89 | } 90 | 91 | void startMqtt() { 92 | client.setServer(MQTT_BROKER, MQTT_PORT); 93 | client.setCallback(callback); 94 | 95 | while (!client.connected()) { 96 | Serial.println("Connecting to MQTT..."); 97 | 98 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 99 | Serial.println("connected"); 100 | } else { 101 | if (client.state() == 5) { 102 | Serial.println("Connection not allowed by broker, possible reasons:"); 103 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 104 | Serial.println("- Wrong Username or password. Check credentials"); 105 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 106 | 107 | } else { 108 | Serial.println("Not possible to connect to Broker Error code:"); 109 | Serial.print(client.state()); 110 | } 111 | 112 | delay(0x7530); 113 | } 114 | } 115 | 116 | char subscibeTopic[100]; 117 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 118 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 119 | 120 | sendToBroker("report/online", "true"); // Reports that the device is online 121 | delay(100); 122 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 123 | delay(100); 124 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 125 | delay(100); 126 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 127 | delay(100); 128 | 129 | char signal[5]; 130 | sprintf(signal, "%d", WiFi.RSSI()); 131 | sendToBroker("report/signal", signal); // Reports the signal strength 132 | delay(100); 133 | } 134 | 135 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 136 | const char s[2] = "/"; 137 | int pos = 0; 138 | tokens[0] = strtok(topic, s); 139 | 140 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 141 | pos++; 142 | tokens[pos] = strtok(NULL, s); 143 | } 144 | 145 | return pos; 146 | } 147 | 148 | void checkMqtt() { 149 | if (!client.connected()) { 150 | startMqtt(); 151 | } 152 | } 153 | 154 | void sendToBroker(char* topic, char* message) { 155 | if (client.connected()) { 156 | char topicArr[100]; 157 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 158 | client.publish(topicArr, message); 159 | } 160 | } 161 | 162 | void turnOff() { 163 | Serial.printf("Turning off...\n"); 164 | digitalWrite(cameraPin, HIGH); 165 | sendToBroker("report/powerState", "OFF"); 166 | } 167 | 168 | void turnOn() { 169 | Serial.printf("Turning on...\n"); 170 | digitalWrite(cameraPin, LOW); 171 | sendToBroker("report/powerState", "ON"); 172 | } 173 | -------------------------------------------------------------------------------- /Devices/contactSensor/contactSensor.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Example-contactSensor" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int sensorPin = 0; 17 | bool sensorOn = true; 18 | bool sensorTriggered = false; 19 | int sensorReportSent = 0; 20 | 21 | void startWifi(); 22 | void startMqtt(); 23 | void turnOff(); 24 | void turnOn(); 25 | void sendReport(bool value); 26 | void checkSensor(); 27 | void checkMqtt(); 28 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 29 | void callback(char* topic, byte* payload, unsigned int length); 30 | void sendToBroker(char* topic, char* message); 31 | 32 | void setup() { 33 | pinMode(sensorPin, INPUT); 34 | Serial.begin(115200); 35 | startWifi(); 36 | startMqtt(); 37 | } 38 | 39 | void loop() { 40 | client.loop(); 41 | checkSensor(); 42 | checkMqtt(); 43 | } 44 | 45 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 46 | Serial.print("Topic:"); 47 | Serial.println(topic); 48 | int tokensNumber = 10; 49 | char* tokens[tokensNumber]; 50 | char message[length + 1]; 51 | splitTopic(topic, tokens, tokensNumber); 52 | sprintf(message, "%c", (char)payload[0]); 53 | for (int i = 1; i < length; i++) { 54 | sprintf(message, "%s%c", message, (char)payload[i]); 55 | } 56 | Serial.print("Message:"); 57 | Serial.println(message); 58 | 59 | char reportChange[100]; 60 | sprintf(reportChange, "%s/report/powerState", MQTT_CLIENT); 61 | 62 | //------------------ACTIONS HERE--------------------------------- 63 | 64 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 65 | if (strcmp(message, "ON") == 0) { 66 | turnOn(); 67 | } else if (strcmp(message, "OFF") == 0) { 68 | turnOff(); 69 | } 70 | } 71 | } 72 | 73 | void startWifi() { 74 | WiFi.mode(WIFI_STA); 75 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 76 | Serial.println("Connecting ..."); 77 | int attempts = 0; 78 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 79 | attempts++; 80 | delay(500); 81 | Serial.print("."); 82 | } 83 | 84 | if (WiFi.status() == WL_CONNECTED) { 85 | Serial.println('\n'); 86 | Serial.print("Connected to "); 87 | Serial.println(WiFi.SSID()); 88 | Serial.print("IP address:\t"); 89 | Serial.println(WiFi.localIP()); 90 | 91 | } else { 92 | Serial.println('\n'); 93 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 94 | } 95 | 96 | delay(500); 97 | } 98 | 99 | void startMqtt() { 100 | client.setServer(MQTT_BROKER, MQTT_PORT); 101 | client.setCallback(callback); 102 | 103 | while (!client.connected()) { 104 | Serial.println("Connecting to MQTT..."); 105 | 106 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 107 | Serial.println("connected"); 108 | 109 | } else { 110 | if (client.state() == 5) { 111 | Serial.println("Connection not allowed by broker, possible reasons:"); 112 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 113 | Serial.println("- Wrong Username or password. Check credentials"); 114 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 115 | 116 | } else { 117 | Serial.println("Not possible to connect to Broker Error code:"); 118 | Serial.print(client.state()); 119 | } 120 | 121 | delay(0x7530); 122 | } 123 | } 124 | 125 | char subscibeTopic[100]; 126 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 127 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 128 | 129 | sendToBroker("report/online", "true"); // Reports that the device is online 130 | delay(100); 131 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 132 | delay(100); 133 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 134 | delay(100); 135 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 136 | delay(100); 137 | 138 | char signal[5]; 139 | sprintf(signal, "%d", WiFi.RSSI()); 140 | sendToBroker("report/signal", signal); // Reports the signal strength 141 | delay(100); 142 | } 143 | 144 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 145 | const char s[2] = "/"; 146 | int pos = 0; 147 | tokens[0] = strtok(topic, s); 148 | 149 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 150 | pos++; 151 | tokens[pos] = strtok(NULL, s); 152 | } 153 | 154 | return pos; 155 | } 156 | 157 | void checkMqtt() { 158 | if (!client.connected()) { 159 | startMqtt(); 160 | } 161 | } 162 | 163 | void checkSensor() { 164 | int buttonState = digitalRead(sensorPin); 165 | if (buttonState == LOW && !sensorTriggered) { 166 | return; 167 | 168 | } else if (buttonState == LOW && sensorTriggered) { 169 | sensorTriggered = false; 170 | sendReport(false); 171 | 172 | } else if (buttonState == HIGH && !sensorTriggered) { 173 | sensorTriggered = true; 174 | sendReport(true); 175 | 176 | } else if (buttonState == HIGH && sensorTriggered) { 177 | return; 178 | } 179 | } 180 | 181 | void turnOff() { 182 | sensorOn = false; 183 | sendToBroker("report/powerState", "OFF"); 184 | } 185 | void turnOn() { 186 | sensorOn = true; 187 | sendToBroker("report/powerState", "ON"); 188 | } 189 | 190 | void sendReport(bool value) { 191 | if (millis() - sensorReportSent > 500 && sensorOn) { 192 | if (value) 193 | sendToBroker("report/detectionState", "true"); 194 | else 195 | sendToBroker("report/detectionState", "false"); 196 | sensorReportSent = millis(); 197 | } 198 | } 199 | 200 | void sendToBroker(char* topic, char* message) { 201 | if (client.connected()) { 202 | char topicArr[100]; 203 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 204 | client.publish(topicArr, message); 205 | } 206 | } -------------------------------------------------------------------------------- /Devices/door/door.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Example-door" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int doorPin = 0; 17 | int openTime = 5000; //Time to keep the door open 18 | 19 | void startWifi(); 20 | void startMqtt(); 21 | void checkMqtt(); 22 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 23 | void callback(char* topic, byte* payload, unsigned int length); 24 | void sendToBroker(char* topic, char* message); 25 | 26 | void turnOff(); 27 | void turnOn(); 28 | void open(); 29 | void close(); 30 | 31 | void setup() { 32 | pinMode(doorPin, OUTPUT); 33 | Serial.begin(115200); 34 | startWifi(); 35 | startMqtt(); 36 | } 37 | 38 | void loop() { 39 | client.loop(); 40 | checkMqtt(); 41 | } 42 | 43 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 44 | Serial.print("Topic:"); 45 | Serial.println(topic); 46 | int tokensNumber = 10; 47 | char* tokens[tokensNumber]; 48 | char message[length + 1]; 49 | splitTopic(topic, tokens, tokensNumber); 50 | sprintf(message, "%c", (char)payload[0]); 51 | for (int i = 1; i < length; i++) { 52 | sprintf(message, "%s%c", message, (char)payload[i]); 53 | } 54 | Serial.print("Message:"); 55 | Serial.println(message); 56 | 57 | //------------------ACTIONS HERE--------------------------------- 58 | 59 | ///Actions here 60 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 61 | if (strcmp(message, "ON") == 0) { 62 | turnOn(); 63 | } else if (strcmp(message, "OFF") == 0) { 64 | turnOff(); 65 | } 66 | } else if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "lockedState") == 0) { 67 | if (strcmp(message, "false") == 0) { 68 | open(); 69 | } else if (strcmp(message, "true") == 0) { 70 | close(); 71 | } 72 | } 73 | } 74 | 75 | void startWifi() { 76 | WiFi.mode(WIFI_STA); 77 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 78 | Serial.println("Connecting ..."); 79 | int attempts = 0; 80 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 81 | attempts++; 82 | delay(500); 83 | Serial.print("."); 84 | } 85 | 86 | if (WiFi.status() == WL_CONNECTED) { 87 | Serial.println('\n'); 88 | Serial.print("Connected to "); 89 | Serial.println(WiFi.SSID()); 90 | Serial.print("IP address:\t"); 91 | Serial.println(WiFi.localIP()); 92 | 93 | } else { 94 | Serial.println('\n'); 95 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 96 | } 97 | 98 | delay(500); 99 | } 100 | 101 | void startMqtt() { 102 | client.setServer(MQTT_BROKER, MQTT_PORT); 103 | client.setCallback(callback); 104 | 105 | while (!client.connected()) { 106 | Serial.println("Connecting to MQTT..."); 107 | 108 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 109 | Serial.println("connected"); 110 | } else { 111 | if (client.state() == 5) { 112 | Serial.println("Connection not allowed by broker, possible reasons:"); 113 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 114 | Serial.println("- Wrong Username or password. Check credentials"); 115 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 116 | 117 | } else { 118 | Serial.println("Not possible to connect to Broker Error code:"); 119 | Serial.print(client.state()); 120 | } 121 | 122 | delay(0x7530); 123 | } 124 | } 125 | 126 | char subscibeTopic[100]; 127 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 128 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 129 | 130 | sendToBroker("report/online", "true"); // Reports that the device is online 131 | delay(100); 132 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 133 | delay(100); 134 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 135 | delay(100); 136 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 137 | delay(100); 138 | 139 | char signal[5]; 140 | sprintf(signal, "%d", WiFi.RSSI()); 141 | sendToBroker("report/signal", signal); // Reports the signal strength 142 | delay(100); 143 | } 144 | 145 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 146 | const char s[2] = "/"; 147 | int pos = 0; 148 | tokens[0] = strtok(topic, s); 149 | 150 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 151 | pos++; 152 | tokens[pos] = strtok(NULL, s); 153 | } 154 | 155 | return pos; 156 | } 157 | 158 | void checkMqtt() { 159 | if (!client.connected()) { 160 | startMqtt(); 161 | } 162 | } 163 | 164 | void sendToBroker(char* topic, char* message) { 165 | if (client.connected()) { 166 | char topicArr[100]; 167 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 168 | client.publish(topicArr, message); 169 | } 170 | } 171 | 172 | void turnOff() { 173 | Serial.println("Turning off"); 174 | // Add code 175 | sendToBroker("report/powerState", "OFF"); 176 | } 177 | 178 | void turnOn() { 179 | Serial.println("Turning on"); 180 | // Add code 181 | sendToBroker("report/powerState", "ON"); 182 | } 183 | 184 | void open() { 185 | Serial.println("Opening"); 186 | digitalWrite(doorPin, LOW); 187 | sendToBroker("report/lockedState", "false"); 188 | delay(openTime); 189 | close(); 190 | } 191 | 192 | void close() { 193 | Serial.println("Closing"); 194 | digitalWrite(doorPin, HIGH); 195 | sendToBroker("report/lockedState", "true"); 196 | } -------------------------------------------------------------------------------- /Devices/doorbell/doorbell.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Example-doorbell" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int bellPin = 0; 17 | bool bellTriggered = false; 18 | int bellReportSend = 0; 19 | 20 | void startWifi(); 21 | void startMqtt(); 22 | void sendBellReport(); 23 | void checkBell(); 24 | void checkMqtt(); 25 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 26 | void callback(char* topic, byte* payload, unsigned int length); 27 | void sendToBroker(char* topic, char* message); 28 | 29 | void setup() { 30 | pinMode(bellPin, INPUT); 31 | Serial.begin(115200); 32 | startWifi(); 33 | startMqtt(); 34 | } 35 | 36 | void loop() { 37 | client.loop(); 38 | checkBell(); 39 | checkMqtt(); 40 | } 41 | 42 | void callback(char* topic, byte* payload, unsigned int length) { // This function runs when there is a new message in the subscribed topic 43 | Serial.print("Topic:"); 44 | Serial.println(topic); 45 | 46 | // Splits the topic and gets the message 47 | int tokensNumber = 10; 48 | char* tokens[tokensNumber]; 49 | char message[length + 1]; 50 | splitTopic(topic, tokens, tokensNumber); 51 | sprintf(message, "%c", (char)payload[0]); 52 | 53 | for (int i = 1; i < length; i++) { 54 | sprintf(message, "%s%c", message, (char)payload[i]); 55 | } 56 | 57 | Serial.print("Message:"); 58 | Serial.println(message); 59 | } 60 | 61 | void startWifi() { 62 | WiFi.mode(WIFI_STA); 63 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 64 | Serial.println("Connecting ..."); 65 | int attempts = 0; 66 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 67 | attempts++; 68 | delay(500); 69 | Serial.print("."); 70 | } 71 | 72 | if (WiFi.status() == WL_CONNECTED) { 73 | Serial.println('\n'); 74 | Serial.print("Connected to "); 75 | Serial.println(WiFi.SSID()); 76 | Serial.print("IP address:\t"); 77 | Serial.println(WiFi.localIP()); 78 | 79 | } else { 80 | Serial.println('\n'); 81 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 82 | } 83 | 84 | delay(500); 85 | } 86 | 87 | void startMqtt() { 88 | client.setServer(MQTT_BROKER, MQTT_PORT); 89 | client.setCallback(callback); 90 | 91 | while (!client.connected()) { 92 | Serial.println("Connecting to MQTT..."); 93 | 94 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 95 | Serial.println("connected"); 96 | } else { 97 | if (client.state() == 5) { 98 | Serial.println("Connection not allowed by broker, possible reasons:"); 99 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 100 | Serial.println("- Wrong Username or password. Check credentials"); 101 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 102 | 103 | } else { 104 | Serial.println("Not possible to connect to Broker Error code:"); 105 | Serial.print(client.state()); 106 | } 107 | 108 | delay(0x7530); 109 | } 110 | } 111 | 112 | char subscibeTopic[100]; 113 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 114 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 115 | 116 | sendToBroker("report/online", "true"); // Reports that the device is online 117 | delay(100); 118 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 119 | delay(100); 120 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 121 | delay(100); 122 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 123 | delay(100); 124 | 125 | char signal[5]; 126 | sprintf(signal, "%d", WiFi.RSSI()); 127 | sendToBroker("report/signal", signal); // Reports the signal strength 128 | delay(100); 129 | } 130 | 131 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 132 | const char s[2] = "/"; 133 | int pos = 0; 134 | tokens[0] = strtok(topic, s); 135 | 136 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 137 | pos++; 138 | tokens[pos] = strtok(NULL, s); 139 | } 140 | 141 | return pos; 142 | } 143 | 144 | void checkMqtt() { 145 | if (!client.connected()) { 146 | startMqtt(); 147 | } 148 | } 149 | 150 | void checkBell() { 151 | int buttonState = digitalRead(bellPin); 152 | if (buttonState == LOW && !bellTriggered) { 153 | return; 154 | 155 | } else if (buttonState == LOW && bellTriggered) { 156 | bellTriggered = false; 157 | 158 | } else if (buttonState == HIGH && !bellTriggered) { 159 | bellTriggered = true; 160 | sendBellReport(); 161 | 162 | } else if (buttonState == HIGH && bellTriggered) { 163 | return; 164 | } 165 | } 166 | 167 | void sendBellReport() { //Avoids sending repeated reports. only once every 5 seconds. 168 | 169 | if (millis() - bellReportSend > 5000) { 170 | bellReportSend = millis(); 171 | sendToBroker("report/detectionState", "true"); 172 | delay(1000); 173 | sendToBroker("report/detectionState", "false"); 174 | } 175 | } 176 | 177 | void sendToBroker(char* topic, char* message) { 178 | if (client.connected()) { 179 | char topicArr[100]; 180 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 181 | client.publish(topicArr, message); 182 | } 183 | } -------------------------------------------------------------------------------- /Devices/electrometer/electrometer.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | #include 5 | 6 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 7 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 8 | #define MQTT_BROKER "smartnest.cz" // Broker host 9 | #define MQTT_PORT 1883 // Broker port 10 | #define MQTT_USERNAME "username" // Username from Smartnest 11 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 12 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 13 | #define FIRMWARE_VERSION "Example-electrometer" // Custom name for this program 14 | 15 | WiFiClient espClient; 16 | PubSubClient client(espClient); 17 | double lastValue = 0; 18 | int interval = 20; 19 | Ticker sendUpdate; 20 | 21 | void startWifi(); 22 | void startMqtt(); 23 | void checkMqtt(); 24 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 25 | void callback(char* topic, byte* payload, unsigned int length); 26 | void sendToBroker(char* topic, char* message); 27 | 28 | void turnOff(); 29 | void turnOn(); 30 | void sendValue(); 31 | double readMeter(); 32 | 33 | void setup() { 34 | Serial.begin(115200); 35 | startWifi(); 36 | startMqtt(); 37 | } 38 | 39 | void loop() { 40 | client.loop(); 41 | checkMqtt(); 42 | } 43 | 44 | double readMeter(){ 45 | // Your code to read the Meter value 46 | return 100.0; 47 | } 48 | 49 | void callback(char* topic, byte* payload, unsigned int length) { //Callback for new messages 50 | Serial.print("Topic:"); 51 | Serial.println(topic); 52 | int tokensNumber = 10; 53 | char* tokens[tokensNumber]; 54 | char message[length + 1]; 55 | splitTopic(topic, tokens, tokensNumber); 56 | sprintf(message, "%c", (char)payload[0]); 57 | for (int i = 1; i < length; i++) { 58 | sprintf(message, "%s%c", message, (char)payload[i]); 59 | } 60 | Serial.print("Message:"); 61 | Serial.println(message); 62 | 63 | } 64 | 65 | void startWifi() { 66 | WiFi.mode(WIFI_STA); 67 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 68 | Serial.println("Connecting ..."); 69 | int attempts = 0; 70 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 71 | attempts++; 72 | delay(500); 73 | Serial.print("."); 74 | } 75 | 76 | if (WiFi.status() == WL_CONNECTED) { 77 | Serial.println('\n'); 78 | Serial.print("Connected to "); 79 | Serial.println(WiFi.SSID()); 80 | Serial.print("IP address:\t"); 81 | Serial.println(WiFi.localIP()); 82 | 83 | } else { 84 | Serial.println('\n'); 85 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 86 | } 87 | 88 | delay(500); 89 | } 90 | 91 | void startMqtt() { 92 | client.setServer(MQTT_BROKER, MQTT_PORT); 93 | client.setCallback(callback); 94 | 95 | while (!client.connected()) { 96 | Serial.println("Connecting to MQTT..."); 97 | 98 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 99 | Serial.println("connected"); 100 | sendUpdate.attach(interval, sendValue); 101 | 102 | } else { 103 | if (client.state() == 5) { 104 | Serial.println("Connection not allowed by broker, possible reasons:"); 105 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 106 | Serial.println("- Wrong Username or password. Check credentials"); 107 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 108 | 109 | } else { 110 | Serial.println("Not possible to connect to Broker Error code:"); 111 | Serial.print(client.state()); 112 | } 113 | 114 | delay(0x7530); 115 | } 116 | } 117 | 118 | char subscibeTopic[100]; 119 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 120 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 121 | 122 | sendToBroker("report/online", "true"); // Reports that the device is online 123 | delay(100); 124 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 125 | delay(100); 126 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 127 | delay(100); 128 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 129 | delay(100); 130 | 131 | char signal[5]; 132 | sprintf(signal, "%d", WiFi.RSSI()); 133 | sendToBroker("report/signal", signal); // Reports the signal strength 134 | delay(100); 135 | } 136 | 137 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 138 | const char s[2] = "/"; 139 | int pos = 0; 140 | tokens[0] = strtok(topic, s); 141 | 142 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 143 | pos++; 144 | tokens[pos] = strtok(NULL, s); 145 | } 146 | 147 | return pos; 148 | } 149 | 150 | void checkMqtt() { 151 | if (!client.connected()) { 152 | Serial.printf("Device lost connection with broker status %, reconnecting...\n", client.connected()); 153 | 154 | if (WiFi.status() != WL_CONNECTED) { 155 | startWifi(); 156 | } 157 | 158 | sendUpdate.detach(); 159 | startMqtt(); 160 | } 161 | } 162 | 163 | void sendToBroker(char* topic, char* message) { 164 | if (client.connected()) { 165 | char topicArr[100]; 166 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 167 | client.publish(topicArr, message); 168 | } 169 | } 170 | 171 | void sendValue() { 172 | double value = readMeter(); 173 | if(value!=lastValue){ 174 | char message[5]; 175 | sprintf(message, "%f", value); 176 | sendToBroker("report/total", message); 177 | } 178 | } 179 | 180 | -------------------------------------------------------------------------------- /Devices/fan/fan.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Example-fan" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int fanPin = 0; 17 | int speed = 0; 18 | 19 | void startWifi(); 20 | void startMqtt(); 21 | void checkMqtt(); 22 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 23 | void callback(char* topic, byte* payload, unsigned int length); 24 | void sendToBroker(char* topic, char* message); 25 | 26 | void setPercentage(int value); 27 | void turnOff(); 28 | void turnOn(); 29 | 30 | void setup() { 31 | pinMode(fanPin, OUTPUT); 32 | Serial.begin(115200); 33 | startWifi(); 34 | startMqtt(); 35 | } 36 | 37 | void loop() { 38 | client.loop(); 39 | checkMqtt(); 40 | } 41 | 42 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 43 | Serial.print("Topic:"); 44 | Serial.println(topic); 45 | int tokensNumber = 10; 46 | char* tokens[tokensNumber]; 47 | char message[length + 1]; 48 | splitTopic(topic, tokens, tokensNumber); 49 | sprintf(message, "%c", (char)payload[0]); 50 | for (int i = 1; i < length; i++) { 51 | sprintf(message, "%s%c", message, (char)payload[i]); 52 | } 53 | Serial.print("Message:"); 54 | Serial.println(message); 55 | 56 | //------------------ACTIONS HERE--------------------------------- 57 | 58 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 59 | if (strcmp(message, "ON") == 0) { 60 | turnOn(); 61 | } else if (strcmp(message, "OFF") == 0) { 62 | turnOff(); 63 | } 64 | } else if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "percentage") == 0) { 65 | int val = atoi(message); 66 | if (val >= 0 && val <= 100) { 67 | setPercentage(val); 68 | } 69 | } 70 | } 71 | 72 | void startWifi() { 73 | WiFi.mode(WIFI_STA); 74 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 75 | Serial.println("Connecting ..."); 76 | int attempts = 0; 77 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 78 | attempts++; 79 | delay(500); 80 | Serial.print("."); 81 | } 82 | 83 | if (WiFi.status() == WL_CONNECTED) { 84 | Serial.println('\n'); 85 | Serial.print("Connected to "); 86 | Serial.println(WiFi.SSID()); 87 | Serial.print("IP address:\t"); 88 | Serial.println(WiFi.localIP()); 89 | 90 | } else { 91 | Serial.println('\n'); 92 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 93 | } 94 | 95 | delay(500); 96 | } 97 | 98 | void startMqtt() { 99 | client.setServer(MQTT_BROKER, MQTT_PORT); 100 | client.setCallback(callback); 101 | 102 | while (!client.connected()) { 103 | Serial.println("Connecting to MQTT..."); 104 | 105 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 106 | Serial.println("connected"); 107 | } else { 108 | if (client.state() == 5) { 109 | Serial.println("Connection not allowed by broker, possible reasons:"); 110 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 111 | Serial.println("- Wrong Username or password. Check credentials"); 112 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 113 | 114 | } else { 115 | Serial.println("Not possible to connect to Broker Error code:"); 116 | Serial.print(client.state()); 117 | } 118 | 119 | delay(0x7530); 120 | } 121 | } 122 | 123 | char subscibeTopic[100]; 124 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 125 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 126 | 127 | sendToBroker("report/online", "true"); // Reports that the device is online 128 | delay(100); 129 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 130 | delay(100); 131 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 132 | delay(100); 133 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 134 | delay(100); 135 | 136 | char signal[5]; 137 | sprintf(signal, "%d", WiFi.RSSI()); 138 | sendToBroker("report/signal", signal); // Reports the signal strength 139 | delay(100); 140 | } 141 | 142 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 143 | const char s[2] = "/"; 144 | int pos = 0; 145 | tokens[0] = strtok(topic, s); 146 | 147 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 148 | pos++; 149 | tokens[pos] = strtok(NULL, s); 150 | } 151 | 152 | return pos; 153 | } 154 | 155 | void checkMqtt() { 156 | if (!client.connected()) { 157 | startMqtt(); 158 | } 159 | } 160 | 161 | void sendToBroker(char* topic, char* message) { 162 | if (client.connected()) { 163 | char topicArr[100]; 164 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 165 | client.publish(topicArr, message); 166 | } 167 | } 168 | 169 | void turnOff() { 170 | Serial.printf("Turning off...\n"); 171 | digitalWrite(fanPin, HIGH); 172 | sendToBroker("report/powerState", "OFF"); 173 | } 174 | 175 | void turnOn() { 176 | Serial.printf("Turning on...\n"); 177 | digitalWrite(fanPin, LOW); 178 | sendToBroker("report/powerState", "ON"); 179 | } 180 | 181 | void setPercentage(int value) { 182 | speed = value; 183 | Serial.printf("Percentage set to %d\n", speed); 184 | } -------------------------------------------------------------------------------- /Devices/gasmeter/gasmeter.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | #include 5 | 6 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 7 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 8 | #define MQTT_BROKER "smartnest.cz" // Broker host 9 | #define MQTT_PORT 1883 // Broker port 10 | #define MQTT_USERNAME "username" // Username from Smartnest 11 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 12 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 13 | #define FIRMWARE_VERSION "Example-gasmeter" // Custom name for this program 14 | 15 | WiFiClient espClient; 16 | PubSubClient client(espClient); 17 | double lastValue = 0; 18 | int interval = 20; 19 | Ticker sendUpdate; 20 | 21 | void startWifi(); 22 | void startMqtt(); 23 | void checkMqtt(); 24 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 25 | void callback(char* topic, byte* payload, unsigned int length); 26 | void sendToBroker(char* topic, char* message); 27 | 28 | void turnOff(); 29 | void turnOn(); 30 | void sendValue(); 31 | double readMeter(); 32 | 33 | void setup() { 34 | Serial.begin(115200); 35 | startWifi(); 36 | startMqtt(); 37 | } 38 | 39 | void loop() { 40 | client.loop(); 41 | checkMqtt(); 42 | } 43 | 44 | double readMeter(){ 45 | // Your code to read the Meter value 46 | return 100.0; 47 | } 48 | 49 | void callback(char* topic, byte* payload, unsigned int length) { //Callback for new messages 50 | Serial.print("Topic:"); 51 | Serial.println(topic); 52 | int tokensNumber = 10; 53 | char* tokens[tokensNumber]; 54 | char message[length + 1]; 55 | splitTopic(topic, tokens, tokensNumber); 56 | sprintf(message, "%c", (char)payload[0]); 57 | for (int i = 1; i < length; i++) { 58 | sprintf(message, "%s%c", message, (char)payload[i]); 59 | } 60 | Serial.print("Message:"); 61 | Serial.println(message); 62 | 63 | } 64 | 65 | void startWifi() { 66 | WiFi.mode(WIFI_STA); 67 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 68 | Serial.println("Connecting ..."); 69 | int attempts = 0; 70 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 71 | attempts++; 72 | delay(500); 73 | Serial.print("."); 74 | } 75 | 76 | if (WiFi.status() == WL_CONNECTED) { 77 | Serial.println('\n'); 78 | Serial.print("Connected to "); 79 | Serial.println(WiFi.SSID()); 80 | Serial.print("IP address:\t"); 81 | Serial.println(WiFi.localIP()); 82 | 83 | } else { 84 | Serial.println('\n'); 85 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 86 | } 87 | 88 | delay(500); 89 | } 90 | 91 | void startMqtt() { 92 | client.setServer(MQTT_BROKER, MQTT_PORT); 93 | client.setCallback(callback); 94 | 95 | while (!client.connected()) { 96 | Serial.println("Connecting to MQTT..."); 97 | 98 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 99 | Serial.println("connected"); 100 | sendUpdate.attach(interval, sendValue); 101 | 102 | } else { 103 | if (client.state() == 5) { 104 | Serial.println("Connection not allowed by broker, possible reasons:"); 105 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 106 | Serial.println("- Wrong Username or password. Check credentials"); 107 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 108 | 109 | } else { 110 | Serial.println("Not possible to connect to Broker Error code:"); 111 | Serial.print(client.state()); 112 | } 113 | 114 | delay(0x7530); 115 | } 116 | } 117 | 118 | char subscibeTopic[100]; 119 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 120 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 121 | 122 | sendToBroker("report/online", "true"); // Reports that the device is online 123 | delay(100); 124 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 125 | delay(100); 126 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 127 | delay(100); 128 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 129 | delay(100); 130 | 131 | char signal[5]; 132 | sprintf(signal, "%d", WiFi.RSSI()); 133 | sendToBroker("report/signal", signal); // Reports the signal strength 134 | delay(100); 135 | } 136 | 137 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 138 | const char s[2] = "/"; 139 | int pos = 0; 140 | tokens[0] = strtok(topic, s); 141 | 142 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 143 | pos++; 144 | tokens[pos] = strtok(NULL, s); 145 | } 146 | 147 | return pos; 148 | } 149 | 150 | void checkMqtt() { 151 | if (!client.connected()) { 152 | Serial.printf("Device lost connection with broker status %, reconnecting...\n", client.connected()); 153 | 154 | if (WiFi.status() != WL_CONNECTED) { 155 | startWifi(); 156 | } 157 | 158 | sendUpdate.detach(); 159 | startMqtt(); 160 | } 161 | } 162 | 163 | void sendToBroker(char* topic, char* message) { 164 | if (client.connected()) { 165 | char topicArr[100]; 166 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 167 | client.publish(topicArr, message); 168 | } 169 | } 170 | 171 | void sendValue() { 172 | double value = readMeter(); 173 | if(value!=lastValue){ 174 | char message[5]; 175 | sprintf(message, "%f", value); 176 | sendToBroker("report/total", message); 177 | } 178 | } 179 | 180 | -------------------------------------------------------------------------------- /Devices/light/light.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Example-light" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int lightPin = 0; 17 | int brightness = 0; 18 | 19 | void startWifi(); 20 | void startMqtt(); 21 | void checkMqtt(); 22 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 23 | void callback(char* topic, byte* payload, unsigned int length); 24 | void sendToBroker(char* topic, char* message); 25 | 26 | void setPercentage(int value); 27 | void turnOff(); 28 | void turnOn(); 29 | 30 | void setup() { 31 | pinMode(lightPin, OUTPUT); 32 | Serial.begin(115200); 33 | startWifi(); 34 | startMqtt(); 35 | } 36 | 37 | void loop() { 38 | client.loop(); 39 | checkMqtt(); 40 | } 41 | 42 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 43 | Serial.print("Topic:"); 44 | Serial.println(topic); 45 | int tokensNumber = 10; 46 | char* tokens[tokensNumber]; 47 | char message[length + 1]; 48 | splitTopic(topic, tokens, tokensNumber); 49 | sprintf(message, "%c", (char)payload[0]); 50 | for (int i = 1; i < length; i++) { 51 | sprintf(message, "%s%c", message, (char)payload[i]); 52 | } 53 | Serial.print("Message:"); 54 | Serial.println(message); 55 | 56 | //------------------ACTIONS HERE--------------------------------- 57 | 58 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 59 | if (strcmp(message, "ON") == 0) { 60 | turnOn(); 61 | } else if (strcmp(message, "OFF") == 0) { 62 | turnOff(); 63 | } 64 | } else if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "percentage") == 0) { 65 | int val = atoi(message); 66 | if (val >= 0 && val <= 100) { 67 | setPercentage(val); 68 | } 69 | } 70 | } 71 | 72 | void startWifi() { 73 | WiFi.mode(WIFI_STA); 74 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 75 | Serial.println("Connecting ..."); 76 | int attempts = 0; 77 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 78 | attempts++; 79 | delay(500); 80 | Serial.print("."); 81 | } 82 | 83 | if (WiFi.status() == WL_CONNECTED) { 84 | Serial.println('\n'); 85 | Serial.print("Connected to "); 86 | Serial.println(WiFi.SSID()); 87 | Serial.print("IP address:\t"); 88 | Serial.println(WiFi.localIP()); 89 | 90 | } else { 91 | Serial.println('\n'); 92 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 93 | } 94 | 95 | delay(500); 96 | } 97 | 98 | void startMqtt() { 99 | client.setServer(MQTT_BROKER, MQTT_PORT); 100 | client.setCallback(callback); 101 | 102 | while (!client.connected()) { 103 | Serial.println("Connecting to MQTT..."); 104 | 105 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 106 | Serial.println("connected"); 107 | } else { 108 | if (client.state() == 5) { 109 | Serial.println("Connection not allowed by broker, possible reasons:"); 110 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 111 | Serial.println("- Wrong Username or password. Check credentials"); 112 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 113 | 114 | } else { 115 | Serial.println("Not possible to connect to Broker Error code:"); 116 | Serial.print(client.state()); 117 | } 118 | 119 | delay(0x7530); 120 | } 121 | } 122 | 123 | char subscibeTopic[100]; 124 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 125 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 126 | 127 | sendToBroker("report/online", "true"); // Reports that the device is online 128 | delay(100); 129 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 130 | delay(100); 131 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 132 | delay(100); 133 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 134 | delay(100); 135 | 136 | char signal[5]; 137 | sprintf(signal, "%d", WiFi.RSSI()); 138 | sendToBroker("report/signal", signal); // Reports the signal strength 139 | delay(100); 140 | } 141 | 142 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 143 | const char s[2] = "/"; 144 | int pos = 0; 145 | tokens[0] = strtok(topic, s); 146 | 147 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 148 | pos++; 149 | tokens[pos] = strtok(NULL, s); 150 | } 151 | 152 | return pos; 153 | } 154 | 155 | void checkMqtt() { 156 | if (!client.connected()) { 157 | startMqtt(); 158 | } 159 | } 160 | 161 | void sendToBroker(char* topic, char* message) { 162 | if (client.connected()) { 163 | char topicArr[100]; 164 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 165 | client.publish(topicArr, message); 166 | } 167 | } 168 | 169 | void turnOff() { 170 | Serial.printf("Turning off...\n"); 171 | digitalWrite(lightPin, HIGH); 172 | sendToBroker("report/powerState", "OFF"); 173 | } 174 | 175 | void turnOn() { 176 | Serial.printf("Turning on...\n"); 177 | digitalWrite(lightPin, LOW); 178 | sendToBroker("report/powerState", "ON"); 179 | } 180 | 181 | void setPercentage(int value) { 182 | brightness = value; 183 | Serial.printf("Percentage set to %d\n", brightness); 184 | } -------------------------------------------------------------------------------- /Devices/lightGroup/lightGroup.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Example-lightGroup" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | 17 | int switchPin1 = 0; 18 | int switchPin2 = 3; 19 | int switchPin3 = 5; 20 | int switchPin4 = 4; 21 | 22 | int brightness1 = 0; 23 | int brightness2 = 0; 24 | int brightness3 = 0; 25 | int brightness4 = 0; 26 | 27 | void startWifi(); 28 | void startMqtt(); 29 | void checkMqtt(); 30 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 31 | void callback(char* topic, byte* payload, unsigned int length); 32 | void sendToBroker(char* topic, char* message); 33 | 34 | void turnOff(int pin); 35 | void turnOn(int pin); 36 | void setBrightness(int index,int value); 37 | 38 | void setup() { 39 | pinMode(switchPin1, OUTPUT); 40 | pinMode(switchPin2, OUTPUT); 41 | pinMode(switchPin3, OUTPUT); 42 | Serial.begin(115200); 43 | startWifi(); 44 | startMqtt(); 45 | } 46 | 47 | void loop() { 48 | client.loop(); 49 | checkMqtt(); 50 | } 51 | 52 | void callback(char* topic, byte* payload, unsigned int length) { //Callback for new messages 53 | Serial.print("Topic:"); 54 | Serial.println(topic); 55 | int tokensNumber = 10; 56 | char* tokens[tokensNumber]; 57 | char message[length + 1]; 58 | splitTopic(topic, tokens, tokensNumber); 59 | sprintf(message, "%c", (char)payload[0]); 60 | for (int i = 1; i < length; i++) { 61 | sprintf(message, "%s%c", message, (char)payload[i]); 62 | } 63 | Serial.print("Message:"); 64 | Serial.println(message); 65 | 66 | //------------------ACTIONS HERE--------------------- 67 | 68 | if (strcmp(tokens[1], "directive") == 0) { 69 | if (strcmp(tokens[2], "powerState1") == 0) { 70 | if (strcmp(message, "ON") == 0) { 71 | turnOn(1); 72 | } else if (strcmp(message, "OFF") == 0) { 73 | turnOff(1); 74 | } 75 | } else if (strcmp(tokens[2], "powerState2") == 0) { 76 | if (strcmp(message, "ON") == 0) { 77 | turnOn(2); 78 | } else if (strcmp(message, "OFF") == 0) { 79 | turnOff(2); 80 | } 81 | } else if (strcmp(tokens[2], "powerState3") == 0) { 82 | if (strcmp(message, "ON") == 0) { 83 | turnOn(3); 84 | } else if (strcmp(message, "OFF") == 0) { 85 | turnOff(3); 86 | } 87 | } else if (strcmp(tokens[2], "powerState4") == 0) { 88 | if (strcmp(message, "ON") == 0) { 89 | turnOn(4); 90 | } else if (strcmp(message, "OFF") == 0) { 91 | turnOff(4); 92 | } 93 | } else if (strcmp(tokens[2], "percentage1") == 0) { 94 | int val = atoi(message); 95 | if (val >= 0 && val <= 100) { 96 | setBrightness(1,val); 97 | } 98 | } else if (strcmp(tokens[2], "percentage2") == 0) { 99 | int val = atoi(message); 100 | if (val >= 0 && val <= 100) { 101 | setBrightness(2,val); 102 | } 103 | } else if (strcmp(tokens[2], "percentage3") == 0) { 104 | int val = atoi(message); 105 | if (val >= 0 && val <= 100) { 106 | setBrightness(3,val); 107 | } 108 | } else if (strcmp(tokens[2], "percentage4") == 0) { 109 | int val = atoi(message); 110 | if (val >= 0 && val <= 100) { 111 | setBrightness(4,val); 112 | } 113 | } 114 | } 115 | } 116 | 117 | void startWifi() { 118 | WiFi.mode(WIFI_STA); 119 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 120 | Serial.println("Connecting ..."); 121 | int attempts = 0; 122 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 123 | attempts++; 124 | delay(500); 125 | Serial.print("."); 126 | } 127 | 128 | if (WiFi.status() == WL_CONNECTED) { 129 | Serial.println('\n'); 130 | Serial.print("Connected to "); 131 | Serial.println(WiFi.SSID()); 132 | Serial.print("IP address:\t"); 133 | Serial.println(WiFi.localIP()); 134 | 135 | } else { 136 | Serial.println('\n'); 137 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 138 | } 139 | 140 | delay(500); 141 | } 142 | 143 | void startMqtt() { 144 | client.setServer(MQTT_BROKER, MQTT_PORT); 145 | client.setCallback(callback); 146 | 147 | while (!client.connected()) { 148 | Serial.println("Connecting to MQTT..."); 149 | 150 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 151 | Serial.println("connected"); 152 | } else { 153 | if (client.state() == 5) { 154 | Serial.println("Connection not allowed by broker, possible reasons:"); 155 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 156 | Serial.println("- Wrong Username or password. Check credentials"); 157 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 158 | 159 | } else { 160 | Serial.println("Not possible to connect to Broker Error code:"); 161 | Serial.print(client.state()); 162 | } 163 | 164 | delay(0x7530); 165 | } 166 | } 167 | 168 | char topic[100]; 169 | sprintf(topic, "%s/#", MQTT_CLIENT); 170 | client.subscribe(topic); 171 | 172 | sendToBroker("report/online", "true"); // Reports that the device is online 173 | delay(100); 174 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 175 | delay(100); 176 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 177 | delay(100); 178 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 179 | delay(100); 180 | 181 | char signal[5]; 182 | sprintf(signal, "%d", WiFi.RSSI()); 183 | sendToBroker("report/signal", signal); // Reports the signal strength 184 | delay(100); 185 | } 186 | 187 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 188 | const char s[2] = "/"; 189 | int pos = 0; 190 | tokens[0] = strtok(topic, s); 191 | 192 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 193 | pos++; 194 | tokens[pos] = strtok(NULL, s); 195 | } 196 | 197 | return pos; 198 | } 199 | 200 | void checkMqtt() { 201 | if (!client.connected()) { 202 | startMqtt(); 203 | } 204 | } 205 | 206 | void sendToBroker(char* topic, char* message) { 207 | if (client.connected()) { 208 | char topicArr[100]; 209 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 210 | client.publish(topicArr, message); 211 | } 212 | } 213 | 214 | void turnOff(int pin) { 215 | Serial.printf("Turning off pin %d...\n", pin); 216 | switch (pin) { 217 | case 1: 218 | digitalWrite(switchPin1, LOW); 219 | sendToBroker("report/powerState1", "OFF"); 220 | break; 221 | case 2: 222 | digitalWrite(switchPin2, LOW); 223 | sendToBroker("report/powerState2", "OFF"); 224 | break; 225 | case 3: 226 | digitalWrite(switchPin3, LOW); 227 | sendToBroker("report/powerState3", "OFF"); 228 | break; 229 | case 4: 230 | digitalWrite(switchPin4, LOW); 231 | sendToBroker("report/powerState4", "OFF"); 232 | break; 233 | default: 234 | break; 235 | } 236 | } 237 | 238 | void turnOn(int pin) { 239 | Serial.printf("Turning on pin %d...\n", pin); 240 | switch (pin) { 241 | case 1: 242 | digitalWrite(switchPin1, HIGH); 243 | sendToBroker("report/powerState1", "ON"); 244 | break; 245 | case 2: 246 | digitalWrite(switchPin2, HIGH); 247 | sendToBroker("report/powerState2", "ON"); 248 | break; 249 | case 3: 250 | digitalWrite(switchPin3, HIGH); 251 | sendToBroker("report/powerState3", "ON"); 252 | break; 253 | case 4: 254 | digitalWrite(switchPin4, HIGH); 255 | sendToBroker("report/powerState4", "ON"); 256 | break; 257 | default: 258 | break; 259 | } 260 | } 261 | 262 | void setBrightness(int index, int value) { 263 | Serial.printf("Percentage %d set to %d\n", index, value); 264 | char val[4]; 265 | sprintf(val, "%d",value); 266 | switch (index) { 267 | case 1: 268 | brightness1 = value; 269 | sendToBroker("report/percentage1", val); 270 | break; 271 | case 2: 272 | brightness2 = value; 273 | sendToBroker("report/percentage2", val); 274 | break; 275 | case 3: 276 | brightness3 = value; 277 | sendToBroker("report/percentage3", val); 278 | break; 279 | case 4: 280 | brightness4 = value; 281 | sendToBroker("report/percentage4", val); 282 | break; 283 | default: 284 | break; 285 | } 286 | } 287 | -------------------------------------------------------------------------------- /Devices/lightRgb/lightRgb.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Example-lightRgb" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int lightPin = 0; 17 | int brightness = 0; 18 | int red = 0; 19 | int green = 0; 20 | int blue = 0; 21 | 22 | void startWifi(); 23 | void startMqtt(); 24 | void checkMqtt(); 25 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 26 | void callback(char* topic, byte* payload, unsigned int length); 27 | void sendToBroker(char* topic, char* message); 28 | 29 | void setBrightness(int value); 30 | int splitColor(char* color, char* tokensC[]); 31 | void setColor(int r, int g, int b); 32 | void turnOff(); 33 | void turnOn(); 34 | 35 | void setup() { 36 | pinMode(lightPin, OUTPUT); 37 | Serial.begin(115200); 38 | startWifi(); 39 | startMqtt(); 40 | } 41 | 42 | void loop() { 43 | client.loop(); 44 | checkMqtt(); 45 | } 46 | 47 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 48 | Serial.print("Topic:"); 49 | Serial.println(topic); 50 | int tokensNumber = 10; 51 | char* tokens[tokensNumber]; 52 | char message[length + 1]; 53 | splitTopic(topic, tokens, tokensNumber); 54 | sprintf(message, "%c", (char)payload[0]); 55 | for (int i = 1; i < length; i++) { 56 | sprintf(message, "%s%c", message, (char)payload[i]); 57 | } 58 | Serial.print("Message:"); 59 | Serial.println(message); 60 | 61 | //------------------ACTIONS HERE--------------------------------- 62 | 63 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 64 | if (strcmp(message, "ON") == 0) { 65 | turnOn(); 66 | } else if (strcmp(message, "OFF") == 0) { 67 | turnOff(); 68 | } 69 | } else if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "percentage") == 0) { 70 | int val = atoi(message); 71 | if (val >= 0 && val <= 100) { 72 | setBrightness(val); 73 | } 74 | } else if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "color") == 0) { 75 | char* tokensC[3]; 76 | splitColor(message, tokensC); 77 | int r = atoi(tokensC[0] + 4); 78 | int g = atoi(tokensC[1]); 79 | int b = atoi(tokensC[2]); 80 | 81 | setColor(r, g, b); 82 | } 83 | } 84 | 85 | void startWifi() { 86 | WiFi.mode(WIFI_STA); 87 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 88 | Serial.println("Connecting ..."); 89 | int attempts = 0; 90 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 91 | attempts++; 92 | delay(500); 93 | Serial.print("."); 94 | } 95 | 96 | if (WiFi.status() == WL_CONNECTED) { 97 | Serial.println('\n'); 98 | Serial.print("Connected to "); 99 | Serial.println(WiFi.SSID()); 100 | Serial.print("IP address:\t"); 101 | Serial.println(WiFi.localIP()); 102 | 103 | } else { 104 | Serial.println('\n'); 105 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 106 | } 107 | 108 | delay(500); 109 | } 110 | 111 | void startMqtt() { 112 | client.setServer(MQTT_BROKER, MQTT_PORT); 113 | client.setCallback(callback); 114 | 115 | while (!client.connected()) { 116 | Serial.println("Connecting to MQTT..."); 117 | 118 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 119 | Serial.println("connected"); 120 | } else { 121 | if (client.state() == 5) { 122 | Serial.println("Connection not allowed by broker, possible reasons:"); 123 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 124 | Serial.println("- Wrong Username or password. Check credentials"); 125 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 126 | 127 | } else { 128 | Serial.println("Not possible to connect to Broker Error code:"); 129 | Serial.print(client.state()); 130 | } 131 | 132 | delay(0x7530); 133 | } 134 | } 135 | 136 | char subscibeTopic[100]; 137 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 138 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 139 | 140 | sendToBroker("report/online", "true"); // Reports that the device is online 141 | delay(100); 142 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 143 | delay(100); 144 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 145 | delay(100); 146 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 147 | delay(100); 148 | 149 | char signal[5]; 150 | sprintf(signal, "%d", WiFi.RSSI()); 151 | sendToBroker("report/signal", signal); // Reports the signal strength 152 | delay(100); 153 | } 154 | 155 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 156 | const char s[2] = "/"; 157 | int pos = 0; 158 | tokens[0] = strtok(topic, s); 159 | 160 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 161 | pos++; 162 | tokens[pos] = strtok(NULL, s); 163 | } 164 | 165 | return pos; 166 | } 167 | 168 | int splitColor(char* color, char* tokensC[]) { 169 | const char s[2] = ","; 170 | int pos = 0; 171 | tokensC[0] = strtok(color, s); 172 | 173 | while (pos < 3 - 1 && tokensC[pos] != NULL) { 174 | pos++; 175 | tokensC[pos] = strtok(NULL, s); 176 | } 177 | 178 | return pos; 179 | } 180 | 181 | void checkMqtt() { 182 | if (!client.connected()) { 183 | startMqtt(); 184 | } 185 | } 186 | 187 | void sendToBroker(char* topic, char* message) { 188 | if (client.connected()) { 189 | char topicArr[100]; 190 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 191 | client.publish(topicArr, message); 192 | } 193 | } 194 | 195 | void turnOff() { 196 | Serial.printf("Turning off...\n"); 197 | digitalWrite(lightPin, HIGH); 198 | sendToBroker("report/powerState", "OFF"); 199 | } 200 | 201 | void turnOn() { 202 | Serial.printf("Turning on...\n"); 203 | digitalWrite(lightPin, LOW); 204 | sendToBroker("report/powerState", "ON"); 205 | } 206 | 207 | void setBrightness(int value) { 208 | brightness = value; 209 | Serial.printf("Percentage set to %d\n", brightness); 210 | } 211 | 212 | void setColor(int r, int g, int b) { 213 | red = r; 214 | green = g; 215 | blue = b; 216 | 217 | Serial.printf("Color set to %d,%d,%d\n", red, green, blue); 218 | char msg[18]; 219 | sprintf(msg, "rgb(%d,%d,%d)", red, green, blue); 220 | sendToBroker("report/color", msg); 221 | } -------------------------------------------------------------------------------- /Devices/lock/lock.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Example-lock" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int lockPin = 0; 17 | int openTime = 5000; //Time to keep the door open 18 | 19 | void startWifi(); 20 | void startMqtt(); 21 | void checkMqtt(); 22 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 23 | void callback(char* topic, byte* payload, unsigned int length); 24 | void sendToBroker(char* topic, char* message); 25 | 26 | void turnOff(); 27 | void turnOn(); 28 | void open(); 29 | void close(); 30 | 31 | void setup() { 32 | pinMode(lockPin, OUTPUT); 33 | Serial.begin(115200); 34 | startWifi(); 35 | startMqtt(); 36 | } 37 | 38 | void loop() { 39 | client.loop(); 40 | checkMqtt(); 41 | } 42 | 43 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 44 | Serial.print("Topic:"); 45 | Serial.println(topic); 46 | int tokensNumber = 10; 47 | char* tokens[tokensNumber]; 48 | char message[length + 1]; 49 | splitTopic(topic, tokens, tokensNumber); 50 | sprintf(message, "%c", (char)payload[0]); 51 | for (int i = 1; i < length; i++) { 52 | sprintf(message, "%s%c", message, (char)payload[i]); 53 | } 54 | Serial.print("Message:"); 55 | Serial.println(message); 56 | 57 | //------------------ACTIONS HERE--------------------------------- 58 | 59 | ///Actions here 60 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 61 | if (strcmp(message, "ON") == 0) { 62 | turnOn(); 63 | } else if (strcmp(message, "OFF") == 0) { 64 | turnOff(); 65 | } 66 | } else if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "lockedState") == 0) { 67 | if (strcmp(message, "false") == 0) { 68 | open(); 69 | } else if (strcmp(message, "true") == 0) { 70 | close(); 71 | } 72 | } 73 | } 74 | 75 | void startWifi() { 76 | WiFi.mode(WIFI_STA); 77 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 78 | Serial.println("Connecting ..."); 79 | int attempts = 0; 80 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 81 | attempts++; 82 | delay(500); 83 | Serial.print("."); 84 | } 85 | 86 | if (WiFi.status() == WL_CONNECTED) { 87 | Serial.println('\n'); 88 | Serial.print("Connected to "); 89 | Serial.println(WiFi.SSID()); 90 | Serial.print("IP address:\t"); 91 | Serial.println(WiFi.localIP()); 92 | 93 | } else { 94 | Serial.println('\n'); 95 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 96 | } 97 | 98 | delay(500); 99 | } 100 | 101 | void startMqtt() { 102 | client.setServer(MQTT_BROKER, MQTT_PORT); 103 | client.setCallback(callback); 104 | 105 | while (!client.connected()) { 106 | Serial.println("Connecting to MQTT..."); 107 | 108 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 109 | Serial.println("connected"); 110 | } else { 111 | if (client.state() == 5) { 112 | Serial.println("Connection not allowed by broker, possible reasons:"); 113 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 114 | Serial.println("- Wrong Username or password. Check credentials"); 115 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 116 | 117 | } else { 118 | Serial.println("Not possible to connect to Broker Error code:"); 119 | Serial.print(client.state()); 120 | } 121 | 122 | delay(0x7530); 123 | } 124 | } 125 | 126 | char subscibeTopic[100]; 127 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 128 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 129 | 130 | sendToBroker("report/online", "true"); // Reports that the device is online 131 | delay(100); 132 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 133 | delay(100); 134 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 135 | delay(100); 136 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 137 | delay(100); 138 | 139 | char signal[5]; 140 | sprintf(signal, "%d", WiFi.RSSI()); 141 | sendToBroker("report/signal", signal); // Reports the signal strength 142 | delay(100); 143 | } 144 | 145 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 146 | const char s[2] = "/"; 147 | int pos = 0; 148 | tokens[0] = strtok(topic, s); 149 | 150 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 151 | pos++; 152 | tokens[pos] = strtok(NULL, s); 153 | } 154 | 155 | return pos; 156 | } 157 | 158 | void checkMqtt() { 159 | if (!client.connected()) { 160 | startMqtt(); 161 | } 162 | } 163 | 164 | void sendToBroker(char* topic, char* message) { 165 | if (client.connected()) { 166 | char topicArr[100]; 167 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 168 | client.publish(topicArr, message); 169 | } 170 | } 171 | 172 | void turnOff() { 173 | Serial.println("Turning off"); 174 | // Add code 175 | sendToBroker("report/powerState", "OFF"); 176 | } 177 | 178 | void turnOn() { 179 | Serial.println("Turning on"); 180 | // Add code 181 | sendToBroker("report/powerState", "ON"); 182 | } 183 | 184 | void open() { 185 | Serial.println("Opening"); 186 | digitalWrite(lockPin, LOW); 187 | sendToBroker("report/lockedState", "false"); 188 | delay(openTime); 189 | close(); 190 | } 191 | 192 | void close() { 193 | Serial.println("Closing"); 194 | digitalWrite(lockPin, HIGH); 195 | sendToBroker("report/lockedState", "true"); 196 | } -------------------------------------------------------------------------------- /Devices/motionSensor/motionSensor.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Example-motionSensor" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int sensorPin = 0; 17 | bool sensorOn = true; 18 | int sensorReportSent = 0; 19 | bool sensorTriggered = false; 20 | 21 | void startWifi(); 22 | void startMqtt(); 23 | void turnOff(); 24 | void turnOn(); 25 | void sendReport(bool value); 26 | void checkSensor(); 27 | void checkMqtt(); 28 | int splitTopic(char *topic, char *tokens[], int tokensNumber); 29 | void callback(char *topic, byte *payload, unsigned int length); 30 | void sendToBroker(char *topic, char *message); 31 | 32 | void setup() { 33 | pinMode(sensorPin, INPUT); 34 | Serial.begin(115200); 35 | startWifi(); 36 | startMqtt(); 37 | } 38 | 39 | void loop() { 40 | client.loop(); 41 | checkSensor(); 42 | checkMqtt(); 43 | } 44 | 45 | void callback(char *topic, byte *payload, unsigned int length) { //A new message has been received 46 | Serial.print("Topic:"); 47 | Serial.println(topic); 48 | int tokensNumber = 10; 49 | char *tokens[tokensNumber]; 50 | char message[length + 1]; 51 | splitTopic(topic, tokens, tokensNumber); 52 | sprintf(message, "%c", (char)payload[0]); 53 | for (int i = 1; i < length; i++) { 54 | sprintf(message, "%s%c", message, (char)payload[i]); 55 | } 56 | Serial.print("Message:"); 57 | Serial.println(message); 58 | 59 | char reportChange[100]; 60 | sprintf(reportChange, "%s/report/powerState", MQTT_CLIENT); 61 | 62 | //------------------ACTIONS HERE--------------------------------- 63 | 64 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 65 | if (strcmp(message, "ON") == 0) { 66 | turnOn(); 67 | } else if (strcmp(message, "OFF") == 0) { 68 | turnOff(); 69 | } 70 | } 71 | } 72 | 73 | void startWifi() { 74 | WiFi.mode(WIFI_STA); 75 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 76 | Serial.println("Connecting ..."); 77 | int attempts = 0; 78 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 79 | attempts++; 80 | delay(500); 81 | Serial.print("."); 82 | } 83 | 84 | if (WiFi.status() == WL_CONNECTED) { 85 | Serial.println('\n'); 86 | Serial.print("Connected to "); 87 | Serial.println(WiFi.SSID()); 88 | Serial.print("IP address:\t"); 89 | Serial.println(WiFi.localIP()); 90 | } else { 91 | Serial.println('\n'); 92 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 93 | } 94 | 95 | delay(500); 96 | } 97 | 98 | void startMqtt() { 99 | client.setServer(MQTT_BROKER, MQTT_PORT); 100 | client.setCallback(callback); 101 | 102 | while (!client.connected()) { 103 | Serial.println("Connecting to MQTT..."); 104 | 105 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 106 | Serial.println("connected"); 107 | } else { 108 | if (client.state() == 5) { 109 | Serial.println("Connection not allowed by broker, possible reasons:"); 110 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 111 | Serial.println("- Wrong Username or password. Check credentials"); 112 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 113 | } else { 114 | Serial.println("Not possible to connect to Broker Error code:"); 115 | Serial.print(client.state()); 116 | } 117 | 118 | delay(0x7530); 119 | } 120 | } 121 | 122 | char subscibeTopic[100]; 123 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 124 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 125 | 126 | sendToBroker("report/online", "true"); // Reports that the device is online 127 | delay(100); 128 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 129 | delay(100); 130 | sendToBroker("report/ip", (char *)WiFi.localIP().toString().c_str()); // Reports the ip 131 | delay(100); 132 | sendToBroker("report/network", (char *)WiFi.SSID().c_str()); // Reports the network name 133 | delay(100); 134 | 135 | char signal[5]; 136 | sprintf(signal, "%d", WiFi.RSSI()); 137 | sendToBroker("report/signal", signal); // Reports the signal strength 138 | delay(100); 139 | } 140 | 141 | int splitTopic(char *topic, char *tokens[], int tokensNumber) { 142 | const char s[2] = "/"; 143 | int pos = 0; 144 | tokens[0] = strtok(topic, s); 145 | 146 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 147 | pos++; 148 | tokens[pos] = strtok(NULL, s); 149 | } 150 | 151 | return pos; 152 | } 153 | 154 | void checkMqtt() { 155 | if (!client.connected()) { 156 | startMqtt(); 157 | } 158 | } 159 | 160 | void checkSensor() { 161 | int buttonState = digitalRead(sensorPin); 162 | if (buttonState == LOW && !sensorTriggered) { 163 | return; 164 | } else if (buttonState == LOW && sensorTriggered) { 165 | sensorTriggered = false; 166 | sendReport(false); 167 | } else if (buttonState == HIGH && !sensorTriggered) { 168 | sensorTriggered = true; 169 | sendReport(true); 170 | } else if (buttonState == HIGH && sensorTriggered) { 171 | return; 172 | } 173 | } 174 | 175 | void turnOff() { 176 | sensorOn = false; 177 | char reportTopic[100]; 178 | sprintf(reportTopic, "%s/report/powerState", MQTT_CLIENT); 179 | client.publish(reportTopic, "OFF"); 180 | } 181 | void turnOn() { 182 | sensorOn = true; 183 | char reportTopic[100]; 184 | sprintf(reportTopic, "%s/report/powerState", MQTT_CLIENT); 185 | client.publish(reportTopic, "ON"); 186 | } 187 | 188 | void sendReport(bool value) { 189 | if (millis() - sensorReportSent > 500 && sensorOn) { 190 | if (value) 191 | sendToBroker("report/detectionState", "true"); 192 | else 193 | sendToBroker("report/detectionState", "false"); 194 | sensorReportSent = millis(); 195 | } 196 | } 197 | 198 | void sendToBroker(char *topic, char *message) { 199 | if (client.connected()) { 200 | char topicArr[100]; 201 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 202 | client.publish(topicArr, message); 203 | } 204 | } -------------------------------------------------------------------------------- /Devices/sprinkler/sprinkler.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Example-sprinkler" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int sprinklerPin = 0; 17 | int wateringTime = 5000; // Select how long do you want the water pump to be active in ms, 5000 = 5 seconds 18 | 19 | void startWifi(); 20 | void startMqtt(); 21 | void checkMqtt(); 22 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 23 | void callback(char* topic, byte* payload, unsigned int length); 24 | void sendToBroker(char* topic, char* message); 25 | 26 | void turnOff(); 27 | void turnOn(); 28 | 29 | void setup() { 30 | pinMode(sprinklerPin, OUTPUT); 31 | Serial.begin(115200); 32 | startWifi(); 33 | startMqtt(); 34 | } 35 | 36 | void loop() { 37 | client.loop(); 38 | checkMqtt(); 39 | } 40 | 41 | void callback(char* topic, byte* payload, unsigned int length) { // This function runs when there is a new message in the subscribed topic 42 | Serial.print("Topic:"); 43 | Serial.println(topic); 44 | 45 | // Splits the topic and gets the message 46 | int tokensNumber = 10; 47 | char* tokens[tokensNumber]; 48 | char message[length + 1]; 49 | splitTopic(topic, tokens, tokensNumber); 50 | sprintf(message, "%c", (char)payload[0]); 51 | 52 | for (int i = 1; i < length; i++) { 53 | sprintf(message, "%s%c", message, (char)payload[i]); 54 | } 55 | 56 | Serial.print("Message:"); 57 | Serial.println(message); 58 | 59 | //------------------ACTIONS HERE--------------------------------- 60 | char reportChange[100]; 61 | 62 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 63 | if (strcmp(message, "ON") == 0) { 64 | turnOn(); 65 | 66 | } else if (strcmp(message, "OFF") == 0) { 67 | turnOff(); 68 | } 69 | } 70 | } 71 | 72 | void startWifi() { 73 | WiFi.mode(WIFI_STA); 74 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 75 | Serial.println("Connecting ..."); 76 | int attempts = 0; 77 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 78 | attempts++; 79 | delay(500); 80 | Serial.print("."); 81 | } 82 | 83 | if (WiFi.status() == WL_CONNECTED) { 84 | Serial.println('\n'); 85 | Serial.print("Connected to "); 86 | Serial.println(WiFi.SSID()); 87 | Serial.print("IP address:\t"); 88 | Serial.println(WiFi.localIP()); 89 | 90 | } else { 91 | Serial.println('\n'); 92 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 93 | } 94 | 95 | delay(500); 96 | } 97 | 98 | void startMqtt() { 99 | client.setServer(MQTT_BROKER, MQTT_PORT); 100 | client.setCallback(callback); 101 | 102 | while (!client.connected()) { 103 | Serial.println("Connecting to MQTT..."); 104 | 105 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 106 | Serial.println("connected"); 107 | 108 | } else { 109 | if (client.state() == 5) { 110 | Serial.println("Connection not allowed by broker, possible reasons:"); 111 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 112 | Serial.println("- Wrong Username or password. Check credentials"); 113 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 114 | 115 | } else { 116 | Serial.println("Not possible to connect to Broker Error code:"); 117 | Serial.print(client.state()); 118 | } 119 | 120 | delay(0x7530); 121 | } 122 | } 123 | 124 | char subscibeTopic[100]; 125 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 126 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 127 | 128 | sendToBroker("report/online", "true"); // Reports that the device is online 129 | delay(100); 130 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 131 | delay(100); 132 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 133 | delay(100); 134 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 135 | delay(100); 136 | 137 | char signal[5]; 138 | sprintf(signal, "%d", WiFi.RSSI()); 139 | sendToBroker("report/signal", signal); // Reports the signal strength 140 | delay(100); 141 | } 142 | 143 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 144 | const char s[2] = "/"; 145 | int pos = 0; 146 | tokens[0] = strtok(topic, s); 147 | 148 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 149 | pos++; 150 | tokens[pos] = strtok(NULL, s); 151 | } 152 | 153 | return pos; 154 | } 155 | 156 | void checkMqtt() { 157 | if (!client.connected()) { 158 | startMqtt(); 159 | } 160 | } 161 | 162 | void turnOff() { 163 | Serial.println("Turning Off.."); 164 | digitalWrite(sprinklerPin, LOW); 165 | sendToBroker("report/powerState", "OFF"); 166 | } 167 | 168 | void turnOn() { 169 | Serial.println("Turning On"); 170 | digitalWrite(sprinklerPin, HIGH); 171 | sendToBroker("report/powerState", "ON"); 172 | 173 | delay(wateringTime); 174 | turnOff(); 175 | } 176 | 177 | void sendToBroker(char* topic, char* message) { 178 | if (client.connected()) { 179 | char topicArr[100]; 180 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 181 | client.publish(topicArr, message); 182 | } 183 | } -------------------------------------------------------------------------------- /Devices/switch/switch.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Example-switch" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int switchPin = 0; 17 | 18 | void startWifi(); 19 | void startMqtt(); 20 | void checkMqtt(); 21 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 22 | void callback(char* topic, byte* payload, unsigned int length); 23 | void sendToBroker(char* topic, char* message); 24 | 25 | void turnOff(); 26 | void turnOn(); 27 | 28 | void setup() { 29 | pinMode(switchPin, OUTPUT); 30 | Serial.begin(115200); 31 | startWifi(); 32 | startMqtt(); 33 | } 34 | 35 | void loop() { 36 | client.loop(); 37 | checkMqtt(); 38 | } 39 | 40 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 41 | Serial.print("Topic:"); 42 | Serial.println(topic); 43 | int tokensNumber = 10; 44 | char* tokens[tokensNumber]; 45 | char message[length + 1]; 46 | splitTopic(topic, tokens, tokensNumber); 47 | sprintf(message, "%c", (char)payload[0]); 48 | for (int i = 1; i < length; i++) { 49 | sprintf(message, "%s%c", message, (char)payload[i]); 50 | } 51 | Serial.print("Message:"); 52 | Serial.println(message); 53 | 54 | //------------------ACTIONS HERE--------------------------------- 55 | 56 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 57 | if (strcmp(message, "ON") == 0) { 58 | turnOn(); 59 | } else if (strcmp(message, "OFF") == 0) { 60 | turnOff(); 61 | } 62 | } 63 | } 64 | 65 | void startWifi() { 66 | WiFi.mode(WIFI_STA); 67 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 68 | Serial.println("Connecting ..."); 69 | int attempts = 0; 70 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 71 | attempts++; 72 | delay(500); 73 | Serial.print("."); 74 | } 75 | 76 | if (WiFi.status() == WL_CONNECTED) { 77 | Serial.println('\n'); 78 | Serial.print("Connected to "); 79 | Serial.println(WiFi.SSID()); 80 | Serial.print("IP address:\t"); 81 | Serial.println(WiFi.localIP()); 82 | 83 | } else { 84 | Serial.println('\n'); 85 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 86 | } 87 | 88 | delay(500); 89 | } 90 | 91 | void startMqtt() { 92 | client.setServer(MQTT_BROKER, MQTT_PORT); 93 | client.setCallback(callback); 94 | 95 | while (!client.connected()) { 96 | Serial.println("Connecting to MQTT..."); 97 | 98 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 99 | Serial.println("connected"); 100 | } else { 101 | if (client.state() == 5) { 102 | Serial.println("Connection not allowed by broker, possible reasons:"); 103 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 104 | Serial.println("- Wrong Username or password. Check credentials"); 105 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 106 | 107 | } else { 108 | Serial.println("Not possible to connect to Broker Error code:"); 109 | Serial.print(client.state()); 110 | } 111 | 112 | delay(0x7530); 113 | } 114 | } 115 | 116 | char subscibeTopic[100]; 117 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 118 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 119 | 120 | sendToBroker("report/online", "true"); // Reports that the device is online 121 | delay(100); 122 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 123 | delay(100); 124 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 125 | delay(100); 126 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 127 | delay(100); 128 | 129 | char signal[5]; 130 | sprintf(signal, "%d", WiFi.RSSI()); 131 | sendToBroker("report/signal", signal); // Reports the signal strength 132 | delay(100); 133 | } 134 | 135 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 136 | const char s[2] = "/"; 137 | int pos = 0; 138 | tokens[0] = strtok(topic, s); 139 | 140 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 141 | pos++; 142 | tokens[pos] = strtok(NULL, s); 143 | } 144 | 145 | return pos; 146 | } 147 | 148 | void checkMqtt() { 149 | if (!client.connected()) { 150 | startMqtt(); 151 | } 152 | } 153 | 154 | void sendToBroker(char* topic, char* message) { 155 | if (client.connected()) { 156 | char topicArr[100]; 157 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 158 | client.publish(topicArr, message); 159 | } 160 | } 161 | 162 | void turnOff() { 163 | Serial.printf("Turning off...\n"); 164 | digitalWrite(switchPin, HIGH); 165 | sendToBroker("report/powerState", "OFF"); 166 | } 167 | 168 | void turnOn() { 169 | Serial.printf("Turning on...\n"); 170 | digitalWrite(switchPin, LOW); 171 | sendToBroker("report/powerState", "ON"); 172 | } 173 | -------------------------------------------------------------------------------- /Devices/switchGroup/switchGroup.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Example-switchGroup" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | 17 | int switchPin1 = 0; 18 | int switchPin2 = 3; 19 | int switchPin3 = 5; 20 | int switchPin4 = 4; 21 | 22 | void startWifi(); 23 | void startMqtt(); 24 | void checkMqtt(); 25 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 26 | void callback(char* topic, byte* payload, unsigned int length); 27 | void sendToBroker(char* topic, char* message); 28 | 29 | void turnOff(int pin); 30 | void turnOn(int pin); 31 | 32 | void setup() { 33 | pinMode(switchPin1, OUTPUT); 34 | pinMode(switchPin2, OUTPUT); 35 | pinMode(switchPin3, OUTPUT); 36 | Serial.begin(115200); 37 | startWifi(); 38 | startMqtt(); 39 | } 40 | 41 | void loop() { 42 | client.loop(); 43 | checkMqtt(); 44 | } 45 | 46 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 47 | Serial.print("Topic:"); 48 | Serial.println(topic); 49 | int tokensNumber = 10; 50 | char* tokens[tokensNumber]; 51 | char message[length + 1]; 52 | splitTopic(topic, tokens, tokensNumber); 53 | sprintf(message, "%c", (char)payload[0]); 54 | for (int i = 1; i < length; i++) { 55 | sprintf(message, "%s%c", message, (char)payload[i]); 56 | } 57 | Serial.print("Message:"); 58 | Serial.println(message); 59 | 60 | //------------------ACTIONS HERE--------------------- 61 | 62 | if (strcmp(tokens[1], "directive") == 0) { 63 | if (strcmp(tokens[2], "powerState1") == 0) { 64 | if (strcmp(message, "ON") == 0) { 65 | turnOn(1); 66 | } else if (strcmp(message, "OFF") == 0) { 67 | turnOff(1); 68 | } 69 | } else if (strcmp(tokens[2], "powerState2") == 0) { 70 | if (strcmp(message, "ON") == 0) { 71 | turnOn(2); 72 | } else if (strcmp(message, "OFF") == 0) { 73 | turnOff(2); 74 | } 75 | } else if (strcmp(tokens[2], "powerState3") == 0) { 76 | if (strcmp(message, "ON") == 0) { 77 | turnOn(3); 78 | } else if (strcmp(message, "OFF") == 0) { 79 | turnOff(3); 80 | } 81 | } else if (strcmp(tokens[2], "powerState4") == 0) { 82 | if (strcmp(message, "ON") == 0) { 83 | turnOn(4); 84 | } else if (strcmp(message, "OFF") == 0) { 85 | turnOff(4); 86 | } 87 | } 88 | } 89 | } 90 | 91 | void startWifi() { 92 | WiFi.mode(WIFI_STA); 93 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 94 | Serial.println("Connecting ..."); 95 | int attempts = 0; 96 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 97 | attempts++; 98 | delay(500); 99 | Serial.print("."); 100 | } 101 | 102 | if (WiFi.status() == WL_CONNECTED) { 103 | Serial.println('\n'); 104 | Serial.print("Connected to "); 105 | Serial.println(WiFi.SSID()); 106 | Serial.print("IP address:\t"); 107 | Serial.println(WiFi.localIP()); 108 | 109 | } else { 110 | Serial.println('\n'); 111 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 112 | } 113 | 114 | delay(500); 115 | } 116 | 117 | void startMqtt() { 118 | client.setServer(MQTT_BROKER, MQTT_PORT); 119 | client.setCallback(callback); 120 | 121 | while (!client.connected()) { 122 | Serial.println("Connecting to MQTT..."); 123 | 124 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 125 | Serial.println("connected"); 126 | } else { 127 | if (client.state() == 5) { 128 | Serial.println("Connection not allowed by broker, possible reasons:"); 129 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 130 | Serial.println("- Wrong Username or password. Check credentials"); 131 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 132 | 133 | } else { 134 | Serial.println("Not possible to connect to Broker Error code:"); 135 | Serial.print(client.state()); 136 | } 137 | 138 | delay(0x7530); 139 | } 140 | } 141 | 142 | char topic[100]; 143 | sprintf(topic, "%s/#", MQTT_CLIENT); 144 | client.subscribe(topic); 145 | 146 | sendToBroker("report/online", "true"); // Reports that the device is online 147 | delay(100); 148 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 149 | delay(100); 150 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 151 | delay(100); 152 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 153 | delay(100); 154 | 155 | char signal[5]; 156 | sprintf(signal, "%d", WiFi.RSSI()); 157 | sendToBroker("report/signal", signal); // Reports the signal strength 158 | delay(100); 159 | } 160 | 161 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 162 | const char s[2] = "/"; 163 | int pos = 0; 164 | tokens[0] = strtok(topic, s); 165 | 166 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 167 | pos++; 168 | tokens[pos] = strtok(NULL, s); 169 | } 170 | 171 | return pos; 172 | } 173 | 174 | void checkMqtt() { 175 | if (!client.connected()) { 176 | startMqtt(); 177 | } 178 | } 179 | 180 | void sendToBroker(char* topic, char* message) { 181 | if (client.connected()) { 182 | char topicArr[100]; 183 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 184 | client.publish(topicArr, message); 185 | } 186 | } 187 | 188 | void turnOff(int pin) { 189 | Serial.printf("Turning off pin %d...\n", pin); 190 | switch (pin) { 191 | case 1: 192 | digitalWrite(switchPin1, LOW); 193 | sendToBroker("report/powerState1", "OFF"); 194 | break; 195 | case 2: 196 | digitalWrite(switchPin2, LOW); 197 | sendToBroker("report/powerState2", "OFF"); 198 | break; 199 | case 3: 200 | digitalWrite(switchPin3, LOW); 201 | sendToBroker("report/powerState3", "OFF"); 202 | break; 203 | case 4: 204 | digitalWrite(switchPin4, LOW); 205 | sendToBroker("report/powerState4", "OFF"); 206 | break; 207 | default: 208 | break; 209 | } 210 | } 211 | 212 | void turnOn(int pin) { 213 | Serial.printf("Turning on pin %d...\n", pin); 214 | switch (pin) { 215 | case 1: 216 | digitalWrite(switchPin1, HIGH); 217 | sendToBroker("report/powerState1", "ON"); 218 | break; 219 | case 2: 220 | digitalWrite(switchPin2, HIGH); 221 | sendToBroker("report/powerState2", "ON"); 222 | break; 223 | case 3: 224 | digitalWrite(switchPin3, HIGH); 225 | sendToBroker("report/powerState3", "ON"); 226 | break; 227 | case 4: 228 | digitalWrite(switchPin4, HIGH); 229 | sendToBroker("report/powerState4", "ON"); 230 | break; 231 | default: 232 | break; 233 | } 234 | } 235 | -------------------------------------------------------------------------------- /Devices/temperatureSensor/temperatureSensor.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | #include 5 | 6 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 7 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 8 | #define MQTT_BROKER "smartnest.cz" // Broker host 9 | #define MQTT_PORT 1883 // Broker port 10 | #define MQTT_USERNAME "username" // Username from Smartnest 11 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 12 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 13 | #define FIRMWARE_VERSION "Example-temperatureSensor" // Custom name for this program 14 | 15 | WiFiClient espClient; 16 | PubSubClient client(espClient); 17 | int sensorPin = 0; 18 | int value = 10; 19 | int interval = 20; 20 | Ticker sendUpdate; 21 | 22 | void startWifi(); 23 | void startMqtt(); 24 | void checkMqtt(); 25 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 26 | void callback(char* topic, byte* payload, unsigned int length); 27 | void sendToBroker(char* topic, char* message); 28 | 29 | void turnOff(); 30 | void turnOn(); 31 | void sendValue(); 32 | 33 | void setup() { 34 | pinMode(sensorPin, INPUT); 35 | Serial.begin(115200); 36 | startWifi(); 37 | startMqtt(); 38 | } 39 | 40 | void loop() { 41 | client.loop(); 42 | checkMqtt(); 43 | } 44 | 45 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 46 | Serial.print("Topic:"); 47 | Serial.println(topic); 48 | int tokensNumber = 10; 49 | char* tokens[tokensNumber]; 50 | char message[length + 1]; 51 | splitTopic(topic, tokens, tokensNumber); 52 | sprintf(message, "%c", (char)payload[0]); 53 | for (int i = 1; i < length; i++) { 54 | sprintf(message, "%s%c", message, (char)payload[i]); 55 | } 56 | Serial.print("Message:"); 57 | Serial.println(message); 58 | 59 | //------------------ACTIONS HERE--------------------------------- 60 | 61 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 62 | if (strcmp(message, "ON") == 0) { 63 | turnOn(); 64 | } else if (strcmp(message, "OFF") == 0) { 65 | turnOff(); 66 | } 67 | } 68 | } 69 | 70 | void startWifi() { 71 | WiFi.mode(WIFI_STA); 72 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 73 | Serial.println("Connecting ..."); 74 | int attempts = 0; 75 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 76 | attempts++; 77 | delay(500); 78 | Serial.print("."); 79 | } 80 | 81 | if (WiFi.status() == WL_CONNECTED) { 82 | Serial.println('\n'); 83 | Serial.print("Connected to "); 84 | Serial.println(WiFi.SSID()); 85 | Serial.print("IP address:\t"); 86 | Serial.println(WiFi.localIP()); 87 | 88 | } else { 89 | Serial.println('\n'); 90 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 91 | } 92 | 93 | delay(500); 94 | } 95 | 96 | void startMqtt() { 97 | client.setServer(MQTT_BROKER, MQTT_PORT); 98 | client.setCallback(callback); 99 | 100 | while (!client.connected()) { 101 | Serial.println("Connecting to MQTT..."); 102 | 103 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 104 | Serial.println("connected"); 105 | sendUpdate.attach(interval, sendValue); 106 | 107 | } else { 108 | if (client.state() == 5) { 109 | Serial.println("Connection not allowed by broker, possible reasons:"); 110 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 111 | Serial.println("- Wrong Username or password. Check credentials"); 112 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 113 | 114 | } else { 115 | Serial.println("Not possible to connect to Broker Error code:"); 116 | Serial.print(client.state()); 117 | } 118 | 119 | delay(0x7530); 120 | } 121 | } 122 | 123 | char subscibeTopic[100]; 124 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 125 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 126 | 127 | sendToBroker("report/online", "true"); // Reports that the device is online 128 | delay(100); 129 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 130 | delay(100); 131 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 132 | delay(100); 133 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 134 | delay(100); 135 | 136 | char signal[5]; 137 | sprintf(signal, "%d", WiFi.RSSI()); 138 | sendToBroker("report/signal", signal); // Reports the signal strength 139 | delay(100); 140 | } 141 | 142 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 143 | const char s[2] = "/"; 144 | int pos = 0; 145 | tokens[0] = strtok(topic, s); 146 | 147 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 148 | pos++; 149 | tokens[pos] = strtok(NULL, s); 150 | } 151 | 152 | return pos; 153 | } 154 | 155 | void checkMqtt() { 156 | if (!client.connected()) { 157 | Serial.printf("Device lost connection with broker status %, reconnecting...\n", client.connected()); 158 | 159 | if (WiFi.status() != WL_CONNECTED) { 160 | startWifi(); 161 | } 162 | 163 | sendUpdate.detach(); 164 | startMqtt(); 165 | } 166 | } 167 | 168 | void sendToBroker(char* topic, char* message) { 169 | if (client.connected()) { 170 | char topicArr[100]; 171 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 172 | client.publish(topicArr, message); 173 | } 174 | } 175 | 176 | void turnOff() { 177 | Serial.printf("Turning off...\n"); 178 | sendToBroker("report/powerState", "OFF"); 179 | } 180 | 181 | void turnOn() { 182 | Serial.printf("Turning on...\n"); 183 | sendToBroker("report/powerState", "ON"); 184 | } 185 | 186 | void sendValue() { 187 | value = analogRead(sensorPin); 188 | char message[5]; 189 | sprintf(message, "%d", value); 190 | sendToBroker("report/temperature", message); 191 | } 192 | -------------------------------------------------------------------------------- /Devices/thermostat/thermostat.ino: -------------------------------------------------------------------------------- 1 | //#include "WiFi.h" //Uncomment for ESP32 2 | #include //Comment for ESP-32 3 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 4 | #include 5 | 6 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 7 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 8 | #define MQTT_BROKER "smartnest.cz" // Broker host 9 | #define MQTT_PORT 1883 // Broker port 10 | #define MQTT_USERNAME "username" // Username from Smartnest 11 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 12 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 13 | #define FIRMWARE_VERSION "Example-thermostat" // Custom name for this program 14 | 15 | WiFiClient espClient; 16 | PubSubClient client(espClient); 17 | int heaterPin = 2; 18 | int sensorpin = A0; 19 | 20 | char topic[100]; 21 | char msg[5]; 22 | double temp = 25; 23 | double setpoint = 25; 24 | double lastTimeSent = 0; 25 | 26 | void startWifi(); 27 | void startMqtt(); 28 | void checkMqtt(); 29 | void mqttLoop(); 30 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 31 | void callback(char* topic, byte* payload, unsigned int length); 32 | 33 | void turnOn(); 34 | void turnOff(); 35 | void setSetpoint(double temp); 36 | void reportTemperature(double temp); 37 | 38 | void setup() { 39 | pinMode(heaterPin, OUTPUT); 40 | pinMode(sensorpin, INPUT); 41 | Serial.begin(115200); 42 | startWifi(); 43 | startMqtt(); 44 | } 45 | 46 | void loop() { 47 | client.loop(); 48 | checkMqtt(); 49 | reportTemperature(temp); 50 | } 51 | 52 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 53 | 54 | int tokensNumber = 10; 55 | char message[length + 1]; 56 | char* tokens[tokensNumber]; 57 | 58 | Serial.print("MQTT Message Received. Topic:"); 59 | Serial.println(topic); 60 | Serial.print("Message:"); 61 | Serial.println(message); 62 | 63 | splitTopic(topic, tokens, tokensNumber); 64 | 65 | sprintf(message, "%c", (char)payload[0]); 66 | for (int i = 1; i < length; i++) { 67 | sprintf(message, "%s%c", message, (char)payload[i]); 68 | } 69 | 70 | //------------------Decide what to do depending on the topic and message--------------------------------- 71 | 72 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { // Turn On or OFF 73 | 74 | if (strcmp(message, "ON") == 0) { 75 | turnOn(); 76 | } else if (strcmp(message, "OFF") == 0) { 77 | turnOff(); 78 | } 79 | 80 | } else if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "setpoint") == 0) { // Set Setpoint Temperature 81 | 82 | double value = atof(message); 83 | if (isnan(value)) { 84 | setpoint = 0; 85 | } 86 | 87 | setSetpoint(value); 88 | 89 | sprintf(topic, "%s/report/setpoint", MQTT_CLIENT); 90 | client.publish(topic, message); 91 | } 92 | } 93 | 94 | void startWifi() { 95 | WiFi.mode(WIFI_STA); 96 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 97 | Serial.println("Connecting ..."); 98 | 99 | while (WiFi.status() != WL_CONNECTED) { 100 | delay(500); 101 | Serial.print("."); 102 | } 103 | 104 | Serial.println('\n'); 105 | Serial.print("Connected to "); 106 | Serial.println(WiFi.SSID()); 107 | Serial.print("IP address:\t"); 108 | Serial.println(WiFi.localIP()); 109 | delay(500); 110 | } 111 | 112 | void startMqtt() { 113 | client.setServer(MQTT_BROKER, MQTT_PORT); 114 | client.setCallback(callback); 115 | 116 | while (!client.connected()) { 117 | Serial.println("Connecting to MQTT..."); 118 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 119 | Serial.println("connected"); 120 | } else { 121 | if (client.state() == 5) { 122 | Serial.println("Connection not allowed by broker, possible reasons:"); 123 | Serial.println("- Device is already online. Wait some seconds until it appears offline"); 124 | Serial.println("- Wrong Username or password. Check credentials"); 125 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 126 | 127 | } else { 128 | Serial.println("Not possible to connect to Broker Error code:"); 129 | Serial.print(client.state()); 130 | } 131 | 132 | delay(0x7530); 133 | } 134 | } 135 | 136 | char subscibeTopic[100]; 137 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 138 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 139 | 140 | sendToBroker("report/online", "true"); // Reports that the device is online 141 | delay(100); 142 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 143 | delay(100); 144 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 145 | delay(100); 146 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 147 | delay(100); 148 | 149 | char signal[5]; 150 | sprintf(signal, "%d", WiFi.RSSI()); 151 | sendToBroker("report/signal", signal); // Reports the signal strength 152 | delay(100); 153 | } 154 | 155 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 156 | const char s[2] = "/"; 157 | int pos = 0; 158 | tokens[0] = strtok(topic, s); 159 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 160 | pos++; 161 | tokens[pos] = strtok(NULL, s); 162 | } 163 | return pos; 164 | } 165 | 166 | void checkMqtt() { 167 | if (!client.connected()) { 168 | startMqtt(); 169 | } 170 | } 171 | 172 | void turnOff() { 173 | digitalWrite(heaterPin, HIGH); 174 | 175 | if (client.connected()) { 176 | sendToBroker("report/temperature", "OFF"); 177 | } 178 | } 179 | 180 | void turnOn() { 181 | digitalWrite(heaterPin, LOW); 182 | 183 | if (client.connected()) { 184 | sendToBroker("report/temperature", "ON"); 185 | } 186 | } 187 | 188 | void setSetpoint(double temp) { 189 | Serial.printf("setpoint changed to %3.2f", temp); 190 | //Do something with the value, Turn on Heater? 191 | } 192 | 193 | void reportTemperature(double temp) { 194 | if (millis() - lastTimeSent > 30000 && client.connected()) { 195 | char message[5]; 196 | sprintf(message, "%d", temp); 197 | sendToBroker("report/value", message); 198 | sendToBroker("report/temperature", message); 199 | lastTimeSent = millis(); 200 | } 201 | } 202 | 203 | void sendToBroker(char* topic, char* message) { 204 | if (client.connected()) { 205 | char topicArr[100]; 206 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 207 | client.publish(topicArr, message); 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /Devices/tv/tv.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Example-tv" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int tvPin = 0; 17 | 18 | void startWifi(); 19 | void startMqtt(); 20 | void checkMqtt(); 21 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 22 | void callback(char* topic, byte* payload, unsigned int length); 23 | void sendToBroker(char* topic, char* message); 24 | 25 | void turnOff(); 26 | void turnOn(); 27 | 28 | void setup() { 29 | pinMode(tvPin, OUTPUT); 30 | Serial.begin(115200); 31 | startWifi(); 32 | startMqtt(); 33 | } 34 | 35 | void loop() { 36 | client.loop(); 37 | checkMqtt(); 38 | } 39 | 40 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 41 | Serial.print("Topic:"); 42 | Serial.println(topic); 43 | int tokensNumber = 10; 44 | char* tokens[tokensNumber]; 45 | char message[length + 1]; 46 | splitTopic(topic, tokens, tokensNumber); 47 | sprintf(message, "%c", (char)payload[0]); 48 | for (int i = 1; i < length; i++) { 49 | sprintf(message, "%s%c", message, (char)payload[i]); 50 | } 51 | Serial.print("Message:"); 52 | Serial.println(message); 53 | 54 | //------------------ACTIONS HERE--------------------------------- 55 | 56 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 57 | if (strcmp(message, "ON") == 0) { 58 | turnOn(); 59 | } else if (strcmp(message, "OFF") == 0) { 60 | turnOff(); 61 | } 62 | } 63 | } 64 | 65 | void startWifi() { 66 | WiFi.mode(WIFI_STA); 67 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 68 | Serial.println("Connecting ..."); 69 | int attempts = 0; 70 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 71 | attempts++; 72 | delay(500); 73 | Serial.print("."); 74 | } 75 | 76 | if (WiFi.status() == WL_CONNECTED) { 77 | Serial.println('\n'); 78 | Serial.print("Connected to "); 79 | Serial.println(WiFi.SSID()); 80 | Serial.print("IP address:\t"); 81 | Serial.println(WiFi.localIP()); 82 | 83 | } else { 84 | Serial.println('\n'); 85 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 86 | } 87 | 88 | delay(500); 89 | } 90 | 91 | void startMqtt() { 92 | client.setServer(MQTT_BROKER, MQTT_PORT); 93 | client.setCallback(callback); 94 | 95 | while (!client.connected()) { 96 | Serial.println("Connecting to MQTT..."); 97 | 98 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 99 | Serial.println("connected"); 100 | } else { 101 | if (client.state() == 5) { 102 | Serial.println("Connection not allowed by broker, possible reasons:"); 103 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 104 | Serial.println("- Wrong Username or password. Check credentials"); 105 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 106 | 107 | } else { 108 | Serial.println("Not possible to connect to Broker Error code:"); 109 | Serial.print(client.state()); 110 | } 111 | 112 | delay(0x7530); 113 | } 114 | } 115 | 116 | char subscibeTopic[100]; 117 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 118 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 119 | 120 | sendToBroker("report/online", "true"); // Reports that the device is online 121 | delay(100); 122 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 123 | delay(100); 124 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 125 | delay(100); 126 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 127 | delay(100); 128 | 129 | char signal[5]; 130 | sprintf(signal, "%d", WiFi.RSSI()); 131 | sendToBroker("report/signal", signal); // Reports the signal strength 132 | delay(100); 133 | } 134 | 135 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 136 | const char s[2] = "/"; 137 | int pos = 0; 138 | tokens[0] = strtok(topic, s); 139 | 140 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 141 | pos++; 142 | tokens[pos] = strtok(NULL, s); 143 | } 144 | 145 | return pos; 146 | } 147 | 148 | void checkMqtt() { 149 | if (!client.connected()) { 150 | startMqtt(); 151 | } 152 | } 153 | 154 | void sendToBroker(char* topic, char* message) { 155 | if (client.connected()) { 156 | char topicArr[100]; 157 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 158 | client.publish(topicArr, message); 159 | } 160 | } 161 | 162 | void turnOff() { 163 | Serial.printf("Turning off...\n"); 164 | digitalWrite(tvPin, HIGH); 165 | sendToBroker("report/powerState", "OFF"); 166 | } 167 | 168 | void turnOn() { 169 | Serial.printf("Turning on...\n"); 170 | digitalWrite(tvPin, LOW); 171 | sendToBroker("report/powerState", "ON"); 172 | } 173 | -------------------------------------------------------------------------------- /Devices/watermeter/watermeter.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | #include 5 | 6 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 7 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 8 | #define MQTT_BROKER "smartnest.cz" // Broker host 9 | #define MQTT_PORT 1883 // Broker port 10 | #define MQTT_USERNAME "username" // Username from Smartnest 11 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 12 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 13 | #define FIRMWARE_VERSION "Example-watermeter" // Custom name for this program 14 | 15 | WiFiClient espClient; 16 | PubSubClient client(espClient); 17 | double lastValue = 0; 18 | int interval = 20; 19 | Ticker sendUpdate; 20 | 21 | void startWifi(); 22 | void startMqtt(); 23 | void checkMqtt(); 24 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 25 | void callback(char* topic, byte* payload, unsigned int length); 26 | void sendToBroker(char* topic, char* message); 27 | 28 | void turnOff(); 29 | void turnOn(); 30 | void sendValue(); 31 | double readMeter(); 32 | 33 | void setup() { 34 | Serial.begin(115200); 35 | startWifi(); 36 | startMqtt(); 37 | } 38 | 39 | void loop() { 40 | client.loop(); 41 | checkMqtt(); 42 | } 43 | 44 | double readMeter(){ 45 | // Your code to read the Meter value 46 | return 100.0; 47 | } 48 | 49 | void callback(char* topic, byte* payload, unsigned int length) { //Callback for new messages 50 | Serial.print("Topic:"); 51 | Serial.println(topic); 52 | int tokensNumber = 10; 53 | char* tokens[tokensNumber]; 54 | char message[length + 1]; 55 | splitTopic(topic, tokens, tokensNumber); 56 | sprintf(message, "%c", (char)payload[0]); 57 | for (int i = 1; i < length; i++) { 58 | sprintf(message, "%s%c", message, (char)payload[i]); 59 | } 60 | Serial.print("Message:"); 61 | Serial.println(message); 62 | 63 | } 64 | 65 | void startWifi() { 66 | WiFi.mode(WIFI_STA); 67 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 68 | Serial.println("Connecting ..."); 69 | int attempts = 0; 70 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 71 | attempts++; 72 | delay(500); 73 | Serial.print("."); 74 | } 75 | 76 | if (WiFi.status() == WL_CONNECTED) { 77 | Serial.println('\n'); 78 | Serial.print("Connected to "); 79 | Serial.println(WiFi.SSID()); 80 | Serial.print("IP address:\t"); 81 | Serial.println(WiFi.localIP()); 82 | 83 | } else { 84 | Serial.println('\n'); 85 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 86 | } 87 | 88 | delay(500); 89 | } 90 | 91 | void startMqtt() { 92 | client.setServer(MQTT_BROKER, MQTT_PORT); 93 | client.setCallback(callback); 94 | 95 | while (!client.connected()) { 96 | Serial.println("Connecting to MQTT..."); 97 | 98 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 99 | Serial.println("connected"); 100 | sendUpdate.attach(interval, sendValue); 101 | 102 | } else { 103 | if (client.state() == 5) { 104 | Serial.println("Connection not allowed by broker, possible reasons:"); 105 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 106 | Serial.println("- Wrong Username or password. Check credentials"); 107 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 108 | 109 | } else { 110 | Serial.println("Not possible to connect to Broker Error code:"); 111 | Serial.print(client.state()); 112 | } 113 | 114 | delay(0x7530); 115 | } 116 | } 117 | 118 | char subscibeTopic[100]; 119 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 120 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 121 | 122 | sendToBroker("report/online", "true"); // Reports that the device is online 123 | delay(100); 124 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 125 | delay(100); 126 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 127 | delay(100); 128 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 129 | delay(100); 130 | 131 | char signal[5]; 132 | sprintf(signal, "%d", WiFi.RSSI()); 133 | sendToBroker("report/signal", signal); // Reports the signal strength 134 | delay(100); 135 | } 136 | 137 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 138 | const char s[2] = "/"; 139 | int pos = 0; 140 | tokens[0] = strtok(topic, s); 141 | 142 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 143 | pos++; 144 | tokens[pos] = strtok(NULL, s); 145 | } 146 | 147 | return pos; 148 | } 149 | 150 | void checkMqtt() { 151 | if (!client.connected()) { 152 | Serial.printf("Device lost connection with broker status %, reconnecting...\n", client.connected()); 153 | 154 | if (WiFi.status() != WL_CONNECTED) { 155 | startWifi(); 156 | } 157 | 158 | sendUpdate.detach(); 159 | startMqtt(); 160 | } 161 | } 162 | 163 | void sendToBroker(char* topic, char* message) { 164 | if (client.connected()) { 165 | char topicArr[100]; 166 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 167 | client.publish(topicArr, message); 168 | } 169 | } 170 | 171 | void sendValue() { 172 | double value = readMeter(); 173 | if(value!=lastValue){ 174 | char message[5]; 175 | sprintf(message, "%f", value); 176 | sendToBroker("report/total", message); 177 | } 178 | } 179 | 180 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Smartnest Logo](https://www.smartnest.cz/img/Logo-vector-login.png) 2 | 3 | # Smartnest 4 | Smartnest is a web service that allows you to connect your Arduino, ESP and other development boards to voice assistants like Amazon Alexa, Google Assistant, Siri, IFTTT and more. 5 | 6 | ## Getting started 7 | 1. Create your free account at [Smartnest](https://www.smartnest.cz) 8 | 2. Create a new device and copy the device ID 9 | 3. Upload the [Blink example code](https://github.com/aososam/Smartnest/tree/master/Tutorials/Blink) to your Board. 10 | 4. Done! Control your devices from any source. 11 | 12 | ## Control your devices: 13 | * From your Computer: Using the web Application [Visit web App](https://www.smartnest.cz/login) 14 | * From your Phone: Download the [Android](https://play.google.com/store/apps/details?id=cz.smartnest.smartnestcz) or [iOS App](https://apps.apple.com/cz/app/smartnest/id1509508554) and manage and control your devices from your mobile. You can also [add the web app icon to your home screen](https://www.docu.smartnest.cz/using-web-app/4.-web-app-on-your-mobile-device) for iOS Android and Windows phone. 15 | * From your tablet: Download the [Android](https://play.google.com/store/apps/details?id=cz.smartnest.smartnestcz) or [iOS App](https://apps.apple.com/cz/app/smartnest/id1509508554) for your Tablet, or using the [web application](https://www.smartnest.cz/login) that will adapt to any screen size. 16 | * Alexa Skill: Use your voice to control your devices, the skill is available in the skill store. [Visit Skill](https://skills-store.amazon.com/deeplink/dp/B07VH46TDC?deviceType=app&share&refSuffix=ss_copy) 17 | * Siri: [Download the Siri shortcut.](https://www.docu.smartnest.cz/siri-integration) Or connect to [Home bride](https://www.docu.smartnest.cz/homebridge-integration) for a better experience. 18 | * Google Home: Link the Smartnest Action with your account using your Google Home App [Google Integration guide](https://www.docu.smartnest.cz/google-home-integration) 19 | * IFTT: Connect your devices with more than 600 available services, [Follow the configuration guide](https://www.docu.smartnest.cz/ifttt-integration) 20 | * Tasmota: Connect your devices using Tasmota firmware to Smartnest and add suppor for all voice assitants and more instantly.Follow the [Configuration guide](https://www.docu.smartnest.cz/tasmota-integration) 21 | * Home Asssistant: Take advantage of the rich interface and integratios of Home Assistant while being able to control your devices with any voice assistant. Follow the [Configuration guide](https://www.docu.smartnest.cz/home-assistant-integration) 22 | 23 | 24 | The connection is made using MQTT, there are plenty of MQTT libraries on the web so feel free to choose the one that suits your project and your Board. 25 | You can also download one of our examples, we use the library pubsubclient from knolleary which supports the following devices: 26 | 27 | * Arduino Ethernet 28 | * Arduino Ethernet Shield 29 | * Arduino YUN 30 | * Arduino WiFi Shield 31 | * Sparkfun WiFly Shield 32 | * TI CC3000 WiFi 33 | * Intel Galileo/Edison 34 | * ESP8266 35 | * ESP32 36 | 37 | Download and install from https://github.com/knolleary/pubsubclient 38 | 39 | ## [Documentation in English](https://www.docu.smartnest.cz) 40 | ## [Documentation in Spanish](https://www.documentacion.smartnest.cz) 41 | ## [Buy me a coffe](https://www.buymeacoffee.com/andressosam) 42 | 43 | 44 | Future developments: 45 | * Camera support. 46 | 47 | ### Contributions: 48 | 49 | * English version of website: Laila Maghawry 50 | * French version of website: Abdoulaye Boubakari 51 | 52 | -------------------------------------------------------------------------------- /Tutorials/Blink/Blink.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Tutorial-Blink" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int lightPin = 2; 17 | 18 | void startWifi(); 19 | void startMqtt(); 20 | void checkMqtt(); 21 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 22 | void callback(char* topic, byte* payload, unsigned int length); 23 | void sendToBroker(char* topic, char* message); 24 | 25 | void setup() { 26 | pinMode(lightPin, OUTPUT); 27 | Serial.begin(115200); 28 | startWifi(); 29 | startMqtt(); 30 | } 31 | 32 | void loop() { 33 | client.loop(); 34 | checkMqtt(); 35 | } 36 | 37 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 38 | Serial.print("Topic:"); 39 | Serial.println(topic); 40 | int tokensNumber = 10; 41 | char* tokens[tokensNumber]; 42 | char message[length + 1]; 43 | splitTopic(topic, tokens, tokensNumber); 44 | sprintf(message, "%c", (char)payload[0]); 45 | for (int i = 1; i < length; i++) { 46 | sprintf(message, "%s%c", message, (char)payload[i]); 47 | } 48 | Serial.print("Message:"); 49 | Serial.println(message); 50 | 51 | //------------------ACTIONS HERE--------------------------------- 52 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 53 | if (strcmp(message, "ON") == 0) { 54 | digitalWrite(lightPin, LOW); 55 | sendToBroker("report/powerState", "ON"); 56 | 57 | } else if (strcmp(message, "OFF") == 0) { 58 | digitalWrite(lightPin, HIGH); 59 | sendToBroker("report/powerState", "OFF"); 60 | } 61 | } 62 | } 63 | 64 | void startWifi() { 65 | WiFi.mode(WIFI_STA); 66 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 67 | Serial.println("Connecting ..."); 68 | int attempts = 0; 69 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 70 | attempts++; 71 | delay(500); 72 | Serial.print("."); 73 | } 74 | 75 | if (WiFi.status() == WL_CONNECTED) { 76 | Serial.println('\n'); 77 | Serial.print("Connected to "); 78 | Serial.println(WiFi.SSID()); 79 | Serial.print("IP address:\t"); 80 | Serial.println(WiFi.localIP()); 81 | 82 | } else { 83 | Serial.println('\n'); 84 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 85 | } 86 | 87 | delay(500); 88 | } 89 | 90 | void startMqtt() { 91 | client.setServer(MQTT_BROKER, MQTT_PORT); 92 | client.setCallback(callback); 93 | 94 | while (!client.connected()) { 95 | Serial.println("Connecting to MQTT..."); 96 | 97 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 98 | Serial.println("connected"); 99 | } else { 100 | if (client.state() == 5) { 101 | Serial.println("Connection not allowed by broker, possible reasons:"); 102 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 103 | Serial.println("- Wrong Username or password. Check credentials"); 104 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 105 | 106 | } else { 107 | Serial.println("Not possible to connect to Broker Error code:"); 108 | Serial.print(client.state()); 109 | } 110 | 111 | delay(0x7530); 112 | } 113 | } 114 | 115 | char subscibeTopic[100]; 116 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 117 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 118 | 119 | sendToBroker("report/online", "true"); // Reports that the device is online 120 | delay(100); 121 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 122 | delay(100); 123 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 124 | delay(100); 125 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 126 | delay(100); 127 | 128 | char signal[5]; 129 | sprintf(signal, "%d", WiFi.RSSI()); 130 | sendToBroker("report/signal", signal); // Reports the signal strength 131 | delay(100); 132 | } 133 | 134 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 135 | const char s[2] = "/"; 136 | int pos = 0; 137 | tokens[0] = strtok(topic, s); 138 | 139 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 140 | pos++; 141 | tokens[pos] = strtok(NULL, s); 142 | } 143 | 144 | return pos; 145 | } 146 | 147 | void checkMqtt() { 148 | if (!client.connected()) { 149 | startMqtt(); 150 | } 151 | } 152 | 153 | void sendToBroker(char* topic, char* message) { 154 | if (client.connected()) { 155 | char topicArr[100]; 156 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 157 | client.publish(topicArr, message); 158 | } 159 | } -------------------------------------------------------------------------------- /Tutorials/Blink/README.md: -------------------------------------------------------------------------------- 1 | ![Smartnest Logo](https://www.smartnest.cz/img/Logo-vector-login.png) 2 | # Blink Tutorial 3 | 4 | ## Connect your own devices to Alexa, Google Home, Siri IFTTT and more 5 | 6 | Learn how you can easily connect your own home automation projects to Alexa, Google Home, Siri or IFTTT and control them even from your computer phone or tablet. This is the first video of a series of videos of how you can automate your home yourself without spending a huge amount of money on expensive products. 7 | 8 | - Project video: 9 | English: https://youtu.be/eGgYHNvwVjY 10 | Spanish: https://youtu.be/B1lAlvrdjww 11 | 12 | - Free web service: 13 | https://www.smartnest.cz 14 | 15 | - Example code: 16 | https://github.com/aososam/Smartnest/blob/master/Tutorials/Blink/Blink.ino 17 | 18 | - PubSubClient MQTT library: 19 | https://github.com/knolleary/pubsubclient 20 | 21 | 22 | 23 | ### Components: 24 | Node Mcu: https://www.aliexpress.com/item/33040293736.html?spm=a2g0o.productlist.0.0.567954c1JMk2lg 25 | 26 | 27 | ### Other social networks: 28 | Instagram: @smartnestcz 29 | Facebook: @smartnestcz 30 | twitter: @smartnestcz 31 | tiktok: @smartnest 32 | linked-in: @andres-sosa 33 | 34 | -------------------------------------------------------------------------------- /Tutorials/Doorbell-Door/Doorbell-Door.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT_DOORBELL "doorbell-Id" // Device Id from smartnest 12 | #define MQTT_CLIENT_LOCK "Lock-Id" // Device Id from smartnest Type Lock 13 | #define FIRMWARE_VERSION "Tutorial-DoorbellLock" // Custom name for this program 14 | 15 | WiFiClient espClient; 16 | PubSubClient client(espClient); 17 | int bellPin = 0; 18 | bool bellTriggered = false; 19 | int bellReportSend = 0; 20 | 21 | int doorPin = 0; 22 | int openTime = 5000; //Time to keep the door open 23 | 24 | void startWifi(); 25 | void startMqtt(); 26 | void sendBellReport(); 27 | void checkBell(); 28 | void checkMqtt(); 29 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 30 | void callback(char* topic, byte* payload, unsigned int length); 31 | 32 | void open(); 33 | void close(); 34 | 35 | void setup() { 36 | pinMode(bellPin, INPUT); 37 | pinMode(doorPin, OUTPUT); 38 | Serial.begin(115200); 39 | startWifi(); 40 | startMqtt(); 41 | } 42 | 43 | void loop() { 44 | client.loop(); 45 | checkBell(); 46 | checkMqtt(); 47 | } 48 | 49 | void callback(char* topic, byte* payload, unsigned int length) { // This function runs when there is a new message in the subscribed topic 50 | Serial.print("Topic:"); 51 | Serial.println(topic); 52 | 53 | // Splits the topic and gets the message 54 | int tokensNumber = 10; 55 | char* tokens[tokensNumber]; 56 | char message[length + 1]; 57 | splitTopic(topic, tokens, tokensNumber); 58 | sprintf(message, "%c", (char)payload[0]); 59 | 60 | for (int i = 1; i < length; i++) { 61 | sprintf(message, "%s%c", message, (char)payload[i]); 62 | } 63 | 64 | Serial.print("Message:"); 65 | Serial.println(message); 66 | 67 | //------------------ACTIONS HERE--------------------------------- 68 | 69 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "lockedState") == 0) { 70 | if (strcmp(message, "false") == 0) { 71 | open(); 72 | } else if (strcmp(message, "true") == 0) { 73 | close(); 74 | } 75 | } 76 | } 77 | 78 | void startWifi() { 79 | WiFi.mode(WIFI_STA); 80 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 81 | Serial.println("Connecting ..."); 82 | int attempts = 0; 83 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 84 | attempts++; 85 | delay(500); 86 | Serial.print("."); 87 | } 88 | 89 | if (WiFi.status() == WL_CONNECTED) { 90 | Serial.println('\n'); 91 | Serial.print("Connected to "); 92 | Serial.println(WiFi.SSID()); 93 | Serial.print("IP address:\t"); 94 | Serial.println(WiFi.localIP()); 95 | 96 | } else { 97 | Serial.println('\n'); 98 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 99 | } 100 | 101 | delay(500); 102 | } 103 | 104 | void startMqtt() { 105 | client.setServer(MQTT_BROKER, MQTT_PORT); 106 | client.setCallback(callback); 107 | 108 | while (!client.connected()) { 109 | Serial.println("Connecting to MQTT..."); 110 | 111 | if (client.connect(MQTT_CLIENT_DOORBELL, MQTT_USERNAME, MQTT_PASSWORD)) { 112 | Serial.println("connected"); 113 | } else { 114 | if (client.state() == 5) { 115 | Serial.println("Connection not allowed by broker, possible reasons:"); 116 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 117 | Serial.println("- Wrong Username or password. Check credentials"); 118 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 119 | 120 | } else { 121 | Serial.println("Not possible to connect to Broker Error code:"); 122 | Serial.print(client.state()); 123 | } 124 | 125 | delay(0x7530); 126 | } 127 | } 128 | 129 | char topic[100]; 130 | sprintf(topic, "%s/#", MQTT_CLIENT_DOORBELL); 131 | client.subscribe(topic); 132 | sprintf(topic, "%s/#", MQTT_CLIENT_LOCK); 133 | client.subscribe(topic); 134 | 135 | sprintf(topic, "%s/report/online", MQTT_CLIENT_DOORBELL); // Reports that the device is online 136 | client.publish(topic, "true"); 137 | 138 | sprintf(topic, "%s/report/online", MQTT_CLIENT_LOCK); // Reports that the device is online 139 | client.publish(topic, "true"); 140 | delay(200); 141 | 142 | char subscibeTopic[100]; 143 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT_DOORBELL); 144 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 145 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT_LOCK); 146 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 147 | 148 | sendToBroker("report/online", "true", MQTT_CLIENT_DOORBELL); // Reports that the device is online 149 | delay(100); 150 | sendToBroker("report/online", "true", MQTT_CLIENT_LOCK); // Reports that the device is online 151 | delay(100); 152 | 153 | sendToBroker("report/firmware", FIRMWARE_VERSION, MQTT_CLIENT_DOORBELL); // Reports the firmware version 154 | delay(100); 155 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str(), MQTT_CLIENT_DOORBELL); // Reports the ip 156 | delay(100); 157 | sendToBroker("report/network", (char*)WiFi.SSID().c_str(), MQTT_CLIENT_DOORBELL); // Reports the network name 158 | delay(100); 159 | 160 | char signal[5]; 161 | sprintf(signal, "%d", WiFi.RSSI()); 162 | sendToBroker("report/signal", signal, MQTT_CLIENT_DOORBELL); // Reports the signal strength 163 | delay(100); 164 | } 165 | 166 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 167 | const char s[2] = "/"; 168 | int pos = 0; 169 | tokens[0] = strtok(topic, s); 170 | 171 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 172 | pos++; 173 | tokens[pos] = strtok(NULL, s); 174 | } 175 | 176 | return pos; 177 | } 178 | 179 | void checkMqtt() { 180 | if (!client.connected()) { 181 | startMqtt(); 182 | } 183 | } 184 | 185 | void checkBell() { 186 | int buttonState = digitalRead(bellPin); 187 | if (buttonState == LOW && !bellTriggered) { 188 | return; 189 | 190 | } else if (buttonState == LOW && bellTriggered) { 191 | bellTriggered = false; 192 | 193 | } else if (buttonState == HIGH && !bellTriggered) { 194 | bellTriggered = true; 195 | sendBellReport(); 196 | 197 | } else if (buttonState == HIGH && bellTriggered) { 198 | return; 199 | } 200 | } 201 | 202 | void sendBellReport() { //Avoids sending repeated reports. only once every 5 seconds. 203 | if (millis() - bellReportSend > 5000) { 204 | sendToBroker("report/detectionState", "true", MQTT_CLIENT_DOORBELL); 205 | bellReportSend = millis(); 206 | } 207 | } 208 | 209 | void open() { 210 | Serial.println("Opening"); 211 | digitalWrite(doorPin, LOW); 212 | sendToBroker("report/lockedState", "false", MQTT_CLIENT_LOCK); 213 | 214 | delay(openTime); 215 | close(); 216 | } 217 | 218 | void close() { 219 | Serial.println("Closing"); 220 | digitalWrite(doorPin, HIGH); 221 | sendToBroker("report/lockedState", "true", MQTT_CLIENT_LOCK); 222 | } 223 | 224 | void sendToBroker(char* topic, char* message, char* clientId) { 225 | if (client.connected()) { 226 | char topicArr[100]; 227 | sprintf(topicArr, "%s/%s", clientId, topic); 228 | client.publish(topicArr, message); 229 | } 230 | } -------------------------------------------------------------------------------- /Tutorials/Doorbell-Door/README.md: -------------------------------------------------------------------------------- 1 | ![Smartnest Logo](https://www.smartnest.cz/img/Logo-vector-login.png) 2 | # Doorbell project 3 | 4 | ## Connect any Doorbell to Alexa 5 | 6 | Learn how to connect any regular doorbell to Alexa and receive the alerts in all your echo devices. + Code for door opener 7 | 8 | - Project video: 9 | English: https://youtu.be/eGgYHNvwVjY 10 | Spanish: https://youtu.be/cgfVXPfCgkc 11 | 12 | - Free web service: 13 | https://www.smartnest.cz 14 | 15 | - Example code: 16 | https://github.com/aososam/Smartnest/blob/master/Tutorials/Doorbell/Doorbell-Door.ino 17 | 18 | - Stl file of the button for 3D printing: 19 | https://github.com/aososam/Smartnest/blob/master/Tutorials/Doorbell/3DButton.stl 20 | 21 | - PubSubClient MQTT library: 22 | https://github.com/knolleary/pubsubclient 23 | 24 | 25 | 26 | ### Components: 27 | Node Mcu: https://www.aliexpress.com/item/33040293736.html?spm=a2g0o.productlist.0.0.567954c1JMk2lg 28 | ESP Wifi Relay Module: https://www.aliexpress.com/item/32840806183.html?spm=a2g0o.productlist.0.0.1db74f12UD31Dn 29 | Optocoupler: https://www.aliexpress.com/item/33016844527.html?spm=a2g0o.productlist.0.0.166d3740YTgNwO 30 | Small breadboard 31 | Resistors 200 and 1K ohm 32 | 33 | 34 | ### Other social networks: 35 | Instagram: @smartnestcz 36 | Facebook: @smartnestcz 37 | twitter: @smartnestcz 38 | tiktok: @smartnest 39 | linked-in: @andres-sosa 40 | -------------------------------------------------------------------------------- /Tutorials/Doorbell/Doorbell.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Tutorial-Doorbell" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int bellPin = 5; 17 | bool bellTriggered = false; 18 | int bellReportSend = 0; 19 | 20 | void startWifi(); 21 | void startMqtt(); 22 | void sendBellReport(); 23 | void checkBell(); 24 | void checkMqtt(); 25 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 26 | void callback(char* topic, byte* payload, unsigned int length); 27 | void sendToBroker(char* topic, char* message); 28 | 29 | void setup() { 30 | pinMode(bellPin, INPUT); 31 | Serial.begin(115200); 32 | startWifi(); 33 | startMqtt(); 34 | } 35 | 36 | void loop() { 37 | client.loop(); 38 | checkBell(); 39 | checkMqtt(); 40 | } 41 | 42 | void callback(char* topic, byte* payload, unsigned int length) { // This function runs when there is a new message in the subscribed topic 43 | Serial.print("Topic:"); 44 | Serial.println(topic); 45 | 46 | // Splits the topic and gets the message 47 | int tokensNumber = 10; 48 | char* tokens[tokensNumber]; 49 | char message[length + 1]; 50 | splitTopic(topic, tokens, tokensNumber); 51 | sprintf(message, "%c", (char)payload[0]); 52 | 53 | for (int i = 1; i < length; i++) { 54 | sprintf(message, "%s%c", message, (char)payload[i]); 55 | } 56 | 57 | Serial.print("Message:"); 58 | Serial.println(message); 59 | } 60 | 61 | void startWifi() { 62 | WiFi.mode(WIFI_STA); 63 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 64 | Serial.println("Connecting ..."); 65 | int attempts = 0; 66 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 67 | attempts++; 68 | delay(500); 69 | Serial.print("."); 70 | } 71 | 72 | if (WiFi.status() == WL_CONNECTED) { 73 | Serial.println('\n'); 74 | Serial.print("Connected to "); 75 | Serial.println(WiFi.SSID()); 76 | Serial.print("IP address:\t"); 77 | Serial.println(WiFi.localIP()); 78 | 79 | } else { 80 | Serial.println('\n'); 81 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 82 | } 83 | 84 | delay(500); 85 | } 86 | 87 | void startMqtt() { 88 | client.setServer(MQTT_BROKER, MQTT_PORT); 89 | client.setCallback(callback); 90 | 91 | while (!client.connected()) { 92 | Serial.println("Connecting to MQTT..."); 93 | 94 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 95 | Serial.println("connected"); 96 | } else { 97 | if (client.state() == 5) { 98 | Serial.println("Connection not allowed by broker, possible reasons:"); 99 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 100 | Serial.println("- Wrong Username or password. Check credentials"); 101 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 102 | 103 | } else { 104 | Serial.println("Not possible to connect to Broker Error code:"); 105 | Serial.print(client.state()); 106 | } 107 | 108 | delay(0x7530); 109 | } 110 | } 111 | 112 | char subscibeTopic[100]; 113 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 114 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 115 | 116 | sendToBroker("report/online", "true"); // Reports that the device is online 117 | delay(100); 118 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 119 | delay(100); 120 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 121 | delay(100); 122 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 123 | delay(100); 124 | 125 | char signal[5]; 126 | sprintf(signal, "%d", WiFi.RSSI()); 127 | sendToBroker("report/signal", signal); // Reports the signal strength 128 | delay(100); 129 | } 130 | 131 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 132 | const char s[2] = "/"; 133 | int pos = 0; 134 | tokens[0] = strtok(topic, s); 135 | 136 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 137 | pos++; 138 | tokens[pos] = strtok(NULL, s); 139 | } 140 | 141 | return pos; 142 | } 143 | 144 | void checkMqtt() { 145 | if (!client.connected()) { 146 | startMqtt(); 147 | } 148 | } 149 | 150 | void checkBell() { 151 | int buttonState = digitalRead(bellPin); 152 | if (buttonState == LOW && !bellTriggered) { 153 | return; 154 | 155 | } else if (buttonState == LOW && bellTriggered) { 156 | bellTriggered = false; 157 | 158 | } else if (buttonState == HIGH && !bellTriggered) { 159 | bellTriggered = true; 160 | sendBellReport(); 161 | 162 | } else if (buttonState == HIGH && bellTriggered) { 163 | return; 164 | } 165 | } 166 | 167 | void sendBellReport() { //Avoids sending repeated reports. only once every 5 seconds. 168 | if (millis() - bellReportSend > 5000) { 169 | sendToBroker("report/detectionState", "true"); 170 | bellReportSend = millis(); 171 | } 172 | } 173 | 174 | void sendToBroker(char* topic, char* message) { 175 | if (client.connected()) { 176 | char topicArr[100]; 177 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 178 | client.publish(topicArr, message); 179 | } 180 | } -------------------------------------------------------------------------------- /Tutorials/Doorbell/README.md: -------------------------------------------------------------------------------- 1 | ![Smartnest Logo](https://www.smartnest.cz/img/Logo-vector-login.png) 2 | # Doorbell project 3 | 4 | ## Connect any Doorbell to Alexa 5 | 6 | Learn how to connect any regular doorbell to Alexa and receive the alerts in all your echo devices. 7 | 8 | - Project video: 9 | English: https://youtu.be/eGgYHNvwVjY 10 | Spanish: https://youtu.be/cgfVXPfCgkc 11 | 12 | - Free web service: 13 | https://www.smartnest.cz 14 | 15 | - Example code: 16 | https://github.com/aososam/Smartnest/blob/master/Tutorials/Doorbell/Doorbell.ino 17 | 18 | - Stl file of the button for 3D printing: 19 | https://github.com/aososam/Smartnest/blob/master/Tutorials/Doorbell/3DButton.stl 20 | 21 | - PubSubClient MQTT library: 22 | https://github.com/knolleary/pubsubclient 23 | 24 | 25 | 26 | ### Components: 27 | Node Mcu: https://www.aliexpress.com/item/33040293736.html?spm=a2g0o.productlist.0.0.567954c1JMk2lg 28 | ESP Wifi Relay Module: https://www.aliexpress.com/item/32840806183.html?spm=a2g0o.productlist.0.0.1db74f12UD31Dn 29 | Optocoupler: https://www.aliexpress.com/item/33016844527.html?spm=a2g0o.productlist.0.0.166d3740YTgNwO 30 | Small breadboard 31 | Resistors 200 and 1K ohm 32 | 33 | 34 | ### Other social networks: 35 | Instagram: @smartnestcz 36 | Facebook: @smartnestcz 37 | twitter: @smartnestcz 38 | tiktok: @smartnest 39 | linked-in: @andres-sosa 40 | 41 | -------------------------------------------------------------------------------- /Tutorials/Plant-Watering/Plant-Watering.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Tutorial-PlantWaterer" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int switchPin = 0; 17 | int wateringTime = 5000; // Select how long do you want the water pump to be active in ms, 5000 = 5 seconds 18 | 19 | void startWifi(); 20 | void startMqtt(); 21 | void checkMqtt(); 22 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 23 | void callback(char* topic, byte* payload, unsigned int length); 24 | void sendToBroker(char* topic, char* message); 25 | 26 | void turnOff(); 27 | void turnOn(); 28 | 29 | void setup() { 30 | pinMode(switchPin, OUTPUT); 31 | Serial.begin(115200); 32 | startWifi(); 33 | startMqtt(); 34 | } 35 | 36 | void loop() { 37 | client.loop(); 38 | checkMqtt(); 39 | } 40 | 41 | void callback(char* topic, byte* payload, unsigned int length) { // This function runs when there is a new message in the subscribed topic 42 | Serial.print("Topic:"); 43 | Serial.println(topic); 44 | 45 | // Splits the topic and gets the message 46 | int tokensNumber = 10; 47 | char* tokens[tokensNumber]; 48 | char message[length + 1]; 49 | splitTopic(topic, tokens, tokensNumber); 50 | sprintf(message, "%c", (char)payload[0]); 51 | 52 | for (int i = 1; i < length; i++) { 53 | sprintf(message, "%s%c", message, (char)payload[i]); 54 | } 55 | 56 | Serial.print("Message:"); 57 | Serial.println(message); 58 | 59 | //------------------ACTIONS HERE--------------------------------- 60 | char reportChange[100]; 61 | 62 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 63 | if (strcmp(message, "ON") == 0) { 64 | turnOn(); 65 | 66 | } else if (strcmp(message, "OFF") == 0) { 67 | turnOff(); 68 | } 69 | } 70 | } 71 | 72 | void startWifi() { 73 | WiFi.mode(WIFI_STA); 74 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 75 | Serial.println("Connecting ..."); 76 | int attempts = 0; 77 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 78 | attempts++; 79 | delay(500); 80 | Serial.print("."); 81 | } 82 | 83 | if (WiFi.status() == WL_CONNECTED) { 84 | Serial.println('\n'); 85 | Serial.print("Connected to "); 86 | Serial.println(WiFi.SSID()); 87 | Serial.print("IP address:\t"); 88 | Serial.println(WiFi.localIP()); 89 | 90 | } else { 91 | Serial.println('\n'); 92 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 93 | } 94 | 95 | delay(500); 96 | } 97 | 98 | void startMqtt() { 99 | client.setServer(MQTT_BROKER, MQTT_PORT); 100 | client.setCallback(callback); 101 | 102 | while (!client.connected()) { 103 | Serial.println("Connecting to MQTT..."); 104 | 105 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 106 | Serial.println("connected"); 107 | 108 | } else { 109 | if (client.state() == 5) { 110 | Serial.println("Connection not allowed by broker, possible reasons:"); 111 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 112 | Serial.println("- Wrong Username or password. Check credentials"); 113 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 114 | 115 | } else { 116 | Serial.println("Not possible to connect to Broker Error code:"); 117 | Serial.print(client.state()); 118 | } 119 | 120 | delay(0x7530); 121 | } 122 | } 123 | 124 | char subscibeTopic[100]; 125 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 126 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 127 | 128 | sendToBroker("report/online", "true"); // Reports that the device is online 129 | delay(100); 130 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 131 | delay(100); 132 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 133 | delay(100); 134 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 135 | delay(100); 136 | 137 | char signal[5]; 138 | sprintf(signal, "%d", WiFi.RSSI()); 139 | sendToBroker("report/signal", signal); // Reports the signal strength 140 | delay(100); 141 | } 142 | 143 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 144 | const char s[2] = "/"; 145 | int pos = 0; 146 | tokens[0] = strtok(topic, s); 147 | 148 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 149 | pos++; 150 | tokens[pos] = strtok(NULL, s); 151 | } 152 | 153 | return pos; 154 | } 155 | 156 | void checkMqtt() { 157 | if (!client.connected()) { 158 | startMqtt(); 159 | } 160 | } 161 | 162 | void turnOff() { 163 | Serial.println("Turning Off.."); 164 | digitalWrite(switchPin, LOW); 165 | sendToBroker("report/powerState", "OFF"); 166 | } 167 | void turnOn() { 168 | Serial.println("Turning On"); 169 | digitalWrite(switchPin, HIGH); 170 | sendToBroker("report/powerState", "ON"); 171 | 172 | delay(wateringTime); 173 | turnOff(); 174 | } 175 | 176 | void sendToBroker(char* topic, char* message) { 177 | if (client.connected()) { 178 | char topicArr[100]; 179 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 180 | client.publish(topicArr, message); 181 | } 182 | } -------------------------------------------------------------------------------- /Tutorials/Plant-Watering/README.md: -------------------------------------------------------------------------------- 1 | ![Smartnest Logo](https://www.smartnest.cz/img/Logo-vector-login.png) 2 | # Plant watering system 3 | 4 | ## Build your own smart watering system 5 | 6 | Learn how to build your own smart watering system and control it using Google home or Alexa. 7 | 8 | - Project video with Google Home: 9 | English: https://youtu.be/rnLqr6cVee4 10 | Spanish: https://youtu.be/xnuJdHDf9Dc 11 | 12 | - Project video with Alexa: 13 | English: https://youtu.be/yqRJXQyrvro 14 | Spanish: https://youtu.be/ayDCDV1MeQQ 15 | 16 | - Project video with Siri: 17 | English: https://youtu.be/b1UvzZ3Likk 18 | Spanish: https://youtu.be/jybtGCYznOQ 19 | Czech: https://youtu.be/5O0FwKqUnIg 20 | 21 | - Free web service: 22 | https://www.smartnest.cz 23 | 24 | - Example code: 25 | https://github.com/aososam/Smartnest/blob/master/Tutorials/Plant-Watering/Plant-Watering.ino 26 | 27 | - PubSubClient MQTT library: 28 | https://github.com/knolleary/pubsubclient 29 | 30 | 31 | 32 | ### Components: 33 | Peristaltic Pump 12V DC 34 | https://www.aliexpress.com/item/4000029323526.html?spm=a2g0o.productlist.0.0.50f77b00OhDFOj 35 | 36 | Silicone Rubber Tube 37 | 38 | ESP Wifi Relay Module 39 | https://www.aliexpress.com/item/32840806183.html?spm=a2g0o.productlist.0.0.1db74f12UD31Dn 40 | 41 | DC-DC Step Down Buck Converter 42 | https://www.aliexpress.com/item/32798886986.html?spm=a2g0o.productlist.0.0.1a95791bHg5z74 43 | 44 | 12 V DC adapter 45 | Water container 46 | 47 | 48 | ### Other social networks: 49 | Instagram: @smartnestcz 50 | Facebook: @smartnestcz 51 | twitter: @smartnestcz 52 | tiktok: @smartnest 53 | linked-in: @andres-sosa 54 | 55 | -------------------------------------------------------------------------------- /Tutorials/Raspberry-Blink/README.md: -------------------------------------------------------------------------------- 1 | ![Smartnest Logo](https://www.smartnest.cz/img/Logo-vector-login.png) 2 | # Blink Tutorial 3 | 4 | ## Connect your own devices to Alexa, Google Home, Siri IFTTT and more 5 | 6 | Learn how you can easily connect your own home automation projects to Alexa, Google Home, Siri or IFTTT and control them even from your computer phone or tablet. 7 | Learn how to connect your Raspberry to all voice assistants easly and for free 8 | 9 | - Project video: 10 | English: 11 | Spanish: 12 | 13 | - Free web service: 14 | https://www.smartnest.cz 15 | 16 | - Example code: 17 | https://github.com/aososam/Smartnest/blob/master/Tutorials/Raspberry-Blink 18 | 19 | 20 | ### Components: 21 | Rapsberry 22 | LED 23 | 222 ohm Resistor 24 | 25 | ## Instructions: 26 | After downloading the repository navigate to the project folder and run the following command 27 | `npm install` 28 | Then change your ClientId, Username and password from smartnest in the file called app.js 29 | Then run the command 30 | `node app.js` 31 | 32 | Thats all! now you are able to discover your devices using Alexa, Google, IFTTT and much more!. 33 | 34 | 35 | ### Other social networks: 36 | Instagram: @smartnestcz 37 | Facebook: @smartnestcz 38 | twitter: @smartnestcz 39 | tiktok: @smartnest 40 | linked-in: @andres-sosa 41 | 42 | -------------------------------------------------------------------------------- /Tutorials/Raspberry-Blink/app.js: -------------------------------------------------------------------------------- 1 | var Gpio = require('onoff').Gpio; //require onoff to control GPIO 2 | var mqtt = require("mqtt"); 3 | 4 | var options = { 5 | clientId: "Your-client-id", //Client Id of your device from smarnest 6 | username: "Your-username", //Username from smarnest 7 | password: "Your-password", //Password or You can also use the API key. 8 | port: 1883, //Port of the broker 9 | clean: true 10 | }; 11 | var client = mqtt.connect("mqtt://smartnest.cz", options); 12 | 13 | var LEDPin = new Gpio(4, 'out'); //declare GPIO4 an output 14 | 15 | client.on("connect", function () { //Connect 16 | console.log("Connected To Smartnest"); 17 | subscribeToDevice(); 18 | }); 19 | 20 | client.on("error", function (error) { //Handle Errors 21 | console.log("Can't connect. error:" + error); 22 | console.log("Connection not allowed by broker, possible reasons:"); 23 | console.log("- Device is already online. Wait some seconds until it appears offline for the broker"); 24 | console.log("- Wrong Username or password. Check credentials"); 25 | console.log("- Client Id does not belong to this username, verify ClientId"); 26 | process.exit(1); 27 | }); 28 | 29 | client.on("message", function (topic, message, packet) { //Handle new messages 30 | 31 | console.log("Message received. Topic:", topic, "Message:", message.toString()); 32 | if (topic.split("/")[1] == "directive") { 33 | if (topic.split("/")[2] == "powerState") { 34 | if (message.toString() == "ON") { 35 | console.log("Turning pin ON") 36 | LEDPin.writeSync(1); 37 | client.publish(options.clientId + "/report/powerState", "ON"); 38 | } else { 39 | console.log("Turning pin OFF") 40 | LEDPin.writeSync(0); 41 | client.publish(options.clientId + "/report/powerState", "OFF"); 42 | } 43 | 44 | } 45 | } 46 | 47 | }); 48 | 49 | function subscribeToDevice() { //Subscribe 50 | client.subscribe(options.clientId + "/#", { qos: 1 }); 51 | console.log("Subscribed to Device topic", options.clientId + "/#"); 52 | } -------------------------------------------------------------------------------- /Tutorials/Raspberry-Blink/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "raspberry-blink", 3 | "version": "1.0.0", 4 | "description": "connects raspberry to Smartnest", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node app.js" 9 | }, 10 | "author": "Andres Sosa", 11 | "license": "ISC", 12 | "dependencies": { 13 | "mqtt": "^4.0.0", 14 | "onoff": "^6.0.0" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Tutorials/Raspberry-Doorbell/README.md: -------------------------------------------------------------------------------- 1 | ![Smartnest Logo](https://www.smartnest.cz/img/Logo-vector-login.png) 2 | # Blink Tutorial 3 | 4 | ## Connect your own devices to Alexa, Google Home, Siri IFTTT and more 5 | 6 | Learn how you can easily use your Raspberry as a doorbell and receive alerts on your echo devices 7 | 8 | - Project video: 9 | English: 10 | Spanish: 11 | 12 | - Free web service: 13 | https://www.smartnest.cz 14 | 15 | - Example code: 16 | https://github.com/aososam/Smartnest/blob/master/Tutorials/Raspberry-Doorbell 17 | 18 | 19 | ### Components: 20 | Rapsberry 21 | LED 22 | 222 ohm Resistor 23 | Amazon Alexa device 24 | 25 | ## Instructions: 26 | After downloading the repository navigate to the project folder and run the following command 27 | `npm install` 28 | Then change your ClientId, Username and password from smartnest in the file called app.js 29 | Then run the command 30 | `node app.js` 31 | 32 | Configure the doorbell sound and settings in your Alexa App. 33 | 34 | 35 | ### Other social networks: 36 | Instagram: @smartnestcz 37 | Facebook: @smartnestcz 38 | twitter: @smartnestcz 39 | tiktok: @smartnest 40 | linked-in: @andres-sosa 41 | 42 | -------------------------------------------------------------------------------- /Tutorials/Raspberry-Doorbell/app.js: -------------------------------------------------------------------------------- 1 | var Gpio = require('onoff').Gpio; //require onoff to control GPIO 2 | var mqtt = require("mqtt"); 3 | 4 | var options = { 5 | clientId: "Your-client-id", //Client Id of your device from smarnest 6 | username: "Your-username", //Username from smarnest 7 | password: "Your-password", //Password or You can also use the API key. 8 | port: 1883, //Port of the broker 9 | clean: true 10 | }; 11 | var client = mqtt.connect("mqtt://smartnest.cz", options); 12 | var connected = false; 13 | 14 | const buttonPin = new Gpio(4, 'in', 'both'); 15 | const ledPin = new Gpio(17, 'out'); 16 | buttonPin.watch((err, value) => buttonStateChanged(value)); 17 | 18 | client.on("connect", function () { //Connect 19 | console.log("Connected To Smartnest"); 20 | connected = true; 21 | }); 22 | 23 | client.on("error", function (error) { //Handle Errors 24 | console.log("Can't connect. error:" + error); 25 | console.log("Connection not allowed by broker, possible reasons:"); 26 | console.log("- Device is already online. Wait some seconds until it appears offline for the broker"); 27 | console.log("- Wrong Username or password. Check credentials"); 28 | console.log("- Client Id does not belong to this username, verify ClientId"); 29 | process.exit(1); 30 | }); 31 | 32 | function buttonStateChanged(value) { 33 | ledPin.writeSync(value); 34 | if (connected && value === 1) { 35 | client.publish(options.clientId + "/report/detectionState", "true"); 36 | } 37 | } -------------------------------------------------------------------------------- /Tutorials/Raspberry-Doorbell/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "raspberry-smartnest", 3 | "version": "1.0.0", 4 | "description": "connects raspberry to Smartnest.cz", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node app.js" 9 | }, 10 | "author": "Andres Sosa", 11 | "license": "MIT", 12 | "dependencies": { 13 | "mqtt": "^4.0.0", 14 | "onoff": "^6.0.0" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Tutorials/Raspberry-DoorbellLock/README.md: -------------------------------------------------------------------------------- 1 | ![Smartnest Logo](https://www.smartnest.cz/img/Logo-vector-login.png) 2 | # Blink Tutorial 3 | 4 | ## Connect your own devices to Alexa, Google Home, Siri IFTTT and more 5 | 6 | Learn how you can easily use your Raspberry as a doorbell and receive alerts on your echo devices, and connect also a Lock so you can allow acces to the door using your voice 7 | 8 | - Project video: 9 | English: 10 | Spanish: 11 | 12 | - Free web service: 13 | https://www.smartnest.cz 14 | 15 | - Example code: 16 | https://github.com/aososam/Smartnest/blob/master/Tutorials/Raspberry-DoorbellLock 17 | 18 | 19 | ### Components: 20 | Rapsberry 21 | Amazon Alexa device 22 | Electronic lock 23 | 24 | ## Instructions: 25 | After downloading the repository navigate to the project folder and run the following command 26 | `npm install` 27 | Then change your ClientId, Username and password from smartnest in the file called app.js 28 | Then run the command 29 | `node app.js` 30 | 31 | Configure the doorbell sound and settings in your Alexa App. 32 | Configure the door settings in your Alexa App. 33 | 34 | 35 | ### Other social networks: 36 | Instagram: @smartnestcz 37 | Facebook: @smartnestcz 38 | twitter: @smartnestcz 39 | tiktok: @smartnest 40 | linked-in: @andres-sosa 41 | 42 | -------------------------------------------------------------------------------- /Tutorials/Raspberry-DoorbellLock/app.js: -------------------------------------------------------------------------------- 1 | var Gpio = require('onoff').Gpio; //require onoff to control GPIO 2 | var mqtt = require("mqtt"); 3 | 4 | let clientIdDoorbell = "Your-Doorbell-Client-ID"; 5 | let clientIdLock = "Your-Lock-Client-ID"; 6 | 7 | var options = { 8 | clientId: clientIdDoorbell, //Client Id of your device from smarnest 9 | username: "Your-username", //Username from smarnest 10 | password: "Your-password", //Password or You can also use the API key. 11 | port: 1883, //Port of the broker 12 | clean: true 13 | }; 14 | var options = { 15 | clientId: clientIdDoorbell, //Client Id of your device from smarnest 16 | username: "Your-username", //Username from smarnest 17 | password: "Your-password", //Password or You can also use the API key. 18 | port: 1883, //Port of the broker 19 | clean: true 20 | }; 21 | var client = mqtt.connect("mqtt://smartnest.cz", options); 22 | var connected = false; 23 | 24 | const buttonPin = new Gpio(4, 'in', 'both'); 25 | const lockPin = new Gpio(17, 'out'); 26 | buttonPin.watch((err, value) => buttonStateChanged(value)); 27 | 28 | client.on("connect", function () { //Connect 29 | console.log("Connected To Smartnest"); 30 | connected = true; 31 | 32 | client.subscribe(clientIdLock + "/#", { qos: 1 }); 33 | client.publish(clientIdLock + "/report/online", "true"); 34 | 35 | }); 36 | 37 | client.on("error", function (error) { //Handle Errors 38 | console.log("Can't connect. error:" + error); 39 | console.log("Connection not allowed by broker, possible reasons:"); 40 | console.log("- Device is already online. Wait some seconds until it appears offline for the broker"); 41 | console.log("- Wrong Username or password. Check credentials"); 42 | console.log("- Client Id does not belong to this username, verify ClientId"); 43 | process.exit(1); 44 | }); 45 | 46 | client.on("message", function (topic, message, packet) { //Handle new messages 47 | 48 | console.log("Message received. Topic:", topic, "Message:", message.toString()); 49 | if (topic.split("/")[1] == "directive") { 50 | 51 | if (topic.split("/")[2] == "lockedState") { 52 | 53 | if (message.toString() == "false") { 54 | openDoor(); 55 | } else { 56 | closeDoor(); 57 | } 58 | 59 | } 60 | } 61 | 62 | }); 63 | 64 | function buttonStateChanged(value) { 65 | if (connected && value === 1) { 66 | client.publish(clientIdDoorbell + "/report/detectionState", "true"); 67 | } 68 | } 69 | 70 | function openDoor() { 71 | console.log("Opening Door") 72 | lockPin.writeSync(1); 73 | client.publish(clientIdLock + "/report/lockedState", "false"); 74 | 75 | setTimeout(() => { 76 | lockPin.writeSync(0); 77 | closeDoor(); 78 | }, 5000); 79 | } 80 | 81 | function closeDoor() { 82 | console.log("Close Door") 83 | lockPin.writeSync(0); 84 | client.publish(clientIdLock + "/report/lockedState", "true"); 85 | 86 | } -------------------------------------------------------------------------------- /Tutorials/Raspberry-DoorbellLock/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "raspberry-smartnest", 3 | "version": "1.0.0", 4 | "description": "connects raspberry to Smartnest.cz", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node app.js" 9 | }, 10 | "author": "Andres Sosa", 11 | "license": "MIT", 12 | "dependencies": { 13 | "mqtt": "^4.0.0", 14 | "onoff": "^6.0.0" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Tutorials/Raspberry-MultipleDevices/README.md: -------------------------------------------------------------------------------- 1 | ![Smartnest Logo](https://www.smartnest.cz/img/Logo-vector-login.png) 2 | # Blink Tutorial 3 | 4 | ## Connect your own devices to Alexa, Google Home, Siri IFTTT and more 5 | 6 | Learn how you can easily connect many devices using the same Raspberry and control them using any voice assistant easily and for free. 7 | 8 | - Project video: 9 | English: 10 | Spanish: 11 | 12 | - Free web service: 13 | https://www.smartnest.cz 14 | 15 | - Example code: 16 | https://github.com/aososam/Smartnest/blob/master/Tutorials/Raspberry-MultipleDevices 17 | 18 | 19 | ### Components: 20 | Rapsberry 21 | LED x 3 22 | 222 ohm Resistors x 3 23 | 24 | ## Instructions: 25 | After downloading the repository navigate to the project folder and run the following command 26 | `npm install` 27 | Then change your ClientId, Username and password from smartnest in the file called app.js 28 | Then run the command 29 | `node app.js` 30 | 31 | Thats all! now you are able to discover your devices and controll them using Alexa, Google, IFTTT and much more!. 32 | 33 | 34 | ### Other social networks: 35 | Instagram: @smartnestcz 36 | Facebook: @smartnestcz 37 | twitter: @smartnestcz 38 | tiktok: @smartnest 39 | linked-in: @andres-sosa 40 | 41 | -------------------------------------------------------------------------------- /Tutorials/Raspberry-MultipleDevices/app.js: -------------------------------------------------------------------------------- 1 | var Gpio = require('onoff').Gpio; //require onoff to control GPIO 2 | var mqtt = require("mqtt"); 3 | 4 | var deviceId1 = "YOUR-DEVICEID-1"; 5 | var deviceId2 = "YOUR-DEVICEID-2"; 6 | var deviceId3 = "YOUR-DEVICEID-3"; 7 | 8 | var options = { 9 | clientId: deviceId1, //Client Id of your device from smarnest 10 | username: "Your-username", //Username from smarnest 11 | password: "Your-password", //Password or You can also use the API key. 12 | port: 1883, //Port of the broker 13 | clean: true 14 | }; 15 | var client = mqtt.connect("mqtt://smartnest.cz", options); 16 | 17 | var device1Pin = new Gpio(4, 'out'); //declare GPIO4 an output 18 | var device2Pin = new Gpio(17, 'out'); //declare GPIO4 an output 19 | var device3Pin = new Gpio(18, 'out'); //declare GPIO4 an output 20 | 21 | client.on("connect", function () { //Connect 22 | console.log("Connected To Smartnest"); 23 | subscribeToDevice(); 24 | }); 25 | 26 | client.on("error", function (error) { //Handle Errors 27 | console.log("Can't connect. error:" + error); 28 | console.log("Connection not allowed by broker, possible reasons:"); 29 | console.log("- Device is already online. Wait some seconds until it appears offline for the broker"); 30 | console.log("- Wrong Username or password. Check credentials"); 31 | console.log("- Client Id does not belong to this username, verify ClientId"); 32 | process.exit(1); 33 | }); 34 | 35 | client.on("message", function (topic, message, packet) { //Handle new messages 36 | 37 | console.log("Message received. Topic:", topic, "Message:", message.toString()); 38 | if (topic.split("/")[1] == "directive") { 39 | if (topic.split("/")[2] == "powerState") { 40 | let device = topic.split("/")[0]; 41 | 42 | if (message.toString() == "ON") { 43 | if (device == deviceId1) { 44 | console.log("Turning ON device 1") 45 | device1Pin.writeSync(1); 46 | } else if (device == deviceId2) { 47 | console.log("Turning ON device 2") 48 | device2Pin.writeSync(1); 49 | } else if (device == deviceId3) { 50 | console.log("Turning ON device 3") 51 | device3Pin.writeSync(1); 52 | } 53 | client.publish(device + "/report/powerState", "ON"); 54 | } else { 55 | 56 | if (device == deviceId1) { 57 | console.log("Turning OFF device 1") 58 | device1Pin.writeSync(0); 59 | } else if (device == deviceId2) { 60 | console.log("Turning OFF device 2") 61 | device2Pin.writeSync(0); 62 | } else if (device == deviceId3) { 63 | console.log("Turning OFF device 3") 64 | device3Pin.writeSync(0); 65 | } 66 | client.publish(device + "/report/powerState", "OFF"); 67 | } 68 | 69 | } 70 | } 71 | 72 | }); 73 | 74 | function subscribeToDevice() { //Subscribe 75 | client.publish(deviceId1 + "/report/online", "true"); 76 | client.publish(deviceId2 + "/report/online", "true"); 77 | client.publish(deviceId3 + "/report/online", "true"); 78 | 79 | client.subscribe(deviceId1 + "/#", { qos: 1 }); 80 | client.subscribe(deviceId2 + "/#", { qos: 1 }); 81 | client.subscribe(deviceId3 + "/#", { qos: 1 }); 82 | console.log("Subscribed to all Devices"); 83 | } -------------------------------------------------------------------------------- /Tutorials/Raspberry-MultipleDevices/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "raspberry-smartnest", 3 | "version": "1.0.0", 4 | "description": "connects raspberry to Smartnest.cz", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node app.js" 9 | }, 10 | "author": "Andres Sosa", 11 | "license": "MIT", 12 | "dependencies": { 13 | "mqtt": "^4.0.0", 14 | "onoff": "^6.0.0" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Tutorials/Raspberry-switchGroup/app.js: -------------------------------------------------------------------------------- 1 | var Gpio = require('onoff').Gpio; //require onoff to control GPIO 2 | var mqtt = require("mqtt"); 3 | 4 | var options = { 5 | clientId: "YOUR-DEVICE-ID", //Client Id of your device from smarnest 6 | username: "YOUR-USERNAME", //Username from smarnest 7 | password: "YOUR-PASSWORD", //Password or You can also use the API key. 8 | port: 1883, //Port of the broker 9 | clean: true 10 | }; 11 | var client = mqtt.connect("mqtt://smartnest.cz", options); 12 | 13 | const pin1 = new Gpio(4, 'out');; 14 | const pin2 = new Gpio(3, 'out');; 15 | const pin3 = new Gpio(2, 'out');; 16 | 17 | client.on("connect", function () { //Connect 18 | console.log("Connected To Smartnest"); 19 | }); 20 | 21 | client.on("error", function (error) { //Handle Errors 22 | console.log("Can't connect" + error); 23 | process.exit(1); 24 | }); 25 | 26 | client.on("message", function (topic, message, packet) { //Handle new messages 27 | 28 | console.log("Message received. Topic:", topic, "Message:", message.toString()); 29 | if (topic.split("/")[1] == "directive") { 30 | 31 | if (topic.split("/")[2] == "powerState1") { 32 | if (message.toString() == "ON") { 33 | console.log("Turning ON pin 1") 34 | pin1.writeSync(1); 35 | client.publish(options.clientId + "/report/powerState1", "ON"); 36 | } else { 37 | console.log("Turning OFF pin 1") 38 | pin1.writeSync(0); 39 | client.publish(options.clientId + "/report/powerState1", "OFF"); 40 | } 41 | 42 | } else if (topic.split("/")[2] == "powerState2") { 43 | if (message.toString() == "ON") { 44 | console.log("Turning ON pin 2") 45 | pin2.writeSync(1); 46 | client.publish(options.clientId + "/report/powerState2", "ON"); 47 | } else { 48 | console.log("Turning OFF pin 2") 49 | pin2.writeSync(0); 50 | client.publish(options.clientId + "/report/powerState2", "OFF"); 51 | } 52 | 53 | } else if (topic.split("/")[2] == "powerState3") { 54 | if (message.toString() == "ON") { 55 | console.log("Turning ON pin 3") 56 | pin3.writeSync(1); 57 | client.publish(options.clientId + "/report/powerState3", "ON"); 58 | } else { 59 | console.log("Turning OFF pin 3") 60 | pin3.writeSync(0); 61 | client.publish(options.clientId + "/report/powerState3", "OFF"); 62 | } 63 | 64 | } 65 | } 66 | 67 | }); -------------------------------------------------------------------------------- /Tutorials/Raspberry-switchGroup/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "raspberry-smartnest", 3 | "version": "1.0.0", 4 | "description": "connects raspberry to Smartnest.cz", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node app.js" 9 | }, 10 | "author": "Andres Sosa", 11 | "license": "MIT", 12 | "dependencies": { 13 | "mqtt": "^4.0.0", 14 | "onoff": "^6.0.0" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Tutorials/Relay-Module/README.md: -------------------------------------------------------------------------------- 1 | ![Smartnest Logo](https://www.smartnest.cz/img/Logo-vector-login.png) 2 | # Relay Project 3 | 4 | ## Use more than one device ID in the same board 5 | 6 | Learn how to use more than one device Id in the same board to control many devices connected to a 2 or three channel relay module. 7 | 8 | - Free web service: 9 | https://www.smartnest.cz 10 | 11 | - Example code: 12 | https://github.com/aososam/Smartnest/blob/master/Tutorials/Relay-Module/Relay-Module.ino 13 | 14 | - PubSubClient MQTT library: 15 | https://github.com/knolleary/pubsubclient 16 | 17 | ### Other social networks: 18 | Instagram: @smartnestcz 19 | Facebook: @smartnestcz 20 | twitter: @smartnestcz 21 | tiktok: @smartnest 22 | linked-in: @andres-sosa 23 | 24 | -------------------------------------------------------------------------------- /Tutorials/Relay-Module/Relay-Module.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Tutorial-RelayModule" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | 17 | int switchPin1 = 0; 18 | int switchPin2 = 3; 19 | int switchPin3 = 5; 20 | int switchPin4 = 4; 21 | 22 | void startWifi(); 23 | void startMqtt(); 24 | void checkMqtt(); 25 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 26 | void callback(char* topic, byte* payload, unsigned int length); 27 | void sendToBroker(char* topic, char* message); 28 | 29 | void turnOff(int pin); 30 | void turnOn(int pin); 31 | 32 | void setup() { 33 | pinMode(switchPin1, OUTPUT); 34 | pinMode(switchPin2, OUTPUT); 35 | pinMode(switchPin3, OUTPUT); 36 | pinMode(switchPin4, OUTPUT); 37 | Serial.begin(115200); 38 | startWifi(); 39 | startMqtt(); 40 | } 41 | 42 | void loop() { 43 | client.loop(); 44 | checkMqtt(); 45 | } 46 | 47 | void callback(char* topic, byte* payload, unsigned int length) { //A new message has been received 48 | Serial.print("Topic:"); 49 | Serial.println(topic); 50 | int tokensNumber = 10; 51 | char* tokens[tokensNumber]; 52 | char message[length + 1]; 53 | splitTopic(topic, tokens, tokensNumber); 54 | sprintf(message, "%c", (char)payload[0]); 55 | for (int i = 1; i < length; i++) { 56 | sprintf(message, "%s%c", message, (char)payload[i]); 57 | } 58 | Serial.print("Message:"); 59 | Serial.println(message); 60 | 61 | //------------------ACTIONS HERE--------------------------------- 62 | 63 | if (strcmp(tokens[1], "directive") == 0) { 64 | if (strcmp(tokens[2], "powerState1") == 0) { 65 | if (strcmp(message, "ON") == 0) { 66 | turnOn(1); 67 | } else if (strcmp(message, "OFF") == 0) { 68 | turnOff(1); 69 | } 70 | } else if (strcmp(tokens[2], "powerState2") == 0) { 71 | if (strcmp(message, "ON") == 0) { 72 | turnOn(2); 73 | } else if (strcmp(message, "OFF") == 0) { 74 | turnOff(2); 75 | } 76 | } else if (strcmp(tokens[2], "powerState3") == 0) { 77 | if (strcmp(message, "ON") == 0) { 78 | turnOn(3); 79 | } else if (strcmp(message, "OFF") == 0) { 80 | turnOff(3); 81 | } 82 | } else if (strcmp(tokens[2], "powerState4") == 0) { 83 | if (strcmp(message, "ON") == 0) { 84 | turnOn(4); 85 | } else if (strcmp(message, "OFF") == 0) { 86 | turnOff(4); 87 | } 88 | } 89 | } 90 | } 91 | 92 | void startWifi() { 93 | WiFi.mode(WIFI_STA); 94 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 95 | Serial.println("Connecting ..."); 96 | int attempts = 0; 97 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 98 | attempts++; 99 | delay(500); 100 | Serial.print("."); 101 | } 102 | 103 | if (WiFi.status() == WL_CONNECTED) { 104 | Serial.println('\n'); 105 | Serial.print("Connected to "); 106 | Serial.println(WiFi.SSID()); 107 | Serial.print("IP address:\t"); 108 | Serial.println(WiFi.localIP()); 109 | 110 | } else { 111 | Serial.println('\n'); 112 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 113 | } 114 | 115 | delay(500); 116 | } 117 | 118 | void startMqtt() { 119 | client.setServer(MQTT_BROKER, MQTT_PORT); 120 | client.setCallback(callback); 121 | 122 | while (!client.connected()) { 123 | Serial.println("Connecting to MQTT..."); 124 | 125 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 126 | Serial.println("connected"); 127 | } else { 128 | if (client.state() == 5) { 129 | Serial.println("Connection not allowed by broker, possible reasons:"); 130 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 131 | Serial.println("- Wrong Username or password. Check credentials"); 132 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 133 | 134 | } else { 135 | Serial.println("Not possible to connect to Broker Error code:"); 136 | Serial.print(client.state()); 137 | } 138 | 139 | delay(0x7530); 140 | } 141 | } 142 | 143 | char subscibeTopic[100]; 144 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 145 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 146 | 147 | sendToBroker("report/online", "true"); // Reports that the device is online 148 | delay(100); 149 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 150 | delay(100); 151 | sendToBroker("report/ip", (char*)WiFi.localIP().toString().c_str()); // Reports the ip 152 | delay(100); 153 | sendToBroker("report/network", (char*)WiFi.SSID().c_str()); // Reports the network name 154 | delay(100); 155 | 156 | char signal[5]; 157 | sprintf(signal, "%d", WiFi.RSSI()); 158 | sendToBroker("report/signal", signal); // Reports the signal strength 159 | delay(100); 160 | } 161 | 162 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 163 | const char s[2] = "/"; 164 | int pos = 0; 165 | tokens[0] = strtok(topic, s); 166 | 167 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 168 | pos++; 169 | tokens[pos] = strtok(NULL, s); 170 | } 171 | 172 | return pos; 173 | } 174 | 175 | void checkMqtt() { 176 | if (!client.connected()) { 177 | startMqtt(); 178 | } 179 | } 180 | 181 | void sendToBroker(char* topic, char* message) { 182 | if (client.connected()) { 183 | char topicArr[100]; 184 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 185 | client.publish(topicArr, message); 186 | } 187 | } 188 | 189 | void turnOff(int pin) { 190 | Serial.printf("Turning off pin %d...\n", pin); 191 | switch (pin) { 192 | case 1: 193 | digitalWrite(switchPin1, LOW); 194 | sendToBroker("report/powerState1", "OFF"); 195 | break; 196 | case 2: 197 | digitalWrite(switchPin2, LOW); 198 | sendToBroker("report/powerState2", "OFF"); 199 | break; 200 | case 3: 201 | digitalWrite(switchPin3, LOW); 202 | sendToBroker("report/powerState3", "OFF"); 203 | break; 204 | case 4: 205 | digitalWrite(switchPin4, LOW); 206 | sendToBroker("report/powerState4", "OFF"); 207 | break; 208 | } 209 | } 210 | 211 | void turnOn(int pin) { 212 | Serial.printf("Turning on pin %d...\n", pin); 213 | switch (pin) { 214 | case 1: 215 | digitalWrite(switchPin1, HIGH); 216 | sendToBroker("report/powerState1", "ON"); 217 | break; 218 | case 2: 219 | digitalWrite(switchPin2, HIGH); 220 | sendToBroker("report/powerState2", "ON"); 221 | break; 222 | case 3: 223 | digitalWrite(switchPin3, HIGH); 224 | sendToBroker("report/powerState3", "ON"); 225 | break; 226 | case 4: 227 | digitalWrite(switchPin4, HIGH); 228 | sendToBroker("report/powerState4", "ON"); 229 | break; 230 | } 231 | } 232 | -------------------------------------------------------------------------------- /Tutorials/Smart-Button/3DButton.stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Smartnestcz/examples/1f919ef896fbf9d8958966da19f0c966fa8e86e0/Tutorials/Smart-Button/3DButton.stl -------------------------------------------------------------------------------- /Tutorials/Smart-Button/README.md: -------------------------------------------------------------------------------- 1 | ![Smartnest Logo](https://www.smartnest.cz/img/Logo-vector-login.png) 2 | # Smart button project 3 | 4 | ## Build your own smart button to control anything 5 | 6 | Learn how to build your own smart button to control any device or a group of devices, you will be able to start any routine, play music, get the weather forecast or get the news. 7 | 8 | - Project video: 9 | English: https://youtu.be/FR4HdwRXhiI 10 | Spanish: https://youtu.be/G7FHqimYNS8 11 | 12 | - Free web service: 13 | https://www.smartnest.cz 14 | 15 | - Example code: 16 | https://github.com/aososam/Smartnest/blob/master/Tutorials/Smart-Button/Smart-Button.ino 17 | 18 | - Stl file of the button for 3D printing: 19 | https://github.com/aososam/Smartnest/blob/master/Tutorials/Smart-Button/3DButton.stl 20 | 21 | - PubSubClient MQTT library: 22 | https://github.com/knolleary/pubsubclient 23 | 24 | 25 | 26 | ### Components: 27 | Node Mcu: https://www.aliexpress.com/item/33040293736.html?spm=a2g0o.productlist.0.0.567954c1JMk2lg 28 | battery 3.7V: https://www.aliexpress.com/item/32887423130.html 29 | Small breadboard 30 | Resistors 1K ohm 31 | box 32 | button 33 | 34 | 35 | ### Other social networks: 36 | Instagram: @smartnestcz 37 | Facebook: @smartnestcz 38 | twitter: @smartnestcz 39 | tiktok: @smartnest 40 | linked-in: @andres-sosa 41 | 42 | -------------------------------------------------------------------------------- /Tutorials/Smart-Button/Smart-Button.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Tutorial-SmartButton" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | 17 | void startWifi(); 18 | void startMqtt(); 19 | void checkMqtt(); 20 | int splitTopic(char* topic, char* tokens[], int tokensNumber); 21 | void callback(char* topic, byte* payload, unsigned int length); 22 | 23 | void setup() { 24 | Serial.println("Starting..."); 25 | Serial.begin(115200); 26 | startWifi(); // Tries to connect to wifi 10 attempts 27 | 28 | if (WiFi.status() == WL_CONNECTED) { // If it is connected to wifi connect to MQTT broker 29 | 30 | startMqtt(); 31 | char report[100]; 32 | 33 | sprintf(report, "%s/report/online", MQTT_CLIENT); // Reports that the device is online 34 | client.publish(report, "true"); 35 | delay(200); 36 | 37 | sprintf(report, "%s/report/detectionState", MQTT_CLIENT); // Reports that the button press was detected (closed and opened) Make sure to select open in the trigger from Alexa 38 | client.publish(report, "true"); 39 | delay(200); 40 | client.publish(report, "false"); 41 | delay(200); 42 | 43 | sprintf(report, "%s/report/online", MQTT_CLIENT); // Reports that the device is offline 44 | client.publish(report, "false"); 45 | delay(200); 46 | 47 | Serial.println("Report sent to broker, now going back to sleep."); 48 | 49 | } else { 50 | Serial.println("Connection to wifi was unsuccessful. going back to sleep"); 51 | } 52 | 53 | ESP.deepSleep(0); 54 | } 55 | 56 | void loop() { 57 | // Empty Loop 58 | } 59 | 60 | void callback(char* topic, byte* payload, unsigned int length) { // This function runs when there is a new message in the subscribed topic 61 | Serial.print("Topic:"); 62 | Serial.println(topic); 63 | 64 | // Splits the topic and gets the message 65 | int tokensNumber = 10; 66 | char* tokens[tokensNumber]; 67 | char message[length + 1]; 68 | splitTopic(topic, tokens, tokensNumber); 69 | sprintf(message, "%c", (char)payload[0]); 70 | 71 | for (int i = 1; i < length; i++) { 72 | sprintf(message, "%s%c", message, (char)payload[i]); 73 | } 74 | 75 | Serial.print("Message:"); 76 | Serial.println(message); 77 | } 78 | 79 | void startWifi() { 80 | WiFi.mode(WIFI_STA); 81 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 82 | Serial.println("Connecting ..."); 83 | int attempts = 0; 84 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 85 | attempts++; 86 | delay(500); 87 | Serial.print("."); 88 | } 89 | 90 | if (WiFi.status() == WL_CONNECTED) { 91 | Serial.println('\n'); 92 | Serial.print("Connected to "); 93 | Serial.println(WiFi.SSID()); 94 | Serial.print("IP address:\t"); 95 | Serial.println(WiFi.localIP()); 96 | 97 | } else { 98 | Serial.println('\n'); 99 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 100 | } 101 | 102 | delay(500); 103 | } 104 | 105 | void startMqtt() { 106 | client.setServer(MQTT_BROKER, MQTT_PORT); 107 | client.setCallback(callback); 108 | 109 | while (!client.connected()) { 110 | Serial.println("Connecting to MQTT..."); 111 | 112 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 113 | Serial.println("connected"); 114 | } else { 115 | if (client.state() == 5) { 116 | Serial.println("Connection not allowed by broker, possible reasons:"); 117 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 118 | Serial.println("- Wrong Username or password. Check credentials"); 119 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 120 | 121 | } else { 122 | Serial.println("Not possible to connect to Broker Error code:"); 123 | Serial.print(client.state()); 124 | } 125 | 126 | delay(0x7530); 127 | } 128 | } 129 | 130 | char subscribeTopic[100]; 131 | sprintf(subscribeTopic, "%s/#", MQTT_CLIENT); 132 | client.subscribe(subscribeTopic); 133 | } 134 | 135 | int splitTopic(char* topic, char* tokens[], int tokensNumber) { 136 | const char s[2] = "/"; 137 | int pos = 0; 138 | tokens[0] = strtok(topic, s); 139 | 140 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 141 | pos++; 142 | tokens[pos] = strtok(NULL, s); 143 | } 144 | 145 | return pos; 146 | } 147 | 148 | void checkMqtt() { 149 | if (!client.connected()) { 150 | startMqtt(); 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /Tutorials/Tower-Fan/README.md: -------------------------------------------------------------------------------- 1 | ![Smartnest Logo](https://www.smartnest.cz/img/Logo-vector-login.png) 2 | 3 | # Tower Fan Project 4 | 5 | ## Connect your tower fan to Alexa 6 | 7 | Learn how to connect and control your tower fan with Alexa, you will be able to turn on and off the fan with your voice. 8 | 9 | - Video tutorials in English: 10 | 11 | 1. ALexa: https://youtu.be/UWkzOEL-NCk 12 | 2. Google Assistant: https://youtu.be/HxelUkG_ko0 13 | 14 | - Video tutorial in Spanish: 15 | 16 | 1. Alexa: https://youtu.be/BaqT_xJNxd0 17 | 2. Google Assistant: https://youtu.be/ywfkB9gprTU 18 | 19 | - Free web service: 20 | https://www.smartnest.cz 21 | 22 | - Example code: 23 | https://github.com/aososam/Smartnest/blob/master/Tutorials/Tower-Fan/Tower-Fan.ino 24 | 25 | - PubSubClient MQTT library: 26 | https://github.com/knolleary/pubsubclient 27 | 28 | ### Other social networks: 29 | 30 | Instagram: @smartnestcz 31 | Facebook: @smartnestcz 32 | twitter: @smartnestcz 33 | tiktok: @smartnest 34 | linked-in: @andres-sosa 35 | -------------------------------------------------------------------------------- /Tutorials/Tower-Fan/Tower-Fan.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client 3 | #include 4 | 5 | #define SSID_NAME "Wifi-name" // Your Wifi Network name 6 | #define SSID_PASSWORD "Wifi-password" // Your Wifi network password 7 | #define MQTT_BROKER "smartnest.cz" // Broker host 8 | #define MQTT_PORT 1883 // Broker port 9 | #define MQTT_USERNAME "username" // Username from Smartnest 10 | #define MQTT_PASSWORD "password" // Password from Smartnest (or API key) 11 | #define MQTT_CLIENT "device-Id" // Device Id from smartnest 12 | #define FIRMWARE_VERSION "Tutorial-Fan" // Custom name for this program 13 | 14 | WiFiClient espClient; 15 | PubSubClient client(espClient); 16 | int switchPin = 0; 17 | 18 | void startWifi(); 19 | void startMqtt(); 20 | void checkMqtt(); 21 | int splitTopic(char *topic, char *tokens[], int tokensNumber); 22 | void callback(char *topic, byte *payload, unsigned int length); 23 | void sendToBroker(char *topic, char *message); 24 | 25 | void setup() { 26 | pinMode(switchPin, OUTPUT); 27 | Serial.begin(115200); 28 | startWifi(); 29 | startMqtt(); 30 | } 31 | 32 | void loop() { 33 | client.loop(); 34 | checkMqtt(); 35 | } 36 | 37 | void callback(char *topic, byte *payload, unsigned int length) { //Callback for new messages 38 | Serial.print("Topic:"); 39 | Serial.println(topic); 40 | int tokensNumber = 10; 41 | char *tokens[tokensNumber]; 42 | char message[length + 1]; 43 | splitTopic(topic, tokens, tokensNumber); 44 | sprintf(message, "%c", (char)payload[0]); 45 | for (int i = 1; i < length; i++) { 46 | sprintf(message, "%s%c", message, (char)payload[i]); 47 | } 48 | Serial.print("Message:"); 49 | Serial.println(message); 50 | 51 | //------------------ACTIONS HERE--------------------------------- 52 | 53 | if (strcmp(tokens[1], "directive") == 0 && strcmp(tokens[2], "powerState") == 0) { 54 | if (strcmp(message, "ON") == 0) { 55 | digitalWrite(switchPin, HIGH); 56 | delay(400); 57 | digitalWrite(switchPin, LOW); 58 | sendToBroker("report/powerState", "ON") 59 | 60 | } else if (strcmp(message, "OFF") == 0) { 61 | digitalWrite(switchPin, HIGH); 62 | delay(400); 63 | digitalWrite(switchPin, LOW); 64 | sendToBroker("report/powerState", "OFF") 65 | } 66 | } 67 | } 68 | 69 | void startWifi() { 70 | WiFi.mode(WIFI_STA); 71 | WiFi.begin(SSID_NAME, SSID_PASSWORD); 72 | Serial.println("Connecting ..."); 73 | int attempts = 0; 74 | while (WiFi.status() != WL_CONNECTED && attempts < 10) { 75 | attempts++; 76 | delay(500); 77 | Serial.print("."); 78 | } 79 | 80 | if (WiFi.status() == WL_CONNECTED) { 81 | Serial.println('\n'); 82 | Serial.print("Connected to "); 83 | Serial.println(WiFi.SSID()); 84 | Serial.print("IP address:\t"); 85 | Serial.println(WiFi.localIP()); 86 | } else { 87 | Serial.println('\n'); 88 | Serial.println('I could not connect to the wifi network after 10 attempts \n'); 89 | } 90 | 91 | delay(500); 92 | } 93 | 94 | void startMqtt() { 95 | client.setServer(MQTT_BROKER, MQTT_PORT); 96 | client.setCallback(callback); 97 | 98 | while (!client.connected()) { 99 | Serial.println("Connecting to MQTT..."); 100 | 101 | if (client.connect(MQTT_CLIENT, MQTT_USERNAME, MQTT_PASSWORD)) { 102 | Serial.println("connected"); 103 | } else { 104 | if (client.state() == 5) { 105 | Serial.println("Connection not allowed by broker, possible reasons:"); 106 | Serial.println("- Device is already online. Wait some seconds until it appears offline for the broker"); 107 | Serial.println("- Wrong Username or password. Check credentials"); 108 | Serial.println("- Client Id does not belong to this username, verify ClientId"); 109 | } else { 110 | Serial.println("Not possible to connect to Broker Error code:"); 111 | Serial.print(client.state()); 112 | } 113 | 114 | delay(0x7530); 115 | } 116 | } 117 | 118 | char subscibeTopic[100]; 119 | sprintf(subscibeTopic, "%s/#", MQTT_CLIENT); 120 | client.subscribe(subscibeTopic); //Subscribes to all messages send to the device 121 | 122 | sendToBroker("report/online", "true"); // Reports that the device is online 123 | delay(100); 124 | sendToBroker("report/firmware", FIRMWARE_VERSION); // Reports the firmware version 125 | delay(100); 126 | sendToBroker("report/ip", (char *)WiFi.localIP().toString().c_str()); // Reports the ip 127 | delay(100); 128 | sendToBroker("report/network", (char *)WiFi.SSID().c_str()); // Reports the network name 129 | delay(100); 130 | 131 | char signal[5]; 132 | sprintf(signal, "%d", WiFi.RSSI()); 133 | sendToBroker("report/signal", signal); // Reports the signal strength 134 | delay(100); 135 | } 136 | 137 | int splitTopic(char *topic, char *tokens[], int tokensNumber) { 138 | const char s[2] = "/"; 139 | int pos = 0; 140 | tokens[0] = strtok(topic, s); 141 | 142 | while (pos < tokensNumber - 1 && tokens[pos] != NULL) { 143 | pos++; 144 | tokens[pos] = strtok(NULL, s); 145 | } 146 | 147 | return pos; 148 | } 149 | 150 | void checkMqtt() { 151 | if (!client.connected()) { 152 | startMqtt(); 153 | } 154 | } 155 | 156 | void sendToBroker(char *topic, char *message) { 157 | if (client.connected()) { 158 | char topicArr[100]; 159 | sprintf(topicArr, "%s/%s", MQTT_CLIENT, topic); 160 | client.publish(topicArr, message); 161 | } 162 | } --------------------------------------------------------------------------------