Sensore di Temperatura e Umidità DHT11 · char server[] = "itismajo.altervista.org"; //IPAddress...
Transcript of Sensore di Temperatura e Umidità DHT11 · char server[] = "itismajo.altervista.org"; //IPAddress...
Area di Progetto di Toto Alex 5Binfo a.s. 2017/18 – ITI E. Majorana
Grugliasco (TO)
Stazione Meteo con Arduino, Anemometro Direzione/Velocita Vento e sensore di temperatura
Sensore di Temperatura e Umidità DHT11
Link per documentazione
https://playground.arduino.cc/Main/DHT11Lib
http://www.mauroalfieri.it/elettronica/dht11-arduino.html
http://www.geeetech.com/wiki/index.php/Electric_thermometer_by_using_DHT11_sensor_module
Anemometro WS2300-15
Link documentazione: http://forum.abacom-online.de/phpBB3/viewtopic.php?f=52&t=3968
Link alternativo (TX20-Principio di funzionamento molto simile e spiegazione più corretta):
https://www.john.geek.nz/2011/07/la-crosse-tx20-anemometer-communication-protocol/
SW Arduino proposto da un forum:
// Wind Sensor WS 2300-15 adapter
// Read sensor data from Digital Input D7 and sends to to PC (UART)
int senspin = 7; // Arduini Digital Pin 7 (D7 on AVR)
boolean stat; // data input status
word dataword; // what we print to PC
// Sensor uses 800 Bd. -> 1.25 ms/bit
// tested with 16 Mhz board...
int bit_ms = 1250; // Duration of 1 bit in µS
int half_bit_ms = 625; // Duration of 1/2 bit in µS
int ledpin = 13; // Pin 13 has an LED connected on most Arduino boards.
// Setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(senspin, INPUT); // Use D7 to read sensor signal
digitalWrite(senspin, HIGH); // turn the pullup on!
pinMode(ledpin, OUTPUT); // Use B5 for Data Indicator LED
Serial.begin(9600); // Set the baud rate for PC
communication
}
// Main loop:
void loop() {
dataword = 0; // Prepare data word
stat = digitalRead(senspin); // get the sensor input
while(stat == HIGH){ // wait for falling edge (sync to
startbit)
stat = digitalRead(senspin);
}
delayMicroseconds(half_bit_ms); //wait for MIDDLE of startbit, we are
now in sync
// Skip the start bit, we now it ist there ;-)
delayMicroseconds(bit_ms);
// Skip four header bits, we don´t want these...
delayMicroseconds(bit_ms);
delayMicroseconds(bit_ms);
delayMicroseconds(bit_ms);
delayMicroseconds(bit_ms);
// Now the 13 bits we want...
// *** Dataword [0..3] = WIND DIRECTION (LSB first)
// *** Dataword [4..12] = WIND SPEED (LSB first)
for (int i=0; i <= 13; i++){
stat = digitalRead(senspin);
if (stat) {
dataword = dataword + (1 << i);
}
delayMicroseconds(bit_ms); // Wait for next bit
}
Serial.println(dataword); // Print out Dataword as readable ASCII
string to PC
digitalWrite(ledpin, HIGH); // Sensor Data Indicaror LED on
delay(200); // Spent some time, to make sure finished
tranmittion
digitalWrite(ledpin, LOW); // Sensor Data Indicaror LED on
}
Dati acquisiti con software proposto dal forum:
Solo direzione vento 0=NORD - 15=N/NW
Dati acquisiti con il mio software (vedi dopo)
Dati con Direzione vento=N (0000) e N/NW (1111) dove DDDD dal 6 bit in poi.
Velocità vento=0 m/s. Dati campionati=30. Gli ultimi 6 bit indicano che il sensore non sta trasmettendo!
Dati soffiando sulla rotellina (VV<>0) e Direzione vento=N/NW=15=1111
////Autore: Vincenzo Maida
//Data: 18/04/2018 - Torino (Italy)
//Acquisizione dati da Anemometro W2003-15 (Direzione Vento e Velocità
Vento)
//Direzione Vento 4 bit. Da 0000=Nord a 1111=N/NW 16 possibili
combinazioni
//Velocità vento: 5 bit. Da 00000=0 m/s a 11111=50 m/s o 180 Km/h (
risluzione 0,1 m/s) ... Da verificare
//Monitoraggio dati acquisiti e variabili interne su monitor seriale
int pinTX=7; //pin sensore di Anemometro
int dati[30];
int i=0;
//variabili gestione monitor
unsigned long tempoCorrente,tempoIniziale;
// the setup routine runs once when you press reset:
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
pinMode(pinTX,INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
while(digitalRead(pinTX)==HIGH){
//Attendi
}
delayMicroseconds(625);
for(i=0; i<30; i++){
dati[i]=digitalRead(pinTX);
delayMicroseconds(1250);
}
stampa();
}
void stampa(){
Serial.println("Stampa acquisizioni:");
for(i=0;i<30;i++){
Serial.print(dati[i]);
Serial.print(" ");
}
Serial.println();
}
CONVERTITORE BINARIO DECIMALE
Utility per la conversione dei dati binari in decimale dell’anemometro.
Codice Arduino (con simulazione)
//Autore: Vincenzo Maida
//Data: 18/05/2018 - Torino (Italy)
//Acquisizione dati da Anemometro W2003-15 (Direzione Vento e Velocità Vento)
//Direzione Vento 4 bit. Da 0000=Nord a 1111=N/NW 16 possibili combinazioni
//Velocità vento: 12 bit. Da 00000=0 m/s a 00011111111=511=51,1 m/s o 180 Km/h (
risluzione 0,1 m/s). Nota: BIT utili 9. Gli ultimi tre (MSB) sono sempre a ZERO
... Da verificare
//Monitoraggio dati acquisiti e variabili interne su monitor seriale
int pinTX=7; //pin sensore di Anemometro
//Vettore di acquisizione (inizializzato per simulazione)
int dati[30]={0,1,0,1,0, //preambolo. Sempre uguale. (0-1-2-3-4)
0,0,0,1, //Direz. vento. 4 bit. dati[5]=LSB; dati[8]=MSB (5-6-
7-8)
1,1,1,1, //Velocità Vento: 12 bit. dati[9]=LSB; dati[20]=MSB
(9-10-11-12)
1,1,1,1, //VV: (13-14-15-16)
1,0,0,0, //VV: i bit dati[18], dati[19] e dati[20] sono sempre
a ZERO. Da verificare! (17-18-19-20)
1,1,1,1, //Check sum: 4 bit; somma di DV e parti di VV (da
capire come funziona!)(21-22-23-24)
1,1,1,1,1}; //Stato alto per più di 1 secondo (dati NON
utili!)(25-26-27-28-29).NOTA si può accorciare il vettore di acq. portandolo a
24/25 valori
int i=0;
//variabili gestione monitor
unsigned long tempoCorrente,tempoIniziale;
// the setup routine runs once when you press reset:
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
pinMode(pinTX,INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
//Ciclo di acquisizione
/*
while(digitalRead(pinTX)==HIGH){
//Attendi
}
delayMicroseconds(625);
for(i=0; i<30; i++){
dati[i]=digitalRead(pinTX);
delayMicroseconds(1250);
}
*/
// simula();
stampa();
delay(10000);
}
void stampa(){
Serial.println("Stampa acquisizioni Direzione Vento (4 bit) indice da 5 a 8
dove 5=LSB e 8=MSB. Dato binario da leggere da destra verso sinistra:");
for(i=5;i<=8;i++){
Serial.print(dati[i]);
Serial.print(" ");
}
//Serial.println();
Serial.print(" = ");
Serial.print(bintodec(5,8));
Serial.print(" DEC in gradi--> ");
Serial.println(bintodec(5,8)*22.5);
Serial.println("Stampa acquisizioni Velocità Vento (12 bit) indice da 9 a 20
dove 9=LSB e 12=MSB. Dato binario da leggere da destra verso sinistra:");
for(i=9;i<=20;i++){
Serial.print(dati[i]);
Serial.print(" ");
}
Serial.print(" = ");
Serial.print(bintodec(9,20));
Serial.print(" DEC in m/s--> ");
Serial.println(bintodec(9,20)*0.1);
}
void simula(){
for(i=0;i<30;i++){
dati[i]=random(2);
}
dati[5]=1;
dati[6]=0;
dati[7]=0;
dati[8]=1;
}
/*************************************************************
* Funzione che converte il vettore binario in decimale
* Usa array globale dati[];
* Inverte i bit: inizio=LSB e fine=MSB
* Input: indici di inizio e fine del vettore da convertire
* Output: dato convertito in decimale
**************************************************************/
int bintodec(int inizio,int fine){
int i=0;
int d=0; //numero in decimale
int e=1; //equivale a 2^0
for(i=inizio;i<=fine; i++){
d=d+dati[i]*e;
e=e*2;
}
return d;
}
Output Monitor Seriale
CLIENT WEB CON SHIELD WiFi ARDUINO
Bozza del programma che acquisisce i dati dai sensori e li invia
ciclicamente al server WEB che li memorizza nel DB
Dati simulati!
Codice Arduino UNO R3
/*
Repeating Wifi Web Client
This sketch connects to a a web server and makes a request
using an Arduino Wifi shield.
Circuit:
* WiFi shield attached to pins SPI pins and pin 7
created 23 April 2012
modified 31 May 2012
by Tom Igoe
modified 13 Jan 2014
by Federico Vanzati
http://www.arduino.cc/en/Tutorial/WifiWebClientRepeating
This code is in the public domain.
*/
#include <SPI.h>
#include <WiFi.h>
//LM 35
int sensorPin = A5;
int sensorValue = 0;
//Variabili per meteo
float vv=20.2; //Velocità vento in m/s
int dv=180; //Direzione vento in Gradi
float t=28.5; //Temperatura in °C
String query="GET
/salva_meteo.php?vv="+String(vv,DEC)+"&dv="+String(dv,DEC)+"&t="+String(t
,DEC)+" HTTP/1.1";
char ssid[] = "AndroidAP"; // your network SSID (name)
char pass[] = "prova2018"; // your network password
int keyIndex = 0; // your network key Index number (needed
only for WEP)
int status = WL_IDLE_STATUS;
// Initialize the Wifi client library
WiFiClient client;
// server address:
char server[] = "itismajo.altervista.org";
//IPAddress server(64,131,82,241);
unsigned long lastConnectionTime = 0; // last time you
connected to the server, in milliseconds
const unsigned long postingInterval = 60L * 1000L; // delay between
updates, in milliseconds. Ogni minuto
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv != "1.1.0") {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP
network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the status:
printWifiStatus();
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
while (client.available()) {
char c = client.read();
Serial.write(c);
}
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
if (client.connect(server, 80)) {
acquisizioneDati();
query="GET
/salva_meteo.php?vv="+String(vv,DEC)+"&dv="+String(dv,DEC)+"&t="+String(t
,DEC)+" HTTP/1.1";
Serial.println("connecting...");
Serial.print("query= ");
Serial.println(query);
// send the HTTP PUT request:
//client.println("GET /latest.txt HTTP/1.1");
client.println(query);
client.println("Host: itismajo.altervista.org");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void acquisizioneDati(){
randomSeed(analogRead(0));
t=random(500)/10.0;
vv=random(250)/10.0;
dv=random(360);
Serial.print("Temperatura:");
Serial.print(t);
Serial.print(" [°C] Velocita' vento: ");
Serial.print(vv);
Serial.print(" [m/s] Direzione Vento:");
Serial.print(dv);
Serial.println(" [Gradi]");
}
CLIENT WEB CON SHIELD ETHERNET
Alternativa “wired” alla WiFi
Bozza del client (non è ciclica. Da rivedere)
Codice Arduino UNO R3 (BOZZA)
/*
Web client
This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen
*/
#include <SPI.h>
#include <Ethernet.h>
//LM 35
int sensorPin = A5; // select the input pin for the potentiometer
int sensorValue = 0;
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the
shield
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x33, 0x67 };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "itismajo.altervista.org"; // name address for Google
(using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
// 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;
//Variabili per meteo
float vv=22.7; //Velocità vento in m/s
int dv=360; //Direzione vento in Gradi
float t=25.5; //Temperatura in °C
String query="GET
/salva_meteo.php?vv="+String(vv,DEC)+"&dv="+String(dv,DEC)+"&t="+String(t
,DEC)+" HTTP/1.1";
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
/*
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
*/
/*
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
Serial.println(query);
/*
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
//client.println("GET /post_helper.php?a=5&b=2 HTTP/1.1");
client.println(query);
client.println("Host: itismajo.altervista.org");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
*/
}
void loop() {
/*
}
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// read the value from the sensor:
*/
// do nothing forevermore:
// while (true){
sensorValue = analogRead(sensorPin);
Serial.print("Valore letto da A0:");
Serial.println(sensorValue);
Serial.print("Temperatura °C:");
Serial.println(sensorValue*0.488);
t=sensorValue*0.488;
inviaDati();
delay(50000);
// }
}
void inviaDati(){
query="GET
/salva_meteo.php?vv="+String(vv,DEC)+"&dv="+String(dv,DEC)+"&t="+String(t
,DEC)+" HTTP/1.1";
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
Serial.println(query);
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
//client.println("GET /post_helper.php?a=5&b=2 HTTP/1.1");
client.println(query);
client.println("Host: itismajo.altervista.org");
client.println("Connection: close");
client.println();
Serial.println("Fine trasmisione");
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
delay(1000);
// if there are incoming bytes available
// from the server, read them and print them:
while(client.available()) {
//Serial.println("In attesa di risposta: ");
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
}
PROGETTO HARDWARE
Il progetto potrebbe essere costituito da:
Una Scheda Arduino UNO R3 (dim. Max 53x68x15h mm)
Una shield WiFi Arduino (connessione Internet con il server web)
Un scheda custom (circuiti per anemometro e sensori+connettori)
Una pila da 9V per alimentare il tutto
Una scatola stagna (contenitore del tutto)
Un anemometro SW2003-15
Un sensore di umidità e temperature digitale tipo: ??
Scheda custom Basetta millefori di dimensioni simili a Arduino UNO R3
1 transistor NPN tipo BC107 o equ.
1 Resistenza da 470 Ohm 1/4W
1 Resistenza da 4,7 KOhm 1/4W
2 o 4 Connettori a pettine per innesto su Arduino UNO
1 presa Jack femmina stereo per c.s. (ingresso sensore temp/umid)
1 presa RJ11 o RJ45 per c.s. (connessione anemometro più eventuale
alim. Estrerna (solo RJ45)
Alloggiamento per pila 9V da c.s. con contatti (facoltativo)
Uno spinotto a 90 gradi (basso ingombro) per alimentare Arduino UNO
con pila
Contatti per pila 9V con spinotto (in alternativa ai 2 punti
precedenti)
Uno spinotto stereo 3,5 mm per sensore temp/umid
Una spina RJ11 o RJ45 per anemometro
Figure:
RJ11 – 1 euro circa
RJ45 – 1 euro (in alternativa a RJ11)
Presa Jack da 3,5 mm per sensore temperatura/umidità. Costo 1,5 euro circa
Alloggiamento/connessione pila 9V (alimentazione) – circa 2 euro (senza pila)
Scatola stagna per contenere il tutto (dim appr. 70x100x100h mm). Costo circa 4
euro