This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot:examples:relay [2017/09/29 13:41] – Somepub | en:iot:examples:relay [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== | ||
| + | Relay module must be connected to controller module or with sensor module. | ||
| + | |||
| + | {{: | ||
| + | {{: | ||
| + | |||
| + | |||
| + | Relay has 3 input holes. | ||
| + | |||
| + | {{: | ||
| + | |||
| + | Needed libraries: | ||
| + | < | ||
| + | |||
| + | The example code below shows if something is connected to the relay module | ||
| + | <code c> | ||
| + | // Includes global variables and librarys that the relay shield uses | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define MODULE_TOPIC " | ||
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| + | |||
| + | #define RELAY_PIN 5 // The relay has been connected to pin 5 (D1) | ||
| + | |||
| + | // If message received switch relay | ||
| + | void iot_received(String topic, String msg) | ||
| + | { | ||
| + | Serial.print(" | ||
| + | Serial.print(topic); | ||
| + | Serial.print(" | ||
| + | Serial.println(msg); | ||
| + | |||
| + | if(topic == MODULE_TOPIC) | ||
| + | { | ||
| + | // If message received and it is 1, then switch relay on | ||
| + | if(msg == " | ||
| + | { | ||
| + | digitalWrite(RELAY_PIN, | ||
| + | } | ||
| + | // If message received and it is 0, then switch relay on | ||
| + | if(msg == " | ||
| + | { | ||
| + | digitalWrite(RELAY_PIN, | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Function started after the connection to the server is established. | ||
| + | void iot_connected() | ||
| + | { | ||
| + | Serial.println(" | ||
| + | iot.subscribe(MODULE_TOPIC); | ||
| + | iot.log(" | ||
| + | } | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | Serial.begin(115200); | ||
| + | Serial.println(" | ||
| + | | ||
| + | // | ||
| + | // | ||
| + | iot.printConfig(); | ||
| + | iot.setup(); | ||
| + | |||
| + | pinMode(RELAY_PIN, | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | iot.handle(); | ||
| + | delay(200); // Wait for 0.2 seconds | ||
| + | } | ||
| + | |||
| + | </ | ||