06-02-2017, 23:45
Od kilku dni walczę z projektem stacji meteo .
W swojej stacji używam czujników BMP180, DHT11. Do tego zestawu postanowiłem dołączyć anemometr (n76ef z maplina). W tym momencie pojawił się problem, szkic z czujnikami wilgotności, temperatury i ciśnienia działa prawidłowo i wysyła dana na stronę Thingspeak.com kanał 214058.
Szkic poniżej.
Postanowiłem połączyć dwa szkice, ten wyżej wymieniony z następującym, który działa samodzielnie i w porcie szeregowym wskazuje poprawne wartości prędkości wiatru.
W wyniku moich wypocin stworzyłem taki szkic. Niestety działa niepoprawnie. Czy ktoś mógł by spojrzeć gdzie tkwi błąd?
W swojej stacji używam czujników BMP180, DHT11. Do tego zestawu postanowiłem dołączyć anemometr (n76ef z maplina). W tym momencie pojawił się problem, szkic z czujnikami wilgotności, temperatury i ciśnienia działa prawidłowo i wysyła dana na stronę Thingspeak.com kanał 214058.
Szkic poniżej.
Kod:
#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>
#include <Wire.h>
#include "Adafruit_BMP085.h" // This is the version 1 library
#define DHTPIN A0
#define DHTTYPE 11// The Temperature/Humidity sensor
Adafruit_BMP085 bmp;
DHT dht(DHTPIN, DHTTYPE);
// Local Network Settings
byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "AHAGS1F5IPQCATX";
const int updateThingSpeakInterval = 16 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
// Initialize Arduino Ethernet Client
EthernetClient client;
void setup()
{
// Start Serial for debugging on the Serial Monitor
Serial.begin(9600);
// Start Ethernet on Arduino
startEthernet();
}
void loop()
{
if (client.available())
{
char c = client.read();
Serial.print(c);
}
//------DHT11--------
int chk = dht.read(DHTPIN);
//char t_buffer[10];
char h_buffer[10];
//float t=dht.readTemperature();
//String temp=dtostrf(t,0,5,t_buffer);
//Serial.println(temp);
//Serial.print(" ");
float h = dht.readHumidity();
String humid = dtostrf(h, 0, 5, h_buffer);
Serial.println(humid);
//-----BMP180-----------
bmp.begin();
float p = (bmp.readPressure() / 100.0); //this is for pressure in hectoPascal
float t2 = (bmp.readTemperature());
char p_buffer[15];
char t2_buffer[15];
String pres = dtostrf(p, 0, 5, p_buffer);
String temp2 = dtostrf(t2, 0, 2, t2_buffer);
Serial.println(pres);
Serial.println(t2);
Serial.println("------------------------");
// }
//----------------
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println("...disconnected");
Serial.println();
client.stop();
}
// Update ThingSpeak
if (!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
{
updateThingSpeak("field1=" + temp2 + "&field2=" + humid + "&field3=" + pres);
}
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 ) {
startEthernet();
}
lastConnected = client.connected();
}
void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + writeAPIKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
lastConnectionTime = millis();
if (client.connected())
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();
failedCounter = 0;
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak failed (" + String(failedCounter, DEC) + ")");
Serial.println();
}
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak Failed (" + String(failedCounter, DEC) + ")");
Serial.println();
lastConnectionTime = millis();
}
}
void startEthernet()
{
client.stop();
Serial.println("Connecting Arduino to network...");
Serial.println();
delay(1000);
// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
else
{
Serial.println("Arduino connected to network using DHCP");
Serial.println();
}
delay(1000);
}
Kod:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#define am 8 //anemometer is connected to pin 8 with pulldown resistor
int cnt = 0; //counter
int rot = 0; // number of rotations
boolean stan = 0; //state of anemometer -open or closed
float wind;
void setup() {
pinMode (am, INPUT);
Serial.begin(9600);
}
void loop() {
cnt = cnt + 1;
if (digitalRead(am) == LOW) { //I've checked that on free position anemometer is always closed
delay(2);
stan = !stan;
while (digitalRead(am) == LOW) ;
rot = rot + 1 ;
}
if (cnt == 100) {
//int czas1=millis();
wind = rot * 2.40114; ///5; //equation calculating average speed of the wind in 3 sec intervals
Serial.println(rot);
Serial.print("Wind speed = ");
Serial.print(wind, 2);
Serial.println(" km/h");
cnt = 0; //counter reset
rot = 0; //rotation number reset
//int czas2 =millis();
//int czas=czas2-czas1;
//Serial.println(czas);
}
delay(8); //this delay is put here to extend time of whole loop to 10ms
}
W wyniku moich wypocin stworzyłem taki szkic. Niestety działa niepoprawnie. Czy ktoś mógł by spojrzeć gdzie tkwi błąd?
Kod:
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>
#include <Wire.h>
#include "Adafruit_BMP085.h" // This is the version 1 library
#define DHTPIN A0
#define DHTTYPE 11// The Temperature/Humidity sensor
#define am 8
Adafruit_BMP085 bmp;
DHT dht(DHTPIN, DHTTYPE);
int cnt = 0; //counter
int rot = 0; // number of rotations
boolean stan = 0; //state of anemometer -open or closed
float wind;
// Local Network Settings
byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "AHAGS1F5IPQCATX";
const int updateThingSpeakInterval = 16 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
// Initialize Arduino Ethernet Client
EthernetClient client;
void setup()
{
// Start Serial for debugging on the Serial Monitor
Serial.begin(9600);
pinMode (am, INPUT);
startEthernet();
}
void loop()
{
if (client.available())
{
char c = client.read();
Serial.print(c);
}
{
cnt = cnt + 1;
if (digitalRead(am) == LOW) { //I've checked that on free position anemometer is always closed
delay(2);
stan = !stan;
while (digitalRead(am) == LOW) ;
rot = rot + 1 ;
}
if (cnt == 100) {
//int czas1=millis();
wind = rot * 2.40114; ///5; //equation calculating average speed of the wind in 3 sec intervals
cnt = 0; //counter reset
rot = 0; //rotation number reset
//int czas2 =millis();
//int czas=czas2-czas1;
//Serial.println(czas);
}
delay(8); //this delay is put here to extend time of whole loop to 10ms
}
//------DHT11--------
int chk = dht.read(DHTPIN);
//char t_buffer[10];
char h_buffer[10];
//float t=dht.readTemperature();
//String temp=dtostrf(t,0,5,t_buffer);
//Serial.println(temp);
//Serial.print(" ");
float h = dht.readHumidity();
String humid = dtostrf(h, 0, 5, h_buffer);
//-----BMP180-----------
bmp.begin();
float p = (bmp.readPressure() / 100.0); //this is for pressure in hectoPascal
float t2 = (bmp.readTemperature());
char p_buffer[15];
char t2_buffer[15];
String pres = dtostrf(p, 0, 5, p_buffer);
String temp2 = dtostrf(t2, 0, 2, t2_buffer);
char wind_buffer[5];
String w = dtostrf(wind,0,5,wind_buffer);
Serial.println(pres);
Serial.println(t2);
Serial.println(w);
Serial.println(humid);
Serial.println("------------------------");
delay(1500);
Serial.println(pres);
Serial.println(t2);
Serial.println(rot);
Serial.println(humid);
delay(1500);
Serial.println("------------------------");
// }
//----------------
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println("...disconnected");
Serial.println();
client.stop();
}
// Update ThingSpeak
if (!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
{
updateThingSpeak("field1=" + temp2 + "field2=" + humid + "field3=" + pres + "field4=" + rot);
}
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 ) {
startEthernet();
}
lastConnected = client.connected();
}
void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + writeAPIKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
lastConnectionTime = millis();
if (client.connected())
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();
failedCounter = 0;
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak failed (" + String(failedCounter, DEC) + ")");
Serial.println();
}
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak Failed (" + String(failedCounter, DEC) + ")");
Serial.println();
lastConnectionTime = millis();
}
}
void startEthernet()
{
client.stop();
Serial.println("Connecting Arduino to network...");
Serial.println();
delay(1000);
// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
else
{
Serial.println("Arduino connected to network using DHCP");
Serial.println();
}
delay(1000);
}