Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Internet of Things with Arduino Cookbook

You're reading from   Internet of Things with Arduino Cookbook Build exciting IoT projects using the Arduino platform

Arrow left icon
Product type Paperback
Published in Sep 2016
Publisher Packt
ISBN-13 9781785286582
Length 188 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Marco Schwartz Marco Schwartz
Author Profile Icon Marco Schwartz
Marco Schwartz
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Connecting an Arduino to the Web FREE CHAPTER 2. Cloud Data Monitoring 3. Interacting with Web Services 4. Machine-to-Machine Interactions 5. Home Automation Projects 6. Fun Internet of Things Projects 7. Mobile Robot Applications Index

Sending data to the cloud

In the last recipe of this chapter, we are actually going to use everything we learned so far in this chapter and apply it to a simple project: sending data to a cloud server, so it can be stored there. This is something that we are going to do many times in this book, but I wanted to give you a short overview first.

Getting ready

For this recipe, you will need the same configuration that we used in the recipe Interacting with basic sensors, but ? with a photocell connected to the Arduino board. Please refer to this recipe to know how to assemble the hardware for the project.

How to do it...

Let's now see the sketch that we will use for this chapter. It is actually very similar to the code for the previous chapter, so I will just highlight the important parts here.

As before, we define the server to which we want to connect the board. Here, we will use the dweet.io service:

char server[] = "dweet.io";

We also define an interval on which we will send data to the dweet.io servers:

unsigned long lastConnectionTime = 0;            
const unsigned long postingInterval = 10L * 1000L;

In the loop() function, we check if it is time to send data. If so, we measure the reading from the sensor, and send this to a function that will send the data:

if (millis() - lastConnectionTime > postingInterval) {

    // Measure light level
    int sensorData = analogRead(A0);

    // Send request
    httpRequest(sensorData);
  }

Let's now see the details of this function:

void httpRequest(int sensorData) {

  // Close existing connection
  client.stop();

  // Connect & send request
  if (client.connect(server, 80)) {
    
    Serial.println("connecting...");
    
    // Send the HTTP PUT request:
    client.println("GET /dweet/for/myarduino?light=" + String(sensorData) + " HTTP/1.1");
    client.println("Host: dweet.io");
    client.println("User-Agent: ArduinoWiFi/1.1");
    client.println("Connection: close");
    client.println();

    // Note the time that the connection was made:
    lastConnectionTime = millis();
  }
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}

As you can see, the function is very similar to what we did in the previous recipe. The main difference is that we pass the measured data as an argument when calling the dweet.io server.

You can now grab the code from the GitHub repository of this book, and upload it to the board.

Tip

Don't forget to change your Wi-Fi name and password here, otherwise it won't work.

Then, open the Serial monitor, and this is what you should see:

How to do it...

If you can see the 'succeeded' message, it means that the data has been correctly stored on the server.

To check that it was actually recorded, you can now go to the following URL:

https://dweet.io/get/latest/dweet/for/myarduino

You should see the answer in JSON format, meaning data was recorded from your board.

How to do it...

How it works...

The Dweet.io service is a very useful (and free) web service to store data coming from your IoT project. We are going to use it extensively in the coming chapters of this book.

See also

I now recommend that you move on to the next chapter, so you can start using what you learned in this introductory chapter to build actual IoT projects!

You have been reading a chapter from
Internet of Things with Arduino Cookbook
Published in: Sep 2016
Publisher: Packt
ISBN-13: 9781785286582
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime