Hello, I am new to this forum and a noob when it comes to programming. I farm and have set up a moisture sensor on a grain dryer where two analog voltages are read by an Uno and the data uploaded to ThingSpeak so I can monitor it remotely. I can get both the moisture and the temperature values to be read and to upload to field 1 in my ThingSpeak channel. But I cannot figure out how to get them both to write in one update, moisture in field 1 and temperature in field 2. I have both fields initiated in ThingSpeak and have successfully written to both fields, but can only write to one or the other. Please find my code below. If you have suggestions of any, I want to learn.
This is are the specific lines in question as to why they won't write to both fields...
// Update ThingSpeak
if(!client.connected() && (millis() - lastConnectionTime > updateInterval))
{
updateThingSpeak("field1="+analogPin0);
updateThingSpeak("field2="+analogPin1);
}
and this is the whole program...
#include <SPI.h>
#include <Ethernet.h>
int moistPin = 0; // analog moisture pin
int tempPin = 1; // analog temperature pin
float calNum = 0; // adjust number for cal
float moist = 0; // moisture variables
float temp = 0;
float temp1 = 0;
float tempCorrection = 0; // temp correction variable
int samples[8]; // variables to make a better precision
int maxi = -100,mini = 100; // to start max/min temperature
int i;
// Local Network Settings
byte mac[] = { x }; // Must be unique on local network
byte ip[] = { x }; // Must be unique on local network
byte gateway[] = { x};
byte subnet[] = { x };
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "x"; // Write API Key for a ThingSpeak Channel
const int updateInterval = 20000; // Time interval in milliseconds to update ThingSpeak
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
// Initialize Arduino Ethernet Client
EthernetClient client;
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, subnet);
delay(1000);
Serial.print("ETHERNET SHIELD ip is : ");
Serial.println(Ethernet.localIP());
// Start Ethernet on Arduino
startEthernet();
}
void loop()
{
temp = ((((analogRead(tempPin) * 5.0) /1023.0)/ .0347)-9.4 );
temp1 = (analogRead(tempPin)); //volts
tempCorrection = ((3.1 - ((analogRead(tempPin) * 5.0) /1023.0)) / .0347 * .05) ;
//added decimals to the intergers
moist = (( 4.8 * analogRead(moistPin) * 5) / 1023) + 6.5 + calNum + tempCorrection;
String analogPin0 = String(moist);
String analogPin1 = String(temp);
String tempvolt = String(temp1);
//Serial.print(tempvolt);
//Serial.print(analogPin1);
//Serial.println();
// Print Update Response to Serial Monitor
if (client.available())
{
char c = client.read();
Serial.print(c);
}
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println();
Serial.println("...disconnected.");
Serial.println();
client.stop();
}
// Update ThingSpeak
if(!client.connected() && (millis() - lastConnectionTime > updateInterval))
{
updateThingSpeak("field1="+analogPin0);
updateThingSpeak("field2="+analogPin1);
}
lastConnected = client.connected();
}
void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
lastConnectionTime = millis();
if (client.connected())
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();
failedCounter = 0;
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();
lastConnectionTime = millis();
}
}
void startEthernet()
{
client.stop();
Serial.println("Connecting Arduino to network...");
Serial.println();
delay(1000);
// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
else {
Serial.println("Arduino connected to network using DHCP");
Serial.println();
Serial.println("Data being uploaded to THINGSPEAK Server.......");
Serial.println();
}
delay(1000);
}