I'm trying to send data from sensor to ThingSpeak, but the AT commands always return Fail. They work sometimes and send data. But, most of the time they return fail.
My Connections:
ESP | Arduino |
---|---|
Gnd | Gnd |
Vcc, ChPD/En | 3.3v |
Rx | 2 |
Tx | 3 |
My code:
#include <SoftwareSerial.h>
#include "DHT.h"
#define DHTPIN 10
#define DHTTYPE DHT11
#define RX 2
#define TX 3
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#define SensorPin 0 // the pH meter Analog output is connected with the Arduino’s Analog
#define ONE_WIRE_BUS 8
LiquidCrystal_I2C lcd(0x27, 16, 2);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DHT dht(DHTPIN, DHTTYPE);
float phValue;
String AP = "Mukadam"; // AP NAME
String PASS = "28oct1997"; // AP PASSWORD
String API = "ZIGGVWBPU0BJ9A0I"; // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
String f1 = "field1";
String f2 = "field2";
String f3 = "field3";
String f4 = "field4";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
SoftwareSerial esp8266(RX,TX);
float Celcius=0;
float Fahrenheit=0;
float b;
int buf[10],temp;
unsigned long int avgValue;
void setup()
{
lcd.begin();
lcd.backlight();
Serial.begin(9600);
dht.begin();
pinMode(13,OUTPUT);
sensors.begin();
lcd.print("WT:");
esp8266.begin(115200);
sendCommand("AT",3,"OK\r\n\r\n");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
void loop()
{
sensors.requestTemperatures();
Celcius=sensors.getTempCByIndex(0);
Fahrenheit=sensors.toFahrenheit(Celcius);
Serial.print("C:");
Serial.print(Celcius);
lcd.setCursor(3,0);
lcd.print(Celcius);
String getData = "GET /update?api_key="+ API +"&"+ f1 +"="+String(Celcius);
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
esp8266.println(getData);
delay(1500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value
{
buf[i]=analogRead(SensorPin);
delay(100);
}
for(int i=0;i<9;i++) //sort the analog from small to large
{
for(int j=i+1;j<10;j++)
{
if(buf[i]>buf[j])
{
temp=buf[i];
buf[i]=buf[j];
buf[j]=temp;
}
}
}
avgValue=0;
for(int i=2;i<8;i++) //take the average value of 6 center sample
avgValue+=buf[i];
float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
phValue=3.5*phValue; //convert the millivolt into pH value
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
lcd.setCursor(9,0);
lcd.print("pH:");
lcd.setCursor(12,0);
lcd.print(phValue);
getData = "GET /update?api_key="+ API +"&"+ f2 +"="+String(phValue);
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
esp8266.println(getData);delay(1500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
lcd.setCursor(12,0);
lcd.print(phValue);
float h = dht.readHumidity();
lcd.setCursor(0,1);
lcd.print("H:");
lcd.setCursor(2,1);
lcd.print(h);
float t = dht.readTemperature();
lcd.setCursor(8,1);
lcd.print("AT:");
lcd.setCursor(11,1);
lcd.print(t);
lcd.setCursor(12,0);
lcd.print(phValue);
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok
{
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
Serial.println("OYI");
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}
And here is serial monitor output:
work sometimes
andalways fail
are contradictory statements ... you cannot have both .... how do the commands fail? .... what errors do you get? .... does any specific command work more often?AT+UART_DEF=9600,8,1,0,0
at 115200 baud. you only need to run it once. the esp remembers the baud rate. or you can use this one github.com/jandrassy/WiFiEspAT/blob/master/examples/Tools/…