Hi, everybody, I hope I'm in the right place, otherwise I apologize to the moderators.
I need help for a sketch that should send the data of a ds18b20 to ThingSpeak.
Can somebody help me? Thank you.
/*
*/
#include <Ethernet.h>
//Include libraries
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire pin 5
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
EthernetClient client;
String postStr;
const char* server = "api.thingspeak.com";
String apiKey = "XXXXXXXX"; // (metti il codice che ti dà thingspeak)
String TUA_VARIABILE;
void setup(void) {
Serial.begin(9600); //Begin serial communication
sensors.begin();
}
void loop(void)
{
// legge il sensore
sensors.requestTemperatures();
Serial.print("Temperature is: ");
//Serial.println(sensors.getTempCByIndex(0));
TUA_VARIABILE = (sensors.getTempCByIndex(0));
Serial.println (TUA_VARIABILE);
//Scrive su thingspeak
if (client.connect(server, 80))
String postStr = apiKey; // è la chiave che hai definito sopra
postStr +="&field1="; // dice che devi scrivere sul campo uno
postStr +=String(TUA_VARIABILE); // qui inserisci il tuo campo che vuoi scrivere.
postStr +="\r\n\r\n"; //conclude la stringa se non ricordo male
Serial.println(postStr);
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length:");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
//client.stop();
delay(20000);
}
Ah! Sorry, I missed a bit of the text (copy and paste error from deepl).
The problem is that no value is sent to the thingspeak channel.
The variable TUA_VARIABLE assumes correctly the value read by the sensor but then I can't send it to the channel.
Thank you very much
pylon:
No, because you've yet to tell us what the problem is.
Hi, I've been looking around and I came up with this:
/*
*/
//Include libraries
#include <Ethernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ThingSpeak.h>
// Data wire pin 5
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
EthernetClient client;
String postStr;
const char* server ="api.thingspeak.com";
String apiKey = "XxxxxxxXXXXXxx"; // (metti il codice che ti dà thingspeak)
float TUA_VARIABILE;
void setup(void) {
Serial.begin(9600); //Begin serial communication
sensors.begin();
}
void loop(void)
{
// legge il sensore
sensors.requestTemperatures();
Serial.print("Temperature is: ");
//Serial.println(sensors.getTempCByIndex(0));
TUA_VARIABILE = (sensors.getTempCByIndex(0));
Serial.println (TUA_VARIABILE); // questa istruzione l'ho messa per essere sicuro che la variabile prendesse il valore letto dal sensore
//Scrive su thingspeak
if (client.connect(server, 80))
{
String postStr = apiKey; // è la chiave che hai definito sopra
postStr +="&field1="; // dice che devi scrivere sul campo uno
postStr +=float(TUA_VARIABILE); // qui inserisci il tuo campo che vuoi scrivere.
postStr +="\r\n\r\n"; //conclude la stringa se non ricordo male
Serial.println(postStr);
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length:");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
}
//client.stop();
delay(20000);
}
What I figured out is that you don't execute the part of the code after the bracket on the instruction.
if (client.connect(server, 80)) as the instruction Serial.println(postStr); never appears on the serial monitor.
But I don't understand why it doesn't connect.
Thank you