can single arduino + esp8266 work with RF receiver?
i don’t know if it is the coding or not …, please help me
//include anything needed for sensor
#include<SoftwareSerial.h>
#include <VirtualWire.h>
SoftwareSerial SoftSerial(10,11); // RX, TX pin
int i,a=0,b=0,c=0;
#define SSID "Lenovo P70-A"
#define PASS "aaaa4444"
#define IP "184.106.153.149" //api.thingspeak.com IP Address
String apiKey = "85OUN0FQEUFO576B"; //Thingspeak key
// Initialization (the int, boolean, etc)
int retry_attempt = 0;
void setup() { //setup for sensors
vw_set_ptt_inverted(true);
vw_set_rx_pin(12); //receiver pin
Serial.begin(9600);
vw_setup(4000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
SoftSerial.begin(9600);
delay(100);
// reset ESP8266 (no need change)
debugPrint("AT+RST");
debugPrint("AT");
if(SoftSerial.find("OK")){
Serial.println("ESP8266 detected!");
connectWiFi();
}
else{
Serial.println("ESP8266 not detected!");
debugPrint("AT+RST");
connectWiFi();
}
}
void loop() {
// Read all the sensors' data
uploadData();
// thingspeak has a 15 secs interval between updates
delay(100);
}
void uploadData()
{
uint8_t buf[VW_MAX_MESSAGE_LEN]; //msg
uint8_t buflen = VW_MAX_MESSAGE_LEN;//lenght of the msg
if (vw_get_message(buf, &buflen)) // Non-blocking, received the data
{
Serial.println("Received");
for(i=1;i==0;i--)
{ //receive 11, switch one and Mode:on
if( buf[0]=='1' && buf[1]=='1' )
{Serial.println(11);a=1;}
if( buf[0]=='1' && buf[1]=='0' )
{Serial.println(10);a=0;}
}
for(i=1;i==0;i--)
{ //receive 21, switch two and Mode:on
if(buf[0]='2')
{
if(buf[1]=='1')
{Serial.println(21);b=1;}
if(buf[1]=='0')
{Serial.println(20);b=0;}
}
}
for(i=1;i==0;i--)
{ //receive 31, switch three and Mode:on
if(buf[0]=='3')
{
if(buf[1]=='1')
{Serial.println(31);c=1;}
if(buf[1]=='0')
{Serial.println(30);c=0;}
}
}
}
char buffer1[16];
char buffer2[16];
char buffer3[16];
String strA = dtostrf(a, 4, 1, buffer1);
String strB = dtostrf(b, 4, 1, buffer2);
String strC = dtostrf(c, 4, 1, buffer3);
Serial.print("a");Serial.print(a);
Serial.print("b");Serial.print(b);
Serial.print("c");Serial.print(c);
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
SoftSerial.println(cmd);
if(SoftSerial.find("Error"))
{Serial.println("AT+CIPSTART error");return;}
// Prepare GET string(edit this for our sensors)
String getStr = "GET /update?api_key=";
getStr += apiKey;
getStr += "&field1=";
getStr += String(strA);
getStr += "&field2=";
getStr += String(strB);
getStr += "&field3=";
getStr += String(strC);
getStr += "\r\n\r\n";
// send data length (no change)
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
SoftSerial.println(cmd);
if(SoftSerial.find(">")){
debugPrint(getStr);
}
else{
SoftSerial.println("AT+CIPCLOSE");
Serial.println("AT+CIPCLOSE");
connectWiFi();
}
}
void debugPrint(String cmd)
{SoftSerial.println(cmd);Serial.println(cmd);}
boolean connectWiFi(){
delay(5000);
SoftSerial.println("AT+CWMODE=1");
SoftSerial.flush();
if(SoftSerial.find("OK"))
{Serial.println("STA mode Selected!");}
String cmd = "AT+CWJAP=\"";
cmd += SSID;
cmd += "\",\"";
cmd += PASS;
cmd += "\"";
debugPrint(cmd);
SoftSerial.flush();
delay(5000);
if(SoftSerial.find("OK"))
{Serial.println("Succesfully joined AP");return true;}
else{
retry_attempt++;
Serial.print("Error connecting to AP \n Retry attempt:");
Serial.println(retry_attempt);
return connectWiFi();
}
}