This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:iot:examples:buzzer [2018/02/27 14:37] – created Somepub | en:iot:examples:buzzer [2024/03/12 16:19] (current) – heiko.pikner | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Buzzer example ====== | ====== Buzzer example ====== | ||
| + | {{: | ||
| + | |||
| + | Needed libraries: | ||
| + | < | ||
| + | |||
| + | The code below will alert the buzz sound when values have been submitted by the mqtt server | ||
| + | |||
| + | <code c> | ||
| + | |||
| + | /* | ||
| + | * IoT Buzzer example | ||
| + | * | ||
| + | * This example subscribe to the " | ||
| + | * will make a sound | ||
| + | * | ||
| + | * Created 02 Febrary 2018 by Heiko Pikner | ||
| + | */ | ||
| + | |||
| + | // Includes global variables and librarys that the Buzzer uses | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define MODULE_TOPIC " | ||
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| + | |||
| + | //Pin definition for the buzzer (GPIO15) | ||
| + | #define BUZZER_PIN D8 | ||
| + | |||
| + | Buzzer buzzer(BUZZER_PIN); | ||
| + | |||
| + | // Splitting string into smaller parts, so that the sound level and length can be read out | ||
| + | // https:// | ||
| + | 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< | ||
| + | { | ||
| + | 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: | ||
| + | // 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) | ||
| + | { | ||
| + | Serial.print(" | ||
| + | Serial.print(topic); | ||
| + | Serial.print(" | ||
| + | Serial.println(msg); | ||
| + | |||
| + | if(topic == MODULE_TOPIC) | ||
| + | { | ||
| + | String Note = getValue(msg,';', | ||
| + | String time = getValue(msg,';', | ||
| + | |||
| + | buzzer.sound(Note.toInt(), | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Function started after the connection to the server is established. | ||
| + | void iot_connected() | ||
| + | { | ||
| + | Serial.println(" | ||
| + | // Subscribe to the topic " | ||
| + | iot.subscribe(MODULE_TOPIC); | ||
| + | iot.log(" | ||
| + | } | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | Serial.begin(115200); | ||
| + | Serial.println(" | ||
| + | |||
| + | // | ||
| + | // | ||
| + | // Print json config to serial | ||
| + | iot.printConfig(); | ||
| + | // Initialize IoT library | ||
| + | iot.setup(); | ||
| + | } | ||
| + | |||
| + | //Main code, which runs in loop | ||
| + | void loop() | ||
| + | { | ||
| + | // IoT behind the plan work, it should be periodically called | ||
| + | iot.handle(); | ||
| + | delay(200); // Wait for 0.2 second | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||