-
Notifications
You must be signed in to change notification settings - Fork 12
/
_P013_HCSR04.ino
209 lines (185 loc) · 6.58 KB
/
_P013_HCSR04.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//#######################################################################################################
//#################################### Plugin 013: HC-SR04 ##############################################
//#######################################################################################################
MyMessage *msgDist013;
#define PLUGIN_013
#define PLUGIN_ID_013 13
#define PLUGIN_NAME_013 "Ultrasonic Sensor - HC-SR04"
#define PLUGIN_VALUENAME1_013 "Distance"
boolean Plugin_013_init = false;
volatile unsigned long Plugin_013_timer = 0;
volatile unsigned long Plugin_013_state = 0;
byte Plugin_013_TRIG_Pin = 0;
byte Plugin_013_IRQ_Pin = 0;
boolean Plugin_013(byte function, struct EventStruct *event, String& string)
{
static byte switchstate[TASKS_MAX];
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_013;
Device[deviceCount].Type = DEVICE_TYPE_DUAL;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].ValueCount = 1;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_013);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_013));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
byte choice = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
String options[2];
options[0] = F("Value");
options[1] = F("State");
int optionValues[2];
optionValues[0] = 1;
optionValues[1] = 2;
string += F("<TR><TD>Mode:<TD><select name='plugin_013_mode'>");
for (byte x = 0; x < 2; x++)
{
string += F("<option value='");
string += optionValues[x];
string += "'";
if (choice == optionValues[x])
string += F(" selected");
string += ">";
string += options[x];
string += F("</option>");
}
string += F("</select>");
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == 2)
{
char tmpString[80];
sprintf_P(tmpString, PSTR("<TR><TD>Threshold:<TD><input type='text' name='plugin_013_threshold' value='%u'>"), Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
string += tmpString;
}
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
String plugin1 = WebServer.arg("plugin_013_mode");
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = plugin1.toInt();
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == 2)
{
String plugin2 = WebServer.arg("plugin_013_threshold");
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = plugin2.toInt();
}
success = true;
break;
}
case PLUGIN_INIT:
{
if (!msgDist013)
msgDist013 = new MyMessage(event->BaseVarIndex, V_DISTANCE);
present(event->BaseVarIndex, S_DISTANCE);
Serial.print("Present Dist: ");
Serial.println(event->BaseVarIndex);
Plugin_013_init = true;
pinMode(Settings.TaskDevicePin1[event->TaskIndex], OUTPUT);
pinMode(Settings.TaskDevicePin2[event->TaskIndex], INPUT_PULLUP);
Plugin_013_IRQ_Pin = Settings.TaskDevicePin2[event->TaskIndex];
attachInterrupt(Settings.TaskDevicePin2[event->TaskIndex], Plugin_013_interrupt, CHANGE);
success = true;
break;
}
case PLUGIN_READ: // If we select value mode, read and send the value based on global timer
{
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == 1)
{
Plugin_013_TRIG_Pin = Settings.TaskDevicePin1[event->TaskIndex];
float value = Plugin_013_read();
String log = F("SR04 : Distance: ");
if (value != -1)
{
UserVar[event->BaseVarIndex] = (float)Plugin_013_timer / 58;
log += UserVar[event->BaseVarIndex];
send(msgDist013->set((uint16_t)UserVar[event->BaseVarIndex]));
success = true;
}
else
log += F("SR04 : Distance: No reading!");
addLog(LOG_LEVEL_INFO,log);
}
break;
}
case PLUGIN_TEN_PER_SECOND: // If we select state mode, do more frequent checks and send only state changes
{
if (Settings.TaskDevicePluginConfig[event->TaskIndex][0] == 2)
{
Plugin_013_TRIG_Pin = Settings.TaskDevicePin1[event->TaskIndex];
byte state = 0;
float value = Plugin_013_read();
if (value != -1)
{
if (value < Settings.TaskDevicePluginConfig[event->TaskIndex][1])
state = 1;
if (state != switchstate[event->TaskIndex])
{
String log = F("SR04 : State ");
log += state;
addLog(LOG_LEVEL_INFO,log);
switchstate[event->TaskIndex] = state;
UserVar[event->BaseVarIndex] = state;
event->sensorType = SENSOR_TYPE_SWITCH;
send(msgDist013->set((uint16_t)UserVar[event->BaseVarIndex]));
}
}
}
success = true;
break;
}
}
return success;
}
/*********************************************************************/
float Plugin_013_read()
/*********************************************************************/
{
float value = -1;
Plugin_013_timer = 0;
Plugin_013_state = 0;
noInterrupts();
digitalWrite(Plugin_013_TRIG_Pin, LOW);
delayMicroseconds(2);
digitalWrite(Plugin_013_TRIG_Pin, HIGH);
delayMicroseconds(10);
digitalWrite(Plugin_013_TRIG_Pin, LOW);
interrupts();
delay(25); // wait for measurement to finish (max 400 cm * 58 uSec = 23uSec)
if (Plugin_013_state == 2)
{
value = (float)Plugin_013_timer / 58;
}
return value;
}
/*********************************************************************/
void Plugin_013_interrupt()
/*********************************************************************/
{
byte pinState = digitalRead(Plugin_013_IRQ_Pin);
if (pinState == 1) // Start of pulse
{
Plugin_013_state = 1;
Plugin_013_timer = micros();
}
else // End of pulse, calculate timelapse between start & end
{
Plugin_013_state = 2;
Plugin_013_timer = micros() - Plugin_013_timer;
}
}