Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
en:iot:examples:buzzer [2018/12/27 12:53]
127.0.0.1 external edit
en:iot:examples:buzzer [2019/11/12 15:32] (current)
timothy_r
Line 3: Line 3:
  
 Needed libraries: Needed libraries:
-<​code>​lib_deps = ITTIoT</​code>​+<​code>​lib_deps = ITTIoT, Buzzer</​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 to the mqtt server
  
 <code c> <code c>
Line 20: Line 20:
 #include <​Arduino.h>​ #include <​Arduino.h>​
 #include <​ittiot.h>​ #include <​ittiot.h>​
 +#include <​Buzzer.h>​
  
-//Pin definition for the relay (GPIO15)+//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"};+ 
 +// 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" 
 +//Check pitches.h for different notes
 void iot_received(String topic, String msg) void iot_received(String topic, String msg)
 { {
Line 36: Line 57:
   Serial.println(msg);​   Serial.println(msg);​
  
-  ​analogWrite(BUZZER_PIN, ​msg.toInt()); +  ​String Note = getValue(msg,';',0); 
-  delay(1000);​ +  ​String time = getValue(msg,';'​,1); 
-  analogWrite(BUZZER_PIN, 0); + 
-  ​pinMode(BUZZER_PINOUTPUT); +  ​buzzer.sound(Note.toInt(),time.toInt());
-  ​digitalWrite(BUZZER_PINLOW);+
  
 } }
Line 63: Line 83:
   iot.setup();​   iot.setup();​
   // Initialize buzzer pin   // Initialize buzzer pin
-  pinMode(BUZZER_PIN,​ OUTPUT); 
-  digitalWrite(BUZZER_PIN,​ LOW); 
  
 } }
Line 73: Line 91:
   iot.handle();​   iot.handle();​
   delay(200);   delay(200);
- 
 } }
- 
- 
 </​code>​ </​code>​
  
  
en/iot/examples/buzzer.1545908025.txt.gz · Last modified: 2019/11/12 15:32 (external edit)
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