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 [2018/09/05 13:51] – Added needed libraries tomyKalmen:iot:examples:buzzer [2024/03/12 16:19] (current) heiko.pikner
Line 3: Line 3:
  
 Needed libraries: Needed libraries:
-<code>lib_deps = ITTIoT</code>+<code>lib_deps = ITTIoT, gmarty2000/Buzzer@^1.0.0</code>
  
-The code below will alert the buzz sound when value has 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>
  
-//Pin definition for the relay (GPIO15)+#define MODULE_TOPIC "ESP30/buzzer" 
 +#define WIFI_NAME "name" 
 +#define WIFI_PASSWORD "password" 
 + 
 +//Pin definition for the buzzer (GPIO15)
 #define BUZZER_PIN D8 #define BUZZER_PIN D8
  
-int freq[]={1047,1175,1319,1397,1568,1760,1976,2093};//Note name: C6 D6 E6 F6 G6 A6 B6 C7   http://newt.phys.unsw.edu.au/jw/notes.html +Buzzer buzzer(BUZZER_PIN); 
-String note[]={"C6""D6", "E6", "F6", "G6", "A6", "B6", "C7"};+ 
 +// 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 
 +String getValue(String data, char separator, int index) 
 +
 +  int found = 0; 
 +  int strIndex[] = {0-1}; 
 +  int maxIndex = data.length()-1; 
 + 
 +  for(int i=0; i<=maxIndex && found<=index; i++) 
 +  { 
 +    if(data.charAt(i)==separator || i==maxIndex) 
 +    { 
 +        found++; 
 +        strIndex[0] = strIndex[1]+1; 
 +        strIndex[1] = (i == maxIndex) ? i+1 : i; 
 +    } 
 +  } 
 +  return found>index ? data.substring(strIndex[0]strIndex[1]) : ""
 +}
  
 // If message received sound the buzz. For example: // If message received sound the buzz. For example:
-// mosquitto_pub -u test -P test -t "ITT/IOT/3/buzzer" -m "512"+// mosquitto_pub -u test -P test -t "ITT/IOT/3/buzzer" -m "49;100" 
 +// The first message is the pitch(integer between 1-8191) and the second message is the duration
 void iot_received(String topic, String msg) void iot_received(String topic, String msg)
 { {
Line 36: Line 62:
   Serial.println(msg);   Serial.println(msg);
  
-  analogWrite(BUZZER_PIN, msg.toInt()); +  if(topic == MODULE_TOPIC
-  delay(1000); +  { 
-  analogWrite(BUZZER_PIN, 0); +    String Note = getValue(msg,';',0); 
-  pinMode(BUZZER_PINOUTPUT); +    String time = getValue(msg,';',1);
-  digitalWrite(BUZZER_PINLOW);+
  
 +    buzzer.sound(Note.toInt(),time.toInt());
 +  }
 } }
  
Line 49: Line 76:
   Serial.println("MQTT connected callback");   Serial.println("MQTT connected callback");
   // Subscribe to the topic "buzzer"   // Subscribe to the topic "buzzer"
-  iot.subscribe("buzzer");+  iot.subscribe(MODULE_TOPIC);
   iot.log("IoT buzzer example!");   iot.log("IoT buzzer example!");
 } }
Line 55: 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 
-  pinMode(BUZZER_PIN, OUTPUT); 
-  digitalWrite(BUZZER_PIN, LOW); 
- 
 } }
  
 +//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.1536155488.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