-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
113 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* NETPIE ESP8266 secure microgear connection sample */ | ||
/* It differs from the normal connection by 2 stpes */ | ||
/* 1. Declare a client as WiFiClientSecure instead of WiFiClient. */ | ||
/* 2. Call microgear.useTLS(true); */ | ||
/* More information visit : https://netpie.io */ | ||
|
||
#include <ESP8266WiFi.h> | ||
#include <MicroGear.h> | ||
|
||
const char* ssid = <WIFI_SSID>; | ||
const char* password = <WIFI_KEY>; | ||
|
||
#define APPID <APPID> | ||
#define KEY <APPKEY> | ||
#define SECRET <APPSECRET> | ||
#define ALIAS "esp8266tls" | ||
|
||
WiFiClientSecure client; | ||
AuthClient *authclient; | ||
|
||
int timer = 0; | ||
MicroGear microgear(client); | ||
|
||
void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) { | ||
Serial.print("Incoming message --> "); | ||
msg[msglen] = '\0'; | ||
Serial.println((char *)msg); | ||
} | ||
|
||
void onFoundgear(char *attribute, uint8_t* msg, unsigned int msglen) { | ||
Serial.print("Found new member --> "); | ||
for (int i=0; i<msglen; i++) | ||
Serial.print((char)msg[i]); | ||
Serial.println(); | ||
} | ||
|
||
void onLostgear(char *attribute, uint8_t* msg, unsigned int msglen) { | ||
Serial.print("Lost member --> "); | ||
for (int i=0; i<msglen; i++) | ||
Serial.print((char)msg[i]); | ||
Serial.println(); | ||
} | ||
|
||
void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) { | ||
Serial.println("Securely connected to NETPIE..."); | ||
microgear.setAlias(ALIAS); | ||
} | ||
|
||
void setup() { | ||
/* Event listener */ | ||
microgear.on(MESSAGE,onMsghandler); | ||
microgear.on(PRESENT,onFoundgear); | ||
microgear.on(ABSENT,onLostgear); | ||
microgear.on(CONNECTED,onConnected); | ||
|
||
Serial.begin(115200); | ||
Serial.println("Starting..."); | ||
|
||
if (WiFi.begin(ssid, password)) { | ||
|
||
while (WiFi.status() != WL_CONNECTED) { | ||
delay(500); | ||
Serial.print("."); | ||
} | ||
} | ||
|
||
Serial.println("WiFi connected"); | ||
Serial.println("IP address: "); | ||
Serial.println(WiFi.localIP()); | ||
|
||
microgear.useTLS(true); | ||
microgear.init(KEY,SECRET,ALIAS); | ||
microgear.connect(APPID); | ||
} | ||
|
||
void loop() { | ||
if (microgear.connected()) { | ||
Serial.println("connected"); | ||
microgear.loop(); | ||
if (timer >= 1000) { | ||
Serial.println("Publish..."); | ||
microgear.chat(ALIAS,"Hello"); | ||
timer = 0; | ||
} | ||
else timer += 100; | ||
} | ||
else { | ||
Serial.println("connection lost, reconnect..."); | ||
if (timer >= 5000) { | ||
microgear.connect(APPID); | ||
timer = 0; | ||
} | ||
else timer += 100; | ||
} | ||
delay(100); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name=ESP8266 Microgear | ||
version=1.1.2 | ||
version=1.1.5 | ||
author=Chavee Issariyapat <[email protected]> | ||
maintainer=Chavee Issariyapat <[email protected]> | ||
sentence=A client library for ESP8266 to connect to NETPIE IOT Platform. | ||
|