Hello,
I am currently working having temperature and humidity sensors work with the Arduino board using an ESP8266 for wifi. Currently the pins for the WiFi module are set to pins 2 and 3 for RX and TX respectively. However, when I switch the pins within the code to pins 0 and 1 for RX and TX respectively. I receive an error code to this degree.
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00 Failed uploading: uploading error: exit status 1
I am unsure what needs to be changed in order for the parts to work on these pins. Any help would be greatly appreciated. Source code will be shown below.
#include <SoftwareSerial.h>
#include <dht11.h>
#define RX 2 // TX of ESP8266 connected to pin2 of Arduino
#define TX 3 // RX of ESP8266 connected to pin3 of Arduino
#define dht_apin 6 // Analog Pin sensor is connected to
#define dht_bpin 7 // Second analog pin sensor is connected to
dht11 dhtObject; // One Sensor Object
dht11 dhtObject2; // second sensor object
String AP = "Test Hotspot2"; // AP NAME
String PASS = "Ssss1234"; // AP PASSWORD
String API = "27X9GGXRFHBBJ4ED"; // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
SoftwareSerial esp8266(RX,TX);
void setup() {
Serial.begin(9600);
esp8266.begin(38400);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
void loop() {
String getData = "GET /update?api_key="+ API +"&field1="+getTemperatureValue()+"&field2="+getHumidityValue()+"&field3="+getTemperatureValue2()+"&field4="+getHumidityValue2();
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");
}
String getTemperatureValue() {
dhtObject.read(dht_apin);
Serial.print(" Temperature(C)= ");
int temp = dhtObject.temperature;
Serial.println(temp);
delay(50);
return String(temp);
}
String getHumidityValue() {
dhtObject.read(dht_apin);
Serial.print(" Humidity in %= ");
int humidity = dhtObject.humidity;
Serial.println(humidity);
delay(50);
return String(humidity);
}
String getTemperatureValue2() {
dhtObject2.read(dht_bpin);
Serial.print(" Temperature(C)= ");
int temp2 = dhtObject2.temperature;
Serial.println(temp2);
delay(50);
return String(temp2);
}
String getHumidityValue2() {
dhtObject2.read(dht_bpin);
Serial.print(" Humidity in %= ");
int humidity2 = dhtObject2.humidity;
Serial.println(humidity2);
delay(50);
return String(humidity2);
}
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("Pass");
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}