Created
January 19, 2015 20:57
-
-
Save belovictor/76e696c06b7c239447ea to your computer and use it in GitHub Desktop.
cc3200 Energia sketch with WiFi and MQTT
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
#include <Wire.h> | |
#include <WiFi.h> | |
#include <PubSubClient.h> | |
#include <Adafruit_TMP006.h> | |
char ssid[] = "<WiFi SSID>"; | |
char pass[] = "<WiFi key>"; | |
int status = WL_IDLE_STATUS; | |
IPAddress ip; | |
// MQTTServer to use | |
char server[] = "<MQTT Server>"; | |
int button1State = 0; | |
int button2State = 0; | |
int lastButton1State = 0; | |
int lastButton2State = 0; | |
const int button1Pin = PUSH1; | |
const int button2Pin = PUSH2; | |
unsigned long currentTime; | |
unsigned long lastTime; | |
unsigned long intervalTime = 30000; | |
WiFiClient wifiClient; | |
PubSubClient client(server, 1883, callback, wifiClient); | |
Adafruit_TMP006 tmp006(0x41); | |
void callback(char* topic, byte* payload, unsigned int length) { | |
// Serial.print("Received message for topic "); | |
Serial.print(topic); | |
Serial.print(": "); | |
// Serial.print("with length "); | |
// Serial.println(length); | |
// Serial.println("Message:"); | |
Serial.write(payload, length); | |
Serial.println(); | |
} | |
void setup() | |
{ | |
pinMode(button1Pin, INPUT_PULLUP); | |
pinMode(button2Pin, INPUT_PULLUP); | |
Wire.begin(); | |
Serial.begin(9600); | |
while (!Serial) { | |
; // wait for serial port to connect. Needed for Leonardo only | |
} | |
if (! tmp006.begin()) { | |
Serial.println("No temperature sensor found"); | |
while (1); | |
} | |
while (status != WL_CONNECTED) { | |
Serial.println("Connecting to WiFi"); | |
status = WiFi.begin(ssid, pass); | |
if (status == WL_CONNECTED) | |
break; | |
delay(10000); | |
} | |
Serial.println("Connected to WiFi"); | |
ip = WiFi.localIP(); | |
Serial.println(ip); | |
while (!client.connect("energiaClient")) { | |
Serial.println("MQTT Connection failed, retrying..."); | |
delay(5000); | |
} | |
Serial.println("MQTT Connected"); | |
while (!client.subscribe("/#")) { | |
Serial.println("MQTT Subscription failed, retrying..."); | |
delay(5000); | |
} | |
Serial.println("MQTT Subscribed"); | |
lastTime = millis(); | |
} | |
void loop() | |
{ | |
currentTime = millis(); | |
client.loop(); | |
button1State = digitalRead(button1Pin); | |
button2State = digitalRead(button2Pin); | |
if (button1State != lastButton1State) { | |
if (button1State == HIGH) { | |
Serial.println("Button 1 pressed"); | |
client.publish("<Topic>","ON"); | |
} else { | |
Serial.println("Button 1 released"); | |
} | |
lastButton1State = button1State; | |
} | |
if (button2State != lastButton2State) { | |
if (button2State == HIGH) { | |
Serial.println("Button 2 pressed"); | |
client.publish("<Topic>","OFF"); | |
} else { | |
Serial.println("Button 2 released"); | |
} | |
lastButton2State = button2State; | |
} | |
if (currentTime - lastTime > intervalTime) { | |
double objt = tmp006.readDieTempC(); | |
Serial.print("Object Temperature: "); Serial.print(objt); Serial.println("*C"); | |
char charVal[10]; | |
dtostrf(objt, 4, 2, charVal); | |
Serial.println(charVal); | |
client.publish("<Topic>", charVal); | |
lastTime = currentTime; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a simple sketch I wrote in Energia IDE for Launchpad 3200 to connect this great board to my home MQTT server and exchange data with my openHAB, specifically turn my home office light on and off and report temperature from onboard sensor.