Go Down

Topic: Posting from ESP8266 to Thingspeak fails sometimes (Read 396 times) previous topic - next topic

Marciokoko

My setup:

The receiver is an UNO receiving data via NRF24L01+ while connected to a laptop USB and an LCD which is being used for verification purposes (to make sure fresh data is being sent).  

The sender is a nano with an NRF24 and a DHT11 powered from 2x18650 batteries being controlled by a bms which is wired to a solar panel.  The nano is about 20 cms away from the UNO for now and is programmed to post every 30 minutes.

Here is are both Tx and Rx.  The issue is that data does get sent every 30 minutes and received by the Rx, but barely any of it gets posted due to errors in the "posting data" part of the code.  Last night I posted from 8pm to today at 6am and it only posted 3 new entries when it should have posted about 20 entries.

Im also posting the resulting code in the SM for the Rx unit which is the one that has the Thingspeak code.  As you can see in the results, sometimes everything goes fine and other times it Fails.:

Code: [Select]

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//UNO 0,1,9,10 - NC
//UNO 2 - ESP Tx
//UNO 3 - ESP Rx
//UNO 7 - NRF-CE
//UNO 8 - NRF-CSN
//UNO11 - NRF-MOSI
//UNO12 - NRF-MISO
//UNO13 - NRF-SCK
//UNOGND- ESP-GND
//UNO3.3- RAIL - NRF-3.3V - ESP3.3V
//UNO5V - LCD-Vcc
//UNOGND- NRF-GND
//UNOGND- LCD-GND
//UNO A4- LCD-SDA
//UNO A5- LCD-SCL
//


#include <SoftwareSerial.h>
#define RX 2
#define TX 3
String AP = "myssid";
String PASS = "mypwd";
String API = "mykey";  
String HOST = "api.thingspeak.com";
String PORT = "80";
String field1 = "field1";
String field2 = "field2";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
SoftwareSerial esp8266Radio(RX,TX);

#define CE_PIN 7
#define CSN_PIN 8
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN);
struct package{
  float humedad = 0;
  float temperatura = 0;
};
typedef struct package Package;
Package dataReceived;
bool newData = false;

void setup() {
  Serial.begin(9600);
  
  setupRadio();

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

void setupRadio(){
  pinMode(10,OUTPUT);
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.openReadingPipe(1, thisSlaveAddress);
  radio.startListening();
  lcd.init();
  lcd.init();
  lcd.backlight();
}

void loop() {
    getData();
    showData();
}

void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1)){
    esp8266Radio.println(command);
    if(esp8266Radio.find(readReplay)){
      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;
 }

void getData() {
    if ( radio.available() ) {
        radio.read( &dataReceived, sizeof(dataReceived) );
        newData = true;
    }
}

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(dataReceived.humedad);
        Serial.println(dataReceived.temperatura);
        lcd.setCursor(1,0);
        lcd.print(dataReceived.humedad);
        lcd.setCursor(1,1);
        lcd.print(dataReceived.temperatura);
        newData = false;
        postDataCloud();
    }
}

void postDataCloud(){
 String getData = "GET /update?api_key="+ API +"&"+ field1 +"="+String(dataReceived.humedad)+"&"+ field2 +"="+String(dataReceived.temperatura);
 Serial.println(getData);
 sendCommand("AT+CIPMUX=1",5,"OK");
 sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
 sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
 esp8266Radio.println(getData);delay(1500);countTrueCommand++;
 sendCommand("AT+CIPCLOSE=0",5,"OK");
}


SENDER NANO
Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <DHT.h>
#include <avr/wdt.h>            
#include <avr/interrupt.h>    
#include <avr/sleep.h>    
#include <avr/power.h>  
#define DHTTYPE DHT11
#define DHT11_PIN 6
#define CE_PIN 7
#define CSN_PIN 8

const byte slaveAddress[5] = {'R','x','A','A','A'};
DHT dht(DHT11_PIN, DHTTYPE);
struct package{
  float humedad = 0;
  float temperatura = 0;
};
typedef struct package Package;
Package data;
RF24 radio(CE_PIN, CSN_PIN);
int nbr_remaining;
#define led 13
ISR(WDT_vect){
        wdt_reset();
}
void configure_wdt(void){
  cli();
  MCUSR = 0;
  WDTCSR |= 0b00011000;
  WDTCSR =  0b01000000 | 0b100001;
  sei();
}
void sleep(int ncycles) {
  nbr_remaining = ncycles;
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  power_adc_disable();
  while (nbr_remaining > 0){
    sleep_mode();
    sleep_disable();
    nbr_remaining = nbr_remaining - 1;
  }
  power_all_enable();
}
void setup(){
  pinMode(led, OUTPUT);
  pinMode(10,OUTPUT);
  dht.begin();
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.setRetries(3,5);
  radio.openWritingPipe(slaveAddress);
  digitalWrite(led, LOW);
  delay(1000);
  configure_wdt();  
  digitalWrite(led, HIGH);
  delay(500);              
  digitalWrite(led, LOW);  
  delay(500);
  digitalWrite(led, HIGH);  
  delay(500);
  digitalWrite(led, LOW);  
  delay(500);

}
void loop(){
  sleep(225);
  send();
  digitalWrite(led, HIGH);  
  delay(500);
  digitalWrite(led, LOW);  
  delay(500);
}
void send() {
  data.humedad = dht.readHumidity();
  data.temperatura = dht.readTemperature();
    bool rslt;
    rslt = radio.write( &data, sizeof(data) );
    Serial.print("Data Sent ");
    Serial.print("data.humedad");
    if (rslt) {
        Serial.println("  Acknowledge received");
        updateMessage();
    }else {
        Serial.println("  Tx failed");
    }
}
void updateMessage() {
}



