|
| 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 | +} |
0 commit comments