Hi,
I'm a beginner with Arduino. I own a Arduino Uno with a network shield.
After some tutorials i'm able to control a few leds and read the temperature from a sensor via the web.
But I want to upload the sensor data too with a interval. I have tried some tutorial's but no one works with a webpage and a uploadfunction.
My question is: Is it possible to do both actions? If yes, how?
This is the code I have now. (It tries the first time to upload and after that i must restart the Arduino to make it available again.
/*
Web Server
A simple web server
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
*/
//-------------------------------------------------------------------------------------------------------
#include <SPI.h>
#include <Ethernet.h>
#include <SdFat.h>
//Temperatuur en luchtvochtigdheid
#include <dht.h>
#include <math.h>
#define dht_dpin A0 //no ; here. Set equal to channel sensor is on
dht DHT;
String writeAPIKey = "XXXXXXXX";
String channelID = "67827";
EthernetClient client2;
float x;
float y;
SdFat SD;
SdFile myFile;
char buffer;
// Enter a MAC address and IP address for your controller below.
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x48, 0xD3 };
// The IP address will be dependent on your local network:
// assign an IP address for the controller:
IPAddress ip(192,168,178,177);
IPAddress gateway(192,168,178,1);
IPAddress subnet(255, 255, 255, 0);
// Initialize the Ethernet server library with the port you want to use.
EthernetServer server(80);
EthernetClient client;
String readString;
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------
// Any extra codes for Declaration :
// Declare Pin 8 as an LED because thats what we will be connecting the LED to.You could use any other pin and would then have to change the pin number.
int led = 8;
int led2 = 9;
int led3 = 7;
int led4 = 6;
//-------------------------------------------------
//-------------------------------------------------------------------------------------------------------
void setup()
{
pinMode(led, OUTPUT); //pin selected to control
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(10, OUTPUT); // ethernet shield uitschakelen
digitalWrite(10, HIGH); // zodat SPI bus vrijkomt
delay(1000);
SD.begin(4, SPI_FULL_SPEED); // om sd-kaart te initialiseren
Serial.begin(9600);
//delay(300); // even opstarten
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.print("Server is at ");
Serial.println(Ethernet.localIP());
Serial.println("Niek's Domotica 1.0");
}
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
void loop()
{
// listen for incoming clients
client = server.available();
if (client)
{
while (client.connected()){
if (client.available()){
char c = client.read();
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
if (c == '\n') {
// Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
myFile.open("page.htm", O_READ);
Read_File_Upto_FS();
if(digitalRead(led)){
//oranje
client.print("aan");
Read_File_Upto_FS();
client.print("uit");
}else{
client.print("uit");
Read_File_Upto_FS();
client.print("aan");
}
Read_File_Upto_FS();
if(digitalRead(led2)){
//blauw
client.print("aan");
Read_File_Upto_FS();
client.print("uit");
}else{
client.print("uit");
Read_File_Upto_FS();
client.print("aan");
}
Read_File_Upto_FS();
if(digitalRead(led3)){
//rood
client.print("aan");
Read_File_Upto_FS();
client.print("uit");
}else{
client.print("uit");
Read_File_Upto_FS();
client.print("aan");
}
Read_File_Upto_FS();
if(digitalRead(led4)){
//Groen
client.print("aan");
Read_File_Upto_FS();
client.print("uit");
}else{
client.print("uit");
Read_File_Upto_FS();
client.print("aan");
}
DHT.read11(dht_dpin);
Read_File_Upto_FS();
client.print(DHT.temperature);
Read_File_Upto_FS();
client.print(DHT.humidity);
Read_File_Upto_End();
myFile.close();
delay(1);
//some code to switch the leds here
readString = "";
client.stop();
}
}
}
}
DHT.read11(dht_dpin);
ThingSpeakUpdate("?key=XXXXXXXXXXXXX&temp=" + String(DHT.temperature) + "&hum="+ String(DHT.humidity));
}
/*------------------------------------------------
Sends sensor data to Thingspeak
Inputs: String, data to be entered for each field
Returns:
------------------------------------------------*/
void ThingSpeakUpdate(String tsData)
{
Serial.println("Date string: " + tsData);
Serial.println("...Connecting to Thingspeak");
// Connecting and sending data to Thingspeak
if(client2.connect("https://api.thingspeak.com", 80))
{
Serial.println("...Connection succesful, updating datastreams");
client2.print("GET /update/" + tsData + "HTTP/1.1\n");
client2.print("Host: https://api.thingspeak.com/update\n");
client2.print("Connection: close\n");
//client2.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client2.print("Content-Type: application/x-www-form-urlencoded\n");
client2.print("Content-Length: ");
client2.print(tsData.length());
client2.print("\n\n");
//client2.println(tsData); //the ""ln" is important here.
// This delay is pivitol without it the TCP client will often close before the data is fully sent
delay(1000);
Serial.println("Thingspeak update sent.");
}
else{
// Failed to connect to Thingspeak
Serial.println("Unable to connect to Thingspeak.");
}
if(!client2.connected()){
client2.stop();
}
client2.flush();
client2.stop();
}
void Read_File_Upto_FS() {
while ((buffer = myFile.read()) != 28) {
client.print((char)buffer);
}
}
void Read_File_Upto_End() {
while ((buffer = myFile.read()) >= 0) {
client.print((char)buffer);
}
}
void Skip_File_Upto_FS() {
while ((buffer = myFile.read()) != 28) {
// nothing
}
}