0

I know this is a common question but the truth is every code is different. Sometimes it's hard to apply the concept to your code, it does not work. So please help me. What is wrong with this code? I got it from GitHub. I'm trying to compile it first before I can do a little modification, it sends temp and humidity data to ThingSpeak cloud servers.

#include <DHT.h> // Including library for dht
#include <ESP8266WiFi.h>
String api_key = "YOUR API KEY"; // Enter your Write API key from ThingSpeak
const char *ssid = "YOUR WIFI SSID"; // replace with your wifi ssid and wpa2 key
const char *pass = "YOUR WIFI PASSWORD";
const char* server = "api.thingspeak.com";
#define DHTPIN 1 // pin where the dht11 is connected
DHT dht;
WiFiClient client;
void setup() {
 Serial.begin(115200);
 delay(10);
 pinMode(2, OUTPUT);
 digitalWrite(2, 0);
 Serial.println("Connecting to ");
 Serial.println(ssid);
 WiFi.begin(ssid, pass);
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 }
 Serial.println("");
 Serial.println("WiFi connected");
}
void loop() {
 int chk = DHT.read(DHTPIN);
 float hum = DHT.humidity;
 float temp = DHT.temperature;
 if (isnan(hum) || isnan(temp)) {
 Serial.println("Failed to read from DHT sensor!");
 return;
 }
 if (client.connect(server, 80)) {
 // "184.106.153.149" or api.thingspeak.com
 String data_to_send = api_key;
 data_to_send += "&field1=";
 data_to_send += hum;
 data_to_send += "&field2=";
 data_to_send += temp;
 data_to_send += "\r\n\r\n";
 client.print("POST /update HTTP/1.1\n");
 client.print("Host: api.thingspeak.com\n");
 client.print("Connection: close\n");
 client.print("X-THINGSPEAKAPIKEY: " + api_key + "\n");
 client.print("Content-Type: application/x-www-form-
 urlencoded\n");
 client.print("Content-Length: ");
 client.print(data_to_send.length());
 client.print("\n\n");
 client.print(data_to_send);
 delay(1000);
 Serial.print("Temperature: ");
 Serial.print(temp);
 Serial.print(" degrees Celcius, Humidity: ");
 Serial.print(hum);
 Serial.println("%. Send to Thingspeak.");
 }
 client.stop();
 Serial.println("Waiting...");
 // ThingSpeak needs minimum 15 sec delay between updates, I've set it to 30 seconds
 delay(10000);
}

When I compile I get this error,

 Arduino: 1.8.10 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E 
 Module), 80 MHz, Flash, 
 Legacy (new can return nullptr), All SSL ciphers (most 
 compatible), 
 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"
 C:\Users\Agent 96\Desktop\thingspeak\thingspeak.ino: In function 'void loop()':
 thingspeak:37:20: error: expected primary-expression before '.' token
 int chk = DHT.read(DHTPIN);
 ^
 thingspeak:38:22: error: expected primary-expression before '.' token
 float hum = DHT.humidity;
 ^
 thingspeak:39:23: error: expected primary-expression before '.' token
 float temp = DHT.temperature;
 ^
 Multiple libraries were found for "DHT.h"
 Used: C:\Users\Agent
 Not used: C:\Users\Agent
 Multiple libraries were found for "ESP8266WiFi.h"
 Used: C:\Users\Agent
 exit status 1
 expected primary-expression before '.' token
 This report would have more information with
 "Show verbose output during compilation"
 option enabled in File -> Preferences.

Please help, I'm kind of a beginner.

dda
1,5951 gold badge12 silver badges17 bronze badges
asked Jan 31, 2020 at 3:36
0

2 Answers 2

1

You defined DHT as dht. So you'll have to replace those DHT with dht and you'll be good to go.

answered Jan 31, 2020 at 16:49
1

Change three lines right after loop() to these:

 int chk = dht.read(DHTPIN);
 float hum = dht.humidity;
 float temp = dht.temperature;

Change 'DHT' into lower case letters. DHT is the class and dht is the object of the DHT class.

Also you have multiple copy of these libraries: DHT.h and Esp8266WiFi.h. Check your cuurent directory where your Arduino code is and also Documents->Arduino->Libraries directory. These libraries should be in the Documents->Arduino->Libraries directory.

answered Jan 31, 2020 at 4:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.