0

I am trying to send the data from DHT sensor (temperature and humidity on 2 channels of Thingspeak cloud server) and firstly display them on Thingspeak cloud. Afterwards I will need to get those data from the cloud and use them with Python in order to make some calculations. Sensor data is sent via SIM900 GSM module.

I succeeded to send some constant value to Thingspeak by executing AT commands, one by one, which I put later in this code: https://pastebin.com/4bP6hgk7 EDIT: Here is the code:

#include <SoftwareSerial.h>
#include "DHT.h"

#define DHTPIN 2
#define DHTTYPE DHT11

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

// Create global varibales to store temperature and humidity
float t; // temperature in celcius
float f; // temperature in fahrenheit
float h; // humidity

SoftwareSerial gsm(7, 8);

void setup() {
  // put your setup code here, to run once:
  dht.begin();

  gsm.begin(9600);
  Serial.begin(9600);

  gsm.println("AT+IPR=9600");
  delay(1000);

  gsm.println("AT+CGATT=1");
  delay(1000);



  gsm.println("AT+SAPBR=1,1");
delay(1000);

  ShowSerialData();

  gsm.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
  delay(1000);

  ShowSerialData();

  gsm.println("AT+SAPBR=3,1,\"APN\",\"prepaidnet\"");
  delay(1000);

  ShowSerialData();

  gsm.println("AT+SAPBR=3,1,\"USER\",\"mts\"");
  delay(1000);

  ShowSerialData();

  gsm.println("AT+SAPBR=3,1,\"PWD\",\"064\"");
  delay(1000);

  ShowSerialData();

  gsm.println("AT+SAPBR=1,1");
  delay(1000);

  ShowSerialData();

  gsm.println("AT+SAPBR=2,1");
  delay(1000);

   ShowSerialData();



   gsm.println("AT+HTTPTERM");
  delay(1000);

   ShowSerialData();

  gsm.println("AT+HTTPINIT");
  delay(2000);

  ShowSerialData();

  gsm.println("AT+HTTPPARA=\"CID\",1");
  delay(3000);

  ShowSerialData();

}

void loop() {
  // put your main code here, to run repeatedly:
  while(gsm.available()) {
    Serial.write(gsm.read());
  }
  if(Serial.available()) {
    gsm.write(Serial.read());
}

 t=readTemp();
h=readHum();

gsm.print("AT+HTTPPARA=\"URL\",\"https://api.thingspeak.com/update?api_key=Z1Q5633A4S1GHIHU&field1=26");
//gsm.print(t);
//gsm.println();
delay(5000);
//gsm.println((char)26);

ShowSerialData();

delay(1000);
gsm.println("AT+HTTPPARA=\"REDIR\",1");
delay(3000);
gsm.println("AT+HTTPSSL=1");
delay(3000);
gsm.println("AT+HTTPACTION=0");  /* Start POST session */
delay(5000);
gsm.println("AT+HTTPREAD");
delay(3000);
gsm.println("AT+HTTPTERM"); /* Terminate HTTP service */
delay(5000);
gsm.println("AT+SAPBR=0,1");
delay(3000);
ShowSerialData();

}

float readTemp() {
  // Read temperature as Celsius
  t = dht.readTemperature();
  //Read humidity
  h = dht.readHumidity();

  // Compute temperature values in Celcius
  t = dht.computeHeatIndex(t,h,false);

  // Check if any reads failed and exit early (to try again).
  if (isnan(t)) {
    Serial.println("Failed to read temp from DHT sensor!");
    return 1;
  }

  return t;
}

float readHum() {
  //Read humidity
  h = dht.readHumidity();
  // Read temperature as Celsius
  t = dht.readTemperature();

  // Compute temperature values in Celcius
  t = dht.computeHeatIndex(t,h,false);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h)) {
    Serial.println("Failed to read hum from DHT sensor!");
    return 1;
  }

  return h;
}

void ShowSerialData()
{
  while(gsm.available()!=0)
    Serial.write(gsm.read());
    if(Serial.available()) {
    gsm.write(Serial.read());
  }
}

The problem is that not the whole script is executed - AT+HTTPPARA command with the parameter "URL" and the URL where the data should be sent is not being executed. I have read many answers but could't find the answer for this. Could anybody help with this issue? Thanks in advance!

  • Line 99 should be a gsm.println not a gsm.print. – Majenko Aug 8 at 21:40
  • Thanks for the suggestion, but putting println didn't resolve the problem. – Miloš Mladenović Aug 9 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.