Arduino ethernet upload data to thingspeak.com

Hello guys :slight_smile: , i'm making a project for my university where Arduino Ethernet should measure the voltage, current, power and energy of a system and upload the data to thingspeak.com. I've already managed to measure these values but i have a problem uploading to the thingspeak because i don't really understand the proccess. This is the code i'm using. I want to upload 4 different fields at real time.
Can you 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 = "7GC7MLY23TD6YLRI";
const int updateThingSpeakInterval = 16 * 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);
    updateThingSpeak("field2=" + analogValue1);
  }

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

  delay(1000);
}

You pretty much did everything possible you could have done wrong with your first post to this forum. Obviously you didn't read the post titled "How to use this forum - please read."

  • Open your code in the Arduino IDE.
  • Tools > Auto Format
  • Copy your sketch.
  • Edit your post to replace your unformatted sketch with the formatted sketch
  • Use code tags(the </> icon)

Hello, i'm sorry for the wrong way of posting. I modified it and i think it's correct now!

Moderator: removed crosspost

Ok, much easier to read now! I recommend that you first test the ethernet system by using the example sketches under File > Examples > Ethernet. ChatServer is a good simple one but you will also want to test with DHCP as your project uses it. After that write the most simple possible sketch to send to thingspeak. Don't use the sensors yet, just send some numbers. Then once you are sure all the individual pieces are working it will be easier to track down what part of your project isn't working. After that if you're still having problems then give us more information on exactly what isn't working(what do you expect to see, what are you seeing instead).

Because you are using DHCP you should be calling Ethernet.maintain() to renew the DHCP lease. See: Ethernet - Arduino Reference.

I have done with Thingspeak, you should change with void updateThingSpeak.

updateThingSpeak("field1="+analogValue1+"&field2="+analogValue2+"&field3="+analogValue3+"&field4="+analogValue4);
}