Skip to content

Instantly share code, notes, and snippets.

@c4r-gists
Created Feb 19, 2018
Embed
What would you like to do?
pio-nodeMCU-MQTT
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Cloud4RPi.h>
const String deviceToken = "__YOUR__DEVICE_TOKEN__";
char wifiSSID[] = "__SSID__";
char wifiPassword[] = "__PASSWORD__";
void ensureWiFiConnection();
Cloud4RPi c4r(deviceToken);
WiFiClient wifiClient;
void setup() {
Serial.begin(9600);
ensureWiFiConnection();
c4r.begin(wifiClient); // Cloud4RPi initialization
if (c4r.ensureConnection()) {
c4r.printLogo();
}
}
void loop() {
ensureWiFiConnection();
if (c4r.ensureConnection(3)) { // number of attempts
c4r.loop();
Serial.print(".");
delay(1000);
}
}
void ensureWiFiConnection() {
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(wifiSSID, wifiPassword);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to Wi-Fi...");
delay(2000);
}
Serial.print("Connected! ");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}
}
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; http://docs.platformio.org/page/projectconf.html
[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
lib_deps =
cloud4rpi-esp-arduino
build_flags=
; Required for PubSub library
-D MQTT_MAX_PACKET_SIZE=1024
-D MQTT_MAX_TRANSFER_SIZE=128
; Enables the verbose output to Serial
-D CLOUD4RPI_DEBUG=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment