0

I have been trying to push data into my API from Arduino UNO with ESP8266. The device connects to WiFi but fails in performing the data push after acquiring from remote sensor node with the nRF24L01 module attached to the Arduino. Here is the receiver node code

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
#include <printf.h>
#include <SoftwareSerial.h>

#define RX 10
#define TX 7 //for SoftwareSerial to communicate with the Tx and Rx of the WiFi module

RF24 radio(8,9); //CE,CSN

const byte address[6] = "00001";

String AP = "*****";
String PASS = "******";

String API = "9O1EQOC9QR5LMSS9";
String HOST = "api.thingspeak.com";
String PORT = "80";
String field1 = "field1";

int countTrueCommand;
int countTimeCommand;
boolean found = false;

int data = 0;
int value = 1;

SoftwareSerial esp8266(RX, TX);

void setup() {
  Serial.begin(9600);
  Serial.println(F("This is the central receiving node"));

  esp8266.begin(115200);
  sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
  sendCommand("AT+CWJAP=\""+AP+"\",\""+PASS+"\"",20,"OK");
  countTrueCommand = 0;

//  radio.begin();
//  radio.openReadingPipe(0, address);  //setting the address at which we will receive the data
//  radio.setPALevel(RF24_PA_MAX);  //can set this as minimum or maximum depending on the distance between the transmitter and the receiver
//  radio.startListening(); //it sets the module to receiver mode
//  Serial.print("Radio set for communication");
//  radio.printDetails();
}

void loop() {
    String getData1 = "GET /update?api_key="+ API +"&"+ field1 +"="+String(data);

    Serial.println(getData1);
    switch (countTrueCommand)
    {
      case 0: {
        sendCommand("AT", 5,"OK");
        break;
      }
//      case 1: {
//        sendCommand("AT+RST", 10, "invalid");
//        break;
//      }
      case 1: {
        sendCommand("AT+CIPMUX=1",10,"OK");
        break;
      }
      case 2: {
        sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
        break;
      }
      case 3: {
        sendCommand("AT+CIPSEND=0," +String(getData1.length()+4),4,">");
        break;
      }
      case 4: {
        esp8266.println(getData1);
        delay(1500);
        countTrueCommand++;
        break;
      }
      case 5: {
        sendCommand("AT+CIPCLOSE=0",5,"OK");
        break;
      }
      case 6: {
        Serial.println(data);
        Serial.print(getData1);
        Serial.print(",");
        if (radio.available())
        {
          radio.read(&value, sizeof(value));
          data = value;
        }
        else{
          data = data;
        }
        countTrueCommand = 0;
        delay(10000);
        break;
      }
    }
  delay(2000);
}

void sendCommand(String command, int maxTime, char readReply[])
{
  Serial.print(countTrueCommand);
  Serial.print(". at command => ?");
  Serial.print(command);
  Serial.print(" ");
  //Serial.print("inside sendCommand loop");
  while(countTimeCommand < (maxTime*1))
  {
    esp8266.println(command); //at+cipsend
    if(esp8266.find(readReply)) //ok
    {
      found = true;
      break;
    }
    countTimeCommand++;
  }
  if(found == true)
  {
    Serial.println("Found!");
    countTrueCommand++;
    countTimeCommand = 0;
  }
  if(found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
  //delay(500);
  found = false;
}

The code with the commented setup function works fine and updates the API. If I remove the comments from the setup function and try to setup the radio, further WiFi communication fails

  • you have a pin conflict. RF24 uses SPI. SPI is on pins 11, 12, 13. you use pin 11 for SoftwareSerial to esp8266 – Juraj Feb 13 at 11:07
  • @Juraj I changed my Software serial Tx from 11 to 7 but am still facing the same issue – ann Feb 13 at 11:42
  • RF interference? why do you need to transfer the data first over RF? isn't WiFi available at the sensor location? – Juraj Feb 13 at 15:15
  • 2
    @Juraj No WiFi is not abled for the remote sensing nodes. There is whole wireless sensor network built over nRF24 actually and they are communicating to the base node in a tree. Adding WiFi to each and every node would not be more effective wouldn't it! – ann Feb 14 at 5:39
  • how are the modules powered? – Juraj Feb 18 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.