Thingspeak: Suhu dan Humidity: Difference between revisions
From OnnoCenterWiki
Jump to navigationJump to search
Onnowpurbo (talk | contribs) Created page with " /* WriteMultipleFields Description: Writes values to fields 1,2,3 and 4 in a single ThingSpeak update every 20 seconds. Hardware: Arduino Ethernet !!..." |
Onnowpurbo (talk | contribs) No edit summary |
||
| Line 1: | Line 1: | ||
#include "ThingSpeak.h" | #include "ThingSpeak.h" | ||
#include <Ethernet.h> | #include <Ethernet.h> | ||
#include "secrets.h" | #include "secrets.h" | ||
#include "DHT.h" | |||
#define DHTPIN 2 // what digital pin we're connected to | |||
// Uncomment whatever type you're using! | |||
#define DHTTYPE DHT11 // DHT 11 | |||
// define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 | |||
DHT dht(DHTPIN, DHTTYPE); | |||
byte mac[] = SECRET_MAC; | byte mac[] = SECRET_MAC; | ||
// Set the static IP address to use if the DHCP fails to assign | // Set the static IP address to use if the DHCP fails to assign | ||
IPAddress ip(192, 168, 0, 177); | IPAddress ip(192, 168, 0, 177); | ||
IPAddress myDns(192, 168, 0, 1); | IPAddress myDns(192, 168, 0, 1); | ||
EthernetClient client; | EthernetClient client; | ||
unsigned long myChannelNumber = SECRET_CH_ID; | unsigned long myChannelNumber = SECRET_CH_ID; | ||
const char * myWriteAPIKey = SECRET_WRITE_APIKEY; | const char * myWriteAPIKey = SECRET_WRITE_APIKEY; | ||
void setup() { | void setup() { | ||
| Line 71: | Line 43: | ||
} | } | ||
// give the Ethernet shield a second to initialize: | // give the Ethernet shield a second to initialize: | ||
delay(1000); | delay(1000); | ||
dht.begin(); | |||
ThingSpeak.begin(client); // Initialize ThingSpeak | ThingSpeak.begin(client); // Initialize ThingSpeak | ||
} | } | ||
void loop() { | void loop() { | ||
float h = dht.readHumidity(); | |||
// Reading temperature or humidity takes about 250 milliseconds! | |||
float t = dht.readTemperature(); | |||
// Read temperature as Celsius (the default) | |||
if (isnan(h) || isnan(t) ) { | |||
Serial.println("Failed to read from DHT sensor!"); | |||
return; | |||
} | |||
// set the fields with the values | // set the fields with the values | ||
ThingSpeak.setField(1, | ThingSpeak.setField(1, t); | ||
ThingSpeak.setField(2, | ThingSpeak.setField(2, h); | ||
// write to the ThingSpeak channel | // write to the ThingSpeak channel | ||
| Line 93: | Line 72: | ||
Serial.println("Problem updating channel. HTTP error code " + String(x)); | Serial.println("Problem updating channel. HTTP error code " + String(x)); | ||
} | } | ||
delay(20000); // Wait 20 seconds to update the channel again | delay(20000); // Wait 20 seconds to update the channel again | ||
} | } | ||
Latest revision as of 11:24, 21 September 2019
#include "ThingSpeak.h"
#include <Ethernet.h>
#include "secrets.h"
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
// define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
byte mac[] = SECRET_MAC;
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);
EthernetClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
void setup() {
Ethernet.init(10); // Most Arduino Ethernet hardware
Serial.begin(115200); //Initialize serial
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
dht.begin();
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
float h = dht.readHumidity();
// Reading temperature or humidity takes about 250 milliseconds!
float t = dht.readTemperature();
// Read temperature as Celsius (the default)
if (isnan(h) || isnan(t) ) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// set the fields with the values
ThingSpeak.setField(1, t);
ThingSpeak.setField(2, h);
// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(20000); // Wait 20 seconds to update the channel again
}