Sending DHT11 data through Ethernet Shield on Thingspeak | Arduino | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
Sending DHT11 data through Ethernet Shield on Thingspeak
No permission to create posts
April 16, 2018
10:57 pm
Avatar
Syed_Tauqeer
New Member
Members
Forum Posts: 1
Member Since:
April 14, 2018
sp_UserOfflineSmall Offline

I am trying to send DHT11 data through Ethernet Shield W5100 on thingspeak. But my code's output is -999 for both temperature and humidity.It also shows -999 on serial monitor of Arduino IDE and also on thingspeak.Please help if someone knows the error in the code

#include <SPI.h>
#include <Ethernet.h>
#include <dht.h>
#define DHT11PIN A0
#define DHTTYPE DHT11
dht DHT11; // OK

 

// Local Network Settings
byte mac[] = { 0xD4, 0xA8, 0xE2, 0xFE, 0xA0, 0xA1 }; // Must be unique on local network
byte ip[] = {192,168,0,150 }; // Must be unique on local network
byte gateway[] = {192,168,0,1 };
byte subnet[] = { 255, 255, 255, 0 };

// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "1OU6VU71STLR"; // Write API Key for a ThingSpeak Channel
const int updateInterval =16*1000; // Time interval in milliseconds to update ThingSpeak

// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;

// Initialize Arduino Ethernet Client
EthernetClient client;

void setup()
{

Serial.begin(115200);
Ethernet.begin(mac, ip, gateway, subnet);
delay(1000);
Serial.print("ETHERNET SHIELD ip is : ");
Serial.println(Ethernet.localIP());
// Start Ethernet on Arduino
startEthernet();
}

void loop()
{

String analogPin0 = String(analogRead(A0), DEC); //OK

// Print Update Response to Serial Monitor
if (client.available())
{
char c = client.read();
Serial.print(c);
}
int chk=DHT11.read(DHT11PIN);
char t_buffer[10];
char h_buffer[10];
float t=(DHT11.temperature);
String temp=dtostrf(t,0,5,t_buffer);
Serial.print(temp);
Serial.print(" ");
float h=(DHT11.humidity);
String humid=dtostrf(h,0,5,h_buffer);
Serial.print(humid);

 

// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println();
Serial.println("...disconnected.");
Serial.println();

client.stop();
}

// Update ThingSpeak
if(!client.connected() && (millis() - lastConnectionTime > updateInterval))
{
updateThingSpeak("field1="+temp+"&field2="+humid);
}

lastConnected = client.connected();
}

void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
client.print("POST /update HTTP/1.1
");
client.print("Host: api.thingspeak.com
");
client.print("Connection: close
");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"
");
client.print("Content-Type: application/x-www-form-urlencoded
");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("

");

client.print(tsData);

lastConnectionTime = millis();

if (client.connected())
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();

failedCounter = 0;
}
else
{
failedCounter++;

Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}

}
else
{
failedCounter++;

Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();

lastConnectionTime = millis();
}
}

void startEthernet()
{

client.stop();

Serial.println("Connecting Arduino to network...");
Serial.println();

delay(1000);

// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
else {
Serial.println("Arduino connected to network using DHCP");
Serial.println();
Serial.println("Data being uploaded to THINGSPEAK Server.......");
Serial.println();
}

delay(1000);
}

May 1, 2018
3:37 pm
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 365
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

If the value is wrong on the serial monitor, its more likely a setup issue, connection issue, or library issue. I would simplify your example, and just get the dht output on the serial monitor working. There is probably a simple example that comes with the library you are using.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
31 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

JudestAboth, concettazx4, lyndarf60, caseybo16, iodiree, emiliacz4

Administrators: Hans: 387, lee: 457