[电子]自制一个小小家庭环境监控站

2015-11-3 15:13:49气象站
54852
实现目标
每隔10分钟,Me Orion主控板通过温湿度传感器采集一次房间环境的温度和湿度。采集成功后,将数据通过WiFi串口模块发送Http请求提交到云端服务器上。

材料准备
Me Orion主控板 x1
WiFi串口模块 x1
温湿度传感器 x1
USB TTL模块 x1
RJ25线 x2
RJ25转杜邦线 x1
all.jpg

制作步骤
1、使用USB TTL模块为WiFi串口模块烧写程序,主要实现WiFi模块通过无线路由器联网功能和发送http请求
wiring_prog.jpg
通过Arduino IDE编程的时候需要把WiFi模块上的切换开关拨到prog模式,正常通讯模式为work。详细配置WiFi模块编程环境见帖子(http://bbs.makeblock.cc/thread-798-1-1.html

ESP8266源代码:
[C++] 纯文本查看 复制代码
#include <ESP8266WiFi.h>

void setup() {
  Serial.begin(115200);
}

int value = 0;
String buffer = "";
void loop() {
   if(Serial.available()){
    char c = Serial.read();
    if(c=='\n'){
      parseBuffer();
    }else{
      buffer+=c;
    }
  }
}
void parseBuffer(){
  buffer = buffer+"/";
  int count = 0;
  int startIndex = 0;
  int endIndex = 0;
  int len = buffer.length();
  if(len<1){
    return;
  }
  String tmp;
  String values[10];
  while(true) {
    startIndex = buffer.indexOf("/", endIndex);
    endIndex = buffer.indexOf("/", startIndex + 1);
    tmp = buffer.substring(startIndex+1, endIndex);
    values[count] = tmp;
    count++;
    if(endIndex==len-1) break;
  }
  if(values[0].equals("setupwifi")){
    setupWIFI(values[1],values[2]);
  }
  if(values[0].equals("wifistatus")){
    wifiStatus();
  }
  if(values[0].equals("request")){
    sendRequest(values[1],values[2],values[3]);
  }
  Serial.println(buffer);
  buffer = "";
}
void setupWIFI(String ssid, String password){

  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid.c_str(), password.c_str());
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
void wifiStatus(){
  Serial.print("/wifistatus/");
  Serial.print(WiFi.status());
}
void sendRequest(String host,String port,String url){
  Serial.print("connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host.c_str(), port.toInt())) {
    Serial.println("connection failed");
    return;
  }
  url.replace("%2F","/");
  
  Serial.print("Requesting URL: ");
  Serial.println(url);
  
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  delay(10);
  
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("closing connection");
}


void urlencode(String str)
{
  str.replace("/","%2F");
}
void urldecode(String str)
{
  str.replace("%2F","/");
}


2、Me Orion驱动温湿度传感器,定时采集温湿度数据
3、登录data.sparkfun.com,注册数据上传帐号。实现数据上传到云端。
station.jpg

Orion源代码:
[C++] 纯文本查看 复制代码
#include "MeOrion.h"
#include "MeHumitureSensor.h"

MeHumiture hs(PORT_6);

const char* host = "data.sparkfun.com";
const char* streamId   = "your stream id";
const char* privateKey = "your private key";

String buffer = "";
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(3000);
  Serial.print("/setupwifi/your wifi ssid/your wifi password\n");
  delay(3000);
}

  // We now create a URI for the request
double lastTime = 0;
void loop() {
  if(millis()/1000-lastTime>600){
    hs.update();
    String url = "/input/";
    url += streamId;
    url += "?private_key=";
    url += privateKey;
    url += "&humidity=";
    url += hs.getHumidity();
    url += "&temp=";
    url += hs.getTemperature();
    Serial.print("/request/data.sparkfun.com/80/");
    url.replace("/","%2F");
    Serial.print(url);
    Serial.print("\n");
    lastTime = millis()/1000;
  }
}
void urlencode(String input)
{
  input.replace("/","%2F");
}
void urldecode(String input)
{
  input.replace("%2F","/");
}

完整代码:https://github.com/xeecos/HomeWeatherStation

效果查看
sparkfun data:https://data.sparkfun.com/makeblock
5.png



1人收藏
2 条回帖
magiccar注册会员2016-6-20 02:14:53
感謝 虎子哥 ,測試成功了!
IOT_Test.png
淡忘瞬间新手会员2016-11-28 13:49:56
感谢分享
需要登陆后才可进行回复 登录

返回顶部
现在加入我们,注册一个账号 账号登陆 QQ账号登陆 微博账号登陆