Data sent to ThingSpeak only twice

9 views (last 30 days)
Iva Jerabkova
Iva Jerabkova on 5 Jul 2020
I am making a sample project from Arduino Project Hub - Plant Communicator
On TS I receive data only 2 times after I upload the sketch. I want to upload data every minute, the first two minutes are successful, after that my Serial monitor shows "message sent to cloud" but nothing new shows up on TS. I also tried with another sketch that just generates numbers and uploads them on TS to make sure that there is nothing wrong with the sensors that I use in this project. With the sketch that generates random numbers and sends them to TS, I get the same outcome: data is uploaded first and second time, after that nothing. (Both sketches compiled without errors. Instead of using WiFi I tried to make a hotspot from my phone - doesn't help either.)
Thanks for any help!
#include "arduino_secrets.h"
#include <WiFi101.h>
#include<WiFiSSLClient.h>
#include <RTCZero.h>
#include "ThingSpeak.h"
const char* ssid = SECRET_SSID; // your network SSID (name)
const char* password = SECRET_PSWD; // your network password
WiFiClient ThingSpeakClient;
unsigned long myChannelNumber = 10;
const char * myWriteAPIKey = SECRET_WRITE_API;
RTCZero rtc; // create RTC object
/* Change these values to set the current initial time */
const byte seconds = 0;
const byte minutes = 28;
const byte hours = 17;
/* Change these values to set the current initial date */
const byte day = 4;
const byte month = 12;
const byte year = 17;
int lightPin = A0; //the analog pin the light sensor is connected to
int tempPin = A1; //the analog pin the TMP36's Vout (sense) pin is connected to
int moisturePin= A2;
// Set this threeshold accordingly to the resistance you used
// The easiest way to calibrate this value is to test the sensor in both dry and wet earth
int threeshold= 800;
bool alert_already_sent=false;
bool email_already_sent=true;
bool already_sent_to_cloud=true;
void setup() {
Serial.begin(9600);
while(!Serial);
delay(2000);
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
while (WiFi.begin(ssid, password) != WL_CONNECTED) {
Serial.print(".");
delay(1500);
}
Serial.println("");
Serial.println("WiFi connected");
rtc.begin(); // initialize RTC 24H format
rtc.setTime(hours, minutes, seconds);
rtc.setDate(day, month, year);
rtc.setAlarmTime(17, 30, 0); // Set the time for the Arduino to send the email
ThingSpeak.begin(ThingSpeakClient);
rtc.setAlarmTime(0, 0, 0); //in this way the request is sent every minute at 0 seconds
rtc.enableAlarm(rtc.MATCH_SS);
rtc.attachInterrupt(thingspeak_alarm);
}
void loop() {
if(!already_sent_to_cloud){
ThingSpeak.setField(1,get_light());
ThingSpeak.setField(2,get_temperature());
ThingSpeak.setField(3,get_average_moisture());
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(1500);
already_sent_to_cloud=true;
delay(1500);
Serial.println("message sent to cloud");
delay(1500);
}
}
float get_temperature(){
int reading = analogRead(tempPin);
float voltage = reading * 3.3;
voltage /= 1024.0;
// Print tempeature in Celsius
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
// Convert to Fahrenheit
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
return temperatureC;
}
int get_average_moisture(){ // make an average of 10 values to be more accurate
int tempValue=0; // variable to temporarly store moisture value
for(int a=0; a<10; a++){
tempValue+=analogRead(moisturePin);
delay(1000);
}
return tempValue/10;
}
int get_light(){
int light_value=analogRead(A0);
return light_value;
}
void thingspeak_alarm(){
already_sent_to_cloud=false;
}

Answers (2)

Vinod
Vinod on 6 Jul 2020
Edited: Vinod on 6 Jul 2020
My guess is the RTC is interrupting the network stack during the transmission of data to ThingSpeak. Maybe eliminate the RTC interrupts/alarms from the picture by using millis() to time between writes to ThingSpeak? So something like:
long lastWriteAt;
...
void loop() {
...
if (millis() - lastWriteAt > 20000) {
ThingSpeak.setField(1,get_light());
ThingSpeak.setField(2,get_temperature());
ThingSpeak.setField(3,get_average_moisture());
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.println("message sent to cloud");
lastWriteAt = millis();
}
}
Keep in mind the "Notes and Warnings" in the millis() reference when modifying the if() condition.

JOSE JAVIER Anaya Velayos
JOSE JAVIER Anaya Velayos on 18 Jan 2024
I have the same problem. I only receive twice in thingspeak and I am not using RTC. I send data every 1 minute. Here is my code:
#include <SPI.h>
#include <WiFi101.h>
//Termopares
#include "max6675.h"
#include <TemperatureZero.h>
TemperatureZero TempZero = TemperatureZero();
char ssid[] = "****************";
char pass[] = "***************";
int status = WL_IDLE_STATUS;
// Initialize the Wifi client library
WiFiClient client;
// ThingSpeak Settings
char server[] = "api.thingspeak.com";
String writeAPIKey = "*******";
unsigned long lastConnectionTime = 0; // track the last connection time
const unsigned long postingInterval = 60L * 1000L; // post data every 60 seconds
//Termopares
int ktcSO = 8;
int ktcCS1 = 9; // chip select primer MAX6675
int ktcCLK = 10;
int ktcCS2 = 7; // chip select segundo MAX6675
int ktcCS3 = 6; // chip select tercer MAX6675
int ktcCS4 = 5; // chip select tercer MAX6675
int ktcCS5 = 4; // chip select tercer MAX6675
int ktcCS6 = 3; // chip select tercer MAX6675
MAX6675 ktc1(ktcCLK, ktcCS1, ktcSO);
MAX6675 ktc2(ktcCLK, ktcCS2, ktcSO);
MAX6675 ktc3(ktcCLK, ktcCS3, ktcSO);
MAX6675 ktc4(ktcCLK, ktcCS4, ktcSO);
MAX6675 ktc5(ktcCLK, ktcCS5, ktcSO);
MAX6675 ktc6(ktcCLK, ktcCS6, ktcSO);
float temp1, temp2,temp3,temp4, temp5,temp6;
void setup() {
// attempt to connect to Wifi network
while ( status != WL_CONNECTED) {
// Connect to WPA/WPA2 Wi-Fi network
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection
delay(10000);
}
httpRequest();
TempZero.init();
}
void loop() {
// if interval time has passed since the last connection,
// then connect again and send data
status = WiFi.status();
while ( status != WL_CONNECTED) {
// Connect to WPA/WPA2 Wi-Fi network
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection
delay(10000);
}
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
void httpRequest() {
Serial.print("Deg C 1 = ");
double temp1 =ktc1.readCelsius();
Serial.print(temp1);
Serial.print("\n");
Serial.print("Deg C 2 = ");
double temp2 =ktc2.readCelsius();
Serial.print(temp2);
Serial.print("\n");
Serial.print("Deg C 3 = ");
double temp3 =ktc3.readCelsius();
Serial.print(temp3);
Serial.print("\n");
Serial.print("Deg C 4 = ");
double temp4 =ktc4.readCelsius();
Serial.print(temp4);
Serial.print("\n");
Serial.print("Deg C 5 = ");
double temp5 =ktc5.readCelsius();
Serial.print(temp5);
Serial.print("\n");
Serial.print("Deg C 6 = ");
double temp6 =ktc6.readCelsius();
Serial.print(temp6);
Serial.print("\n");
//Internal Temperature (Chip)
float temperature = TempZero.readInternalTemperature();
Serial.print("Internal Temperature is : ");
Serial.println(temperature);
// create data string to send to ThingSpeak
String data = String("field1=" + String(temp1) + "&field2=" + String(temp2)+ "&field3=" + String(temp3)+ "&field4=" + String(temp4) + "&field5="+ String(temp5) + "&field6="+ String(temp6) +"&field7="+ String(temperature));
// close any connection before sending a new request
client.stop();
// POST data to ThingSpeak
int aux=client.connect(server, 80);
Serial.println(aux);
if (aux) {
client.println("POST /update HTTP/1.1");
client.println("Host: api.thingspeak.com");
client.println("Connection: close");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("X-THINGSPEAKAPIKEY: "+writeAPIKey);
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.print(data.length());
client.print("\n\n");
client.print(data);
}
else{
Serial.print("No entra en ThingSpeak\n");
}
// note the last connection time
lastConnectionTime = millis();
}

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Prepare and Analyze Data in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!