Skip to content

Instantly share code, notes, and snippets.

@dzsquared
Created September 5, 2020 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dzsquared/7c212fbb1c174bc5f060c90426ea41f7 to your computer and use it in GitHub Desktop.
Save dzsquared/7c212fbb1c174bc5f060c90426ea41f7 to your computer and use it in GitHub Desktop.
#include <AZ3166WiFi.h>
#include "MQTTClient.h"
#include "MQTTNetwork.h"
#include "Telemetry.h"
#include "Sensor.h"
int status = WL_IDLE_STATUS;
int arrivedcount = 0;
bool hasWifi = false;
static DevI2C *ext_i2c;
static HTS221Sensor *ht_sensor;
static char buffInfo[128];
// the MQTT broker address and port
const char* mqttServer = "192.168.6.62";
int port = 1883;
#define READ_ENV_INTERVAL 2000
static volatile uint64_t msReadEnvData = 0;
void initWifi()
{
Screen.print("IoT DevKit\r\n \r\nConnecting...\r\n");
if (WiFi.begin() == WL_CONNECTED)
{
IPAddress ip = WiFi.localIP();
Screen.print(1, ip.get_address());
hasWifi = true;
Screen.print(2, "Running... \r\n");
}
else
{
Screen.print(1, "No Wi-Fi\r\n ");
}
}
int sendMQTTmsg() {
char* topic = "temperatures";
MQTTNetwork mqttNetwork;
MQTT::Client<MQTTNetwork, Countdown> client = MQTT::Client<MQTTNetwork, Countdown>(mqttNetwork);
arrivedcount = 0;
char msgBuf[100];
sprintf(msgBuf, "Connecting to MQTT server %s:%d", mqttServer, port);
Serial.println(msgBuf);
int rc = mqttNetwork.connect(mqttServer, port);
if (rc != 0) {
Serial.println("Connected to MQTT server failed");
} else {
Serial.println("Connected to MQTT server successfully");
}
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = 3;
data.clientID.cstring = "sensor-1";
// if you secured your MQTT broker with username/password
//data.username.cstring = "";
//data.password.cstring = "";
if ((rc = client.connect(data)) != 0) {
Serial.println("MQTT client connect to server failed");
}
MQTT::Message message;
float tempC = 0;
float humidity = 0;
ht_sensor->getTemperature(&tempC);
float tempF = (tempC * 1.8) + 32 - 5;
ht_sensor->getHumidity(&humidity);
char buf[100];
sprintf(buf, "{\"tempF\": %0.2f , \"humidity\": %0.2f }", tempF, humidity);
message.qos = MQTT::QOS0;
message.retained = false;
message.dup = false;
message.payload = (void*)buf;
message.payloadlen = strlen(buf); //+1
rc = client.publish(topic, message);
Serial.println(buf);
sprintf(buf, "%0.1f F %0.1f %%", tempF, humidity);
Screen.print(2, buf);
if ((rc = client.unsubscribe(topic)) != 0) {
Serial.println("MQTT client unsubscribe from server failed");
}
if ((rc = client.disconnect()) != 0) {
Serial.println("MQTT client disconnect from server failed");
}
mqttNetwork.disconnect();
return 0;
}
void setup() {
//Initialize serial and Wi-Fi:
Serial.begin(115200);
initWifi();
if ((ext_i2c = new DevI2C(D14, D15)) == NULL)
{
Serial.println("Failed to initialize I2C.");
}
if ((ht_sensor = new HTS221Sensor(*ext_i2c)) == NULL)
{
Serial.println("Failed to initialize humidity and temperature sensor.");
}
ht_sensor->init(NULL);
ht_sensor->reset();
}
void loop() {
Serial.println("\r\n>>>Enter Loop");
if (hasWifi) {
sendMQTTmsg();
}
// wait 3 minutes before sending another MQTT message
delay(180000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment