Hi All
Hope you are doing well
I am trying to send DHT22 data via Sim800L to ThingSpeak.
This is my code;
#include <SoftwareSerial.h>
#include <dht.h>
dht DHT;
SoftwareSerial gsm(3, 2); // RX, TX
#define DHT22_PIN 8
int chk;
int humi = 0;
int temp = 0;
void setup()
{
Serial.begin(9600);
gsm.begin(9600);
modem_init();
data_init();
internet_init();
}
void loop()
{
chk = DHT.read22(DHT22_PIN);
switch (chk)
{
case DHTLIB_OK:
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
case DHTLIB_ERROR_CONNECT:
Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
Serial.print("Ack High error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
Serial.print("Humidity: ");
Serial.print(DHT.humidity, 1);
Serial.print("%");
Serial.print(",\t");
Serial.print("Temperature: ");
Serial.print(DHT.temperature, 1);
Serial.println("*C");
temp = DHT.temperature;
humi = DHT.humidity;
delay(5000);
Send_data();
}
void modem_init()
{
Serial.println("Please wait.....");
gsm.println("AT");
delay(1000);
gsm.println("AT+CMGF=1");
delay(1000);
gsm.println("AT+CNMI=2,2,0,0,0");
delay(1000);
}
void data_init()
{
Serial.println("Please wait.....");
gsm.println("AT");
delay(1000); delay(1000);
gsm.println("AT+CPIN?");
delay(1000); delay(1000);
gsm.print("AT+SAPBR=3,1");
gsm.write(',');
gsm.write('"');
gsm.print("contype");
gsm.write('"');
gsm.write(',');
gsm.write('"');
gsm.print("GPRS");
gsm.write('"');
gsm.write(0x0d);
gsm.write(0x0a);
delay(1000); ;
gsm.print("AT+SAPBR=3,1");
gsm.write(',');
gsm.write('"');
gsm.print("APN");
gsm.write('"');
gsm.write(',');
gsm.write('"');
gsm.print("internet"); //APN Here
gsm.write('"');
gsm.write(0x0d);
gsm.write(0x0a);
delay(1000);
gsm.print("AT+SAPBR=3,1");
gsm.write(',');
gsm.write('"');
gsm.print("USER");
gsm.write('"');
gsm.write(',');
gsm.write('"');
gsm.print("vodafone");
gsm.write('"');
gsm.write(0x0d);
gsm.write(0x0a);
delay(1000);
gsm.print("AT+SAPBR=3,1");
gsm.write(',');
gsm.write('"');
gsm.print("PWD");
gsm.write('"');
gsm.write(',');
gsm.write('"');
gsm.print("vodafone");
gsm.write('"');
gsm.write(0x0d);
gsm.write(0x0a);
delay(2000);
gsm.print("AT+SAPBR=1,1");
gsm.write(0x0d);
gsm.write(0x0a);
delay(3000);
}
void internet_init()
{
Serial.println("Please wait.....");
delay(1000);
gsm.println("AT+HTTPINIT");
delay(1000); delay(1000);
gsm.print("AT+HTTPPARA=");
gsm.print('"');
gsm.print("CID");
gsm.print('"');
gsm.print(',');
gsm.println('1');
delay(1000);
}
void Send_data()
{
gsm.print("AT+HTTPPARA=");
gsm.print('"');
gsm.print("URL");
gsm.print('"');
gsm.print(',');
gsm.print('"');
gsm.print("https:");
gsm.print('/');
gsm.print('/');
gsm.print("api.thingspeak.com/update?api_key=xxxxxxxxxxxxxxxxx&field1=");
///xxx is the api key
gsm.print(temp); //>>>>>> variable 1 (temperature)
gsm.print("&field2=");
gsm.print(humi); //>>>>>> variable 2 (Humidity)
gsm.print('"');
gsm.write(0x0d);
gsm.write(0x0a);
delay(1000);
gsm.println("AT+HTTPACTION=0");
delay(1000);
}
When I type the url to my browser, it updates the ThingSpeak so it's working well. But I am having trouble to do that with this code. And I am powering up the Sim800L from 5v pin of Arduino Uno, maybe this is the issue, I don't know.
Can you see my mistake? Please help asap if you can.
And also I am using the forum for the first time. If I've done any mistake, I apologize from you all.
King Regards to All Community.