Hello, so i am receiving data vis MQTT on Nodemcu. I want to send that data to LCD Screen connected to arduino. The arduino is connected to Nodemcu and i am using SPI to send data. Nodemcu is master and arduino is slave. But my code doesnt seem to work. Can someone help me out as tomorrow is my deadline for IOT Project.
CODE:
#include<SPI.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "Kunal";
const char* password = "asdsad";
const char* mqttServer = "m14.cloudmqtt.com";
const int mqttPort = bla;
const char* mqttUser = "bla";
const char* mqttPassword = bla";
WiFiClient espClient;
PubSubClient client(espClient);
char buff[] = "Hello slave\n";
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
buff[i] = (char)payload[i];
SPI.transfer((char)payload[i]);
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
void setup() {
Serial.begin(9600);
SPI.begin(); /* begin SPI */
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
client.publish("esp/test", "Hello from ESP8266");
client.subscribe("esp/test");
//Serial.begin(9600); /* begin serial with 9600 baud */
}
void loop() {
client.loop();
}