-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbotIoTctrl.hs
181 lines (167 loc) · 4.05 KB
/
chatbotIoTctrl.hs
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
#include <Process.h>
#include <YunClient.h>
#include <SPI.h>
IPAddress server(1,214,89,9);
int port=9981;
YunClient client;
//root dst
String root="http://:";
//urls
String dusturl = "/dust/";
String fanurl = "/fanchk/";
//measure Common
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
//input
int imeasurePin = 0; //Connect dust sensor to Arduino A0 pin
int iledPower = 2; //Connect 3 led driver pins of dust sensor to Arduino D2
float ivoMeasured = 0;
float icalcVoltage = 0;
float idustDensity = 0;
int irst=0;
int iqrst=0;
//output
int omeasurePin = 1; //Connect dust sensor to Arduino A1 pin
int oledPower = 3; //Connect 3 led driver pins of dust sensor to Arduino D3
float ovoMeasured = 0;
float ocalcVoltage = 0;
float odustDensity = 0;
int orst=0;
int oqrst=0;
//fanounput
int motorPin=6;
void setup() {
// Initialize Bridge
Serial.begin(9600);
Bridge.begin();
delay(10);
//output setup
pinMode(iledPower,OUTPUT);
pinMode(oledPower,OUTPUT);
pinMode(motorPin, OUTPUT);
}
void loop()
{
//wifichk();
if(chker(fanurl))
{
fan(true);
}
else
{
fan(false);
}
delay(3000);
iqrst=dust25calc(0);
oqrst=dust25calc(1);
dustreg(root+dusturl,iqrst,oqrst);
}
int dust25calc(int flag)
{
if(flag==0)
{
digitalWrite(iledPower,LOW); // power on the LED
delayMicroseconds(samplingTime);
ivoMeasured = analogRead(imeasurePin); // read the input dust value
}
else
{
digitalWrite(oledPower,LOW); // power on the LED
delayMicroseconds(samplingTime);
ovoMeasured = analogRead(omeasurePin); // read the output dust value
}
delayMicroseconds(deltaTime);
if(flag==0)
{
digitalWrite(iledPower,HIGH); // turn the LED off
}
else
{
digitalWrite(oledPower,HIGH); // turn the LED off
}
delayMicroseconds(sleepTime);
if(flag==0)
{
// 0 - 5V mapped to 0 - 1023 integer values
// recover voltage
icalcVoltage = ivoMeasured * (5.0 / 1024.0);
// linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
// Chris Nafis (c) 2012
idustDensity = 0.17 * icalcVoltage - 0.1;
return (int(idustDensity*1000/2.5));
}
else
{
// 0 - 5V mapped to 0 - 1023 integer values
// recover voltage
ocalcVoltage = ovoMeasured * (5.0 / 1024.0);
// linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
// Chris Nafis (c) 2012
odustDensity = 0.17 * ocalcVoltage - 0.1;
return (int(odustDensity*1000/2.5));
}
}
void dustreg(String url, int ivalue, int ovalue)
{
// We now create a URI for the request
url+=String(ivalue)+"/";
url+=String(ovalue);
Serial.print("Requesting URL: ");
Serial.println(url);
Process p; // Create a process and call it "p"
p.begin("curl"); // Process that launch the "curl" command
p.addParameter(url); // Add the URL parameter to "curl"
p.run(); // Run the process and wait for its termination
while (p.available()>0) {
char c = p.read();
//Serial.print(c);
}
// Ensure the last bit of data is sent.
Serial.flush();
}
bool chker(String param)
{
//Serial.print("Requesting URL: ");
//Serial.println(root+param);
Process p; // Create a process and call it "p"
p.begin("curl"); // Process that launch the "curl" command
p.addParameter(root+param); // Add the URL parameter to "curl"
p.run(); // Run the process and wait for its termination
while (p.available()>0) {
char c = p.read();
Serial.flush();
if(c=='o'){
return true;
break;
}
else{
return false;
break;
}
}
}
void wifichk()
{
Process wifiCheck; // initialize a new process
wifiCheck.runShellCommand("/usr/bin/pretty-wifi-info.lua"); // command you want to run
// while there's any characters coming back from the
// process, print them to the serial monitor:
while (wifiCheck.available() > 0) {
char c = wifiCheck.read();
Serial.print(c);
}
Serial.println();
delay(5000);
}
void fan(bool onoroff)
{
if(onoroff==false)
{
analogWrite(motorPin, 0);
}
else
{
analogWrite(motorPin, 255);
}
}