Hello,
I'm trying to monitor my solar system usage with arduino, i can see that thingspeak is getting data by number on enteries, but there are no dots on graphs?
What might be wrong, here is the code:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 };
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "*************************;
const int updateThingSpeakInterval = 20 * 1000;
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
EthernetClient client;
//Amprite lugemine ACS712
const int analogIn = A9;
int mVperAmp = 66;
int RawValue= 0;
int ACSoffset = 2500;
double Voltage = 0;
double Amps = 0;
//Voltide lugemine 68k/10k divider
int analogInput = A8;
float vout = 0.0;
int vin = 0.0;
float R1 = 68600.0;
float R2 = 10000.0;
int value = 0;
//Watid
int watid = 0;
/////////////////////////////////////////////////////////////////
void setup(){
pinMode(analogInput, INPUT);
Serial.begin(9600);
startEthernet();
}
/////////////////////////////////////////////////////////////////
void loop(){
watid = (Amps * vin);
RawValue = analogRead(analogIn);
Voltage = (RawValue / 1024.0) * 5000;
Amps = ((Voltage - ACSoffset) / mVperAmp);
if (Amps<0.1){
Amps=0;
}
value = analogRead(analogInput);
vout = (value * 5.0) / 1024.0;
vin = vout / (R2/(R1+R2));
if (vin<0.09) {
vin=0.0;
}
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="+vin);
updateThingSpeak("field3="+watid);
}
// 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
");
client.print("Host: api.thingspeak.com
");
client.print("Connection: close
");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"
");
client.print("Content-Type: application/x-www-form-urlencoded
");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("
");
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);
}