// Code for Node MCU only //
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point /
#define WLAN_SSID ""
#define WLAN_PASS ""
/************ Adafruit.io Setup /
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME ""
#define AIO_KEY "**"
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/voltage");
Adafruit_MQTT_Publish photocel2 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/current");
Adafruit_MQTT_Publish photocel3 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Kwh");
Adafruit_MQTT_Publish photocel4 = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/watt");
void MQTT_connect();
#include <SoftwareSerial.h>
SoftwareSerial s(D4, D3);
#include <ArduinoJson.h>
void setup()
{
Serial.begin(115200);
delay(10);
Serial.println(F("Solar Power Monitoring"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
// Setup MQTT subscription for onoff feed.
//mqtt.subscribe(&onoffbutton);
s.begin(9600); while (!Serial) continue;
}
uint32_t x = 0;
void loop()
{
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(s);
if (root == JsonObject::invalid()) return;
delay(0);
Serial.println("IOT based sun tracking Solar Monitoring station");
root.prettyPrintTo(Serial);
Serial.print("voltage");
int voltage = root["voltage"];
Serial.print(voltage);
Serial.print("Amps");
int Amps = root["Amps"];
Serial.print(Amps);
Serial.print("Kwh");
int Kwh = root["Kwh"];
Serial.print(Kwh);
Serial.print("watt");
int watt = root["watt"];
Serial.print(watt);
{
Serial.println("");
Serial.println("---------------------xxxxx--------------------");
}
// function definition further below.
MQTT_connect();
// this is our 'wait for incoming subscription packets' busy subloop
// try to spend your time her
// Now we can publish stuff!
Serial.println(F("\nSolar monitoring... "));
if (! photocell.publish(voltage))
{
Serial.println(F("Failed"));
} else
{
Serial.println(F("OK!"));
}
if (! photocel2.publish(Amps))
{
Serial.println(F("Failed"));
} else
{
Serial.println(F("OK!"));
}
if (! photocel3.publish(Kwh))
{
Serial.println(F("Failed"));
} else
{
Serial.println(F("OK!"));
}
if (! photocel4.publish(watt))
{
Serial.println(F("Failed"));
} else
{
Serial.println(F("OK!"));
}
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
/*
if(! mqtt.ping()) {
mqtt.disconnect();
}
*/
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.println("Connecting to Adafruit... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("Adafruit Connected!");
}