Buzzer example

The code above will alert the buzz sound when value has been submitted to the mqtt server

/*
 * 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
 */
 
#include <Arduino.h>
#include <ittiot.h>
 
//Pin definition for the relay (GPIO15)
#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
String note[]={"C6", "D6", "E6", "F6", "G6", "A6", "B6", "C7"};
 
// If message received sound the buzz. For example:
// mosquitto_pub -u test -P test -t "ITT/IOT/3/buzzer" -m "512"
void iot_received(String topic, String msg)
{
  Serial.print("MSG FROM USER callback, topic: ");
  Serial.print(topic);
  Serial.print(" payload: ");
  Serial.println(msg);
 
  analogWrite(BUZZER_PIN, msg.toInt());
  delay(1000);
  analogWrite(BUZZER_PIN, 0);
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);
 
}
 
// 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("buzzer");
  iot.log("IoT buzzer example!");
}
 
void setup()
{
  Serial.begin(115200);
  Serial.println("Booting");
 
  // Print json config to serial
  iot.printConfig();
  // Initialize IoT library
  iot.setup();
  // Initialize buzzer pin
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);
 
}
 
void loop()
{
  // IoT behind the plan work, it should be periodically called
  iot.handle();
  delay(200);
 
}
en/iot/examples/buzzer.txt · Last modified: 2018/03/20 10:59 by Somepub
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