Upload DHT11 sensor values using GSM 900 | 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
Upload DHT11 sensor values using GSM 900
No permission to create posts
February 13, 2018
6:18 pm
Avatar
seales

Silver
Members
Forum Posts: 10
Member Since:
February 13, 2018
sp_UserOfflineSmall Offline

Good day folks, I am currently building a smart irrigation project using the Arduino UNO. 
Basically, a soil moisture sensor starts and stop a 12V DC water pump. A DHT 11 temperature sensor is just used for indication. I was able to get this part functioning correctly. 

I would like to upload these values to thingspeak using a GSM 900 sim. I am able to get a connection to thingspeak and update the channel field but the temperature values are not accurate that is they are 1020 on the channel graph and 29, 28 etc on my Arduino serial monitor. Any help rectifying this would be greatly appreciated. Below is the code I tried thus far.

#include <SoftwareSerial.h>
#include <String.h>
#include <dht.h>
int sensorPin = 0;    // set the input pin for the potentiometer
int sensorValue = 0;

dht DHT;

#define DHT11_PIN A0

 
SoftwareSerial gprsSerial(7,8);


float temp=0.0;


void setup()
{
  gprsSerial.begin(9600);               // the GPRS baud rate   
  Serial.begin(9600);    // the GPRS baud rate 

  delay(1000);
}
 
void loop()
{
      int chk = DHT.read11(DHT11_PIN);
      temp=analogRead(A0);
      delay(2);          
      
   
  if (gprsSerial.available())
    Serial.write(gprsSerial.read());

  gprsSerial.println("AT");
  delay(1000);

  gprsSerial.println("AT+CPIN?");
  delay(1000);

  gprsSerial.println("AT+CREG?");
  delay(1000);

  gprsSerial.println("AT+CGATT?");
  delay(1000);

  gprsSerial.println("AT+CIPSHUT");
  delay(1000);

  gprsSerial.println("AT+CIPSTATUS");
  delay(2000);

  gprsSerial.println("AT+CIPMUX=0");
  delay(2000);
 
  ShowSerialData();
 
  gprsSerial.println("AT+CSTT=\"myAPN\"");//start task and setting the APN,
  delay(1000);
 
  ShowSerialData();
 
  gprsSerial.println("AT+CIICR");//bring up wireless connection
  delay(3000);
 
  ShowSerialData();
 
  gprsSerial.println("AT+CIFSR");//get local IP adress
  delay(2000);
 
  ShowSerialData();
 
  gprsSerial.println("AT+CIPSPRT=0");
  delay(3000);
 
  ShowSerialData();
  
  gprsSerial.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"");//start up the connection
  delay(6000);
 
  ShowSerialData();
 
  gprsSerial.println("AT+CIPSEND");//begin send data to remote server
  delay(4000);
  ShowSerialData();
  
    String str="GET https://api.thingspeak.com/update?api_key=QT7O3U5O8HFEFBY8&field1=0" + String(temp);
  gprsSerial.println(temp);//begin send data to remote server
  delay(4000);
  ShowSerialData();

  gprsSerial.println((char)26);//sending
  delay(5000);//waitting for reply, important! the time is base on the condition of internet 
  gprsSerial.println();
 
  ShowSerialData();
 
  gprsSerial.println("AT+CIPSHUT");//close the connection
  delay(100);
  ShowSerialData();
} 
void ShowSerialData()
{
  while(gprsSerial.available()!=0)
    Serial.write(gprsSerial.read());
}
February 13, 2018
7:36 pm
Avatar
cstapels
Moderator
Members


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

Should this line 

gprsSerial.println(temp);//begin send data to remote server

be 

gprsSerial.println(str);//begin send data to remote server

 

and I think

 String str="GET https://api.thingspeak.com/update?api_key=QT7O3U5O8HFEFBY8&field1=0" + String(temp);

should be

String str="GET https://api.thingspeak.com/update?api_key=QT7O3U5O8HFEFBY8&field1=" + String(temp);

Although a leading zero probably wont make a difference.

If that doesn't help, please post the serial output too.

