/
loudness_sensor.ino
101 lines (78 loc) · 4.37 KB
/
loudness_sensor.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Copyright 2015-2017 AllThingsTalk
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
This sketch is an example sketch to deploy the Grove Loudness sensor (101020063) to the
AllThingsTalk IoT developer cloud.
*/
#include <Ethernet2.h>
#include <EthernetClient.h>
#include <PubSubClient.h>
#include <ATT_IOT.h> //AllThingsTalk IoT library
#include <SPI.h> //required to have support for signed/unsigned long type.
#include "keys.h" // Keep all your personal account information in a seperate file
ATTDevice Device(DEVICEID, CLIENTID, CLIENTKEY); //create the object that provides the connection to the cloud to manager the device.
#define httpServer "api.AllThingsTalk.io" // HTTP API Server host
#define mqttServer httpServer // MQTT Server Address
// Define PIN numbers for assets
// For digital and analog sensors, we recommend to use the physical pin id as the asset id
// For other sensors (I2C and UART), you can select any unique number as the asset id
#define loudnessId 2 // Analog sensor is connected to pin A2 on grove shield
//required for the device
void callback(char* topic, byte* payload, unsigned int length);
EthernetClient ethClient;
PubSubClient pubSub(mqttServer, 1883, callback, ethClient);
void setup()
{
Serial.begin(57600); // Init serial link for debugging
while(!Serial && millis() < 1000); // Make sure you see all output on the monitor. After 1 sec, it will skip this step, so that the board can also work without being connected to a pc
Serial.println("Starting sketch");
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0xE1, 0x3E}; // Adapt to your Arduino MAC Address
if (Ethernet.begin(mac) == 0) // Initialize the Ethernet connection:
{
Serial.println(F("DHCP failed,end"));
while(true); //we failed to connect, halt execution here.
}
while(!Device.Connect(ðClient, httpServer)) // connect the device with the IOT platform.
Serial.println("retrying");
Device.AddAsset(loudnessId, "Loudness sensor", "loudness", false, "{\"type\": \"integer\",\"minimum\":0,\"maximum\":1023}"); // Create the sensor asset for your device
while(!Device.Subscribe(pubSub)) // make certain that we can receive message from the iot platform (activate mqtt)
Serial.println("retrying");
pinMode(loudnessId, INPUT); // Initialize the digital pin as an input.
Serial.println("Loudness sensor is ready for use!");
}
int sensorVal = 0;
int prevVal = -1;
unsigned long time;
void loop()
{
time = millis();
while(time + 2000 > millis()){ // Measure for 2 seconds, take the max, so we know the max sound level during those 2 seconds.
int current = analogRead(loudnessId);
if(current > sensorVal)
sensorVal = current;
}
if(sensorVal != prevVal){ // Only send values when actually changed.
Device.Send(String(sensorVal), loudnessId);
Serial.println(sensorVal);
prevVal = sensorVal;
}
Device.Process();
sensorVal = 0;
}
// Callback function: handles messages that were sent from the iot platform to this device.
void callback(char* topic, byte* payload, unsigned int length)
{
char message_buff[length + 1]; //need to copy over the payload so that we can add a /0 terminator, this can then be wrapped inside a string for easy manipulation
strncpy(message_buff, (char*)payload, length); //copy over the data
message_buff[length] = '\0'; //make certain that it ends with a null
Serial.print("Payload: "); //show some debugging
Serial.println(message_buff);
Serial.print("topic: ");
Serial.println(topic);
}