Created
November 7, 2017 21:23
-
-
Save jhaury/96f24de0e55e777d000813caef37bf79 to your computer and use it in GitHub Desktop.
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 <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
// not available on ESP-01, but needed in the code | |
#define OLED_RESET 4 | |
Adafruit_SSD1306 display(OLED_RESET); | |
const char* ssid = "SSID"; // ssid | |
const char* password = "password"; // password | |
const char* mqtt_server = "10.0.0.10"; // mqqt server | |
const char* inTopic = "inTopic"; // topic esp will subscribe to | |
const char* clientId = "esp-testing"; // id of the esp | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
long lastMsg = 0; | |
char msg[50]; | |
int value = 0; | |
void setup() | |
{ | |
// set gpio0 and gpio2 as sda, scl | |
Wire.begin(0,2); | |
// initialize with the I2C addr 0x3C (for the 128x32)(initializing the display) | |
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); | |
Serial.begin(9600); | |
setup_wifi(); | |
client.setServer(mqtt_server, 1883); | |
client.setCallback(callback); | |
} | |
void setup_wifi() { | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
String message = (char*)payload; | |
// if payload = X | |
// if ((char)payload[0] == '1') { | |
// on any payload | |
if ((char)payload[0]) { | |
// do stuff | |
display.clearDisplay(); | |
// oled will wrap lines, single line stops at U, lol, at you get it... | |
// display.print("abcdefghijklmnopqrstuvwxyz"); | |
display.setTextColor(WHITE); | |
display.setTextSize(1); | |
display.setCursor(0,0); | |
// print the message we get from mqtt | |
display.print(message); | |
// if payload = 0, using this to clear the line | |
} else if ((char)payload[0] == '0') { | |
// do stuff | |
display.clearDisplay(); | |
display.setTextColor(BLACK); | |
display.setTextSize(1); | |
display.setCursor(0,0); | |
display.print("....................."); | |
} | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
if (client.connect(clientId)) { | |
Serial.println("connected"); | |
// Once connected, publish an announcement... | |
// client.publish("outTopic", "hello world"); | |
// ... and resubscribe | |
client.subscribe(inTopic); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void displayTempHumid(){ | |
delay(2000); | |
display.clearDisplay(); | |
display.setTextColor(WHITE); | |
display.setTextSize(1); | |
display.setCursor(0,0); | |
display.print("Humidity: "); | |
display.print("10"); | |
display.print(" %\t"); | |
display.setCursor(0,10); | |
display.print("Temperature: "); | |
display.print("71"); | |
display.print(" F"); | |
display.setCursor(0,20); | |
display.print("Temperature: "); | |
display.print("53"); | |
display.print(" F"); | |
} | |
void loop() | |
{ | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
// displayTempHumid(); | |
display.display(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment