Failing to post data to thingspeak | ThingSpeak API | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
Failing to post data to thingspeak
No permission to create posts
August 15, 2019
10:01 pm
Avatar
marciokoko

Silver
Members
Forum Posts: 10
Member Since:
August 15, 2019
sp_UserOfflineSmall Offline

I'm using an Arduino and an Esp8266 (esp01) to post data to the api. Here is the part of the Arduino code that posts the data:

void postDataCloud(){
String getData = "GET /update?api_key="+ API +"&"+ field1 +"="+String(dataReceived.humedad)+"&"+ field2 +"="+String(dataReceived.temperatura);
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");
}

but I get this in my serial monitor:

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

And the data doesn't get posted.

How can I debug this?

August 16, 2019
4:22 pm
Avatar
marciokoko

Silver
Members
Forum Posts: 10
Member Since:
August 15, 2019
sp_UserOfflineSmall Offline

I left it running all night and oddly enough it did post SOME data points. 3 in total, (26,32), (25,33) and (16,29) which dont show up in the SM log but they seem like reasonable values. Below is the SM output for the remainer of the night.

[code]
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 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
Data received 21.00
31.00
GET /update?api_key=mykey&field1=21.00&field2=31.00
0. at command => AT+CIPMUX=1 OYI
1. at command => AT+CIPSTART=0,"TCP","api.thingspeak.com",80
[/code]

August 16, 2019
5:04 pm
Avatar
Vinod

MathWorks
Members
Forum Posts: 327
Member Since:
May 1, 2016
sp_UserOfflineSmall Offline

Have you taken a look at the examples in the ThingSpeak Library here: https://github.com/mathworks/thingspeak-arduino

I usually recommend people try the library than try to roll your own code.

August 16, 2019
7:14 pm
Avatar
marciokoko

Silver
Members
Forum Posts: 10
Member Since:
August 15, 2019
sp_UserOfflineSmall Offline

oh well I hadnt thought of looking for a library for it but it makes total sense.

Anyway Im looking into it and since I have an UNO sending via ESP, I would need to use AT Commands examples which is this one:
https://github.com/mathworks/thingspeak-arduino/blob/master/examples/ESP8266/via%20AT%20commands/WriteMultipleFields/WriteMultipleFields.ino

although it doesnt really have any at commands in the sketch, right?

August 16, 2019
7:32 pm
Avatar
marciokoko

Silver
Members
Forum Posts: 10
Member Since:
August 15, 2019
sp_UserOfflineSmall Offline

One more thing, I'm compiling but I get a WiFiEsp.h library reference not found. Is that from a different repo?

Found it while reading the example...thanks

August 17, 2019
1:02 pm
Avatar
marciokoko

Silver
Members
Forum Posts: 10
Member Since:
August 15, 2019
sp_UserOfflineSmall Offline

Thanks. So I have switched to the Thingspeak library and I modified my Rx code to this :

#include
#include
LiquidCrystal_I2C lcd(0x27,20,4);

#include
#include
#include

//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("
Connected.");
}

// 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 >> TIMEOUT >>>
-301
Data received 15.00
32.00

August 18, 2019
12:39 pm
Avatar
marciokoko

Silver
Members
Forum Posts: 10
Member Since:
August 15, 2019
sp_UserOfflineSmall Offline

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. But in the end it still stopped posting after that.

I guess it has something to do with the esp's connection to my wifi maybe timing out or something.

August 18, 2019
5:13 pm
Avatar
marciokoko

Silver
Members
Forum Posts: 10
Member Since:
August 15, 2019
sp_UserOfflineSmall Offline

Ok so now I can see what happened in the SM:

Data received 30.00
33.00
Setting ESP8266 baudrate to 19200...
Searching for ESP8266...[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 1.5.4
found it!
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 25.00
33.00
Setting ESP8266 baudrate to 19200...
Searching for ESP8266...[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 1.5.4
found it!
Attempting to connect to SSID: thanos
[WiFiEsp] Connected to thanos
.
Connected.
[WiFiEsp] Connecting to api.thingspeak.com

here is where it hung....and didnt post to lcd either, so I dont understand where it is getting stuck since the order is:

getData()-gets data from NRF24 radio...
showData()-prints data to lcd...
postDataCloud()...gets esp ready and posts to ts

According to that order, it received the data 25 & 33, this is from the showData(), which is after the getData(). So its getting the data fine, its not showing it on the LCD, but it is going to postDataCloud() fine, because it shows lines about finding the esp and connecting to the ssid, so its a problem of the esp connecting to the ts api and then not trying again.

August 19, 2019
9:03 am
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 771
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

Are you able to successfully post data now?

August 19, 2019
1:23 pm
Avatar
marciokoko

Silver
Members
Forum Posts: 10
Member Since:
August 15, 2019
sp_UserOfflineSmall Offline

Yes, intermittently but the problem is that after a certain point, it stops posting completely..

August 19, 2019
1:33 pm
Avatar
Vinod

MathWorks
Members
Forum Posts: 327
Member Since:
May 1, 2016
sp_UserOfflineSmall Offline

I've seen this happen before when the code is leaking memory. After a while the code either runs out of runtime memory, or, gets overwritten by the stack, and stops working. If the problem goes away if you power-cycle your ESP8266 and nothing else, this would be the problem I would bet money on. The memory leak could be in your code or potentially even libraries you may be using.

August 19, 2019
8:38 pm
Avatar
marciokoko

Silver
Members
Forum Posts: 10
Member Since:
August 15, 2019
sp_UserOfflineSmall Offline

I remember having a similar issue when using my first esp01 to post t&h via an mcu to emoncms until I discovered I could use the esp01 standalone and I ditched the mcu. In this case I can't because I use Nrf24l01 radios to transmit and receive before the receiver posts to the cloud via wifi. So my sender needs pins for the Nrf and for the dht11 and it has no access to wifi. But the receiver also needs pins for the Nrf and the Esp, so I couldn't connect the Esp to the Nrf radio directly.

My option would be to receive and use a more stable wifi shield or module.

August 20, 2019
11:54 am
Avatar
marciokoko

Silver
Members
Forum Posts: 10
Member Since:
August 15, 2019
sp_UserOfflineSmall Offline

Today I power cycled the esp01 module by unplugging and replugging the vcc cable (3.3V) and without touching the rest of the setup, but I have not received any new data.

Other times I usually reset the mcu by closing and opening the Serial Monitor in the Arduino IDE and i start getting new data. So Its an issue with the mcu, not the esp.

So to sum up:
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.

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
21 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

Hema, askask, Rakwireless, budiardi basuki, oliver009, shelby1234

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457