This is my first post to the arduino community and I am so exciting!
I have manage to build a small circuit with an ultrasound sensor, a piezo and 2 leds.
Everythings works fine and I am very happy.
I have also add an Ethernet Shield and I want to POST sensor’s data to a server.
here is my code
#define led4 10
#define led5 9
int speakerPin = 3;
// ??????? ?????
int tones1 = 200;
int tones2 = 630;
int tones3 = 1200;
// ??????????
const int anPin1 = 0;
long distance1;
int distance2;
void setup() {
Serial.begin(9600); // sets the serial port to 9600
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
}
void read_sensors(){
/*
Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
*/
distance1 = (analogRead(anPin1)/2)*2.54;
distance2 = distance1/100;
}
void print_all(){
Serial.print("S1");
Serial.print(" ");
Serial.print(distance1);
Serial.print("cm");
Serial.print(" ");
Serial.print(distance2);
Serial.print("m");
Serial.println();
}
void loop() {
if (distance1 < 40 & distance1 > 15) {
tone(speakerPin, tones3);
digitalWrite(led4, HIGH); //BLUE
delay(350);
noTone(speakerPin);
digitalWrite(led4, LOW);
}
else {
}
if (distance1 <= 15) {
tone(speakerPin, tones3);
digitalWrite(led5, HIGH); //RED
delay(250);
noTone(speakerPin);
digitalWrite(led5, LOW);
}
else {
}
if (distance1 > 40){
Serial.println("tabani");
}
else {
}
read_sensors();
print_all();
delay(3000);
}
I need some extra code so I can make my ethernet shield to post data to a server.
Any help would be much apreciated.
I have search around and collect some bits from the web.
I add new code to my sketch that I believe it will work.
Please have a look bellow:
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
EthernetClient client;
#define led4 10
#define led5 9
int speakerPin = 3;
// tones definition
int tones1 = 200;
int tones2 = 630;
int tones3 = 1200;
// variables
const int anPin1 = 0;
long distance1;
int distance2;
// create a string of these variables
String data;
void setup() {
Serial.begin(115200); // sets the serial port to 115200
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
}
void read_sensors(){
/*
Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
*/
distance1 = (analogRead(anPin1)/2)*2.54;
distance2 = distance1/100;
data = "";
}
void print_all(){
Serial.print("S1");
Serial.print(" ");
Serial.print(distance1);
Serial.print("cm");
Serial.print(" ");
Serial.print(distance2);
Serial.print("m");
Serial.println();
}
void loop() {
if (distance1 < 40 & distance1 > 15) {
tone(speakerPin, tones3);
digitalWrite(led4, HIGH); //BLUE
delay(350);
noTone(speakerPin);
digitalWrite(led4, LOW);
}
else {
}
if (distance1 <= 15) {
tone(speakerPin, tones3);
digitalWrite(led5, HIGH); //RED
delay(250);
noTone(speakerPin);
digitalWrite(led5, LOW);
}
else {
}
if (distance1 > 40){
Serial.println("tabani");
}
else {
}
data = distance1;
if (client.connect("www.*****.*************.com",80)) { // REPLACE WITH YOUR SERVER ADDRESS
client.println("POST /add.php HTTP/1.1");
client.println("Host: *****.*************.com"); // SERVER ADDRESS HERE TOO
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
}
if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
}
read_sensors();
print_all();
delay(3000);
}
Howeve when I run verify to Arduino IDE I get an ambliguous overload for ‘operator=’ in data = distance1’
Am sure I miss something or I have added code that is not necessary.
Can you please advise corrections on the above code ??? I would be really gratefull.
The function analogRead() returns an integer. You divide it by integer 2. That lowers the resolution because that calculation will be done with integers. After that you multiply with a float (2.54) that calculation is done with float. That float is converted into a long 'distance1'. Maybe the compiler doesn't grumble, but I do when so much is going on in a single line
data = distance1;
The String 'data' should get the integer 'distance1'.
I thought I should drop a line what I have manage so far.
Here is the code so far, which needs many corrections and I kindly ask your help.
This is my very first attempt and I got into deep directly. However I have manage to collects some bits around the web and created the code bellow. Sensor works, serial displays values but unfortunatelly data are not been posted to server.
I have try several different variations but without luck.
Request don’t reach the server. (checked apache’s logs)
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
EthernetClient client;
long duration, cm;
int trigPin = 12; //Trig - green Jumper
int echoPin = 11; //Echo - yellow Jumper
String data;
void setup() {
//Serial Port begin
Serial.begin (115200);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
data = "";
}
void loop()
{
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = (duration/2) / 29.1;
// Serial.println(data);
Serial.print(cm);
Serial.print("cm");
Serial.println();
Serial.println("Posting!");
postdata();
Serial.println(data);
delay(5000);
}
void postdata(){
data+="";
data+="tank1=";
data+=cm;
data+="&tank2=";
data+=0;
if (client.connect("www.f2d.gr",80)) { // SERVER ADDRESS
client.println("POST /tankmonitor/add.php HTTP/1.1");
client.println("Host: www.f2d.gr"); // SERVER ADDRESS AGAIN
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("User-Agent: Arduino/1.0");
client.print("Content-Length: ");
client.println(data.length());
Serial.println(data.length());
client.println();
client.print(data);
}
if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
}
}
Will it work with a 'GET', so that the data can be entered with a browser ?
Can you add more Serial.println() in your sketch to show what is going on.
For example after the "if (client.connect...", and print also the 'data' and more.
I want to know if you are connected or not.
For you information the code bellow works smoothly
I found it on the Internet and it posts data from a DHT11 Temperature sensor every 15 minutes.
#include <DHT.h>
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
EthernetClient client;
#define DHTPIN 2 // SENSOR PIN
#define DHTTYPE DHT11 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS
DHT dht(DHTPIN, DHTTYPE);
long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL
int t = 0; // TEMPERATURE VAR
int h = 0; // HUMIDITY VAR
String data;
void setup() {
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
dht.begin();
delay(10000); // GIVE THE SENSOR SOME TIME TO START
h = (int) dht.readHumidity();
t = (int) dht.readTemperature();
data = "";
}
void loop(){
currentMillis = millis();
if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
previousMillis = currentMillis;
h = (int) dht.readHumidity();
t = (int) dht.readTemperature();
}
// data = "temp1=" + t + "&hum1=" + h;
data+="";
data+="temp1=";
data+=(int) dht.readTemperature();
data+="&hum1=";
data+=(int) dht.readHumidity();
if (client.connect("www.f2d.gr",80)) { // SERVER ADDRESS
client.println("POST /tempmonitor/add.php HTTP/1.1");
client.println("Host: www.f2d.gr"); // SERVER ADDRESS AGAIN
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("User-Agent: Arduino/1.0");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
}
if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
}
Serial.println(data);
delay(900000); // WAIT 15 MINUTES BEFORE SENDING AGAIN
}
However I want to make it work with another sensor (ultrasonic sensor US-20)
Therefore I duplicate database and create the proper files (altering some php code for the database name and table columns)
I will get back to you with the serial.print output