|
#include <HttpClient.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include "sys_api.h"
#define port 443
//
Ameba A1_Lite
#define YelloLED 9 //PA15
//
HUB 5168
#define GreenLED 3 //PA30
#define BlueLED 9 //PA15
char ssid1[] = "
"; //
your network SSID (name)
char pass1[] = "
"; //
your network password
char ssid2[] = "
"; //
your network SSID (name)
char pass2[] = "
"; //
your network password
char ssid3[] = "
"; //Your WiFi SSID
char pass3[] = "
"; //Your WiFi
Password
char ssid4[] = "
"; //Your WiFi SSID
char pass4[] = "
"; //Your WiFi
Password
String
Linetoken = "
";
char host[] = "notify-api.line.me"; //LINE Notify API
URL
String
url = "/api/notify"; //POST header
int status =
WL_IDLE_STATUS;
//
Initialize the Wifi client library
WiFiSSLClient
client;
void wifiConnect(void);
void printWifiStatus(void);
void sendMessage(String
token, String message);
void setup(void) {
pinMode(BlueLED,
OUTPUT);
digitalWrite(BlueLED,
HIGH); // turn the LED on
(HIGH is the voltage level)
// start serial
port:
Serial.begin(115200);
// check for the
presence of the shield:
if (WiFi.status() ==
WL_NO_SHIELD) {
Serial.println("WiFi
shield not present");
// don't continue:
while (true);
}
Serial.print("Configuring
access point...");
wifiConnect();
// you're
connected now, so print out the status:
printWifiStatus();
int
number = 55688;
String MESSAGE = "Test
string\r\n" +
String(number);
sendMessage(Linetoken,
MESSAGE);
}
void loop(void) {
// if there are
incoming bytes available
// from the
server, read them and print them:
while (client.available()) {
char
c = client.read();
Serial.write(c);
}
// if the server's
disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting
from server.");
client.stop();
// do nothing
forevermore:
while (true);
}
}
void wifiConnect(void) {
char
ssid[20],pass[20],num=0;
unsigned long WifiConnectingTimeout; //連線逾時時間
// scan for nearby
networks:
Serial.println("**
Scan Networks **");
int
numSsid = WiFi.scanNetworks();
if (numSsid
== -1) {
Serial.println("Couldn't
get a wifi connection");
while (true);
}
for (int thisNet = 0; thisNet <
numSsid; thisNet++) {
strcpy(ssid,
WiFi.SSID(thisNet));
if(strcmp(ssid,
ssid1) == 0)
{
strcpy(pass,
pass1);
num=1;
break;
}
else if(strcmp(ssid,
ssid2) == 0)
{
strcpy(pass,
pass2);
num=2;
break;
}
else if(strcmp(ssid,
ssid3) == 0)
{
strcpy(pass,
pass3);
num=3;
break;
}
else if(strcmp(ssid,
ssid4) == 0)
{
strcpy(pass,
pass4);
num=4;
break;
}
}
if (num
== 0) {
Serial.println("Couldn't
get a wifi connection");
while (true);
}
// Connect to
WPA/WPA2 network. Change this line if using open or WEP network:
Serial.print("Connecting
to ");
Serial.println(ssid);
WiFi.begin(ssid,
pass);
WifiConnectingTimeout =
millis();
while (WiFi.status() !=
WL_CONNECTED) {
Serial.print("Attempting
to connect to SSID: ");
Serial.println(ssid);
if ((millis() -
WifiConnectingTimeout) > 10000) //等待
10秒
break;
delay(500);
Serial.print(".");
}
}
void printWifiStatus(void) {
// print the SSID
of the network you're attached to:
Serial.print("SSID:
");
Serial.println(WiFi.SSID());
// print your WiFi
shield's IP address:
Serial.println("WiFi
connected");
Serial.print("IP
Address: ");
IPAddress ip =
WiFi.localIP();
Serial.println(ip);
// print the
received signal strength:
long
rssi = WiFi.RSSI();
Serial.print("signal
strength (RSSI):");
Serial.print(rssi);
Serial.println("
dBm");
}
void sendMessage(String
token, String message) {
// if there's a
successful connection:
if (client.connect(host,
port)) {
message = "message=" +
message;
String request =
/*
Post url */
"POST
" +
url + "
HTTP/1.1\r\n" +
/*
Headers */
"Host:
" +
host + "\r\n" +
"Connection:
close\r\n" +
"Authorization:
Bearer " +
token + "\r\n" +
"Content-Length:
" +
String(message.length()) + "\r\n" +
"Content-Type:
application/x-www-form-urlencoded\r\n\r\n" +
/*
Body */
message + "\r\n";
client.print(request);
Serial.print(request);
delay(2000);
String response = "";
response =
client.readString();
Serial.println(response); //Display
the result of responsing
Serial.println("Updated!");
}
else {
Serial.println("connected
fail");
sys_reset();
}
}
|