Hey everyone,
I'm completely new to arduino, got no idea what i' doing. My project is to make a tank level monitor, that uses the HC-SR04 Ultrasonic sensor, Ethernet shield and Uno( I know, its been done to death and I have trawled through the forums before this). I mashed two codes together, a tank level code, which converts the distance to a percentage (displaying output on serial), and an example ethernet code to upload analog voltage input to thingspeak. Each program I have tested separately and works fine, however when I put the two codes together, replacing the analog voltage reading with my tank level code, the code uploads to the board, displays the levels on the serial monitor, however does not display the data on thingspeak. I'm pretty sure my errors are in the string lines of the code, but as I said, completely new to this and have little to no clue what I'm actually doing, any help greatly appreciated.
/*
Arduino Thingspeak Ethernet Shield Example Code
Matched with Tank Level Code using HC-SR04 sensor
*/
#include <SPI.h>
#include <Ethernet.h>
// Local Network Settings
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 }; // Must be unique on local network
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "14IHAXPD31CPDX4Y";
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;
int trig = 2;
int echo = 4;
// Initialize Arduino Ethernet Client
EthernetClient client;
void setup()
{
// Start Serial for debugging on the Serial Monitor
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
// Start Ethernet on Arduino
startEthernet();
}
void loop()
{
long t = 0, h = 0, hp = 0;
//Transmitting Pulse
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
//waiting for pulse
t = pulseIn(echo, HIGH);
//Calculating Distance
h = t / 58;
h = h - 6; // offset correction
h = 50 - h; //water height, 0-50cm
hp = 2 * h; //distance in %
//Sending to computer
Serial.print(hp);
Serial.print("\n");
delay(1000);
// Gets weird after this
String digitalWrite;
// 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="+hp);
}
// 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);
}