74 lines
2.0 KiB
C++
74 lines
2.0 KiB
C++
#include <SPI.h>
|
|
#include <WiFiNINA.h>
|
|
#include "DHT.h"
|
|
|
|
#define DHTPIN 7
|
|
#define DHTTYPE DHT11
|
|
DHT dht(DHTPIN, DHTTYPE);
|
|
|
|
char ssid[] = ""; // your network SSID (name)
|
|
char pass[] = ""; // your network password (use for WPA, or use as key for WEP)
|
|
int keyIndex = 0 // your network key index number (needed only for WEP)
|
|
int status = WL_IDLE_STATUS;
|
|
|
|
IPAddress server(,,,); // numeric IP (no DNS)
|
|
//char server = ""; // name address (using DNS)
|
|
String PATH_NAME = "/search";
|
|
|
|
WiFiClient client;
|
|
|
|
void setup() {
|
|
//Initialize serial and wait for port to open:
|
|
Serial.begin(9600);
|
|
dht.begin();
|
|
while ( status != WL_CONNECTED) {
|
|
Serial.print("Attempting to connect to Network named: ");
|
|
Serial.println(ssid); // print the network name (SSID);
|
|
// Connect to WPA/WPA2 network:
|
|
status = WiFi.begin(ssid, pass);
|
|
}
|
|
// 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);
|
|
}
|
|
|
|
void loop(){
|
|
// 30sec measurement interval
|
|
delay(30000);
|
|
|
|
float h = dht.readHumidity();
|
|
float t = dht.readTemperature();
|
|
|
|
String queryString = String("?humidity=") + String(h) + String("&temperature=") + String(t);
|
|
|
|
if (client.connect(server, 80)) {
|
|
Serial.println("MAKE A HTTP REQUEST");
|
|
Serial.println(queryString);
|
|
} else {
|
|
Serial.println("connection failed");
|
|
}
|
|
// send HTTP request header
|
|
client.println("GET "+ PATH_NAME + queryString + " HTTP/1.1");
|
|
client.println("Host: google.com");
|
|
client.println("Connection: close");
|
|
client.println(); // end HTTP request header
|
|
//send HTTP body
|
|
client.println(queryString);
|
|
|
|
while(client.available())
|
|
{
|
|
// read an incoming byte from the server and print them to serial monitor:
|
|
char c = client.read();
|
|
Serial.print(c);
|
|
}
|
|
if(!client.connected())
|
|
{
|
|
// if the server's disconnected, stop the client:
|
|
Serial.println("disconnected");
|
|
client.stop();
|
|
}
|
|
} |