Skip to content

Instantly share code, notes, and snippets.

@bittailor
Created January 14, 2016 22:09
Show Gist options
  • Save bittailor/d19deb070040851ac40d to your computer and use it in GitHub Desktop.
Save bittailor/d19deb070040851ac40d to your computer and use it in GitHub Desktop.
#include <Wire.h>
#include <IPStack.h>
#include <Countdown.h>
#include <MQTTClient.h>
#include <Adafruit_INA219.h>
#include "GPRSClient.h"
SoftwareSerial sSoftwareSerial(7,8);
GPRSClient sGprsClient(sSoftwareSerial,9);
IPStack sIpStack(sGprsClient);
MQTT::Client<IPStack, Countdown, 50, 1> sMqttClient = MQTT::Client<IPStack, Countdown, 50, 1>(sIpStack);
int sArrivedcount = 0;
MQTT::Message sMessage;
Adafruit_INA219 sSensor;
void messageArrived(MQTT::MessageData& md)
{
MQTT::Message& message = md.message;
Serial.print(F("Message "));
Serial.print(++sArrivedcount);
Serial.print(F(" arrived: qos "));
Serial.print(message.qos);
Serial.print(F(", retained "));
Serial.print(message.retained);
Serial.print(F(", dup "));
Serial.print(message.dup);
Serial.print(F(", packetid "));
Serial.println(message.id);
Serial.print(F("Payload "));
Serial.println((char*)message.payload);
}
void connect()
{
char hostname[] = "iot.eclipse.org";
int port = 1883;
Serial.print(F("Connecting to "));
Serial.print(hostname);
Serial.print(F(":"));
Serial.println(port);
int rc = sIpStack.connect(hostname, port);
if (rc != 1)
{
Serial.print(F("rc from TCP connect is "));
Serial.println(rc);
}
Serial.println(F("MQTT connecting"));
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = 3;
data.clientID.cstring = (char*)"bt-solar";
data.willFlag = true;
data.will.topicName.cstring = (char*)"bt/solar/status";
data.will.message.cstring = (char*)"offline";
data.will.retained = true;
data.will.qos = MQTT::QOS1;
rc = sMqttClient.connect(data);
if (rc != 0)
{
Serial.print(F("rc from MQTT connect is "));
Serial.println(rc);
}
Serial.println(F("MQTT connected"));
char buf[20];
strcpy(buf, "online");
sMessage.qos = MQTT::QOS1;
sMessage.retained = true;
sMessage.dup = false;
sMessage.payload = (void*)buf;
sMessage.payloadlen = strlen(buf)+1;
sMqttClient.publish("bt/solar/status", sMessage);
rc = sMqttClient.subscribe("bt/solar/command", MQTT::QOS2, messageArrived);
if (rc != 0)
{
Serial.print(F("rc from MQTT subscribe is "));
Serial.println(rc);
}
Serial.println(F("MQTT subscribed"));
}
void setup()
{
Serial.begin(115200);
Serial.println(F("** MQTT PAHO over GPRS example **"));
Serial.println(F("send any character to start ..."));
while(!Serial.available()){}
Serial.println(F(" ... starting"));
sSensor.begin();
sSoftwareSerial.begin(9600);
sGprsClient.begin("1210","gprs.swisscom.ch");
connect();
}
void loop()
{
static unsigned long next = 0;
static float current = 0;
static unsigned long counter = 0;
float current_mA = sSensor.getCurrent_mA();
current += current_mA;
counter++;
if (!sMqttClient.isConnected()) {
connect();
}
if(millis() < next) {
sMqttClient.yield(100);
return;
}
next = millis() + (1000UL * 10UL);
current = current / counter;
float shuntvoltage = sSensor.getShuntVoltage_mV();
float busvoltage = sSensor.getBusVoltage_V();
float loadvoltage = busvoltage + (shuntvoltage / 1000);
Serial.print(F("Bus Voltage: ")); Serial.print(busvoltage); Serial.println(F(" V"));
Serial.print(F("Shunt Voltage: ")); Serial.print(shuntvoltage); Serial.println(F(" mV"));
Serial.print(F("Load Voltage: ")); Serial.print(loadvoltage); Serial.println(F(" V"));
Serial.print(F("Current: ")); Serial.print(current_mA); Serial.println(F(" mA"));
Serial.print(F("counter: ")); Serial.print(counter); Serial.println(F(""));
Serial.print(F("Average Current: ")); Serial.print(current); Serial.println(F(" mA"));
Serial.println("");
char buf[30];
dtostrf(current,4,2,buf);
sMessage.qos = MQTT::QOS0;
sMessage.retained = false;
sMessage.dup = false;
sMessage.payload = (void*)buf;
sMessage.payloadlen = strlen(buf)+1;
int rc = sMqttClient.publish("bt/solar/panel/A/current", sMessage);
current = 0;
counter = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment