Un shield ethernet ENC28J60 plus un capteur DHT 11
#include "EtherShield.h"
#include <stdlib.h>
#include <string.h>
#include <DHT.h>
static uint8_t mymac[6] = {
0x54,0x55,0x58,0x10,0x00,0x23};
static uint8_t myip[4] = {
192,168,0,25};
#define MYWWWPORT 80
#define BUFFER_SIZE 550
#define DHTPIN 2 // pin n°2 de l'arduino pour connecter le signal du DHT11
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
static uint8_t buf[BUFFER_SIZE+1];
staticfloat h, t;
int nnn=0 ;
char numstr[6];
EtherShield es=EtherShield();
uint16_t http200ok(void)
{
return(es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n")));
}
// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage(uint8_t *buf)
{
uint16_t plen;
plen=http200ok();
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<html><head><title>ENC28J60 Ethernet</title></head><body>"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
Température : "));
dtostrf(t,4,1,numstr);
plen=es.ES_fill_tcp_data(buf,plen,numstr);
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("*C"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
Humidité : "));
dtostrf(h,4,1,numstr);
plen=es.ES_fill_tcp_data(buf,plen,numstr);
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR(" %"));
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("
<a href=.>[mise à jour]</a> ")); // add a Refresh button
itoa(nnn,numstr,10);
plen=es.ES_fill_tcp_data(buf,plen,numstr);
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("</body></html>"));
nnn++;
return(plen);
}
void setup(){
Serial.begin(9600);
// Initialise SPI interface
es.ES_enc28j60SpiInit();
// initialize enc28j60
es.ES_enc28j60Init(mymac);
// init the ethernet/ip layer:
es.ES_init_ip_arp_udp_tcp(mymac,myip, MYWWWPORT);
dht.begin();
}
void loop(){
uint16_t plen, dat_p;
while(1) {
// read packet, handle ping and wait for a tcp packet:
dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
/* dat_p will be unequal to zero if there is a valid
* http get */
if(dat_p==0){
// no http request
continue;
}
// tcp port 80 begin
if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){
// head, post and other methods:
dat_p=http200ok();
dat_p=es.ES_fill_tcp_data_p(buf,dat_p,PSTR("<h1>200 xxxx OK</h1>"));
goto SENDTCP;
}
// just one web page in the "root directory" of the web server
if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){
dat_p=print_webpage(buf);
goto SENDTCP;
}
else{
dat_p=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 401 Unauthorized\r\nContent-Type: text/html\r\n\r\n<h1>401 Unauthorized</h1>"));
goto SENDTCP;
}
SENDTCP:
float h = dht.readHumidity();
float t = dht.readTemperature(); //ici
Serial.println("c");
es.ES_www_server_reply(buf,dat_p); // send web page data
// tcp port 80 end
}
}
Ai-je bien placé la lecture du capteur ?
Pas sûr, car la création de la page web se fait deux fois pour un seul refresh, preuve avec le Serial.println
Mais surprise si je ne demande que l’une ou l’autre des valeurs, c’est un seul passage.
Je ne sais pas non plus comment gérer les valeurs non conformes
Qui a une solution ?