Last active
February 5, 2020 04:53
-
-
Save AgustinPelaez/222afac6f0fe6a19c1bc15ea052c2180 to your computer and use it in GitHub Desktop.
MQTT nodeMCU with Ubidots
This file contains hidden or 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
// MQTT-Ubidots example using Grove Temperature Sensor v1.2 in A0 and Grove Relay on D6 (pin 12) | |
#include <PubSubClient.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFiMulti.h> | |
#define WIFISSID "wifi-ssid" | |
#define PASSWORD "IOT2016" | |
#define TOPIC1 "/v1.6/devices/motor/relay/lv" | |
#define TOPIC2 "/v1.6/devices/motor/temp" | |
#define TOKEN "ZCj1-----------------ZLNP9U" | |
#define PIN 12 | |
int a; | |
const int B = 4275; // B value of the thermistor | |
const int R0 = 100000; // R0 = 100k | |
const int pinTempSensor = A0; // Grove - Temperature Sensor connected to A0 | |
char mqttBroker[] = "things.ubidots.com"; | |
char payload[100]; | |
ESP8266WiFiMulti WiFiMulti; | |
WiFiClient c; | |
PubSubClient client(c); | |
void callback(char* topic, byte* payload, unsigned int length) { | |
char p[length + 1]; | |
memcpy(p, payload, length); | |
p[length] = NULL; | |
String message(p); | |
if (message == "0") { | |
digitalWrite(PIN, LOW); | |
} else { | |
digitalWrite(PIN, HIGH); | |
} | |
Serial.write(payload, length); | |
Serial.println(topic); | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.println("Attempting MQTT connection..."); | |
// Attempt to connect | |
if (client.connect("nodemcu", TOKEN,"")) { | |
Serial.println("connected"); | |
client.subscribe(TOPIC1); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 2 seconds"); | |
// Wait 2 seconds before retrying | |
delay(2000); | |
} | |
} | |
} | |
void setup() { | |
pinMode(PIN, OUTPUT); | |
Serial.begin(115200); | |
delay(10); | |
// We start by connecting to a WiFi network | |
WiFiMulti.addAP(WIFISSID, PASSWORD); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Wait for WiFi... "); | |
while(WiFiMulti.run() != WL_CONNECTED) { | |
Serial.print("."); | |
delay(500); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
client.setServer(mqttBroker, 1883); | |
client.setCallback(callback); | |
} | |
void loop() { | |
if (!client.connected()) { | |
reconnect(); | |
} | |
a = analogRead(pinTempSensor ); | |
float R = 1023.0/((float)a)-1.0; | |
R = 100000.0*R; | |
float temperature = 1.0/(log(R/100000.0)/B+1/298.15)-273.15; //convert to temperature as explained by SEEEDStudio's wiki ; | |
sprintf(payload,"%s%d.%02d%s", "{\"value\":", (int)temperature, (int)(temperature*100)%100,"}"); | |
Serial.println(payload); | |
client.loop(); | |
client.publish(TOPIC2, payload); | |
memset(payload, 0, sizeof(payload)); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment