0.丟資料給某網站

當你有需要用GET法丟資料給某Server時,以下程式是個很好的範例程式

1.當你的目標是一般的ip或 http:// 開頭的網站時

#include <Arduino.h>

#include <ESP8266WiFi.h> // 提供Wi-Fi功能的程式庫

#include <ESP8266HTTPClient.h>

#include <ESP8266WiFiMulti.h>

////////////////////////////

#define SSID "tomorrows"

#define PASSWORD "----"

#define host "192.168.1.57:8080"

////////////////////////////

ESP8266WiFiMulti WiFiMulti;

WiFiServer server(8080); // 宣告網站伺服器物件與埠號

const int httpPort = 8080;

unsigned long t1,t2;

int i=0;

void data_to_web(String data3, String data4){

String url = "http://";

url+=host;

url+="/";

url+=data3;

url+=",";

url+=data4;

// This will send the request to the server

if((WiFiMulti.run() == WL_CONNECTED)) {

HTTPClient http;

http.begin(url);

int httpCode = http.GET();

if(httpCode > 0) {

if(httpCode == HTTP_CODE_OK) {

String payload = http.getString(); }

} else {}

http.end(); }}

void setup() {

Serial.begin(9600);

WiFiMulti.addAP(SSID, PASSWORD);

}

void loop(){

i++;

data_to_web(String(123),String(i));

delay(1000);

}

2.上丟Node-red (基本上也是丟給某ip)

#include <Arduino.h>

#include <ESP8266WiFi.h> // 提供Wi-Fi功能的程式庫

#include <ESP8266HTTPClient.h>

#include <ESP8266WiFiMulti.h>

////////////////////////////

#define SSID "tomorrows"

#define PASSWORD "------"

#define host "192.168.1.155:1880"

////////////////////////////

ESP8266WiFiMulti WiFiMulti;

WiFiServer server(1880); // 宣告網站伺服器物件與埠號

const int httpPort = 1880;

unsigned long t1,t2;

int i=0;

void data_to_web(String data3){

String url = "http://";

url+=host;

url+="/esp8266_1?data=";

url+=data3;

// This will send the request to the server

if((WiFiMulti.run() == WL_CONNECTED)) {

HTTPClient http;

http.begin(url);

int httpCode = http.GET();

if(httpCode > 0) {

if(httpCode == HTTP_CODE_OK) {

String payload = http.getString(); }

} else {}

http.end(); }}

void setup() {

Serial.begin(9600);

WiFiMulti.addAP(SSID, PASSWORD);

}

void loop(){

i++;

if(i>127){

i= -55;

}

data_to_web(String(i));

delay(1000);

}

3.當你需要丟資料給https:// (例如Google Apps Script 連動 Google sheet)

函式庫下載 HTTPSRedirect

#include "ESP8266WiFi.h"

#include "HTTPSRedirect.h"

const char* ssid = "填入Wifi SSID";

const char* password = "填入Wifi 密碼";

const char* host = "script.google.com";

const char *GScriptId = "AKfycbx7ni19fQc1Gr6CCGjDKiSXQ9W2JKYloSodKv7yNP4df8jFWZMU"; // Google script ID*(請見後方註解)

const int httpsPort = 443;

int i = 0;

HTTPSRedirect* client ;

void setup() {

Serial.begin(115200);

Serial.print("Connecting to wifi: ");

Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

}

Serial.println("");

Serial.println("WiFi connected");

Serial.println("IP address: ");

Serial.println(WiFi.localIP());

client = new HTTPSRedirect(httpsPort);

client->setPrintResponseBody(true);

client->setContentTypeHeader("application/json");

client->connect(host, httpsPort);

Serial.print("Connecting to ");

Serial.println(host);

}

void loop() {

i++;

String url3 = String("/macros/s/") + GScriptId + "/exec?data=";

url3 += i; //你要傳送的第一筆資料

url3 += ",";

url3 += 2*i; //你要傳送的第二筆資料

Serial.println("GET Data");

if (client->GET(url3, host)){

Serial.println("GET SUCCESS");

}

delay(4000);

}