arduino + esp8266 + sensor

Hey guys,
Im new to arduino and i’m trying to uses it to read some humidity sensor information and send it to thingspeak website.
The program ive put together is able to connect to the access point but when it tries to send the information to thingspeak it starts a loop and no information is sent.
Im lost here, i dont know if its something wrong with the code or if it’s a lack of memory from arduino (because it isn’t even displaying the print messages esp8266.println( “RECEIVED: OK” ))

This is the answer from the serial monitor:

AT+RST


OK
"qXÑzÂC! ™±[!ÿ ÐS!=Ñnä ÐS!}ÑFä Ð!HÕòIÔ!1¤(ÙÃÖa#)t+ a0Ø aŸÉr’÷
Ai-Thinker Technology Co.,Ltd.

ready
AT+CWJAP="teste","qwertyuiop"

WIFI CONNECTED
WIFI GOT IP
AT+CWMODE=1

busy p...

OK
AT+CIFSR

+CIFSR:STAIP,"192.168.43.123"
+CIFSR:STAMAC,"5c:cf:7f:8b:51:19"

OK
AT+CIPMUX=1


OK
AT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSEND=46
AT+CIPCLOSE
AT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSEND=46
AT+CIPCLOSE

This is my program:

#include <SoftwareSerial.h>
int sensor_humid = A0; //entrada do botao, mudar para A0 para o sensor.
int value_humid;

//-- Hardware Serial
//#define _baudrate 9600
 
//RX pino 2, TX pino 3
SoftwareSerial esp8266(2, 3);
 
#define DEBUG true
//*-- IoT Information
#define SSID "test"
#define PASS "qwertyuiop"
#define IP "184.106.153.149" // ThingSpeak IP Address: 184.106.153.149

// GET /update?key=[THINGSPEAK_KEY]&field1=[data 1]&field2=[data 2]...;
String GET = "GET /update?key=UTY2RG15WLM53test";
 
void setup()
{
  Serial.begin(9600);
  esp8266.begin(19200);
 
  sendData("AT+RST\r\n", 2000, DEBUG); // rst
  // Conecta a rede wireless
  sendData("AT+CWJAP=\"teste\",\"qwertyuiop\"\r\n", 2000, DEBUG);
  delay(3000);
  sendData("AT+CWMODE=1\r\n", 1000, DEBUG);
  // Mostra o endereco IP
  sendData("AT+CIFSR\r\n", 1000, DEBUG);
  // Configura para multiplas conexoes
  sendData("AT+CIPMUX=1\r\n", 1000, DEBUG);
  // Inicia o web server na porta 80
  //sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG);
}

void loop() {
  value_humid = analogRead(sensor_humid); //digital para botao e analogRead para o sensor
  String humid=String(value_humid);// turn integer to string
  updateTS(humid);
  delay(3000); //
}
//----- update the  Thingspeak string with 3 values
void updateTS(String H)
{
  // ESP8266 Client
  String cmd = "AT+CIPSTART=\"TCP\",\"";// Setup TCP connection
  cmd += IP;
  cmd += "\",80";
  sendDebug(cmd);
  delay(2000);
  if( Serial.find( "Error" ) )
  {
    esp8266.print( "RECEIVED: Error\nExit1" );
    return;
  }

  cmd = GET + "&field1=" + H +"\r\n";
  Serial.print( "AT+CIPSEND=" );
  Serial.println( cmd.length() );
  if(Serial.find( ">" ) )
  {
    esp8266.print(">");
    esp8266.print(cmd);
    Serial.print(cmd);
  }
  else
  {
    sendDebug( "AT+CIPCLOSE" );//close TCP connection
  }
  if( Serial.find("OK") )
  {
    esp8266.println( "RECEIVED: OK" );
  }
  else
  {
    esp8266.println( "RECEIVED: Error\nExit2" );
  }
}

 
String sendData(String command, const int timeout, boolean debug)
{
  // Envio dos comandos AT para o modulo
  String response = "";
  esp8266.print(command);
  long int time = millis();
  while ( (time + timeout) > millis())
  {
    while (esp8266.available())
    {
      // The esp has data so display its output to the serial window
      char c = esp8266.read(); // read the next character.
      response += c;
    }
  }
  if (debug)
  {
    Serial.print(response);
  }
  return response;
}

void sendDebug(String cmd)
{
  esp8266.print("SEND: ");
  esp8266.println(cmd);
  Serial.println(cmd);
}

Im using an arduino UNO, esp8266-esp01, and sensor yl-69.

ss.ino (2.5 KB)

Just an observation, you dont need the arduino for this. You could load the arduino environment on the ESP, connect it to the sensor and just do it all using that.

PS. look at your thingspeak key...

My esp 8266 is the esp 1 version, i dont think it can handle code.
This one: https://alselectro.files.wordpress.com/2015/05/esp1.png

The test part of the key was intentional.

I was able to make the following code work for 30 minutes, but after trying another network ssid it just didnt work anymore, anyone knows why?

#include <SoftwareSerial.h>
SoftwareSerial esp8266(2, 3);

#define IP "184.106.153.149" // thingspeak.com's IP

#define DEBUG true

#define soilMoisturePIN A0 // soil moisture sensor connected to Analog Pin 0

String GET = "GET /update?key=BDX2W4D32M66PQ2X&";    // put here your thingspeak key
String GET1 = "field1=";

String soilMoist = "";                           // we send the soil moisture value


String soilMoisture (){

    float moisture;
    char moisture_c[6];
    //calculates the percentage of soil moisture
    moisture = analogRead(soilMoisturePIN);
    moisture = 100*(1-(moisture)/1023);

    dtostrf(moisture, 0, 1, moisture_c); //converts floats to strings

    return (String) moisture_c;
}



void upadateValues(String moisture_c){
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += IP;
  cmd += "\",80";
  esp8266.println(cmd);
  delay(2000);
  if(esp8266.find("Error")){
    Serial.print("Error1");
    return;
  }
  cmd = GET + GET1;
  cmd += moisture_c;
  cmd += "\r\n";
  Serial.print(cmd);
  esp8266.print("AT+CIPSEND=");
  esp8266.println(cmd.length());
  if(esp8266.find(">")){
    esp8266.print(cmd);
  }else{
    esp8266.println("AT+CIPCLOSE");
  }
}
 
void setup()
{
  pinMode(soilMoisturePIN, INPUT);
  Serial.begin(9600);
  esp8266.begin(19200);
//  sensors.begin();
  sendData("AT+RST\r\n", 2000, DEBUG); // rst
  // Conecta a rede wireless
  sendData("AT+CWJAP=\"Fictio\",\"qwertyuiop\"\r\n", 2000, DEBUG);
  delay(3000);
  sendData("AT+CWMODE=1\r\n", 1000, DEBUG);
  // Mostra o endereco IP
  sendData("AT+CIFSR\r\n", 1000, DEBUG);
  // Configura para multiplas conexoes
  sendData("AT+CIPMUX=1\r\n", 1000, DEBUG);
  // Inicia o web server na porta 80
  //sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG);
 }


void loop(){
  soilMoist = soilMoisture();
  upadateValues(soilMoist);
  delay(5000);
}

String sendData(String command, const int timeout, boolean debug)
{
  // Envio dos comandos AT para o modulo
  String response = "";
  esp8266.print(command);
  long int time = millis();
  while ( (time + timeout) > millis())
  {
    while (esp8266.available())
    {
      // The esp has data so display its output to the serial window
      char c = esp8266.read(); // read the next character.
      response += c;
    }
  }
  if (debug)
  {
    Serial.print(response);
  }
  return response;
}

void sendDebug(String cmd)
{
  esp8266.print("SEND: ");
  esp8266.println(cmd);
  Serial.println(cmd);
}

You might need to be a bit more specific about what doesnt work.... wifi registration, or connecting to thingspeak

ps. Thingspeak doesnt like updates more frequently than 20 seconds (so it says on their site)