Marciokoko

Here are the Serial Monitor results:

Code: [Select]

0mand => AT 0. at command => AT OYI
1. at command => AT+CWMODE=1 OYI
2. at command => AT+CWJAP="myssid","mypwd" OYI
Data received 26.00
32.00
GET /update?api_key=mykey&field1=26.00&field2=32.00
3. at command => AT+CIPMUX=1 OYI
4. at command => AT+CIPSTART=0,"TCP","api.thingspeak.com",80 Fail
0. at command => AT+CIPSEND=0,66 Fail
1. at command => AT+CIPCLOSE=0 Fail
-----------------
0mand => AT 0. at command => AT OYI
1. at command => AT+CWMODE=1 OYI
2. at command => AT+CWJAP="myssid","mypwd" OYI
Data received 26.00
32.00
GET /update?api_key=mykey&field1=26.00&field2=32.00
3. at command => AT+CIPMUX=1 OYI
4. at command => AT+CIPSTART=0,"TCP","api.thingspeak.com",80 Fail
0. at command => AT+CIPSEND=0,66 Fail
1. at command => AT+CIPCLOSE=0 Fail
Data received 26.00
32.00
GET /update?api_key=mykey&field1=26.00&field2=32.00
0. at command => AT+CIPMUX=1 OYI
1. at command => AT+CIPSTART=0,"TCP","api.thingspeak.com",80 Fail
0. at command => AT+CIPSEND=0,66 Fail
1. at command => AT+CIPCLOSE=0 Fail
Data received 25.00
33.00
GET /update?api_key=mykey&field1=25.00&field2=33.00
0. at command => AT+CIPMUX=1 OYI
1. at command => AT+CIPSTART=0,"TCP","api.thingspeak.com",80 Fail
0. at command => AT+CIPSEND=0,66 Fail
1. at command => AT+CIPCLOSE=0 Fail
Data received 25.00
33.00
GET /update?api_key=mykey&field1=25.00&field2=33.00
0. at command => AT+CIPMUX=1 OYI
1. at command => AT+CIPSTART=0,"TCP","api.thingspeak.com",80 OYI
2. at command => AT+CIPSEND=0,66 Fail
1. at command => AT+CIPCLOSE=0 Fail
Data received 25.00
33.00
GET /update?api_key=mykey&field1=25.00&field2=33.00
0. at command => AT+CIPMUX=1 OYI
1. at command => AT+CIPSTART=0,"TCP","api.thingspeak.com",80 OYI
2. at command => AT+CIPSEND=0,66 OYI
4. at command => AT+CIPCLOSE=0 OYI
Data received 25.00
33.00
GET /update?api_key=mykey&field1=25.00&field2=33.00
5. at command => AT+CIPMUX=1 OYI
6. at command => AT+CIPSTART=0,"TCP","api.thingspeak.com",80 Fail
0. at command => AT+CIPSEND=0,66 Fail
1. at command => AT+CIPCLOSE=0 Fail
Data received 26.00
32.00
GET /update?api_key=mykey&field1=26.00&field2=32.00
0. at command => AT+CIPMUX=1 OYI
1. at command => AT+CIPSTART=0,"TCP","api.thingspeak.com",80 Fail
0. at command => AT+CIPSEND=0,66 Fail
1. at command => AT+CIPCLOSE=0 Fail
Data received 26.00
32.00
GET /update?api_key=mykey&field1=26.00&field2=32.00
0. at command => AT+CIPMUX=1 OYI
1. at command => AT+CIPSTART=0,"TCP","api.thingspeak.com",80 OYI
2. at command => AT+CIPSEND=0,66 Fail
1. at command => AT+CIPCLOSE=0 Fail
Data received 26.00
32.00
GET /update?api_key=mykey&field1=26.00&field2=32.00
0. at command => AT+CIPMUX=1 OYI
1. at command => AT+CIPSTART=0,"TCP","api.thingspeak.com",80 OYI
2. at command => AT+CIPSEND=0,66 Fail
1. at command => AT+CIPCLOSE=0 Fail
Data received 26.00
32.00
GET /update?api_key=mykey&field1=26.00&field2=32.00
0. at command => AT+CIPMUX=1 OYI
1. at command => AT+CIPSTART=0,"TCP","api.thingspeak.com",80 Fail
0. at command => AT+CIPSEND=0,66 Fail
1. at command => AT+CIPCLOSE=0 Fail

