Created
September 20, 2017 21:30
-
-
Save jimhansson/777f7abcd152677da19eaaf100c84ce7 to your computer and use it in GitHub Desktop.
one of the arduino mqtt projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <DHT.h> | |
#include <Dhcp.h> | |
#include <Dns.h> | |
#include <Ethernet.h> | |
#include <EthernetClient.h> | |
#include <EthernetServer.h> | |
#include <EthernetUdp.h> | |
#include <ELClient.h> | |
#include <ELClientCmd.h> | |
#include <ELClientMqtt.h> | |
#include <ELClientResponse.h> | |
#include <ELClientRest.h> | |
#include <FP.h> | |
// Initialize a connection to esp-link using the normal hardware serial port both for | |
// SLIP and for debug messages. | |
ELClient esp(&Serial, &Serial); | |
// Initialize CMD client (for GetTime) | |
ELClientCmd cmd(&esp); | |
// Initialize the MQTT client | |
ELClientMqtt mqtt(&esp); | |
// Callback made from esp-link to notify of wifi status changes | |
// Here we just print something out for grins | |
void wifiCb(void* response) { | |
ELClientResponse *res = (ELClientResponse*)response; | |
if (res->argc() == 1) { | |
uint8_t status; | |
res->popArg(&status, 1); | |
if(status == STATION_GOT_IP) { | |
Serial.println("WIFI CONNECTED"); | |
} else { | |
Serial.print("WIFI NOT READY: "); | |
Serial.println(status); | |
} | |
} | |
} | |
bool connected; | |
// Callback when MQTT is connected | |
void mqttConnected(void* response) { | |
Serial.println("MQTT connected!"); | |
//mqtt.subscribe("/esp-link/1"); | |
//mqtt.subscribe("/hello/world/#"); | |
//mqtt.subscribe("/esp-link/2", 1); | |
//mqtt.publish("/esp-link/0", "test1"); | |
connected = true; | |
} | |
// Callback when MQTT is disconnected | |
void mqttDisconnected(void* response) { | |
Serial.println("MQTT disconnected"); | |
connected = false; | |
} | |
// Callback when an MQTT message arrives for one of our subscriptions | |
void mqttData(void* response) { | |
ELClientResponse *res = (ELClientResponse *)response; | |
Serial.print("Received: topic="); | |
String topic = res->popString(); | |
Serial.println(topic); | |
Serial.print("data="); | |
String data = res->popString(); | |
Serial.println(data); | |
} | |
void mqttPublished(void* response) { | |
Serial.println("MQTT published"); | |
} | |
DHT dht(9, DHT11); | |
void setup() { | |
Serial.begin(19200); | |
Serial.println("EL-Client starting!"); | |
// Sync-up with esp-link, this is required at the start of any sketch and initializes the | |
// callbacks to the wifi status change callback. The callback gets called with the initial | |
// status right after Sync() below completes. | |
esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired) | |
bool ok; | |
do { | |
ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds | |
if (!ok) Serial.println("EL-Client sync failed!"); | |
} while(!ok); | |
Serial.println("EL-Client synced!"); | |
// Set-up callbacks for events and initialize with es-link. | |
mqtt.connectedCb.attach(mqttConnected); | |
mqtt.disconnectedCb.attach(mqttDisconnected); | |
mqtt.publishedCb.attach(mqttPublished); | |
mqtt.dataCb.attach(mqttData); | |
mqtt.setup(); | |
//Serial.println("ARDUINO: setup mqtt lwt"); | |
//mqtt.lwt("/lwt", "offline", 0, 0); //or mqtt.lwt("/lwt", "offline"); | |
Serial.println("EL-MQTT ready"); | |
dht.begin(); | |
} | |
static int count; | |
static uint32_t last; | |
void loop() { | |
esp.Process(); | |
if (connected && (millis()-last) > 4000) { | |
int mosture = analogRead(0); | |
Serial.print("mosture: "); | |
Serial.println(mosture); | |
int photo = analogRead(1); | |
Serial.print("Photo: "); | |
Serial.println(photo); | |
float hum = dht.readHumidity(); | |
Serial.print("Humidity: "); | |
Serial.println(hum); | |
float temp = dht.readTemperature(); | |
Serial.print("Temperature: "); | |
Serial.println(temp); | |
float temp_f = dht.readTemperature(true); | |
if(isnan(hum) || isnan(temp) || isnan(temp_f)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
// Compute heat index in Fahrenheit (the default) | |
float hif = dht.computeHeatIndex(temp_f, hum); | |
// Compute heat index in Celsius (isFahreheit = false) | |
float hic = dht.computeHeatIndex(temp, hum, false); | |
Serial.print("HeatIndex: "); | |
Serial.println(hic); | |
Serial.println("publishing"); | |
String most(mosture); | |
String fields = "field1=" + most + | |
"&field2=" + hum + | |
"&field3=" + temp + | |
"&field4=" + hif + | |
"&field5=" + photo + | |
"&status=MQTTPUBLISH"; | |
Serial.println(fields.c_str()); | |
mqtt.publish("channels/83815/publish/CA0MWSO0QLZIS3YL", fields.c_str()); | |
uint32_t t = cmd.GetTime(); | |
Serial.print("Time: "); Serial.println(t); | |
last = millis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
next time I will hide the channel key in eeprom!!!!!