This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot:examples:relay [2017/12/18 11:08] – New pictures tomykalm | 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 module must be connected to controller module or with sensor module. | ||
Line 11: | Line 11: | ||
{{: | {{: | ||
- | The example code above shows if something is connected to the relay module | + | Needed libraries: |
- | < | + | <code> |
+ | 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 < | ||
#include < | #include < | ||
- | #define RELAY_PIN 5 | + | #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 | // If message received switch relay | ||
Line 26: | Line 33: | ||
Serial.print(" | Serial.print(" | ||
Serial.println(msg); | Serial.println(msg); | ||
- | if(msg == " | ||
- | { | ||
- | digitalWrite(RELAY_PIN, | ||
- | } | ||
- | if(msg == " | + | if(topic == MODULE_TOPIC) |
{ | { | ||
- | digitalWrite(RELAY_PIN, | + | |
+ | if(msg == " | ||
+ | { | ||
+ | digitalWrite(RELAY_PIN, | ||
+ | } | ||
+ | // If message received and it is 0, then switch relay on | ||
+ | if(msg == " | ||
+ | { | ||
+ | | ||
+ | } | ||
} | } | ||
} | } | ||
+ | // Function started after the connection to the server is established. | ||
void iot_connected() | void iot_connected() | ||
{ | { | ||
Serial.println(" | Serial.println(" | ||
- | iot.subscribe(" | + | iot.subscribe(MODULE_TOPIC); // subscribe to topic relay |
iot.log(" | iot.log(" | ||
} | } | ||
Line 46: | Line 59: | ||
void setup() | void setup() | ||
{ | { | ||
- | Serial.begin(115200); | + | Serial.begin(115200); |
Serial.println(" | Serial.println(" | ||
+ | | ||
+ | // | ||
+ | // | ||
+ | iot.printConfig(); | ||
+ | iot.setup(); | ||
- | | + | pinMode(RELAY_PIN, |
- | iot.setup(); | + | |
- | | + | |
} | } | ||
void loop() | void loop() | ||
{ | { | ||
- | iot.handle(); | + | iot.handle(); |
- | delay(200); | + | delay(200); |
} | } | ||
- | |||
</ | </ |