Sunday, December 13, 2015

Sending sensor data to a web server

Build your own IOT service! Collect sensor data and send it to a web/database server.

Today's project uses an Arduino equipped with a Ethernet shield, and a DHT-11 temperature / humidity sensor.

Arduino UNO
Arduino Ethernet Shield
DHT-11 Module

(ESP8266 / BME280 Version)
(UNO / WiFi BME280 Version)

The Arduino reads the DHT-11, and submits the data to a php script on a hosted web server. That php page inserts the data into a mySQL database, and another php page creates a web page displaying the data as you can see below.






Every 5 minutes the Arduino submits a new data packet.

For the server side code.

For Heat Index, Dew Point, and Wind Chill calculations.


The Arduino sketch is below:



#include <DHT.h>
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
EthernetClient client;

#define DHTPIN 6 // 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(115200);

 pinMode(7, OUTPUT);      // sets the digital pin as output
 pinMode(5, OUTPUT);      // sets the digital pin as output
 digitalWrite(7, HIGH);
 digitalWrite(5, LOW);

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=";

data.concat(t);

data.concat("&hum1=");

data.concat(h);


if (client.connect("www.xxxxxx.com",80)) { // REPLACE WITH YOUR SERVER ADDRESS
client.println("POST /add.php HTTP/1.1");
client.println("Host: xxxxxx.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
}

delay(300000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
}





If you are interested in recreating this project, the zip file with the Arduino code, libraries, php pages, and sql for the mySQL database is available at files

Customized projects available as well as hosting and support.

This project inspired by https://github.com/alexpais/iot-arduino-weather-station

10 comments:

  1. I have this setup, however its not populating the database with values. I am not getting any data. Upon debugging in the serial monitor it shows the data is populating the variables - could you assist me?

    ReplyDelete
    Replies
    1. Make sure your database connection values are good.

      Delete
  2. I am getting 0 into the database now - zero's. Just looking for help.

    ReplyDelete
    Replies
    1. Did you set the destination url and host url properly in the code?

      Delete
  3. hello i am undertaking a similar project and would like to know what you are using to send the data to the webserver?http?
    thanks in advance

    ReplyDelete
  4. Me too, I'm getting "0" value at my server database.
    pls help me,thanks

    ReplyDelete
    Replies
    1. Are you seeing correct values in the serial monitor? Do you have your database field names correct? There are a number of reasons this could happen. check your database credentials, column names in add.php, etc.

      Delete
  5. hey, i want to send my sensor data to server without using wifi. im using gsm. so how am i gonna do that? i have no idea

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...