floresta

I have been posting to ThingSpeak without any trouble for several years, originally with an Arduino clone and ethernet shield but currently with an ESP8266.  I have never attempted to communicate from an Arduino to an ESP8266 using the AT command set as you are doing.

I started out by sending data to ThingSpeak and interpreting the responses using my own code but then switched to using the ThingSpeak library.   

You might consider replacing your UNO/ESP8266 combination with NodeMCU or Lolin (Wemos) ESP8266 hardware and also consider using the ThingSpeak library.

Don

Marciokoko

#3
Aug 17, 2019, 06:14 pm Last Edit: Aug 17, 2019, 06:16 pm by Marciokoko
Thanks.  So I have switched to the Thingspeak library and I modified my Rx code to this :

Code: [Select]

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

//UNO 0 - ESP
//UNO 1 - ESP
//UNO 7 - NRF-CE
//UNO 8 - NRF-CSN
//UNO 9 -
//UNO10 -
//UNO11 - NRF-MOSI
//UNO12 - NRF-MISO
//UNO13 - NRF-SCK
//UNOGND- ESP-GND
//UNO3.3- RAIL - NRF-3.3V - ESP3.3V
//UNO5V - LCD-Vcc
//UNOGND- NRF-GND
//UNOGND- LCD-GND
//UNO A4- LCD-SDA
//UNO A5- LCD-SCL

#include "ThingSpeak.h"
#include "WiFiEsp.h"
#include "secrets.h"

char ssid[] = SECRET_SSID;   // your network SSID (name)
char pass[] = SECRET_PASS;   // your network password
int keyIndex = 0;            // your network key Index number (needed only for WEP)
WiFiEspClient  client;

#define CE_PIN 7
#define CSN_PIN 8
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN);
struct package{
  float humedad = 0;
  float temperatura = 0;
};
typedef struct package Package;
Package dataReceived;
bool newData = false;

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(2,3); // RX, TX
#define ESP_BAUDRATE  19200
#else
#define ESP_BAUDRATE  115200
#endif

unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;

// Initialize our values
int number1 = 0;
int number2 = random(0,100);
int number3 = random(0,100);
int number4 = random(0,100);
String myStatus = "";

void setup() {
  //Initialize serial and wait for port to open
  Serial.begin(115200);  // Initialize serial
  
  //NRF
  setupRadio();

  // initialize serial for ESP module  
  setEspBaudRate(ESP_BAUDRATE);
  
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo native USB port only
  }

  Serial.print("Searching for ESP8266...");
  // initialize ESP module
  WiFi.init(&Serial1);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }
  Serial.println("found it!");
  
  ThingSpeak.begin(client);  // Initialize ThingSpeak
}

void setupRadio(){
  pinMode(10,OUTPUT);
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.openReadingPipe(1, thisSlaveAddress);
  radio.startListening();

  lcd.init();
  lcd.init();
  lcd.backlight();
}

void loop() {
    getData();
    showData();
}

void getData() {
    if ( radio.available() ) {
        radio.read( &dataReceived, sizeof(dataReceived) );
        newData = true;
    }
}

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(dataReceived.humedad);
        Serial.println(dataReceived.temperatura);
        lcd.setCursor(1,0);
        lcd.print(dataReceived.humedad);
        lcd.setCursor(1,1);
        lcd.print(dataReceived.temperatura);
        newData = false;
        postDataCloud();
    }
}

void postDataCloud(){
 // Connect or reconnect to WiFi
  if(WiFi.status() != WL_CONNECTED){
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(SECRET_SSID);
    while(WiFi.status() != WL_CONNECTED){
      WiFi.begin(ssid, pass);  // Connect to WPA/WPA2 network. Change this line if using open or WEP network
      Serial.print(".");
      delay(5000);    
    }
    Serial.println("\nConnected.");
  }

  // set the fields with the values
  ThingSpeak.setField(1, dataReceived.humedad);
  ThingSpeak.setField(2, dataReceived.temperatura);

  // figure out the status message
  if(number1 > number2){
    myStatus = String("field1 is greater than field2");
  }
  else if(number1 < number2){
    myStatus = String("field1 is less than field2");
  }
  else{
    myStatus = String("field1 equals field2");
  }
  
  // set the status
  ThingSpeak.setStatus(myStatus);
  
  // write to the ThingSpeak channel
  int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  if(x == 200){
    Serial.println("Channel update successful.");
  }
  else{
    Serial.println("Problem updating channel. HTTP error code " + String(x));
  }  
}

// This function attempts to set the ESP8266 baudrate. Boards with additional hardware serial ports
// can use 115200, otherwise software serial is limited to 19200.
void setEspBaudRate(unsigned long baudrate){
  long rates[6] = {115200,74880,57600,38400,19200,9600};

  Serial.print("Setting ESP8266 baudrate to ");
  Serial.print(baudrate);
  Serial.println("...");

  for(int i = 0; i < 6; i++){
    Serial1.begin(rates[i]);
    delay(100);
    Serial1.print("AT+UART_DEF=");
    Serial1.print(baudrate);
    Serial1.print(",8,1,0,0\r\n");
    delay(100);  
  }
    
  Serial1.begin(baudrate);
}


But it's not posting.  I get this in the serial monitor.  I think it may have posted once to Thingspeak :

Code: [Select]
Setting ESP8266 baudrate to 19200...
Searching for ESP8266...[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 1.5.4
found it!
Data received 14.00
32.00
Attempting to connect to SSID: thanos
[WiFiEsp] Connected to thanos
.
Connected.
[WiFiEsp] Connecting to api.thingspeak.com
[WiFiEsp] Disconnecting  3
Channel update successful.
Data received 14.00
32.00
[WiFiEsp] Connecting to api.thingspeak.com
[WiFiESetting ESP8266 baudrate to 19200...
Searching for ESP8266...[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 1.5.4
found it!
Data received 14.00
32.00
Attempting to connect to SSID: thanos
[WiFiEsp] Connected to thanos
.[WiFiEsp] Connected to thanos
.
Connected.
[WiFiEsp] Connecting to api.thingspeak.com
[WiFiEsp] Disconnecting  3
Channel update successful.
Data received 15.00
32.00
[WiFiEsp] Connecting to api.thingspeak.com
[WiFiEsp] >>> TIMEOUT >>>
-301
Data received 15.00
32.00

floresta

I am not familiar with the WiFiEsp library but some quick research seems to indicate that it is used to communicate between an Arduino and an ESP8266.  I guess it essentially generates the AT commands for you.

It appears that the use of this library allows you to also use the ThingSpeak library but I have never done things that way so I really can't give you any reliable help.  That being said ... your implementation of the ThingSpeak library looks correct to me so my guess is that your problem lies elsewhere. 

You haven't told us anything about the ESP8266 hardware that you are using.  The early ESP 01 devices don't have much in the way of available I/O ports so if that is what you have then you probably need to use the UNO/ESP8266 combination.  If you have or can get one of the ESP 12 based devices that I mentioned before and if you don't need more than one analog input you really should consider ditching the UNO and working solely with the ESP8266.

Don


Marciokoko

Thanks.  So I tried moving the esp/wifi setup code from the setup() to the postCloudData() function thinking the whole esp setup would happen each time the UNO is ready to post, and well, I let it run and at least it posted 4 entries instead of 1 like before.

And yes I understand I can use the ESP by itself, I actually have a set up where it posts data to an instance of emoncms and it has been doing so for quite a while flawlessly.  I just wanted to try other options. 

Unfortunately my project collected data from a dht11 but it also uses radio to send data from the warehouse its located in, remotely, to a receiving UNO which is the one that posts to the cloud.  So the esp wouldnt have space to accept so many pins from the nrf24 module. 

Ill keep messing with it.

Marciokoko

Ok my issue is that it stops "collecting data".  The setup works and posts data to thingspeak a few times and then it stops posting.  The serial monitor stays at the last logged data point.  If I close the SM and reopen it, the mcu restarts (which is normal) and a few minutes later as expected, i get data points logged and posted to thingspeak.

I thought it might be a usb-serial port issue, i dunno why, Ive just always had issues with it.  So I first moved the Rx-UNO to standalone power instead of having it connected to my laptop and it collects data just fine, about 5-6 data points and then it stops.  So nothing had changed.

The next time it hung up, I power cycled the esp8266 on the Rx-UNO just to make sure if it was a problem with posting data because the ESP was stuck, but no new posts after power cycling the esp01 by itself, so the issue must be in the Rx-UNO.

Its not the Tx-Nano because when using the serial monitor and LCD screen, I would get data constantly, non stop.  Im not sure what else to try. 

floresta

Ok my issue is that it stops "collecting data".  The setup works and posts data to thingspeak a few times and then it stops posting.  ...
How often are you trying to send your data to ThingSpeak?

Have you received any nasty messages back from ThingSpeak?

Don

Marciokoko

Every 30 minutes.  No nasty messages from thingspeak.

Go Up