Hello everyone! How you guys doing? Im having a little problem trying to use a ESP8266 with a arduino mega 2560. I have connected the RX of the ESP on the TX1 Pin of the board (18), and the TX on the RX1 Pin to the board (17). Im using a Mega 2560. The actual code is:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);
#define DHTPIN 51 // what pin we're connected to
#define DHTTYPE DHT11
#include <SoftwareSerial.h>
#include <HardwareSerial.h>
HardwareSerial Serial1(17, 18);
#include <arduino-timer.h>
#include "DHT.h"
#include <LiquidCrystal.h>
auto t = timer_create_default();
DHT dht(DHTPIN, DHTTYPE);
float temperature, humidity;
const char *api_key="PT46QPZM2QBBDND1"; // Api de escrita do ThingSpeak
static char postUrl[150];
void httpGet(String ip, String path, int port=80);
void setup(void) {
lcd.begin(16, 2);
Serial1.begin(9600);
Serial.begin(9600);
lcd.print("Reading sensor");
lcd.print("WIFI Connecting");
Serial.println("Conectando Wifi....");
connect_wifi("AT+CWMODE=1",1000);
connect_wifi("AT+CWQAP",1000);
connect_wifi("AT+RST",5000);
connect_wifi("AT+CWJAP=\"FGHN\",\"cp04182126\"",10000);
Serial.println("Wifi Connected");
lcd.clear();
lcd.print("WIFI Connected.");
delay(2000);
dht.begin();
}
void loop() {
humidity = dht.readHumidity();
temperature = dht.readTemperature();
delay(2000);
lcd.clear();
char tempF[6];
char humF[6];
dtostrf(temperature, 5, 1, tempF);
dtostrf(humidity, 2, 0, humF);
lcd.print("T:");
lcd.print(tempF);
lcd.print((char)223);
lcd.print("C ");
lcd.print("H: ");
lcd.print(humF);
lcd.print("%");
send2server();
}
void send2server()
{
humidity = dht.readHumidity();
temperature = dht.readTemperature();
char tempStr[8];
char humidStr[8];
dtostrf(temperature, 5, 3, tempStr);
dtostrf(humidity, 5, 3, humidStr);
sprintf(postUrl, "update?api_key=%s&field1=%s&field2=%s",api_key,humidStr,tempStr);
httpGet("api.thingspeak.com", postUrl, 80);
}
void httpGet(String ip, String path, int port)
{
int resp;
String atHttpGetCmd = "GET /"+path+" HTTP/1.0\r\n\r\n";
//AT+CIPSTART="TCP","192.168.20.200",80
String atTcpPortConnectCmd = "AT+CIPSTART=\"TCP\",\""+ip+"\","+port+"";
connect_wifi(atTcpPortConnectCmd,1000);
int len = atHttpGetCmd.length();
String atSendCmd = "AT+CIPSEND=";
atSendCmd+=len;
connect_wifi(atSendCmd,1000);
connect_wifi(atHttpGetCmd,1000);
}
void connect_wifi(String cmd, int t)
{
int temp=0,i=0;
while(1)
{
lcd.clear();
lcd.print(cmd);
Serial.println(cmd);
Serial1.println(cmd);
while(Serial1.available())
{
char const *ok = "OK";
if(Serial1.find(*ok))
i=8;
}
delay(t);
if(i>5)
break;
i++;
}
if(i==8)
{
Serial.println("OK");
lcd.setCursor(0,1);
lcd.print("OK");
}
else
{
Serial.println("Error");
lcd.setCursor(0,1);
lcd.print("Error");
}
}
But im getting this error:
Can anyone help? What im a doing wrong? Im a newbie with Arduino. Thanks!