ESP8266 mqtt messages don't get published - arduino

So I got the following situation: I'm trying to publish data from an DHT11 sensor (sensor is working just fine) via mqtt to my broker. But the messages don't get published - I'm testing it via client.state, which returns 5, which means "the client was not authorized to connect" (API Documentation).
This is my code (I replaced sensible data with XXs):
#include "DHT.h" //DHT Bibliothek laden
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#define WLAN_SSID "XXXXXXX"
#define WLAN_PASS "XXXXXXX"
const char* mqtt_server = "XXXXXXX";
#define mqtt_user "XXXXXX"
#define mqtt_password "XXXXXX"
#define DHTPIN D4 //Der Sensor wird an PIN D4 angeschlossen
#define DHTTYPE DHT11 // Es handelt sich um den DHT11 Sensor
#define humidity_topic "esp01/humidity"
#define temperature_topic "esp01/temperature"
#define status_topic "esp01/status"
DHT dht(DHTPIN, DHTTYPE); //Der Sensor wird ab jetzt mit „dht“ angesprochen
WiFiClient espClient;
PubSubClient client(mqtt_server, 1883, espClient);
const char* clientID = "clientID";
float Luftfeuchtigkeit = 0.0;
float Temperatur = 0.0;
void setup() {
Serial.begin(9600); //Serielle Verbindung starten
dht.begin(); //DHT11 Sensor starten
client.connect(clientID);
setup_wifi();
client.setServer(mqtt_server, 1883);
if (!client.connected()) {
reconnect();
}
client.loop();
Luftfeuchtigkeit = dht.readHumidity(); //die Luftfeuchtigkeit auslesen und unter „Luftfeuchtigkeit“ speichern
Temperatur = dht.readTemperature();//die Temperatur auslesen und unter „Temperatur“ speichern
Serial.print(F("Luftfeuchtigkeit: ")); //Im seriellen Monitor den Text und
Serial.print(String(Luftfeuchtigkeit).c_str()); //die Dazugehörigen Werte anzeigen
Serial.println(" %");
client.publish(humidity_topic, String(Luftfeuchtigkeit).c_str());
Serial.print("Temperatur: ");
Serial.print(Temperatur);
Serial.println(" Grad Celsius");
client.publish(temperature_topic, String(Temperatur).c_str(), true);
}
void loop() {
delay(2000);
Serial.print("Luftfeuchtigkeit: "); //Im seriellen Monitor den Text und
Serial.print(String(Luftfeuchtigkeit).c_str()); //die Dazugehörigen Werte anzeigen
Serial.println(" %");
client.publish(humidity_topic, String(Luftfeuchtigkeit).c_str(), true);
Serial.print("Temperatur: ");
Serial.print(Temperatur);
Serial.println(" Grad Celsius");
client.publish(temperature_topic, String(Temperatur).c_str(), true);
delay(60000);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
int _try = 0;
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(clientID)) {
Serial.println("connected");
client.publish(humidity_topic, String(Luftfeuchtigkeit).c_str(), true);
} else {
Serial.print("failed, state: ");
Serial.println(client.state());
Serial.println("attempting reconnection...");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
The output looks like this:
19:13:42.245 ->
19:13:42.245 -> Connecting to XXXXXXX
19:13:42.292 -> .......
19:13:46.608 -> WiFi connected
19:13:46.608 -> IP address:
19:13:46.645 -> XXXXXXX
19:13:46.645 -> Attempting MQTT connection...failed, state: 5
19:13:46.740 -> attempting reconnection...
19:13:51.706 -> Attempting MQTT connection...failed, state: 5
19:13:51.937 -> attempting reconnection...
19:13:56.919 -> Attempting MQTT connection...failed, state: 5
19:13:57.064 -> attempting reconnection...
To me it looks like the ESP gets rejected by the server, but user, password and servername are correct - I just don't get what's the problem. I'd love some help!

Your code defines a username and password for the MQTT broker but never actually uses them. You need to pass them to the connect method or else they do nothing.
if (client.connect(clientID)) {
should be
if (client.connect(clientID, mqtt_user, mqtt_password)) {
You're also calling client.connect(clientID); before you connect to wifi and before you set the server, which is a bold choice and not helpful. Don't do that.
client.connect(clientID);
setup_wifi();
client.setServer(mqtt_server, 1883);
if (!client.connected()) {
You might take a look over PubSubClient's examples to see how to use it properly.
should be
setup_wifi();
client.setServer(mqtt_server, 1883);
if (!client.connected()) {

Related

esp 32 with esp now and mqtt connection

i trying to acheive some home automation with esp 32. my project is like following
i have several end points with esp 8266 acting likes slave switches sending Id datas to an esp 32 who is my gateway.
both slaves (esp8266) and master gateway (esp 32, are communicating
with espnow library
the gateway should be connected to an mqtt server ( rasperry pi) who
dispatches automation command depending on endpoint Id.
For now i succesfully have communication between esp8266 slaves and esp 32 gateway
i also have succesfully setted my rasperry pi as mqtt broker with home automation command
But i'm blocked when i need to add wifi connection and mqtt messaging to my ESP 32 gateway
My code on the esp32 gateway is
#include <esp_now.h>
#include <WiFi.h>
#include <PubSubClient.h>
#define LED 2
int myData;
char ssid[] = "my ssid";
char password[] = "mypass";
char mqtt_server[] = "192.168.1.55";
WiFiClient espClient;
PubSubClient MQTTclient(espClient);
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("data: ");
Serial.println(myData);
/*
if(myData == 1){
String reponse="device1";
MQTTclient.publish("homecommand",reponse.c_str());
}
*/
for (int t=0; t<3; t++)
{
digitalWrite(LED,HIGH);
delay(50);
digitalWrite(LED,LOW);
delay(50);
}
}
void MQTTconnect() {
while (!MQTTclient.connected()) {
Serial.print("Attente MQTT connection...");
String clientId = "TestClient-";
clientId += String(random(0xffff), HEX);
// test connexion
if (MQTTclient.connect(clientId.c_str(),"","")) {
Serial.println("connected");
} else { // si echec affichage erreur
Serial.print("ECHEC, rc=");
Serial.print(MQTTclient.state());
Serial.println(" nouvelle tentative dans 5 secondes");
delay(5000);
}
}
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
pinMode(LED,OUTPUT);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
/*
Serial.println("Connect to Wifi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected");
MQTTclient.setServer(mqtt_server, 1883);
if (!MQTTclient.connected()) {
MQTTconnect();
}*/
}
void loop() {
// put your main code here, to run repeatedly:
}
The code works well, when i trigger one of my slaves i receive the message print it in serial an blink 3 times
But when i uncomment the part about connecting to my home router and my mqtt broker i lost the 'On DataRecv' callback
So my question is "Is it possible to have an Esp working as espnow gateway to gather messages and at the same time being connected to my home router for mqtt publishing ?"
If yes i would appreciate some help to implement it.
Thanks a lot.
Swap
WiFi.mode(WIFI_STA);
for
WiFi.mode(WIFI_AP_STA);
re:
https://www.electrosoftcloud.com/en/esp32-wifi-and-esp-now-simultaneously/

Not able to Receive subscribed Message with PubSubClient.h in Arduino WeMos D1 R32

I'm using Arduino Wemos Esp32 D1 R32.
I'm trying to make an MQTT connection between My Arduino board and a MQTT Broker.
When I try to send messages from my Arduino, It works just fine. (I'm using a pullup button to send a message.) Whenever I press the button a message is sent.
But when I try to read a message and write it on the Serial with the callback function it just doesn't work.
Could someone please help me with that issue?
Here is my code:
[code]
#include <ETH.h>
#include <WiFi.h>
#include <WiFiAP.h>
#include <WiFiClient.h>
#include <WiFiGeneric.h>
#include <WiFiMulti.h>
#include <WiFiScan.h>
#include <WiFiServer.h>
#include <WiFiSTA.h>
#include <WiFiType.h>
#include <WiFiUdp.h>
#include <PubSubClient.h>
#define BUFFER_SIZE 100
#define pinBotao1 25 //input botao pullup
#define led 26 //output acende led
#define pot 4 //input Resistor
//WiFi
const char* SSID = "******"; // SSID / nome da rede WiFi que deseja se conectar
const char* PASSWORD = "******"; // Senha da rede WiFi que deseja se conectar
WiFiClient wifiClient;
//MQTT Server
const char* BROKER_MQTT = "mqtt.eclipse.org"; //URL do broker MQTT que se deseja utilizar
int BROKER_PORT = 1883; // Porta do Broker MQTT
#define ID_MQTT "BCI0111"
#define TOPIC_PUBLISH "BCIBotao111"
PubSubClient MQTT(wifiClient); // Instancia o Cliente MQTT passando o objeto espClient
//Declaração das Funções
void mantemConexoes(); //Garante que as conexoes com WiFi e MQTT Broker se mantenham ativas
void conectaWiFi(); //Faz conexão com WiFi
void conectaMQTT(); //Faz conexão com Broker MQTT
void enviaPacote(); //Envia uma mensagem ao Broker.
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void setup() {
pinMode(pinBotao1, INPUT_PULLUP);
pinMode(led, OUTPUT);
Serial.begin(115200);
conectaWiFi();
MQTT.setServer(BROKER_MQTT, BROKER_PORT);
MQTT.setCallback(callback);
}
void loop() {
mantemConexoes();
enviaValores();
MQTT.loop();
}
void mantemConexoes() {
if (!MQTT.connected()) {
conectaMQTT();
}
conectaWiFi(); //se não há conexão com o WiFI, a conexão é refeita
}
void conectaWiFi() {
if (WiFi.status() == WL_CONNECTED) {
return;
}
Serial.print("Conectando-se na rede: ");
Serial.print(SSID);
Serial.println(" Aguarde!");
WiFi.begin(SSID, PASSWORD); // Conecta na rede WI-FI
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
WiFi.begin(SSID, PASSWORD);
}
Serial.println();
Serial.print("Conectado com sucesso, na rede: ");
Serial.print(SSID);
Serial.print(" IP obtido: ");
Serial.println(WiFi.localIP());
}
void conectaMQTT() {
while (!MQTT.connected()) {
Serial.print("Conectando ao Broker MQTT: ");
Serial.println(BROKER_MQTT);
//if (MQTT.connect(ID_MQTT,USER_MQTT,PASSWORD_MQTT)) {
if (MQTT.connect(ID_MQTT)) {
Serial.println("Conectado ao Broker com sucesso!");
}
else {
Serial.println("Nao foi possivel se conectar ao broker.");
Serial.println("Nova tentativa de conexao em 10s");
delay(10000);
}
}
}
void enviaValores() {
bool estadoBotao1;
int estadoPot;
estadoBotao1 = digitalRead(pinBotao1);
estadoPot = char(analogRead(pot));
if (estadoBotao1==LOW) {
//Botao Apertado
MQTT.publish(TOPIC_PUBLISH, "estadoPot");
Serial.println("Botao1 APERTADO. Payload enviado.");
}
delay(1000);
}
You need to subscribe to one or more topics to receive messages. Typically you subscribe to the topic(s) you are interested in after connecting to MQTT, but you may also subscribe at other times based on your app's logic.
In this case you can change conectaMQTT like this:
void conectaMQTT() {
while (!MQTT.connected()) {
Serial.print("Conectando ao Broker MQTT: ");
Serial.println(BROKER_MQTT);
//if (MQTT.connect(ID_MQTT,USER_MQTT,PASSWORD_MQTT)) {
if (MQTT.connect(ID_MQTT)) {
Serial.println("Conectado ao Broker com sucesso!");
if (MQTT.subscribe(TOPIC_SUBSCRIBE)) {
Serial.println("Succesful subscription.");
} else {
Serial.println("Subscription failed.");
}
}
else {
Serial.println("Nao foi possivel se conectar ao broker.");
Serial.println("Nova tentativa de conexao em 10s");
delay(10000);
}
}
}

mqtt between esp8266 and arduino with PubSubclient

I am using ESP8266 with arduino using WiFiEsp library.I want to make MQTT connection with arduino so I use PubSubclient library.i got error:
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
my code is:
#include <WiFiEsp.h>
#include <WiFiEspClient.h>
#include <WiFiEspUdp.h>
#include "SoftwareSerial.h"
#include <PubSubClient.h>
IPAddress server(212, 72, 74, 21);
char ssid[] = "atmel"; // your network SSID (name)
char pass[] = "bets56789"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
// Initialize the Ethernet client object
WiFiEspClient espClient;
PubSubClient client(espClient);
SoftwareSerial soft(2,3); // RX, TX
void setup() {
// initialize serial for debugging
Serial.begin(9600);
// initialize serial for ESP module
soft.begin(115200);
// initialize ESP module
WiFi.init(&soft);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
// you're connected now, so print out the data
Serial.println("You're connected to the network");
//delay(2000);
//connect to MQTT server
client.setServer(server, 1883);
client.setCallback(callback);
}
//print any message received for subscribed topic
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void loop() {
// put your main code here, to run repeatedly:
if (!client.connected()) {
reconnect();
}
client.loop();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect, just a name to identify the client
if (client.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outSHADAB","hello world");
// ... and resubscribe
client.subscribe("inShadab");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
I am using ESP8266 with arduino using WiFiEsp library.I want to make MQTT connection with arduino so I use PubSubclient library
I don't think the PubSubClient supports the WiFiEsp as a network layer from an arduino.
While the doc list the ESP8266 I believe this is running the PubSubClient directly on the ESP8266 hardware.

ESP 8266 Program Issue

Description
I am writing a web server on ESP-8266(01), that will control 3 locks using Arduino Mega.
The locks will be controlled by Bluetooth ESP 8266 (get method from phone) and using my laptops browser.
I can do it all with bluetooth HC-05 (easily).
The issue I am facing is with my ESP-8266 wifi module.
I need to send 3 different strings from ESP for Arduino to read it.
If it's "AAAA" Arduino will read it and unlock door one, if it BBBB then door 2 and etc etc.....
So I used this code:
#include <ESP8266WiFi.h>
const char* ssid = "Do-Not-Ask-For-Password";
const char* password = "ITISMYPASSWORD";
//const char* ssid = "`STAR-NET-Azhar-32210352";
//const char* password = "ITISMYPASSWORD";
int ledPin = 2; // GPIO2
WiFiServer server(80);
// Update these with values suitable for your network.
IPAddress ip(192,168,8,128); //Node static IP
IPAddress gateway(192,168,8,1);
IPAddress subnet(255,255,255,0);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
ESP.wdtDisable();
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
// Serial.println("new client");
// while(!client.available()){
// delay(1);
// }
if(!client.available()){
delay(1);
client.flush();
}
// Read the first line of the request
String request = client.readStringUntil('\r');
//Serial.println(request);
client.flush();
// Match the request
int value1 = LOW;
int value2 = LOW;
int value3 = LOW;
if (request.indexOf("/LOCK=ON1") != -1) {
//digitalWrite(ledPin, HIGH);
value1 = HIGH;
Serial.println("AAAA");
}
if(request.indexOf("/LOCK=ON2") != -1){
value2 = HIGH;
Serial.println("BBBB");
}
if(request.indexOf("/LOCK=ON3") != -1){
value3 = HIGH;
Serial.println("CCCC");
}
// Set ledPin according to the request
//digitalWrite(ledPin, value);
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<br><br>");
client.print("LOCK ONE IS NOW: ");
if(value1 == HIGH) {
client.print("UNLOCKED");
// Serial.println("A");
} else {
client.print("LOCKED");
}
client.println("<br><br>");
client.print("LOCK TWO IS NOW: ");
if(value2 == HIGH) {
client.print("UNLOCKED");
// Serial.println("B");
} else {
client.print("LOCKED");
}
client.println("<br><br>");
client.print("LOCK THREE IS NOW: ");
if(value3 == HIGH) {
client.print("UNLOCKED");
//Serial.println("C");
} else {
client.print("LOCKED");
}
client.println("<br><br>");
client.println("CLICK HERE TO UNLOCK THE LOCK ONE<br>");
client.println("CLICK HERE TO UNLOCK THE LOCK TWO<br>");
client.println("CLICK HERE TO UNLOCK THE LOCK THREE<br>");
client.println("</html>");
delay(100);
//Serial.println("client disconnected");
// Serial.println("");
}
The Result I Get on Browser is ( Which is What i need):
enter image description here
ISSUE:
Now The Issue Is Some Times it misses the serial data i send, For Example if i click unlock door 1, It sends data and i click unlock door 2 it does nothn and when i reset it sends data from all 3 but for two to 3 times and then module resets it self ....
This Is What I got From Serial Monitor:
enter image description here
Now i have searched alot and its watchdre out what i did wrong in the code.
this is how my ESP is connected:
vcc & CHPD to 3.3V ragulator
gnd to gnd of powersuply and to arduino gnd...
GP 0 ------> 3.3v with 2.2k Resistor ( in non flashing state).
Rst ------> 3.3v with resistor
RX to TX
TX to RX
Any Ideas how to fix it?
Is There any other way to control Arduino pins (6 different pins) from ESP 8266 module??
ive been trying from many days please help!!
Ok I am Posting It For those who are facing the same problem!! i took the Hello ESP example And Modified it to Print Serial!!!
On Mega My Serial Was Started at 9600 and i started Serial On ESP at 9600 aswell :| So This is the Code .... Slow Server and WDT issue is Almost Gone....
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
//const int RELAY_PIN = 2; //RELAY
const char* ssid = "Do-Not-Ask-For-Password";
const char* password = "AeDrki$32ILA!$#2";
MDNSResponder mdns;
ESP8266WebServer server(80);
void handleRoot() {
server.send(200, "text/plain", "hWelcome To Door Unlock Project By Haziq Sheikh");
}
void handleNotFound(){
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup(void){
// pinMode(RELAY_PIN, OUTPUT);
Serial.begin(9600);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
//digitalWrite(RELAY_PIN, 1);
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// digitalWrite(RELAY_PIN, 0);
if (mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/lock1", [](){
server.send(200, "text/plain", "Okay Door One Unlocked");
Serial.println("AAAA");
});
server.on("/lock2", [](){
server.send(200, "text/plain", "Okay -- Door 2 Unlocked");
Serial.println("BBBB");
});
server.on("/lock3", [](){
server.send(200, "text/plain", "Okay -- Door 3 is Unlocked");
Serial.println("CCCC");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
}

Can't commute a relay with Arduino wifi shield + Xively

I’m trying to turn on and off an LED light bulb connected to a Tinterkit Relay.
I’m using an Arduino UNO r3 connected to internet thanks to an official Arduino Wifi shield.
I’ve done a simple website with two buttons to send a 1 (on) or a 0 (off) to my Xively account.
I’ve written a code to detect the last posted channel value. The code is working fine and I’m able to detect a 1 or a 0 every 3 seconds approximately. The problem is that the relay doesn’t commute.
Please please, I would appreciate a lot your help to solve this problem.
Here is the code:
#include <SPI.h>
#include <WiFi.h>
#include <HttpClient.h>
#include <Xively.h>
#include <TinkerKit.h>
char ssid[] = "XXXXXX";
char pass[] = "XXXXXX";
int state;
int status = WL_IDLE_STATUS;
char myIntStream[]="LED";
char xivelyKey[] = "XXXXXX";
#define FEED_ID XXXXXX
TKMosFet relay(O0);
XivelyDatastream datastreams[] = {
XivelyDatastream(myIntStream, strlen(myIntStream), DATASTREAM_INT),
};
XivelyFeed feed(FEED_ID,datastreams,1);
WiFiClient client;
XivelyClient xivelyclient(client);
void printWifiStatus() {
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void setup() {
Serial.begin(9600);
Serial.println("LED light bulb");
Serial.println();
while ( status != WL_CONNECTED) {
Serial.println("Connecting to internet ...");
status = WiFi.begin(ssid, pass);
Serial.println("");
delay(5000);
}
Serial.println("Connected to wifi");
printWifiStatus();
}
void loop() {
Serial.println();
Serial.print("Datastream is:");
Serial.println(datastreams[0]);
Serial.print("LED value = ");
state = datastreams[0].getInt();
Serial.print(state);
if(state == 0){
relay.off();
Serial.println(" => Light is on.");
}
else if(state == 1){
relay.on();
Serial.println(" => Light is off.");
}
xivelyclient.get(feed, xivelyKey);
}

Resources