Fasensor: Difference between revisions
From Futuragora Wiki
| Line 22: | Line 22: | ||
==Fasensor Code== | ==Fasensor Code== | ||
<pre> | <pre> | ||
// | //FA Sensor 2 V2.0.1 4/8/2018 msantos 13/08/2018 reset | ||
#include <avr/wdt.h> | |||
#include <Wire.h> | #include <Wire.h> | ||
#include <Adafruit_BMP085.h> | #include <Adafruit_BMP085.h> | ||
#include "DHT.h" | #include "DHT.h" | ||
#include <SPI.h> | |||
#include <Ethernet.h> | |||
#define DHTPIN A1// what digital pin we're connected to | #define DHTPIN A1// what digital pin we're connected to | ||
// Uncomment whatever type you're using! | // Uncomment whatever type you're using! | ||
| Line 38: | Line 41: | ||
DHT dht(DHTPIN, DHTTYPE); | DHT dht(DHTPIN, DHTTYPE); | ||
Adafruit_BMP085 bmp; | Adafruit_BMP085 bmp; | ||
// Enter a MAC address for your controller below. | // Enter a MAC address for your controller below. | ||
byte mac[] = { 0xFE, 0xED, 0xCE, 0xEF, 0xCE, 0xED }; | byte mac[] = { 0xFE, 0xED, 0xCE, 0xEF, 0xCE, 0xED }; | ||
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS) | //IPAddress server(74,125,232,128); // numeric IP for Google (no DNS) | ||
char server[] = "api.thingspeak.com"; | char server[] = "api.thingspeak.com"; | ||
// Set the static IP address to use if the DHCP fails to assign | // Set the static IP address to use if the DHCP fails to assign | ||
IPAddress ip(192,168,1,110); | IPAddress ip(192,168,1,110); | ||
// Initialize the Ethernet client library | // Initialize the Ethernet client library | ||
// with the IP address and port of the server | // with the IP address and port of the server | ||
// that you want to connect to (port 80 is default for HTTP): | // that you want to connect to (port 80 is default for HTTP): | ||
EthernetClient client; | EthernetClient client; | ||
unsigned long LastTimeRun = 0; // Last time inverter analysis has run | |||
/// const int BatVoltagePin = A0; | |||
///float BatVoltage ; | |||
///int Namostras = 10; // number of samples | |||
///int VPinMedio; | |||
void setup() { | void setup() { | ||
Serial.begin(9600); | Serial.begin(9600); | ||
Serial.println(F("FA Sensor serial at 9600")); | |||
Serial.println(F("created by futuragora.pt")); | |||
Serial.println(""); | |||
if (!bmp.begin()) { | if (!bmp.begin()) { | ||
Serial.println("Could not find a valid BMP085 sensor, check wiring!"); | Serial.println(F("Could not find a valid BMP085 sensor, check wiring!")); | ||
while (1) {} | while (1) {} | ||
} | } | ||
| Line 63: | Line 73: | ||
} | } | ||
if (Ethernet.begin(mac) == 0) { | if (Ethernet.begin(mac) == 0) { | ||
Serial.println("Failed to configure Ethernet using DHCP"); | Serial.println(F("Failed to configure Ethernet using DHCP")); | ||
// no point in carrying on, so do nothing forevermore: | // no point in carrying on, so do nothing forevermore: | ||
// try to congifure using IP address instead of DHCP: | // try to congifure using IP address instead of DHCP: | ||
| Line 69: | Line 79: | ||
} | } | ||
delay(1000); // give the Ethernet shield a second to initialize: | delay(1000); // give the Ethernet shield a second to initialize: | ||
Serial.println(" | Serial.println(F("Testing IP connection...")); | ||
// if you get a connection, report back via serial: | // if you get a connection, report back via serial: | ||
if (client.connect(server, 80)) { | if (client.connect(server, 80)) { | ||
Serial.println(" | Serial.println("Conection test: OK"); | ||
Serial.println(" | Serial.println("Disconnecting."); | ||
client.stop(); | client.stop(); | ||
} | } | ||
else { | else { | ||
Serial.println("connection failed"); // kf you didn't get a connection to the server: | Serial.println("connection failed"); // kf you didn't get a connection to the server: | ||
} | } | ||
} | dht.begin(); | ||
void loop() | } | ||
void loop() | |||
{ | { | ||
if ((LastTimeRun + 360000) < millis()) | |||
{ // Main routine running each 60 seconds | |||
LastTimeRun = millis(); | |||
float h = dht.readHumidity(); | float h = dht.readHumidity(); | ||
{ | { | ||
if (client.connect(server, 80)) { | if (client.connect(server, 80)) { | ||
// sendig data to server | // sendig data to server | ||
client.print("GET https://api.thingspeak.com/update?api_key= | Serial.println(F("")); | ||
client.print("field1="); | Serial.println(F("-----------------------------")); | ||
Serial.print(F("Connected: ")); | |||
Serial.println(Ethernet.localIP()); | |||
Serial.println(F("Sending to ... FASensor1 ")); | |||
Serial.println(F("")); | |||
client.print("GET https://api.thingspeak.com/update?api_key=TESTKEY&"); | |||
client.print("field1="); | |||
client.print(bmp.readTemperature()); | client.print(bmp.readTemperature()); | ||
client.print("&&"); | client.print("&&"); | ||
| Line 98: | Line 117: | ||
client.print("field3="); | client.print("field3="); | ||
client.print(dht.readHumidity()); | client.print(dht.readHumidity()); | ||
client.println(); | /// client.print("&&"); | ||
/// client.print("field4="); | |||
/// client.print(12.4); | |||
client.println(); | |||
client.println(); | client.println(); | ||
client.stop(); | client.stop(); | ||
Serial.print(F("Temperature = ")); | |||
Serial.print(bmp.readTemperature()); | |||
Serial.println(F(" oC ")); | |||
Serial.print(F("Pressure = ")); | |||
Serial.print(bmp.readPressure()); | |||
Serial.println(F(" ")); | |||
Serial.print(F("Humidity = ")); | |||
Serial.print(dht.readHumidity()); | |||
Serial.println(F(" ")); | |||
Serial.println(F("")); | |||
Serial.println(F("Finish sending, disconnecting.")); | |||
Serial.println(F("-----------------------------")); | |||
Serial.println(F(" ")); | |||
} | } | ||
else { | else { | ||
Serial.println("connection failed"); | Serial.println(F("connection failed")); | ||
} | } | ||
} | |||
} | |||
} | |||
</pre> | </pre> | ||
Revision as of 14:57, 13 August 2018
Fasensor é um sensor montado a partir de um arduino, com sensor de temperatura, humidade e pressão atmosférica. Pode consultar o projecto em:
Dados recolhidos: Temperatura, Humidade, Pressão Atmosférica
Componentes
Arduino ethernet
BMP085
DHT11
Fasensor Code
//FA Sensor 2 V2.0.1 4/8/2018 msantos 13/08/2018 reset
#include <avr/wdt.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#define DHTPIN A1// what digital pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
// Connect VCC of the BMP085 sensor to 3.3V (NOT 5.0V!)
// Connect GND to Ground
// Connect SCL to i2c clock - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 5
// Connect SDA to i2c data - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 4
// EOC is not used, it signifies an end of conversion
// XCLR is a reset pin, also not used here
DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;
// Enter a MAC address for your controller below.
byte mac[] = { 0xFE, 0xED, 0xCE, 0xEF, 0xCE, 0xED };
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "api.thingspeak.com";
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,1,110);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
unsigned long LastTimeRun = 0; // Last time inverter analysis has run
/// const int BatVoltagePin = A0;
///float BatVoltage ;
///int Namostras = 10; // number of samples
///int VPinMedio;
void setup() {
Serial.begin(9600);
Serial.println(F("FA Sensor serial at 9600"));
Serial.println(F("created by futuragora.pt"));
Serial.println("");
if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP085 sensor, check wiring!"));
while (1) {}
}
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
if (Ethernet.begin(mac) == 0) {
Serial.println(F("Failed to configure Ethernet using DHCP"));
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
delay(1000); // give the Ethernet shield a second to initialize:
Serial.println(F("Testing IP connection..."));
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("Conection test: OK");
Serial.println("Disconnecting.");
client.stop();
}
else {
Serial.println("connection failed"); // kf you didn't get a connection to the server:
}
dht.begin();
}
void loop()
{
if ((LastTimeRun + 360000) < millis())
{ // Main routine running each 60 seconds
LastTimeRun = millis();
float h = dht.readHumidity();
{
if (client.connect(server, 80)) {
// sendig data to server
Serial.println(F(""));
Serial.println(F("-----------------------------"));
Serial.print(F("Connected: "));
Serial.println(Ethernet.localIP());
Serial.println(F("Sending to ... FASensor1 "));
Serial.println(F(""));
client.print("GET https://api.thingspeak.com/update?api_key=TESTKEY&");
client.print("field1=");
client.print(bmp.readTemperature());
client.print("&&");
client.print("field2=");
client.print(bmp.readPressure());
client.print("&&");
client.print("field3=");
client.print(dht.readHumidity());
/// client.print("&&");
/// client.print("field4=");
/// client.print(12.4);
client.println();
client.println();
client.stop();
Serial.print(F("Temperature = "));
Serial.print(bmp.readTemperature());
Serial.println(F(" oC "));
Serial.print(F("Pressure = "));
Serial.print(bmp.readPressure());
Serial.println(F(" "));
Serial.print(F("Humidity = "));
Serial.print(dht.readHumidity());
Serial.println(F(" "));
Serial.println(F(""));
Serial.println(F("Finish sending, disconnecting."));
Serial.println(F("-----------------------------"));
Serial.println(F(" "));
}
else {
Serial.println(F("connection failed"));
}
}
}
}
Code Test sensors
#include <Wire.h>
#include <Adafruit_BMP085.h>
/***************************************************
This is an example for the BMP085 Barometric Pressure & Temp Sensor
Designed specifically to work with the Adafruit BMP085 Breakout
----> https://www.adafruit.com/products/391
These displays use I2C to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include "DHT.h"
#define DHTPIN A1 // what digital pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
// Connect VCC of the BMP085 sensor to 3.3V (NOT 5.0V!)
// Connect GND to Ground
// Connect SCL to i2c clock - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 5
// Connect SDA to i2c data - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 4
// EOC is not used, it signifies an end of conversion
// XCLR is a reset pin, also not used here
DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}
// fast integer version with rounding
//int Celcius2Fahrenheit(int celcius)
//{
// return (celsius * 18 + 5)/10 + 32;
//}
//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
return celsius + 273.15;
}
// dewPoint function NOAA
// reference (1) : http://wahiduddin.net/calc/density_algorithms.htm
// reference (2) : http://www.colorado.edu/geography/weather_station/Geog_site/about.htm
//
double dewPoint(double celsius, double humidity)
{
// (1) Saturation Vapor Pressure = ESGG(T)
double RATIO = 373.15 / (273.15 + celsius);
double RHS = -7.90298 * (RATIO - 1);
RHS += 5.02808 * log10(RATIO);
RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1/RATIO ))) - 1) ;
RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
RHS += log10(1013.246);
// factor -3 is to adjust units - Vapor Pressure SVP * humidity
double VP = pow(10, RHS - 3) * humidity;
// (2) DEWPOINT = F(Vapor Pressure)
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558 - T);
}
// delta max = 0.6544 wrt dewPoint()
// 6.9 x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity*0.01);
double Td = (b * temp) / (a - temp);
return Td;
}
void setup() {
Serial.begin(9600);
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}
}
void loop() {
Serial.print("Humidity = ");
Serial.print(dht.readHumidity());
Serial.println(" *H");
Serial.print("Temperature = ");
Serial.print(bmp.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bmp.readPressure());
Serial.println(" Pa");
// Calculate altitude assuming 'standard' barometric
// pressure of 1013.25 millibar = 101325 Pascal
Serial.print("Altitude = ");
Serial.print(bmp.readAltitude());
Serial.println(" meters");
// you can get a more precise measurement of altitude
// if you know the current sea level pressure which will
// vary with weather and such. If it is 1015 millibars
// that is equal to 101500 Pascals.
Serial.print("Real altitude = ");
Serial.print(bmp.readAltitude(101500));
Serial.println(" meters");
Serial.println();
delay(5000);
}
PHP and Mysql
Create a database with three tables: temperature, humidity, pressure. Use the following code:
CREATE TABLE `fasensor7`.`temperature` ( `id` INT( 255 ) NOT NULL AUTO_INCREMENT , `timestamp` VARCHAR( 255 ) NOT NULL , `temperature` VARCHAR( 255 ) NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM ; CREATE TABLE `fasensor7`.`pressure` ( `id` INT( 255 ) NOT NULL AUTO_INCREMENT , `timestamp` VARCHAR( 255 ) NOT NULL , `pressure` VARCHAR( 255 ) NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM ; CREATE TABLE `fasensor7`.`humidity` ( `id` INT( 255 ) NOT NULL AUTO_INCREMENT , `timestamp` VARCHAR( 255 ) NOT NULL , `temperature` VARCHAR( 255 ) NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM ; CREATE TABLE `fasensor7`.`light` ( `id` INT( 255 ) NOT NULL AUTO_INCREMENT , `timestamp` VARCHAR( 255 ) NOT NULL , `pressure` VARCHAR( 255 ) NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM ;
Add 2 files to your server: connect.php and add.php:
<?php
include("conec.php");
$link=Conection();
$timestamp=date('Y-m-d H:i:s');
mysql_query("insert into temperature (timestamp,temperature) values ('$timestamp', '".$_REQUEST["temperature"]."')");
mysql_query ("insert into pressure (timestamp,pressure) values ('$timestamp', '".$_REQUEST["pressure"]."')");
mysql_query ("insert into humidity (timestamp,humidity) values ('$timestamp', '".$_REQUEST["humidity"]."')");
mysql_query ("insert into temperature2 (timestamp,temperature2) values ('$timestamp', '".$_REQUEST["temperature2"]."')");
mysql_query ("insert into dew (timestamp,dew) values ('$timestamp', '".$_REQUEST["dew"]."')");
mysql_query ("insert into light (timestamp,light) values ('$timestamp', '".$_REQUEST["light"]."')");
?>
Code
Libraries
https://github.com/adafruit/Adafruit_Sensor
https://github.com/roltel/arduino-hub/tree/master/libraries/Adafruit_BMP085
Arduino Hub - https://github.com/roltel/arduino-hub
PHP Code Arduino Mysql
Create Table:
