This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot:examples:buzzer [2018/03/20 08:59] – Somepub | en:iot:examples:buzzer [2024/03/12 16:19] (current) – heiko.pikner | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| {{: | {{: | ||
| - | The code above will alert the buzz sound when value has been submitted | + | Needed libraries: |
| + | < | ||
| + | |||
| + | The code below will alert the buzz sound when values have been submitted | ||
| <code c> | <code c> | ||
| Line 15: | Line 18: | ||
| */ | */ | ||
| + | // Includes global variables and librarys that the Buzzer uses | ||
| #include < | #include < | ||
| #include < | #include < | ||
| + | #include < | ||
| - | //Pin definition for the relay (GPIO15) | + | #define MODULE_TOPIC " |
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| + | |||
| + | //Pin definition for the buzzer | ||
| #define BUZZER_PIN D8 | #define BUZZER_PIN D8 | ||
| - | int freq[]={1047, | + | Buzzer buzzer(BUZZER_PIN); |
| - | String | + | |
| + | // Splitting string into smaller parts, so that the sound level and length can be read out | ||
| + | // https://stackoverflow.com/questions/9072320/ | ||
| + | String | ||
| + | { | ||
| + | int found = 0; | ||
| + | int strIndex[] = {0, -1}; | ||
| + | int maxIndex = data.length()-1; | ||
| + | |||
| + | for(int i=0; i< | ||
| + | { | ||
| + | if(data.charAt(i)==separator || i==maxIndex) | ||
| + | { | ||
| + | found++; | ||
| + | strIndex[0] = strIndex[1]+1; | ||
| + | strIndex[1] = (i == maxIndex) ? i+1 : i; | ||
| + | } | ||
| + | } | ||
| + | return found> | ||
| + | } | ||
| // If message received sound the buzz. For example: | // If message received sound the buzz. For example: | ||
| - | // mosquitto_pub -u test -P test -t " | + | // mosquitto_pub -u test -P test -t " |
| + | // 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 33: | Line 62: | ||
| Serial.println(msg); | Serial.println(msg); | ||
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | digitalWrite(BUZZER_PIN, LOW); | + | |
| + | buzzer.sound(Note.toInt(), | ||
| + | } | ||
| } | } | ||
| Line 46: | Line 76: | ||
| Serial.println(" | Serial.println(" | ||
| // Subscribe to the topic " | // Subscribe to the topic " | ||
| - | iot.subscribe(" | + | iot.subscribe(MODULE_TOPIC); |
| iot.log(" | iot.log(" | ||
| } | } | ||
| Line 52: | Line 82: | ||
| void setup() | void setup() | ||
| { | { | ||
| - | Serial.begin(115200); | + | Serial.begin(115200); |
| Serial.println(" | Serial.println(" | ||
| + | // | ||
| + | // | ||
| // 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, | ||
| - | digitalWrite(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); |
| } | } | ||
| - | |||
| </ | </ | ||