-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino.ino
224 lines (185 loc) · 5.09 KB
/
Arduino.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <bufferfiller.h>
#include <enc28j60.h>
#include <EtherCard.h>
#include <net.h>
#include <stash.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
# define Hall sensor 2
int contador;
float temperature ;
float humidity;
float pressure ;
#define ALTITUDE 0
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[700];
static uint32_t timer;
char mensagem[80];
const byte websiteip[] = {192,168,0,107};
const char website[] PROGMEM = "192.168.0.107";
static byte myip[]={192,168,0,113};
static byte gwip[]={192,168,0,1};
static void my_callback (byte status, word off, word len)
{
Serial.println("-----------------------------------------------------");
Serial.println("entrou no mycalllback");
Serial.println("-----------------------------------------------------");
Ethernet::buffer[off+len] = 0; //mensagem do retorno do servidor
// Serial.print((const char*) Ethernet::buffer + off); //mostra todo o retorno
Serial.print((const char*) Ethernet::buffer + off + 157); //mostra somente ok
}
// Const def biruta
int pin=0; // A0 entrada do biruta *
float valor =0;
float Winddir =0;
// Constants definitions anem.
const float pi = 3.14159265; // Numero pi
int period = 2500; // Tempo de medida(miliseconds) 5000
int radius = 147; // Raio do anemometro(mm)
// Variable definitions
unsigned int counter = 0; // magnet counter for sensor
unsigned int RPM = 0; // Revolutions per minute
float speedwind = 0; // Wind speed (Km/h)
Adafruit_BME280 bme; // I2C
void setup() {
Serial.begin(9600);//serial
//bme280
bool status;
status = bme.begin(0x76); //The I2C address of the sensor I use is 0x76
if (!status) {
Serial.print("Sensor not working, check connections");
while (1);
}
// Anemômetro
pinMode(2, INPUT); // Entrada anemometro *
digitalWrite(2, HIGH); //internall pull-up active
Serial.println("Obtendo IP:");
if (ether.begin(sizeof Ethernet::buffer, mymac,53) == 0){
Serial.println( "Falha ao acessar o controlador Ethernet");
}
ether.staticSetup(myip, gwip);
ether.printIp("IP: ", ether.myip);
ether.printIp("Gatway: ", ether.gwip);
ether.printIp("Sevidor DNS: ", ether.dnsip);
if (!ether.dnsLookup(website)){
Serial.println("Falha no DNS");
}
ether.parseIp(ether.hisip, "192.168.0.107"); //IP do servidor manual
ether.printIp("Servidor: ", ether.hisip);
Serial.println(" ");
}
// Calculo da velocidade wind *************
void windvelocity(){
speedwind = 0;
counter = 0;
attachInterrupt(0, addcount, RISING);
unsigned long millis();
long startTime = millis();
while(millis() < startTime + period) {
}
}
void RPMcalc(){
RPM=((counter)*60)/(period/500); // Calculate revolutions per minute (RPM)
}
void SpeedWind(){
speedwind = (((4 * pi * radius * RPM)/60) / 500)*3.6; // Calculate wind speed on km/h
}
void addcount(){
counter++;
}
// Fim calculo veloc wind *****************
void loop() {
//anenometro
windvelocity();
RPMcalc();
SpeedWind();
//biruta
valor = analogRead(pin)* (5.0 / 1023.0);
if (valor <= 0.27) {
Winddir = 315;
}
else if (valor <= 0.32) {
Winddir = 270;
}
else if (valor <= 0.38) {
Winddir = 225;
}
else if (valor <= 0.45) {
Winddir = 180;
}
else if (valor <= 0.57) {
Winddir = 135;
}
else if (valor <= 0.75) {
Winddir = 90;
}
else if (valor <= 1.25) {
Winddir = 45;
}
else {
Winddir = 000;
}
//pluviometro
Serial.println(valor);//bme280
getPressure();
getHumidity();
getTemperature();
char t[50];
char h[50];
char p[50];
char w[50];
char sw[50];
dtostrf(pressure,1,2,p);
dtostrf(Winddir,1,2,w);
dtostrf(speedwind,1,2,sw);
dtostrf(temperature,1,2,t);
dtostrf(humidity,1,2,h);
//Printing Temperature
String temperatureString = String(temperature,1);
Serial.write("Temperatura:");
Serial.print(temperatureString);
Serial.write((char)223);
Serial.write("C ");
Serial.println("");
//Printing Humidity
String humidityString = String(humidity,0);
Serial.write("Umidade: ");
Serial.print(humidityString);
Serial.write("%");
Serial.println("");
//Printing Pressure
Serial.write("Pressao: ");
String pressureString = String(pressure,2);
Serial.print(pressureString);
Serial.write(" hPa");
Serial.println("");
Serial.print("Direcao: ");
Serial.print(Winddir);
Serial.print(" graus ");
Serial.print("--Veloc ");
Serial.print(speedwind);
Serial.print(" Km/h ");
Serial.println("");
ether.packetLoop(ether.packetReceive());
if (millis() > timer){
timer = millis() + 300000;//60000;//14000;//4000
ether.persistTcpConnection(true);
sprintf(mensagem, "?temperatura=%s&pressao=%s&umidade=%s&velocidade=%s&direcao=%s",t,p,h,sw,w);//diz que o valor é nulo,e nao insere no banco
ether.browseUrl(PSTR("/teste.php"),mensagem, website, my_callback);
}
delay(100);
}
float getTemperature()
{
temperature = bme.readTemperature();
}
float getHumidity()
{
humidity = bme.readHumidity();
}
float getPressure()
{
pressure = bme.readPressure();
pressure = bme.seaLevelForAltitude(ALTITUDE,pressure);
pressure = pressure/100.0F;
}