February 14, 2018
10:13 am
Avatar
seales

Silver
Members
Forum Posts: 10
Member Since:
February 13, 2018
sp_UserOfflineSmall Offline

    String str="GET https://api.thingspeak.com/update?api_key=421TXMLMB71W7Q3G&field1=0" + String(temp);
  mySerial.println(temp);//begin send data to remote server

Thanks for the reply. I realized that change had to be made and thanks for pointing that out.

String str="GET https://api.thingspeak.com/update?api_key=QT7O3U5O8HFEFBY8&field1=" + String(temp);

Tried this but still no change. I am using a DHT 11 temperature sensor connected to digital pin 2

February 14, 2018
2:31 pm
Avatar
cstapels
Moderator
Members


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

Can you show what the serial monitor says for this line?

gprsSerial.println(temp);//begin send data to remote server

or do something like Serial.println(temp) so we can see what the format of the request is exactly when it is sent?

Do you get no update at all on your channel, or still just incorrect numbers?

February 14, 2018
3:43 pm
Avatar
seales

Silver
Members
Forum Posts: 10
Member Since:
February 13, 2018
sp_UserOfflineSmall Offline

SEND OK
347
AT+CIPSHUT

SHUT OK
AT+CREG?

+CREG: 0,1

OK
AT+CGATT?AT+CSTT="myApn"

OK
AT+CIICR

OK
AT+CIFSR

10.224.152.203
AT+CIPSPRT=0

OK
AT+CIPSTART="TCP","api.thingspeak.com","80"

OK

CONNECT OKAT+CIPSEND
GET https://api.thingspeak.com/update?api_key=QT7O3U5O8HFEFBY8&amp;

SEND OK
348

 

This is what the serial monitor says. And yes I get an update on the channel but incorrect values.

February 14, 2018
3:47 pm
Avatar
seales

Silver
Members
Forum Posts: 10
Member Since:
February 13, 2018
sp_UserOfflineSmall Offline

AT
AT+CPIN?
AT+CREG?
AT+CGATT?
AT+CIPSHUT
AT+CIPSTATUS
ATAT+CSTT="myApn"
AT+CIICR
AT+CIFSR
AT+CIPSPRT=0
AT+CIPSTART="TCP","api.thingspeak.com","80"
AT+CIPSEND
Temperature = 23.00
GET https://api.thingspeak.com/update?api_key=QT7O3U5O8HFEFBY8&amp;

SEND OK
359

This is what I am seeing on the serial monitor as I added:

Serial.print("Temperature = ");
Serial.println(DHT.temperature);

But the value I am seeing on thingspeak is 325.

February 14, 2018
4:57 pm
Avatar
cstapels
Moderator
Members


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

Sorry, I repeated the same mistake I pointed out in your code above!

I meant for you to print str, not temp.

Can you add Serial.println(str) and share the output?

My concern is that I don't see the value, or even field1= at the end of the string you showed.

GET https://api.thingspeak.com/update?api_key=XXXXXXXXXXXXXXXX&amp;

I am looking for something similar to this:

GET https://api.thingspeak.com/update?api_key=XXXXXXXXXXXXXXXX&field1=23;

If the buffer is getting filled, you can try it in shorter segments. 

perhaps

gprsSerial.print("GET https://api.thingspeak.com/update");//begin send data to remote server

gprsSerial.println("?api_key=XXXXXXXXXXXXXXXX&field1=23");

February 14, 2018
7:17 pm
Avatar
seales

Silver
Members
Forum Posts: 10
Member Since:
February 13, 2018
sp_UserOfflineSmall Offline

cstapels said

Sorry, I repeated the same mistake I pointed out in your code above!

I meant for you to print str, not temp.

Can you add Serial.println(str) and share the output?

Hello no need to apologize, I am really grateful for the help and advice.

I added the Serial.println(str) and this is what I got on the serial monitor:

CONNECT OKAT+CIPSEND

GET https://api.thingspeak.com/update?api_key=QT7O3U5O8HFEFBY8&field1=01.00

Temperature = 25.00

