Skip to content

Commit

Permalink
add SecureConnect sample
Browse files Browse the repository at this point in the history
  • Loading branch information
chavee committed May 31, 2016
1 parent 79b2ee1 commit afd6913
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 11 deletions.
8 changes: 7 additions & 1 deletion AuthClient.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/*
MicroGear-Arduino-ESP8255 library
NetPIE Project
http://netpie.io
*/

#ifndef AUTHCLIENT_H
#define AUTHCLIENT_H

#include <ESP8266WiFi.h>
#include "Client.h"
#include "SHA1.h"
#include "debug.h"
//#include "debug.h"

#define GEARAUTHHOST "ga.netpie.io"
#define GEARAUTHPORT 8080
Expand Down
2 changes: 1 addition & 1 deletion MicroGear.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <EEPROM.h>
#include "SHA1.h"
#include "AuthClient.h"
#include "debug.h"
//#include "debug.h"

#define GBPORT 1883
#define GBSECUREPORT 8883
Expand Down
16 changes: 8 additions & 8 deletions examples/Basic/Basic.ino
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <AuthClient.h>
#include <MicroGear.h>
#include <MQTTClient.h>
#include <SHA1.h>
#include <Arduino.h>
/* 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 <EEPROM.h>
#include <MicroGear.h>

const char* ssid = <WIFI_SSID>;
Expand Down Expand Up @@ -43,7 +43,7 @@ void onLostgear(char *attribute, uint8_t* msg, unsigned int msglen) {

void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) {
Serial.println("Connected to NETPIE...");
microgear.setName("mygear");
microgear.setName(ALIAS);
}


Expand Down Expand Up @@ -81,7 +81,7 @@ void loop() {
microgear.loop();
if (timer >= 1000) {
Serial.println("Publish...");
microgear.chat("mygear","Hello");
microgear.chat(ALIAS,"Hello");
timer = 0;
}
else timer += 100;
Expand Down
96 changes: 96 additions & 0 deletions examples/SecureConnect/SecureConnect.ino
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);
}
2 changes: 1 addition & 1 deletion library.properties
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.
Expand Down

0 comments on commit afd6913

Please sign in to comment.