Things used in this project

Code

Paho Chat ClientPython
Terminal chat client implemented with Paho's Python MQTT client library.
'''
Terminal chat client with Paho's Python MQTT implementation.
'''

# Import Paho and time libraries
import paho.mqtt.client as mqtt
import time

# Define various MQTT callback functions - we'll only be using some of these,
# but the others are defined here to help with debugging should you need them
def on_connect(client, userdata, flags, rc):
    print("Connected! rc:", rc)

def on_message(client, userdata, message):
    if str(message.topic) != pubtop:
        print(str(message.topic), str(message.payload.decode("utf-8")))

def on_subscribe(client, userdata, mid, granted_qos):
    print("Subscribed:", str(mid), str(granted_qos))

def on_unsubscribe(client, userdata, mid):
    print("Unsubscribed:", str(mid))

def on_publish(client, userdata, mid):
    print("Publish:", client)

def on_log(client, userdata, level, buf):
    print("log:", buf)

def on_disconnect(client, userdata, rc):
    if rc != 0:
        print("Unexpected disconnection.")

# Set the address of your broker and your port. For a local broker, use the IP
# address. Otherwise, use the web address.
broker_address = "iot.eclipse.org"
# broker_address = <insert your IP address here>
port = 1883
# port = 8883 # port for TLS/SSL

# Create the MQTT client and set the callback functions you want to use
client = mqtt.Client()
client.on_subscribe = on_subscribe
client.on_unsubscribe = on_unsubscribe
client.on_connect = on_connect
client.on_message = on_message
time.sleep(1) # Sleep for a beat to ensure things occur in order

# Input username, password, and pub/sub topics in the terminal
user = input('Username: ')
pw = input('Password: ')
pubtop = input('Publish topic: ')
subtop = input('Subscribe topic: ')

# Set user/pass, connect to the broker, start the loop, and subscribe.
# - It's important to do this in order! Subscribing before connecting won't work
client.username_pw_set(user, pw)
client.connect(broker_address, port)
client.loop_start()
client.subscribe(subtop)

# Chat loop
while True:
    chat = input()
    if chat == 'QUIT':
        break
    elif chat == 'SUBSCRIBE':
        new_subtop = input('Subscribe to topic: ')
        client.subscribe(new_subtop)
    elif chat == 'UNSUBSCRIBE':
        unsubtop = input('Unsubscribe from topic: ')
        client.unsubscribe(unsubtop)
    elif chat == 'PUBLISH':
        pubtop = input('Publish to new topic: ')
    else:
        client.publish(pubtop, chat)

# Disconnect and stop the loop!
client.disconnect()
client.loop_stop()
Anduino WiFi Chat DisplayArduino
This Arduino script lets you display the most recent chat message on your AnduinoWiFi shield!
#include <WiFi101.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "AnduinoLCD.h"

// WiFi parameters
#define WLAN_SSID     "<insert your WiFi SSID here>"
#define WLAN_PASS     "<insert your WiFi password here>"

// MQTT parameters
#define MQTT_BROKER   "iot.eclipse.org" // or "<MQTT broker static IP address>"
#define MQTT_PORT     1883
#define MQTT_USER     "<insert your MQTT username here>"
#define MQTT_PASS     "<insert your MQTT password here>"

// Client instances
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_BROKER, MQTT_PORT, MQTT_USER,MQTT_PASS);

// Subscriber feeds for chat - change topics and add subscriptions as needed
const char SUB1[] PROGMEM = "/chat/test1";
const char SUB2[] PROGMEM = "/chat/test2";
Adafruit_MQTT_Subscribe feed_sub1 = Adafruit_MQTT_Subscribe(&mqtt, SUB1);
Adafruit_MQTT_Subscribe feed_sub2 = Adafruit_MQTT_Subscribe(&mqtt, SUB2);

// AnduinoLCD instance
AnduinoLCD LCD = AnduinoLCD(ST7735_CS_PIN, ST7735_DC_PIN, 13);

void setup() {
  Serial.begin(115200);
  delay(3000);

  // Connect to WiFi
  wifiConnect();

  // Initialize LCD
  LCD.begin();
  LCDinit();

  // Subscribe to chat topics
  mqtt.subscribe(&feed_sub1);
  mqtt.subscribe(&feed_sub2);
}

char *msg;
char *topic;

void loop() {
  mqttConnect(); // Connect to or ping the MQTT broker

  // MQTT subscription loop - check for subscription message, find out which
  // topic it's in, then print the message to the screen
  Adafruit_MQTT_Subscribe *subscription;
  // readSubscription(90000) holds loop 90 sec or until a message is received
  while ((subscription = mqtt.readSubscription(90000))) {
    if (subscription == &test1sub) {
      msg = (char*)test1sub.lastread;
      topic = "/chat/test1";
      Serial.print(topic);
      Serial.println(msg);
      LCDmessage(topic, msg);
    }
    if (subscription == &test2sub) {
      msg = (char*)test2sub.lastread;
      topic = "/chat/test2";
      Serial.print(topic);
      Serial.println(msg);
      LCDmessage(topic, msg);
    }
  }
}

// Function to connect to WiFi
void wifiConnect() {
  delay(10);
  Serial.print(F("Connecting to "));
  Serial.println(WLAN_SSID);
  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F("."));
  }
  Serial.println(F("WiFi connected!"));
}

// Function to connect to (or ping) the MQTT broker
void mqttConnect() {
  int8_t rc;

  // If connected, ping the broker to keep connection
  if (mqtt.connected()) {
    mqtt.ping();
    return;
  }

  Serial.println(F("Connecting to MQTT broker..."));

  // Connect to the MQTT broker
  while ((rc = mqtt.connect()) != 0) {
    Serial.println(mqtt.connectErrorString(rc));
    Serial.println(F("Retrying MQTT connection in 2 seconds..."));
    mqtt.disconnect();
    delay(2000);
  }
  Serial.println(F("MQTT connected!"));
}

// Function to initialize Anduino LCD screen
void LCDinit() {
  LCD.setBacklight(ON);
  LCD.splashScreen();
  LCD.fillRect(0, 40, 160, 88, ST7735_BLACK);
}

// Function to clear previous message and display new message
void LCDmessage(char *topic, char *msg) {
  LCD.fillRect(0, 40, 160, 88, ST7735_BLACK);

  // Print topic small
  LCD.setTextColor(ST7735_WHITE);
  LCD.setTextSize(0.5);
  LCD.setTextWrap(true);
  LCD.setCursor(0, 40);
  LCD.println(topic);

  // Print message bigger
  LCD.setTextColor(ST7735_WHITE);
  LCD.setTextSize(2);
  LCD.setTextWrap(true);
  LCD.setCursor(0,50);
  LCD.println(msg);
}
andium/Anduino
An Arduino library for the Andium(Anduino) shield. Transform your Arduino into an AndiumNode.

Credits

Purple square 800 i8q8grig5o
Adam Cellon
0 projects • 1 follower
Building and breaking (mostly breaking) things whenever I can.
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaBlog