Hey, I am using Arduino's with Ethernet shields for quite a while no. The arduino sends the data to the database. I want to use this on my ESP 8266 with WiFi too, so it doesn't need an ethernet cable.
The following code is an example of one of my arduino's with ethernet:
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xF0, 0x08
};
IPAddress ip(192, 168, 0, 226);
EthernetClient client;
EthernetServer server(80);
String data;
#define sample 300 //this is how many samples the device takes per reading
//more information for #define http://arduino.cc/en/Reference/Define
int inPin = 5; //analog 5 (antenna to 5, resistance to ground)
float val; //where to store info from analog 5
int pin11 = 9; //positive pin of the speaker, you might want to change it for convenience, 13 for example.
int array1[sample]; //creates an array with number of elements equal to "sample"
//more information about arrays http://arduino.cc/en/Reference/Array
unsigned long averaging; //the program uses this variable to store the sum of each array it makes
void setup() {
Serial.begin(9600);
Serial.println ("Server is starting");
Ethernet.begin(mac);
server.begin();
Serial.print("Server is at: ");
Serial.println(Ethernet.localIP());
}
void loop() {
for(int i = 0; i < sample; i++){ //this code tells the program to fill each element in the array we made with
array1[i] = analogRead(inPin); //information from the antenna wire coming out of the Arduino
averaging += array1[i]; //more information about for loops http://arduino.cc/en/Reference/For
} //the averaging line is simply saying: add averaging to whatever is in array position i
//averaging += array[i] is the same as averaging = averaging + array[i]
//for more information about += http://arduino.cc/en/Reference/IncrementCompoun
val = averaging / sample; //here the program takes the sum of all numbers in array1, and divides by the number of elements "sample"
val = constrain(val, 0, 100); //this constrains the variable value to between two numbers 0 and 100
val = map(val, 0, 100, 0, 200); //for more information about constrain http://arduino.cc/en/Reference/Constrain //the map statement tells the program to map out 0-100 to 0-255, 255 is
analogWrite(pin11,val); //the threashold of analogWrite for more information about map http://arduino.cc/en/Reference/Map
averaging = 0; //this line of code sets averaging back to zero so it can be used again
if(val >=1){
val = constrain(val, 1, 100);
val = map(val, 1, 100, 1, 255);
Serial.println("dicht");
}
else
{
Serial.println("open");
}
Serial.println(val);
delay(1000);
/* while(val>=10){
val = analogRead(inPin); // reads in the values from analog 5 and
Serial.println("waiting");
Serial.print("Value of val= ");
Serial.println(val);
delay(1000);
}*/
{
//Serial.println(val);
//Serial.println(val);
if(val ==0){
data = String("temp=") + val *100 + String("&") + String("humidity=0") + String("&sid=8005");
Serial.print(data);
Serial.print("Connecting to server...");
if (client.connect("http://www.mysite.nl",80)) {
Serial.println(" connected!");
client.println("POST /store.php HTTP/1.1");
client.println("Host: www.mysite.nl");
client.println("Accept: */*");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println("");
client.println(data);
//Serial.println(data.length());
Serial.println("");
//Serial.println(data);
Serial.println("Data verstuurd");
} else {
Serial.println(" Connection failed...");
Serial.print("Connection status: ");
Serial.println(client.connect("www.de-geboortecontrole.nl",80));
}
{
if (client.connected()) {
delay(1);
client.stop(); // DISCONNECT FROM THE SERVER
}
}
delay(1000);
}
}
}
I used this code to test my ESP board and it works:
#include <ESP8266WiFi.h>
const char* ssid = "******";
const char* password = "********";
const char* host = "www.example.com";
void setup()
{
Serial.begin(9600);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
}
void loop()
{
WiFiClient client;
Serial.printf("\n[Connecting to %s ... ", host);
if (client.connect(host, 80))
{
Serial.println("connected]");
Serial.println("[Sending a request]");
client.print(String("GET /") + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"\r\n"
);
Serial.println("[Response:]");
while (client.connected() || client.available())
{
if (client.available())
{
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
client.stop();
Serial.println("\n[Disconnected]");
}
else
{
Serial.println("connection failed!]");
client.stop();
}
delay(5000);
}
Eth
I don't really know how I can send data like the Ethernet code. I'm new with WiFi boards, can someone please help me with this?
Kind Regards