Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot:examples:relay [2017/12/18 11:08] – New pictures tomykalmen:iot:examples:relay [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 1: Line 1:
-======  Relay ======+======  Relay example======
  
 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:
 {{:en:iot:examples:relaypicture3.jpg?200|}} {{:en:iot:examples:relaypicture3.jpg?200|}}
  
-The example code above shows if something is connected to the relay module +Needed libraries: 
-<code c>+<code>lib_deps = ITTIoT</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 <Arduino.h> #include <Arduino.h>
 #include <ittiot.h> #include <ittiot.h>
  
-#define RELAY_PIN 5+#define MODULE_TOPIC "ESP30/relay" 
 +#define WIFI_NAME "name" 
 +#define WIFI_PASSWORD "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(" payload: ");   Serial.print(" payload: ");
   Serial.println(msg);   Serial.println(msg);
-  if(msg == "1") 
-  { 
-    digitalWrite(RELAY_PIN, HIGH); 
-  } 
  
-  if(msg == "0")+  if(topic == MODULE_TOPIC)
   {   {
-    digitalWrite(RELAY_PIN, LOW);+    // If message received and it is 1, then switch relay on 
 +    if(msg == "1"
 +    { 
 +      digitalWrite(RELAY_PIN, HIGH); 
 +    } 
 +    // If message received and it is 0, then switch relay on 
 +    if(msg == "0"
 +    { 
 +      digitalWrite(RELAY_PIN, LOW); 
 +    }
   }   }
 } }
  
 +// Function started after the connection to the server is established.
 void iot_connected() void iot_connected()
 { {
   Serial.println("MQTT connected callback");   Serial.println("MQTT connected callback");
-  iot.subscribe("relay");+  iot.subscribe(MODULE_TOPIC); // subscribe to topic relay
   iot.log("IoT relay example!");   iot.log("IoT relay example!");
 } }
Line 46: Line 59:
 void setup() void setup()
 { {
-  Serial.begin(115200);+  Serial.begin(115200); // setting up serial connection parameter
   Serial.println("Booting");   Serial.println("Booting");
 +  
 +  //iot.setConfig("wname", WIFI_NAME);
 +  //iot.setConfig("wpass", WIFI_PASSWORD);
 +  iot.printConfig(); // print json config to serial
 +  iot.setup(); // Initialize IoT library
  
-  iot.printConfig(); // print json config to serial //Peale Serial.begin ja enne iot.setup +  pinMode(RELAY_PIN, OUTPUT); // The relay pin is defined as output type
-  iot.setup(); +
-  pinMode(RELAY_PIN, OUTPUT);+
 } }
  
 void loop() void loop()
 { {
-  iot.handle();   +  iot.handle(); // IoT behind the plan work, it should be periodically called 
-  delay(200);+  delay(200); // Wait for 0.2 seconds
 } }
- 
  
 </code> </code>
en/iot/examples/relay.1513595280.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0