EPS8266 wifi + arduinomega "Thingspeak"

I'm trying to upload data information to ThingSpeak, using Arduino Mega 2560 and the ESP8266 for the WiFi connection. For now I'm just trying to upload data using a DHT22 for temperature/humidity but I can't do it and I'm not sure where the error is. I can't get a graphic's data in thingspeak. Help me please!
This is my code:
// Librería para usar SoftwareSerial
#include <SoftwareSerial.h>
#include <DHT.h>
SoftwareSerial espSerial = SoftwareSerial(18,19); // arduino RX pin=2 arduino TX pin=3
// Conecte el pin 2 al pin TX del ESP8266 - Conecte el pin 3 al pin RX del ESP8266
#define DHTPIN 12 // Conecte el pin de datos al pin 5 del Arduino
#define DHTTYPE DHT22 //defina el tipo de sensor
DHT dht(DHTPIN, DHTTYPE); //crear objeto DHT
String apiKey = "RWAB7FVFE5F9PU2Q"; // Remplace con el API KEY WRITE de ThingSpeak
String ssid="GALAXYA12C89F"; // Wifi network SSID
String password ="gianderi12"; // Wifi network password
//======================================================================== tiempoRespuesta
void tiempoRespuesta(int waitTime){
long t=millis();
char c;
while (t+waitTime>millis()){
if (espSerial.available()){
c=espSerial.read();
Serial.print(c);
}
}
}
//========================================================================
boolean thingSpeakWrite(float value1, float value2){
String cmd = "AT+CIPSTART="TCP",""; // Conexión TCP
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "",80";
espSerial.println(cmd); //Mandar datos seriales al ESP8266
Serial.println(cmd);
if(espSerial.find("Error")){
Serial.println("AT+CIPSTART error");
return false;
}
String getStr = "GET /update?api_key="; // Prepara el String GET a mandar
getStr += apiKey;
getStr +="&field1=";
getStr += String(value1);//convertir de flotante a String
getStr +="&field2=";
getStr += String(value2);
// getStr +="&field3=";
// getStr += String(value3);
// ...
getStr += "\r\n\r\n";
// enviar la longitud de la cadena
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
espSerial.println(cmd);
Serial.println(cmd);
delay(100);
if(espSerial.find(">")){//si Ok envíe la cadena de GET
espSerial.print(getStr);
Serial.print(getStr);
}
else{
espSerial.println("AT+CIPCLOSE");
// Que el usuario sepa que no se están enviando datos
Serial.println("AT+CIPCLOSE");
return false;
}
return true;
}
//================================================================================ setup
void setup() {

Serial.begin(115200);
dht.begin(); // Empiece la comunicacnión con el sensor DHT11
espSerial.begin(115200); // Habilite la comunicación serial
//La velocidad problable del módulo ESP8266 es 115200 bauds
// Por esa razón se debe configurar primero esta velocidad (115200) y correr el programa las líneas de código
// a continuación cambian la velocidad pero necesitan que se les quite el comentario.
// Corra el código con las siguientes líneas descomentadas y luego vuelva a correr pero cambiando 115200 por
//9600 y volviendo a comentar las líneas

espSerial.println("AT+CWMODE=1"); // Configure el ESP8266 como cliente
tiempoRespuesta(1000);
espSerial.println("AT+CWJAP=""+ssid+"",""+password+"""); // Configure el SSID y password para conexión WIFI
tiempoRespuesta(5000);
Serial.println("Setup completed");
}

void loop() {
// Lea los valores del sensor
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(t) || isnan(h)) {
Serial.println("Fallo al leer el sensor");
}
else {
Serial.println("Temp="+String(t)+"°C"); //imprimimos los valores en el monitor serial
Serial.println("Humedad="+String(h)+"%");
thingSpeakWrite(t,h); // Escriba los valores a ThingSpeak
}
// thingspeak necesita al menos 15 segundos para actualizar
delay(16000);
}

start by reading this: How to get the best out of this forum - Using Arduino / Installation & Troubleshooting - Arduino Forum

And then return to your post, edit it, and include code tags around your code. It will help others help you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.