• CC3000 做个人无线气象站


    As open-source hardware users and makers, we love playing with new chips, boards and tools. And there is one chip which is quite popular these days: the CC3000 WiFi chip from TI. This chip comes with many promises: cheap (around $10), easy to use, low-power … It was featured in many articles around the web, but somehow it was quite hard to use with Arduino as there was no breakout board or library available. Luckily, Adafruit solved that for us with a nice breakout board and a working library for Arduino. In this article, I will show you how to use this chip for home automation purposes. Remember thatweather station project? We are going to do the same: measure the temperature and the humidity. But this time we won’t display the information on an LCD screen. Instead, we will transmit the data wirelessly via WiFi to your computer and display it there. Excited ? Let’s get started!

    Hardware requirements

    The whole project is based on the Arduino platform, so of course you will need an Arduino board. I really recommend using the Arduino Uno board for this project, as it is the only board that is currently compatible with the CC3000 library at the time this article was written.

    Then, you need the famous CC3000 chip. There are also many alternatives to do that. What I recommend is using the Adafruit CC3000 breakout board, which is the only one I tested that worked without problem. It is nice and compact, has voltage regulators onboard, as well as an onboard antenna. I tried the official TI CC3000 board, but it never worked properly, and you have the use level shifters as well (the CC3000 works with 3.3V, and the Arduino Uno with 5V). The other alternative is to make your own breakout board, there are many PCB layout available online.

    You also need a DHT temperature & humidity sensor. I used the DHT11 sensor, but this project would also work fine with a DHT22, you would just have one line of code to change. You will also need a 10K Ohm resistor to be used with the DHT sensor.

    Finally, you need a breadboard and some jumper wires to make the connections between the different parts.

    • Arduino Uno
    • Adafruit CC3000 breakout board
    • DHT11 sensor
    • 10K Ohm resistor
    • Breadboard and some jumper wires

    Software requirements

    For this project, you need the usual Arduino IDE. You will also need the Adafruit’s CC3000 library, as well as the DHT sensor library. To install a library, just download the folders, and put them into your /Arduino/libraries/ folder.

    You will also need a web server running on your computer. There are thousands of tutorial for that, but I recommend using MAMP if you are on OS X.

    Hardware configuration

    The hardware configuration for this project is actually not that complicated, thanks to the good informations that you will find on the CC3000 breakout board. Connect the IRQ pin of the CC3000 board to pin number 3 of the Arduino board, VBAT to pin 5, and CS to pin 10. Then, you need to connect the SPI pins to the Arduino board: MOSI, MISO, and CLK go to pins 11,12, and 13, respectively. Finally, take care of the power supply: Vin goes to the Arduino 5V, and GND to GND.

    The DHT sensor is easier to connect: just plug the pin number 1 to the Arduino’s 5V, pin number 4 to GND, and pin number 2 to Arduino pin 7. Finally, put the 10K resistor between the sensor pins number 1 and 2.

    Testing the WiFi chip

    As this tutorial is really about the CC3000 chip, I don’t want to spend too much time on the DHT sensor. You will find a good test sketch inside the library, and I also included a test sketch inside the project’s GitHub repository.

    To see if your WiFi chip is correctly wired and operational, I recommend to use the test sketches that come with the Adafruit library. I used for example the one called WebClient. Just open it from the library’s folder, and save it to a new file (you need to be able to modify the sketch to enter your WiFi network name & password). Then, modify the sketch with the correct data for you WiFi network, and upload to the board. You can then open your serial monitor, and if everything was wired correctly (and your Internet connection is working!) you should see your Arduino connecting to the web, then connecting to the Adafruit’s website, and printing some information.

    Putting it all together

    We can now dive into the home automation part of this project. The goal is to get the sensor’s data, send it via WiFi to a server (running on your computer), and display the information. The code for each part is quite long, so I will only discuss the important parts. To get the complete code, simply go to the GitHub repository of the project.

    First, the Arduino sketch. If you had a look at the example file from Adafruit, you should already have an idea of what are the important parts of the sketch. Of course, you need to import the right libraries:

    #include <Adafruit_CC3000.h>
    #include <ccspi.h>
    #include <SPI.h>
    #include <string.h>
    #include <stdlib.h>
    #include "utility/debug.h"
    #include "DHT.h"

    Then, you need to define inside the code what is specific to your configuration: WiFi name & password, IP address of your computer, and port of your server (usually 80):

    We can then create the CC3000 instance:

    And let’s not forget to create the instance for the DHT sensor:

    In the setup() part of the sketch, we need to initialize the DHT sensor:

    And to connect the CC3000 to the network:

    In the loop() part, we need to get data from the sensor, and to send it to the server by doing a GET request. The first part is really simple:

    You just need to take care of converting these floats to Strings, because you want to insert this information into an HTTP request. I wrote a simple function for that, you can look at the details in the code:

    Finally, I send the request with this send_request() function. This function basically converts the String of the request to an array of chars (for speed reasons), and connect to the server with:

    And then send the request with this line:

    Finally, it reads out the answer (which we don’t use in this project, but you could perfectly get data back from the server):

    We then wait 10 seconds between each temperature & humidity update. Once again, to get the complete code, just go to the GitHub repository of the project.

    It’s now time to write the server part. As usual, I used a combination of HTML, JavaScript, and PHP. The PHP part will receive the data from the board at each GET request, and save it into files:

    Of course, you could use more elaborate ways to save the data like XML or SQL but I wanted to keep things simple. Then, I used two other PHP scripts to actually display the data on the main HTML page. This is for example the one for the temperature:

    Finally, the main page just organizes all this information in two separate blocks. This is for example one of them:

    同时,我们需要一段 Javascript 代码,每秒更新网页上的数据:

    It is based on jQuery, which is also included in the GitHub repository of the project. I also put some CSS in the page so it doesn’t look to ugly, I let you see the details in the repository.

    That’s all for the software part, so it is time to test it out! Upload the sketch to the Arduino board, open the serial monitor to check that everything is working correctly, and also open the html page inside your favorite browser. Wait a bit so that the board can send the first set of data, and this is what you should see:

    湿度、温度值显示在网页上

    You can play with the sensor to see the data being automatically updated on the page. Congratulations, you just built a simple WiFi-connected weather station! I used temperature & humidity as the “data” in this tutorial, but it can be anything you want: data from a motion detector, door/window contact … You can also modify the server part so that it sends some information back to the board, for example to control a relay.

    As always, please comment below if you have any question, a suggestion on how to improve the article, or if you find some nasty bugs in the code! Finally, this is the list of the parts that were used in this project:

  • 相关阅读:
    CentOS7安装mysql
    strusts2的开发包
    CentOS7配置mavne国内镜像
    卸载linux自带的jdk
    Centos7安装nodejs
    redis启动方式
    bash学习笔记——Shell变量
    bash学习笔记——bash是什么
    教育管理系统——android家长客户端
    php入门学习——php与jsp对比
  • 原文地址:https://www.cnblogs.com/NL34/p/3537822.html
Copyright © 2020-2023  润新知