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:buzzer [2020/07/20 11:26] – external edit 127.0.0.1en:iot:examples:buzzer [2024/03/12 16:19] (current) heiko.pikner
Line 3: Line 3:
  
 Needed libraries: Needed libraries:
-<code>lib_deps = ITTIoT@1.0.5, Buzzer</code>+<code>lib_deps = ITTIoT, gmarty2000/Buzzer@^1.0.0</code>
  
-The code below will alert the buzz sound when values have been submitted to the mqtt server+The code below will alert the buzz sound when values have been submitted by the mqtt server
  
 <code c> <code c>
Line 18: Line 18:
  */  */
  
 +// Includes global variables and librarys that the Buzzer uses
 #include <Arduino.h> #include <Arduino.h>
 #include <ittiot.h> #include <ittiot.h>
 #include <Buzzer.h> #include <Buzzer.h>
 +
 +#define MODULE_TOPIC "ESP30/buzzer"
 +#define WIFI_NAME "name"
 +#define WIFI_PASSWORD "password"
  
 //Pin definition for the buzzer (GPIO15) //Pin definition for the buzzer (GPIO15)
Line 27: Line 32:
 Buzzer buzzer(BUZZER_PIN); Buzzer buzzer(BUZZER_PIN);
  
 +// Splitting string into smaller parts, so that the sound level and length can be read out
 // https://stackoverflow.com/questions/9072320/split-string-into-string-array // https://stackoverflow.com/questions/9072320/split-string-into-string-array
 String getValue(String data, char separator, int index) String getValue(String data, char separator, int index)
Line 43: Line 49:
     }     }
   }   }
- 
   return found>index ? data.substring(strIndex[0], strIndex[1]) : "";   return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
 } }
Line 57: Line 62:
   Serial.println(msg);   Serial.println(msg);
  
-  String Note = getValue(msg,';',0); +  if(topic == MODULE_TOPIC) 
-  String time = getValue(msg,';',1); +  { 
- +    String Note = getValue(msg,';',0); 
-  buzzer.sound(Note.toInt(),time.toInt());+    String time = getValue(msg,';',1);
  
 +    buzzer.sound(Note.toInt(),time.toInt());
 +  }
 } }
  
Line 69: Line 76:
   Serial.println("MQTT connected callback");   Serial.println("MQTT connected callback");
   // Subscribe to the topic "buzzer"   // Subscribe to the topic "buzzer"
-  iot.subscribe("ESP01/buzzer");+  iot.subscribe(MODULE_TOPIC);
   iot.log("IoT buzzer example!");   iot.log("IoT buzzer example!");
 } }
Line 75: Line 82:
 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);
   // Print json config to serial   // Print json config to serial
   iot.printConfig();   iot.printConfig();
   // Initialize IoT library   // Initialize IoT library
   iot.setup();   iot.setup();
-  // Initialize buzzer pin 
- 
 } }
  
 +//Main code, which runs in loop
 void loop() void loop()
 { {
   // IoT behind the plan work, it should be periodically called   // IoT behind the plan work, it should be periodically called
   iot.handle();   iot.handle();
-  delay(200);+  delay(200); // Wait for 0.2 second
 } }
 +
 </code> </code>
  
  
en/iot/examples/buzzer.1595244384.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