Paso 5: Códigos de Arduino
El siguiente fragmento de código lee la temperatura y humedad:
// DHT setting#include "DHT.h"#define DHTPIN 2 // what pin we're connected to #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHTTYPE);
void setup() {...dht.begin();...}
void loop() {... float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); ... }
Esta solicitud enviar datos al servidor Losant:
char hostname[] = "triggers.losant.com"; char feeduri[] = "your losant webhook uri";
void loop() {...structureWebhookRequest(content);...}
void structureWebhookRequest(String content) { // close any connection before send a new request. // This will free the socket on the WiFi shield wifiClient.stop(); String contentType = "application/json"; // if there's a successful connection: if (wifiClient.connect(hostname, 443)) { wifiClient.print("POST "); //Do a POST wifiClient.print(feeduri); // On the feedURI wifiClient.println(" HTTP/1.1"); wifiClient.print("Host: "); wifiClient.println(hostname); //with hostname header wifiClient.println("Connection: close"); wifiClient.print("Content-Type: "); wifiClient.println(contentType); wifiClient.print("Content-Length: "); wifiClient.println(content.length()); wifiClient.println(); wifiClient.println(content); wifiClient.println(); #ifdef DEBUG Serial.println(content); #endif } else { // if you couldn't make a connection: #ifdef DEBUG Serial.println(); Serial.println("connection failed"); #endif }