ESP8266-向物聯網雲平台發送數據--dweet


方法一:

//向物聯網平台發送數據
//發送數據格式:  https://dweet.io/dweet/for/my-thing-name?hello=world  免費平台
//my-thing-name 是自己起的地址名稱;hello=world 鍵值對數據
//獲取數據  https://dweet.io/get/latest/dweet/for/my-thing-name   用網頁打開

#include <ESP8266WiFi.h>
const char* ssid = "jia";  //這里輸入wifi名稱
const char* password = "lm654321";  //這里輸入wifi密碼
const char* host= "dweet.io";  //數據存儲平台

void setup() {
  Serial.begin(115200);
  delay(10);

   //接下來板子開始要連線到路由器了
  Serial.println();
  Serial.println();
  Serial.print("lianjie to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);//開始連線

  while (WiFi.status() != WL_CONNECTED) {  //如果沒有連接成功,就輸入"."
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi lianxian cengong");//輸入wifi連線成功
  Serial.println(WiFi.localIP());//輸出IP
  
}

void loop() {
delay(5000);
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;//建立一個TCP客戶端
if (!client.connect(host,80))  //如果沒有連接成功
{
  Serial.println("lianjie sibai");
  return;
  }
//發送請求
Serial.print("Requesting URL:  ");
client.print(String("GET /dweet/for/esp8266_liming?temperature=")+"28"+"&humidity="+"40"+" HTTP/1.1\r\n"+"Host: "+host+"\r\n"+"Connection: close\r\n\r\n");
//esp8266_liming  自己起的名字-不要跟別人重名
//temperature的值是28;humidity的值是40

delay(10);
unsigned long timeout=millis();
while(client.available()==0)
{//判斷是否有響應信息
if(millis()-timeout>5000)  //如果超時5秒,就失敗
{
  Serial.println(">>>Client Timeout!");
  client.stop();
  return;
  }}
  
//讀取返回的信息
while(client.available())
{
  String line=client.readStringUntil('\r');
  Serial.print(line);
  }

  //關閉連接
  Serial.println();
  Serial.println("lianjie guanbi");

}

 

在串口監視器看到 :

說明發送成功

 

用網頁打開 https://dweet.io/get/latest/dweet/for/esp8266_liming   可以看到

 

 

 

 

用python讀取數據

from urllib.request import urlopen   #導入urlopen函數

#讀取網頁內容,如果網頁中有中文要用“utf-8”解碼
html = urlopen( "https://dweet.io/get/latest/dweet/for/esp8266_liming").read().decode('utf-8')
print(html,type(html))

#后面用正則表達式 提取需要的內容

 

 

 

 

 

天子驕龍 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM