Hello guys, i'm making a project for my university where Arduino Ethernet should measure the voltage, current, power and energy consumed of a system and then upload the data to thingspeak.com.
The problem is that the data transfer from arduino to thingspeak is very slow but i need it to be in real time or as close as posible to real time..
I searched the web but couldn't find a way to change the rate of uploading. Also my arduino board is equipted with a PoE module but when i remove the usb it doesn't work. Any ideas why?
This is the code i'm using. I would appreciate any help!
int readValue;
int amps;
int totamps;
int avgamps;
float watt;
int time;
int amphr;
float energy;
float average;
float Voltage;
#include <LiquidCrystal.h> //Συμπεριλαμβάνει την library της LCD module για ευκολότερη χρήση.
LiquidCrystal lcd(8, 9, 5, 7, 3, 2); //Προσδιορισμός pins στα οποία είναι συνδεδεμένη η οθόνη LCD.
#include <SPI.h> //Συμπεριλαμβάνει την library της SPI.
#include <Ethernet.h>
// Local Network Settings
byte mac[] = { 0x2C, 0x33, 0x7A, 0xF9, 0x84, 0x3F }; // Must be unique on local network
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "R0IC3BJ3042RB8RY";
const int updateThingSpeakInterval = 1 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
// Initialize Arduino Ethernet Client
EthernetClient client;
//Συμπεριλαμβάνει την library τoυ Ethernet.
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
void setup() {
lcd.begin(16, 2);
Serial.begin(9600); // sets the serial port to 9600.
startEthernet(); // Start Ethernet on Arduino
}
void loop() {
long milisec = millis(); // Υπολογισμός χρόνου σε milliseconds.
long time = milisec / 1000; // Μετατροπή των milliseconds σε seconds.
readValue = analogRead(A0);
Voltage = (5.07 * 2 * readValue / 1000);
//ACS712 Αισθητήρας ρεύματος ,Παίρνει 1000 μετρήσεις απο τον αισθητήρα με διαφορά 1milisec και βγάζει το μέσο όρο.
float average = 0;
for (int i = 0; i < 1000; i++) {
average = average + (0.0264 * analogRead(A1) - 13.54) / 1000;
delay(1);
}
totamps = totamps + average; //Yπολογισμός συνολικών Amperes.
avgamps = totamps / time; //Yπολογισμός μέσου όρου Amperes.
amphr = (avgamps * time) / 3600; //Yπολογισμός AHr.
watt = Voltage * average; // power=voltage*current
energy = (watt * time) / 3600; //Τα Watt-sec μετατρέπονται σε Watt-Hr διαιρώντας με 1hr(3600sec).
// Εκτύπωση των τιμών στη σειριακή οθόνη.
Serial.print("VOLTS : ");
Serial.println(Voltage);
Serial.print("AMPERES :");
Serial.println(average);
Serial.print("POWER :");
Serial.print(watt);
Serial.println("Watt");
Serial.print("ENERGY CONSUMED :");
Serial.print(energy);
Serial.println("WHr");
Serial.println(""); // print the next sets of parameter after a blank line
//Εμφάνιση στην οθόνη LCD.
lcd.setCursor(1, 0);
lcd.print(Voltage);
lcd.print("V");
lcd.setCursor(10, 0);
lcd.print(average);
lcd.print("A");
lcd.setCursor(1, 1); // set the cursor at 1st col and 1st row
lcd.print(watt);
lcd.print("W ");
lcd.setCursor(10, 1); // set the cursor at 1st col and 2nd row
lcd.print(energy);
lcd.print("WH ");
// Read value from Analog Input Pin 0
String analogValue0 = String(Voltage, DEC);
String analogValue1 = String(average, DEC);
// 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("...disconnected");
Serial.println();
client.stop();
}
// Update ThingSpeak
if (!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
{
updateThingSpeak ("field1="+analogValue0+"&field2="+analogValue1+"&field3="+watt+"&field4="+energy);
}
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 ) {
startEthernet();
}
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(100);
// 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();
}
delay(100);
}