6:18 pm


Silver
February 13, 2018

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()); }
7:36 pm


Moderators
March 7, 2017

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.
10:13 am


Silver
February 13, 2018

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
2:31 pm


Moderators
March 7, 2017

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?
3:43 pm


Silver
February 13, 2018

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&
SEND OK
348
This is what the serial monitor says. And yes I get an update on the channel but incorrect values.
3:47 pm


Silver
February 13, 2018

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&
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.
4:57 pm


Moderators
March 7, 2017

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&
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
7:17 pm


Silver
February 13, 2018

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&
9:53 am


Moderators
March 7, 2017

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?
10:07 am


Silver
February 13, 2018

#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.
10:32 am


Silver
February 13, 2018

9:44 am


Moderators
March 7, 2017

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.
3:17 pm


Silver
February 13, 2018

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.
6:39 pm


Silver
February 13, 2018

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
3:22 pm


Moderators
March 7, 2017

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.
Most Users Ever Online: 166
Currently Online:
35 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
rw950431: 272
Vinod: 240
piajola: 95
turgo: 70
vespapierre: 63
Adarsh_Murthy: 62
Member Stats:
Guest Posters: 1
Members: 8665
Moderators: 1
Admins: 2
Forum Stats:
Groups: 3
Forums: 14
Topics: 1600
Posts: 5760
Newest Members:
Advantagetreeexperts, laundrydaddyuk, techhhelp5, ken, tran, huldacormierModerators: cstapels: 460
Administrators: Hans: 405, lee: 457