Data not uploading to thingspeak | Arduino | 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
Data not uploading to thingspeak
No permission to create posts
February 12, 2018
10:13 am
Avatar
Bird
New Member
Members
Forum Posts: 4
Member Since:
January 30, 2018
sp_UserOfflineSmall Offline

Hi,

 

I'm having issues with uploading my data to thingspeak, I'm not sure why my data is not uploading, as other code will upload data from the same sensors to a the channel.

I've tried multiple locations for ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); line, all of which will no upload data to the website. 

It's not the case that new empty fields are added online as there are no new entires made, and I know that the API and channel are correct as another sample piece of code uploads data (only pressure and temperature).

The difficulty comes when adding in the data for the additional sensors.

However, all sensors are correctly outputting data to the serial.

In short, I'm a little confused and not sure why it isn't uploading anything.

Any pointers at all massively appreciated!

Thanks

 

#include <OneWire.h>
#include <Wire.h>
#include <MS5803_14.h>
#include <EEPROM.h>
#include "ThingSpeak.h"

 

#define USE_ETHERNET_SHIELD
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetClient client;

 

#define StartConvert 0
#define ReadTemperature 1
#define EEPROM_write(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) EEPROM.write(address+i, pp[i]);}
#define EEPROM_read(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) pp[i]=EEPROM.read(address+i);}
#define compensationFactorAddress 8 //the address of the factor stored in the EEPROM
#define VREF 5000 //for arduino uno, the ADC reference is the power(AVCC), that is 5000mV
float compensationFactor;

const byte numReadings = 20; //the number of sample times
byte ECsensorPin = A1; //EC Meter analog output,pin on analog 1
byte DS18B20_Pin = 2; //DS18B20 signal, pin on digital 2
unsigned int AnalogSampleInterval = 25, printInterval = 1000, tempSampleInterval = 1000; //analog sample interval;serial print interval;temperature sample interval
unsigned int readings[numReadings]; // the readings from the analog input
byte index = 0; // the index of the current reading
unsigned long AnalogValueTotal = 0; // the running total
unsigned int AnalogAverage = 0, averageVoltage = 0; // the average
unsigned long AnalogSampleTime, printTime, tempSampleTime;
float temperature, ECcurrent, ECcurrentRaw;

//Temperature chip i/o
OneWire ds(DS18B20_Pin); // on digital pin 2

//Pressure sensor
MS_5803 sensor = MS_5803(4096);
float pressure;
float prestemperature;

unsigned long myChannelNumber = 413693;
const char * myWriteAPIKey = "6RWGP1O11WFOGKDC";

void setup() {

Ethernet.begin(mac);
ThingSpeak.begin(client);
// initialize serial communication with computer:
Serial.begin(9600);

 

// initialize all the readings to 0:

for (byte thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
TempProcess(StartConvert); //let the DS18B20 start the convert
readCharacteristicValues();
AnalogSampleTime = millis();
printTime = millis();
tempSampleTime = millis();
if (sensor.initializeMS_5803()) {
Serial.println( "MS5803 CRC check OK." );
}
else {
Serial.println( "MS5803 CRC check FAILED!" );
}
Serial.println("CLEARDATA");
Serial.println("Analog value,Voltage(mV),Temperature,EC, Pressure(mBar), Pressuretemp");

 

delay(3000);
}

void loop()
{

 

/*
Every once in a while,sample the analog value and calculate the average.
*/
sensor.readSensor();
float pressure = sensor.pressure();
float prestemperature = sensor.temperature();

 

if (millis() - AnalogSampleTime >= AnalogSampleInterval)
{
AnalogSampleTime = millis();
// subtract the last reading:
AnalogValueTotal = AnalogValueTotal - readings[index];
// read from the sensor:
readings[index] = analogRead(ECsensorPin);
// add the reading to the total:
AnalogValueTotal = AnalogValueTotal + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
AnalogAverage = AnalogValueTotal / numReadings;
}

/*
Every once in a while,MCU read the temperature from the DS18B20 and then let the DS18B20 start the convert.
Attention:The interval between start the convert and read the temperature should be greater than 750 millisecond,or the temperature is not accurate!
*/
if (millis() - tempSampleTime >= tempSampleInterval)
{
tempSampleTime = millis();
temperature = TempProcess(ReadTemperature); // read the current temperature from the DS18B20
TempProcess(StartConvert); //after the reading,start the convert for next reading
}
/*
Every once in a while,print the information on the serial monitor.
*/
if (millis() - printTime >= printInterval)
{
printTime = millis();
averageVoltage = AnalogAverage * (float)5000 / 1024;

ThingSpeak.setField( 1, String(AnalogAverage));
ThingSpeak.setField( 2, String(averageVoltage));
ThingSpeak.setField( 3, String(temperature));
Serial.print(AnalogAverage); //analog average,from 0 to 1023
Serial.print(", ");
Serial.print(averageVoltage); //millivolt average,from 0mv to 4995mV
Serial.print(", ");
Serial.print(temperature); //current temperature
Serial.print(", ");

 

float TempCoefficient = 1.0 + 0.0185 * (temperature - 25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.0185*(fTP-25.0));
float CoefficientVolatge = (float)averageVoltage / TempCoefficient;

 

if (CoefficientVolatge < 50)
{

ThingSpeak.setField( 5, String(pressure));
ThingSpeak.setField( 6, String(prestemperature));
Serial.print("No solution!"); //25^C 1413us/cm<-->about 216mv if the voltage(compensate)<150,that is <1ms/cm,out of the range
Serial.print (", ");
Serial.print(pressure);
Serial.print(", ");
Serial.println(prestemperature);

 

ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

 

}

else if (CoefficientVolatge > 3300)
{

 

ThingSpeak.setField( 5, String(pressure));
ThingSpeak.setField( 6, String(prestemperature));
Serial.println("Out of the range!"); //>20ms/cm,out of the range
Serial.print (", ");
//Serial.print(milli_time);
Serial.print(pressure);
Serial.print(", ");
Serial.println(prestemperature);

 

ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

 

}
else
{
if (CoefficientVolatge <= 448)ECcurrent = 6.84 * CoefficientVolatge - 64.32; //1ms/cm<EC<=3ms/cm
else if (CoefficientVolatge <= 1457)ECcurrent = 6.98 * CoefficientVolatge - 127; //3ms/cm<EC<=10ms/cm
else ECcurrent = 5.3 * CoefficientVolatge + 2278; //10ms/cm<EC<20ms/cm
ECcurrentRaw = ECcurrent / 1000;
ECcurrent = ECcurrent / compensationFactor / 1000;

ThingSpeak.setField( 4, String(ECcurrent));
ThingSpeak.setField( 5, String(pressure));
ThingSpeak.setField( 6, String(prestemperature));

Serial.print(ECcurrent, 2); //two decimal
Serial.print(", ");
Serial.print(pressure);
Serial.print(", ");
Serial.println(prestemperature);

 

ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

 

}

 

}

delay (16000);
}

 

int getMedianNum(int bArray[], int iFilterLen)
{
int bTab[iFilterLen];
for (byte i = 0; i < iFilterLen; i++)
{
bTab[i] = bArray[i];
}
int i, j, bTemp;
for (j = 0; j < iFilterLen - 1; j++)
{
for (i = 0; i < iFilterLen - j - 1; i++)
{
if (bTab[i] > bTab[i + 1])
{
bTemp = bTab[i];
bTab[i] = bTab[i + 1];
bTab[i + 1] = bTemp;
}
}
}
if ((iFilterLen & 1) > 0)
bTemp = bTab[(iFilterLen - 1) / 2];
else
bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
return bTemp;
}

void readCharacteristicValues()
{
EEPROM_read(compensationFactorAddress, compensationFactor);
if (EEPROM.read(compensationFactorAddress) == 0xFF && EEPROM.read(compensationFactorAddress + 1) == 0xFF && EEPROM.read(compensationFactorAddress + 2) == 0xFF && EEPROM.read(compensationFactorAddress + 3) == 0xFF)
{
compensationFactor = 1.0; // If the EEPROM is new, the compensationFactorAddress is 1.0(default).
EEPROM_write(compensationFactorAddress, compensationFactor);
}
}

/*
ch=0,let the DS18B20 start the convert;ch=1,MCU read the current temperature from the DS18B20.
*/

float TempProcess(bool ch)
{
//returns the temperature from one DS18B20 in DEG Celsius
static byte data[12];
static byte addr[8];
static float TemperatureSum;
if (!ch) {
if ( !ds.search(addr)) {
Serial.println("no more sensors on chain, reset search!");
ds.reset_search();
return 0;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return 0;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized!");
return 0;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
}
else {
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
TemperatureSum = tempRead / 16;
}
return TemperatureSum;
}

February 13, 2018
4:37 pm
Avatar
cstapels
Moderator
Members


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

It looks like you are doing a lot of rapid write fields commands.  Im not sure about all the else conditions though. There is a limit on the rate of write operations, they are different for different users, but generally much slower than the arduino loop.  You should only need one 

ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

for all set fields operations.  Each writeFields command counts as a message against the rate limit for ThingSpeak.

The format should be similar to this

.setFields(1..)

.setFields(7..)

.setFields(4..)

.setFields(3..)

...

.writeFields(..)

Wait a while.

repeat.

You could try a simplified code that sets fixed numbers for your fields and then writes and then waits.  That would also help debug if it is a memory issue.  Let us know if that helps.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
23 Guest(s)

Currently Browsing this Page:
2 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

JamesVex, meghanfr69, raqueldk69, soniafw2, vitalijDrine, JosephAmapy

Administrators: Hans: 387, lee: 457