Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
Mohamed Fadiga
Published © GPL3+

NubChat - chat app for devices (and humans) with PubNub

NubChat is an app that let communicate with human and devices using pubnub APIs.

BeginnerFull instructions provided1 hour1,990
NubChat - chat app for devices (and humans) with PubNub

Things used in this project

Hardware components

Android device
Android device
×1

Software apps and online services

PubNub Publish/Subscribe API
PubNub Publish/Subscribe API
Arduino IDE
Arduino IDE

Story

Read more

Code

NubChat.ino

Arduino
#include <ArduinoJson.h>
#include "PubNub.h"
#include "LWiFi.h"
#include "LWiFiClient.h"

#define WIFI_AP "" // your WiFi_AP name
#define WIFI_PASSWORD "" //your WiFi password
#define WIFI_AUTH LWIFI_OPEN //

char pubkey[] = ""; //Pubnub publish key
char subkey[] = ""; //pubnub subscribe key
char channel[] = "LinkitONE"; //a channel for this device

LWiFiClient *client;
DynamicJsonBuffer jsonBuffer;

void setup()
{
  Serial.begin(9600);
  while (!LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))delay(1000); //wait for a WiFi connection
  PubNub.begin(pubkey, subkey); //setup pubnub

  //Subscribing with REST API always gives an emtpy message at the first call.
  //So we call it once in the setup, to throw away this first message.
  subscribe();
}


void loop()
{
  JsonObject& j1 = jsonBuffer.parseArray(subscribe())[0] ; //Subscribe and convert message to a JSON object
  String text = j1["text"]; //get the message content.

  //Create JSON for response
  JsonObject& j2 = jsonBuffer.createObject();
  j2["sender"] = channel;
  j2["type"] = "0";

  if (text.substring(0, 2).equalsIgnoreCase("DW"))
  {
    int pin = text.substring(2).toInt();
    boolean val = text.substring(text.indexOf(" ") + 1).equalsIgnoreCase("HIGH");
    pinMode(pin, OUTPUT);
    digitalWrite(pin, val);
    j2["text"] = "done";
  }
  else if (text.substring(0, 2).equalsIgnoreCase("AW"))
  {
    int pin = text.substring(2).toInt();
    boolean val = text.substring(text.indexOf(" ") + 1).toInt();
    pinMode(pin, OUTPUT);
    analogWrite(pin, val);
    j2["text"] = "done";
  }
  else if (text.substring(0, 2).equalsIgnoreCase("DR"))
  {
    int pin = text.substring(2).toInt();
    pinMode(pin, INPUT);
    j2["text"] = digitalRead(pin) ? "HIGH" : "LOW";
  }
  else if (text.substring(0, 2).equalsIgnoreCase("AR"))
  {
    int pin = text.substring(2).toInt();
    pinMode(pin, INPUT);
    j2["text"] = analogRead(pin);
  }
  else j2["text"] = "Invalid command";
  String message;
  j1.printTo(message);
  publish(j1["sender"], message.c_str()); //Send response
}

void publish(const char *channel, const char *message)
{
  if (!(client = PubNub.publish(channel, message)))
  {
    Serial.println("publishing error");
    delay(1000);
  }
  while (client->connected())
  {
    while (client->connected() && !client->available());
    client->read();
  }
  client->stop();
}

String subscribe()
{
  String response = "";
  PubSubClient *pclient;
  while (!(pclient = PubNub.subscribe(channel)))
  {
    Serial.println("subscription error");
    delay(1000);
  }
  while (pclient->wait_for_data())response += (char)pclient->read();
  pclient->stop();
  Serial.println(response);
  return response;
}

NubChat app for Android

PubNub library for Linkit ONE

ArduinoJson

Credits

Mohamed Fadiga

Mohamed Fadiga

8 projects β€’ 36 followers
Teaher/ Maker / developer / IoT addicted / Sprinter πŸ“šπŸ’‘πŸ€“πŸ’». Passionate about anime, manga and pop-punk music. πŸ₯πŸ“–πŸŽ¬πŸŽΈ

Comments