Skip to content

Commit 717bdd4

Browse files
naztchavee
authored andcommitted
Add Basic Asynchronous Publish example. (#8)
* Add Basic Asynchronous Publish example. * Add comment to setAlias method. * Remove unused logic.
1 parent 516cbba commit 717bdd4

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/* NETPIE ESP8266 basic asynchronous publish sample */
2+
/* More information visit : https://netpie.io */
3+
4+
#include <ESP8266WiFi.h>
5+
#include <MicroGear.h>
6+
#include "CMMC_Interval.hpp"
7+
8+
const char* ssid = <WIFI_SSID>;
9+
const char* password = <WIFI_KEY>;
10+
11+
#define APPID <APPID>
12+
#define KEY <APPKEY>
13+
#define SECRET <APPSECRET>
14+
#define ALIAS "esp8266"
15+
16+
17+
WiFiClient client;
18+
CMMC_Interval timer001;
19+
20+
int timer = 0;
21+
MicroGear microgear(client);
22+
23+
/* If a new message arrives, do this */
24+
void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) {
25+
msg[msglen] = '\0';
26+
String _msg = String((char*)msg);
27+
28+
/* print message */
29+
Serial.print("Incoming message --> ");
30+
Serial.print(_msg);
31+
Serial.printf(" at [%lu] \r\n", millis());
32+
33+
}
34+
35+
void onFoundgear(char *attribute, uint8_t* msg, unsigned int msglen) {
36+
Serial.print("Found new member --> ");
37+
for (int i = 0; i < msglen; i++) {
38+
Serial.print((char)msg[i]);
39+
}
40+
Serial.println();
41+
}
42+
43+
void onLostgear(char *attribute, uint8_t* msg, unsigned int msglen) {
44+
Serial.print("Lost member --> ");
45+
for (int i = 0; i < msglen; i++) {
46+
Serial.print((char)msg[i]);
47+
}
48+
Serial.println();
49+
}
50+
51+
/* When a microgear is connected, do this */
52+
void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) {
53+
Serial.println("Connected to NETPIE...");
54+
/*
55+
microgear can set its own alias, which to be used for others make a function call chat().
56+
The alias will appear on the key management portal of netpie.io
57+
*/
58+
59+
/* Set the alias of this microgear ALIAS */
60+
microgear.setAlias(ALIAS);
61+
}
62+
63+
64+
void setup() {
65+
/* Add Event listeners */
66+
67+
/* Call onMsghandler() when new message arraives */
68+
microgear.on(MESSAGE, onMsghandler);
69+
70+
/* Call onFoundgear() when new gear appear */
71+
microgear.on(PRESENT, onFoundgear);
72+
73+
/* Call onLostgear() when some gear goes offline */
74+
microgear.on(ABSENT, onLostgear);
75+
76+
/* Call onConnected() when NETPIE connection is established */
77+
microgear.on(CONNECTED, onConnected);
78+
79+
Serial.begin(115200);
80+
Serial.println("Starting...");
81+
82+
/* Initial WIFI, this is just a basic method to configure WIFI on ESP8266. */
83+
/* You may want to use other method that is more complicated, but provide better user experience */
84+
if (WiFi.begin(ssid, password)) {
85+
while (WiFi.status() != WL_CONNECTED) {
86+
delay(500);
87+
Serial.print(".");
88+
}
89+
}
90+
91+
Serial.println("WiFi connected");
92+
Serial.println("IP address: ");
93+
Serial.println(WiFi.localIP());
94+
95+
Serial.println("Connecting to NETPIE.io");
96+
/* Initial with KEY, SECRET and also set the ALIAS here */
97+
microgear.init(KEY, SECRET, ALIAS);
98+
99+
/* connect to NETPIE to a specific APPID */
100+
microgear.connect(APPID);
101+
}
102+
103+
void loop() {
104+
/* Call this method regularly otherwise the connection may be lost */
105+
microgear.loop();
106+
/* To check if the microgear is still connected */
107+
if (microgear.connected()) {
108+
// call microgear.chat every 2 seconds
109+
timer001.every_ms(2000, []() {
110+
Serial.printf("Publish at [%lu]\r\n", millis());
111+
microgear.chat(ALIAS, "HELLO");
112+
});
113+
114+
}
115+
else {
116+
Serial.println("connection lost, reconnect...");
117+
microgear.connect(APPID);
118+
delay(2000);
119+
}
120+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef CMMC_NETPIE_INTERVAL_H
2+
#define CMMC_NETPIE_INTERVAL_H
3+
#include <Arduino.h>
4+
#include <functional>
5+
6+
class CMMC_Interval
7+
{
8+
private:
9+
unsigned long _prev;
10+
unsigned long _now;
11+
unsigned long _threshold_ms;
12+
13+
public:
14+
typedef std::function<void(void)> void_callback_t;
15+
16+
CMMC_Interval() {
17+
_prev = millis();
18+
_now = millis();
19+
};
20+
21+
~CMMC_Interval() {
22+
};
23+
24+
void every_ms(unsigned long ms, void_callback_t cb) {
25+
_threshold_ms = ms;
26+
_now = millis();
27+
unsigned long diff = _now - _prev;
28+
if (diff >= _threshold_ms) {
29+
_prev = millis();
30+
cb();
31+
}
32+
}
33+
};
34+
35+
36+
#endif
37+

0 commit comments

Comments
 (0)