Omet navegació

14.1) Pasem dades al telèfon

Servidor web simple

Anem a agafar el muntatge del DHT22 per fer que la targeta «Nano 33 IoT» es convertisca en una mini estació meteorològica.

Esquema DHT22 + Nano 33 IoT

Copia el programa aquest. No el carregues encara a la teua Nano.

/*
 WiFi Web Server send data from a DHT22

 Circuit:
 * Nano 33 IoT
 * DHT22

 created for arduino 25 Nov 2012
 by Tom Igoe

ported for sparkfun esp32 
31.01.2017 by Jan Hendrik Berlin

Modificat per Carles Ferrando per canviar la ESP32 per la «Arduino Nano 33 IoT» , esborrar el codi del led
i arreglar una mica la presentació de la pagina 
 */
#include <WiFiNINA.h>
WiFiServer server(80);

#include "arduino_secrets.h" 
//please enter your sensitive data in the Secret tab
char ssid[] = SECRET_SSID;                // your network SSID (name)
char pass[] = SECRET_PASS;                // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;             // the Wi-Fi radio's status
int ledState = LOW;                       //ledState used to set the LED
unsigned long previousMillisInfo = 0;     //will store last time Wi-Fi information was updated
unsigned long previousMillisLED = 0;      // will store the last time LED was updated
const int intervalInfo = 5000;            // interval at which to update the board information

//Posem llibreries i variable per al sensor DHT22
#include <DHT.h>
DHT dht_19_DHT22(2,DHT22);
int TempC;
int Humitat;

void setup() {
  // declarem el sensor de T i H
  dht_19_DHT22.begin(); 
   delay(10);
   
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial);

  // set the LED as output
  pinMode(LED_BUILTIN, OUTPUT);

  // attempt to connect to Wi-Fi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to network: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

   Serial.println("");
    Serial.println("WiFi connected.");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
   
  // you're connected now, so print out the data:
  Serial.println("You're connected to the network");
  Serial.println("---------------------------------------");
  
  server.begin();
}

void loop() {
  WiFiClient client = server.available();   // listen for incoming clients
 
 if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();
             client.println("<BODY style='background-color:white'>");
             client.println("<font style='color:green'>");
             client.println("<font style='font-family:Liberation Serif'>");
             client.println("<center>");
             client.println("<H1 style='font-size:400%;'>Temperatura i Humitats remotes</H1>");
             client.println("<br />");
             client.println("<br />"); 
             
       // Calculs del sensor DHT22
            TempC = (int)(dht_19_DHT22.readTemperature());
           Humitat = (int)(dht_19_DHT22.readHumidity());
 
            // escrivim la Temperatura i Humitat a la web
           client.print("<br><span style=font-size:4em> Temperatura C = ");
           client.print(TempC);
           client.print("</span>");

            client.print("<br><span style=font-size:4em> Humitat % = ");
            client.print(Humitat);
            client.print("</span>");
      
             client.println("</BODY>");
             client.println("</HTML>");
      
            // The HTTP response ends with another blank line:
                       client.println();
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }


      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}// final programa
 

Guarda-lo amb el nom Nano_WifiDades dins la carepta Arduino del teu ordinador.

Crea dins la carpeta Nano_WifiDades un fitxer que es diga arduino_secrets.h amb les dues ratlles següents de contingut:

#define SECRET_SSID "El teu nom de la xarxa wifi o SSID"
#define SECRET_PASS "La contrasenya de la teua wifi"

Mira la imatge per veure com quedarà:

arduino_secrets.h

Carrega el programa ara i mira el monitor serie:

Connexió Wifi DHT22

Ara entra en eixa adreça IP mostrada amb l'ordinador:

Servidor amb Nano 33IoT + DHT22

També pots entrar amb el telèfon:

Servidor Nano 33 IoT des de mòbil

Per suposat pots connectar altres sensors i passar dades al teu mòbil.