Hey,
I want to send temperature data DS18B20 using SIM800L v2 to ThingSpeak from an Arduino Uno R4 wifi.
I found this code from this tutorial, which I adjusted (using chatgpt) just for my sensor, but when I expect "Response from ThingSpeak:", I get just blank text.
I am new in Arduino, so I have been trying troubleshooting it watching other similar guides, but I cannot get it work.
Here is the latest code:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>
// Pin configuration
#define DS18B20_PIN 2 // DS18B20 data pin
#define SIM800_TX 10 // SIM800L TXD pin
#define SIM800_RX 11 // SIM800L RXD pin
// SoftwareSerial for SIM800L
SoftwareSerial SIM800(SIM800_TX, SIM800_RX);
// ThingSpeak settings
const char* APN = "apn"; // APN for your SIM card
const char* USERNAME = ""; // Username for your APN (if required)
const char* PASSWORD = ""; // Password for your APN (if required)
const char* THINGSPEAK_API_KEY = "APIKEY"; // ThingSpeak API Key
const char* THINGSPEAK_HOST = "SERVER"; // ThingSpeak server hostname
OneWire oneWire(DS18B20_PIN);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
SIM800.begin(9600);
sensors.begin();
delay(1000);
Serial.println("Initializing SIM800L...");
SIM800.println("AT");
delay(1000);
SIM800.println("AT+CREG?");
delay(1000);
SIM800.println("AT+CGATT?");
delay(1000);
}
void loop() {
float temperature = readTemperature();
if (!isnan(temperature)) {
if (!isGPRSConnected()) {
connectGPRS();
}
sendToThingSpeak(temperature);
disconnectGPRS();
} else {
Serial.println("Error reading temperature");
}
delay(10000); // Wait for 10 seconds before the next update
}
float readTemperature() {
sensors.requestTemperatures();
return sensors.getTempCByIndex(0);
}
boolean isGPRSConnected() {
SIM800.println("AT+CGATT?");
String response = readSerial();
return response.indexOf("+CGATT: 1") != -1;
}
void connectGPRS() {
SIM800.println("AT+CGATT=1");
delay(1000);
SIM800.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
delay(1000);
SIM800.println("AT+SAPBR=3,1,\"APN\",\"" + String(APN) + "\"");
delay(1000);
SIM800.println("AT+SAPBR=3,1,\"USER\",\"" + String(USERNAME) + "\"");
delay(1000);
SIM800.println("AT+SAPBR=3,1,\"PWD\",\"" + String(PASSWORD) + "\"");
delay(1000);
SIM800.println("AT+SAPBR=1,1");
delay(1000);
SIM800.println("AT+HTTPINIT");
delay(1000);
SIM800.println("AT+HTTPPARA=\"CID\",1");
delay(1000);
}
void sendToThingSpeak(float temperature) {
String url = "/update?api_key=" + String(THINGSPEAK_API_KEY) + "&field1=" + String(temperature);
SIM800.println("AT+HTTPPARA=\"URL\",\"" + String(THINGSPEAK_HOST) + url + "\"");
delay(1000);
SIM800.println("AT+HTTPACTION=0");
delay(15000); // Wait for the HTTP request to complete (adjust as needed)
SIM800.println("AT+HTTPREAD");
String response = readSerial();
Serial.println("Response from ThingSpeak:");
Serial.println(response);
}
void disconnectGPRS() {
SIM800.println("AT+HTTPTERM");
delay(1000);
SIM800.println("AT+SAPBR=0,1");
delay(1000);
SIM800.println("AT+CGATT=0");
delay(1000);
}
String readSerial() {
String response = "";
while (SIM800.available()) {
char c = SIM800.read();
response += c;
}
return response;
}