GET https://api.thingspeak.com/update?api_key=QT7O3U5O8HFEFBY8&amp;

February 15, 2018
9:53 am
Avatar
cstapels
Moderator
Members


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

Does that code result in the value 1.00 being added to ThingSpeak? I realize that 25.00 is the value you want, but what you showed was requesting a value of 1.00 to be written.  

If all that is true, then either temp is not being updated before it gets added to str or there may be a memory problem using the string class.

I would try just hard coding a number and making sure it writes correctly to ThingSpeak first.  

you can write
 str="GET https://api.thingspeak.com/update?api_key=QT7O3U5O8HFEFBY8&field1=123"

 then 

gprsSerial.println(str);

and make sure you see the value 123.

 

I am also curious about this line after you write the GET request:

gprsSerial.println((char)26);//sending 

why are you sending the value 26?

February 15, 2018
10:07 am
Avatar
seales

Silver
Members
Forum Posts: 10
Member Since:
February 13, 2018
sp_UserOfflineSmall Offline

#include <SoftwareSerial.h>
#include <String.h>
#include <dht.h>
int sensorPin = 0; // set the input pin for the potentiometer
int sensorValue = 0;

dht DHT;

#define DHT11_PIN A0

SoftwareSerial gprsSerial(7,8);

 

float temp=0.0;

void setup()
{
gprsSerial.begin(9600); // the GPRS baud rate
Serial.begin(9600); // the GPRS baud rate

delay(1000);
}

void loop()
{
temp=analogRead(A0);
temp=temp*0.4887;
delay(2);

if (gprsSerial.available())
Serial.write(gprsSerial.read());

gprsSerial.println("AT");
delay(1000);

gprsSerial.println("AT+CPIN?");
delay(1000);

gprsSerial.println("AT+CREG?");
delay(1000);

gprsSerial.println("AT+CGATT?");
delay(1000);

gprsSerial.println("AT+CIPSHUT");
delay(1000);

gprsSerial.println("AT+CIPSTATUS");
delay(2000);

gprsSerial.println("AT+CIPMUX=0");
delay(2000);

ShowSerialData();

gprsSerial.println("AT+CSTT=\"myApn\"");//start task and setting the APN,
delay(1000);

ShowSerialData();

gprsSerial.println("AT+CIICR");//bring up wireless connection
delay(3000);

ShowSerialData();

gprsSerial.println("AT+CIFSR");//get local IP adress
delay(2000);

ShowSerialData();

gprsSerial.println("AT+CIPSPRT=0");
delay(3000);

ShowSerialData();

gprsSerial.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"");//start up the connection
delay(6000);

ShowSerialData();

gprsSerial.println("AT+CIPSEND");//begin send data to remote server
delay(4000);
ShowSerialData();

String str="GET https://api.thingspeak.com/update?api_key=QT7O3U5O8HFEFBY8&field1=0" + String(temp);
Serial.println(str);
gprsSerial.println(str);//begin send data to remote server
delay(4000);
ShowSerialData();

gprsSerial.println((char)26);//sending
delay(5000);//waitting for reply, important! the time is base on the condition of internet
gprsSerial.println();

ShowSerialData();

gprsSerial.println("AT+CIPSHUT");//close the connection
delay(100);
ShowSerialData();
}
void ShowSerialData()
{
while(gprsSerial.available()!=0)
Serial.write(gprsSerial.read());

 

int chk = DHT.read11(DHT11_PIN);
Serial.print("Temperature = ");
Serial.println(DHT.temperature);
Serial.print("Humidity = ");
Serial.println(DHT.humidity);
delay(5000);
sensorValue = analogRead(sensorPin);
delay(1000);
Serial.print("sensor = " );
Serial.println(sensorValue);
sensorValue = analogRead(sensorPin);
delay(5000);
Serial.print("sensor = " );
Serial.println(sensorValue);
}

This is the new code I uploaded and I placed the Serial.print("Temperature = "); code lines to the end of the code.

After using this code, the serial monitor shows this:

OK
CONNECT OKTemperature = 28.00
Humidity = 74.00
sensor = 1023
sensor = 1023
AT+CIPSEND
Temperature = 28.00
Humidity = 75.00
sensor = 1023
sensor = 1023
GET https://api.thingspeak.com/update?api_key=QT7O3U5O8HFEFBY8&field1=0499.94
GET https://api.thingspeak.com/update?api_key=QT7O3U5O8HFEFBY8&Temperature = 28.00
Humidity = 74.00
sensor = 1023
sensor = 1023
SEND OK
523Temperature = 28.00
Humidity = 74.00
sensor = 1023
sensor = 1023
CLOSED
Temperature = -999.00
Humidity = -999.00

 Based on what you said you were not seeing, I am now able to see:

GET https://api.thingspeak.com/update?api_key=QT7O3U5O8HFEFBY8&field1=0499.94
GET https://api.thingspeak.com/update?api_key=QT7O3U5O8HFEFBY8&Temperature = 28.00

as shown above. The problem is that the 28 value is not uploaded to thingspeak but the value I am seeing seems to be the 499.94.

February 15, 2018
10:32 am
Avatar
seales

Silver
Members
Forum Posts: 10
Member Since:
February 13, 2018
sp_UserOfflineSmall Offline

why are you sending the value 26?  

This line came with the code I sampled and made my adjustment too. I am yet to figure out what exactly it does

February 16, 2018
9:44 am
Avatar
cstapels
Moderator
Members


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

At the top of your loop, you have

temp=analogRead(A0);
temp=temp*0.4887;

If you want the value written to ThingSpeak to be 28, then set these lines to be

temp=(DHT.temperature);  ,

 

The value of 26 (gprsSerial.println((char)26);//sending ) may be a number of bytes sent.  I would try removing it and then investigating the difference.

February 17, 2018
3:17 pm
Avatar
seales

Silver
Members
Forum Posts: 10
Member Since:
February 13, 2018
sp_UserOfflineSmall Offline

cstapels said

The value of 26 (gprsSerial.println((char)26);//sending ) may be a number of bytes sent.  I would try removing it and then investigating the difference.  

Based on research this char 26 indicated the end of data.

Thanks again for your feedback. Based on your last reply I made some adjustments to the code and Eureka I got it to work. I removed the following code lines altogether:

float temp=0.0

temp=analogRead(A0);
temp=temp*0.4887;

and just replaced  (temp) with (dht.temperature) in code line:

String str="GET https://api.thingspeak.com/update?api_key=QT7O3U5O8HFEFBY8&field1=" + String(DHT.temperature);

With this, the temperature value is uploaded to thingspeak correctly. The sensor also has a humidity value and I would like to upload that to a second field on the channel, so I would get both temperature and humidity uploaded on two fields. I tried several adjustments to the code but no luck with the two values upload. Advice with this would be appreciated.

February 18, 2018
6:39 pm
Avatar
seales

Silver
Members
Forum Posts: 10
Member Since:
February 13, 2018
sp_UserOfflineSmall Offline

String str="GET https://api.thingspeak.com/update?api_key=QT7O3U5O8HFEFBY8&field1="+String(DHT.temperature)+"?api_key=QT7O3U5O8HFEFBY8&field2="+String(DHT.humidity) ;

With more trial and error I got the two values to upload simultaneously. thanks once again for all the advice

February 20, 2018
3:22 pm
Avatar
cstapels
Moderator
Members


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

Thanks for posting the solution.  If you want to make it a little shorter, you can actually skip the second repeated API key.

String str="GET https://api.thingspeak.com/update?api_key=XXXXXXXXXXXXXXXX&field1="+String(DHT.temperature)+"&field2="+String(DHT.humidity);

Should work as well.

February 20, 2018
6:31 pm
Avatar
seales

Silver
Members
Forum Posts: 10
Member Since:
February 13, 2018
sp_UserOfflineSmall Offline

Ok thanks, I will give the shorter version a try.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
22 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:

JamesVex, meghanfr69, raqueldk69, soniafw2, vitalijDrine, JosephAmapy

Administrators: Hans: 387, lee: 457