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
et:iot:examples:buzzer [2023/01/28 18:41] – external edit (Unknown date) 127.0.0.1et:iot:examples:buzzer [2025/02/14 09:09] (current) karllall
Line 1: Line 1:
 +====== (Buzzer) Sumisti näidis ======
 +{{:en:iot:examples:soundmodule.png?200|}}
 +
 +Vajaminevad teegid:
 +<code>lib_deps = robolabor/ITTIoT@^2.0.0, gmarty2000/Buzzer@^1.0.0</code>
 +
 +Näidiskood paneb sumisti tööle kui mqtt serverist tuleb vastav käsklus:
 +
 +<code c>
 +
 +/*
 + * IoT Buzzer example
 + *
 + * This example subscribe to the "buzzer" topic. When a message received, then it
 + * will make a sound
 + *
 + * Created 02 Febrary 2018 by Heiko Pikner
 + */
 +
 +// Includes global variables and librarys that the Buzzer uses
 +#include <Arduino.h>
 +#include <ittiot.h>
 +#include <Buzzer.h>
 +
 +#define MODULE_TOPIC "ESP30/buzzer"
 +#define WIFI_NAME "name"
 +#define WIFI_PASSWORD "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://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:
 +// 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)
 +{
 +  Serial.print("MSG FROM USER callback, topic: ");
 +  Serial.print(topic);
 +  Serial.print(" payload: ");
 +  Serial.println(msg);
 +
 +  if(topic == MODULE_TOPIC)
 +  {
 +    String Note = getValue(msg,';',0);
 +    String time = getValue(msg,';',1);
 +
 +    buzzer.sound(Note.toInt(),time.toInt());
 +  }
 +}
 +
 +// Function started after the connection to the server is established.
 +void iot_connected()
 +{
 +  Serial.println("MQTT connected callback");
 +  // Subscribe to the topic "buzzer"
 +  iot.subscribe(MODULE_TOPIC);
 +  iot.log("IoT buzzer example!");
 +}
 +
 +void setup()
 +{
 +  Serial.begin(115200); // setting up serial connection parameter
 +  Serial.println("Booting");
 +
 +  //iot.setConfig("wname", WIFI_NAME);
 +  //iot.setConfig("wpass", WIFI_PASSWORD);
 +  // 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
 +}
 +
 +</code>
 +
  
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