Skip to content

Instantly share code, notes, and snippets.

@banasrini
Last active April 3, 2018 20:29
Show Gist options
  • Save banasrini/0fcfa02301f4e11c0f50 to your computer and use it in GitHub Desktop.
Save banasrini/0fcfa02301f4e11c0f50 to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include <Ethernet.h>
#include "string.h"
#include "iotconnector.h"
// Some Ethernet shields have a MAC address printed on a sticker on the shield;
// fill in that address here, or choose your own at random:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char pubkey[] = "demo";
char subkey[] = "demo";
char channel[] = "iotchannel";
char uuid[] = "Arduino";
iotbridge arduino;
void initialize(){
Serial.begin(9600);
Serial.println("Serial set up");
while (!Ethernet.begin(mac)) {
Serial.println("Ethernet setup error");
delay(1000);
}
Serial.println("Ethernet set up");
}
void do_something(String value){
Serial.println("in the callback");
Serial.println(value);
}
void setup()
{
initialize();
arduino.init( pubkey, subkey, uuid);
Serial.println("PubNub set up");
}
void loop()
{
String returnmessage;
Ethernet.maintain();
//Publish
Serial.println("publishing a message");
arduino.send(channel,"\"Hey There\"");
//Subscribe
Serial.println("waiting for a message");
returnmessage = arduino.connect(channel);
// callback function of sorts, to work with the received message
do_something(returnmessage);
Serial.println();
}
#include "PubNub.h"
#include <SPI.h>
#include <Ethernet.h>
class iotbridge{
public:
const char *publish_key;
const char *subscribe_key;
const char *uuid;
const char *channel;
const char *message;
bool init(const char *publish_key, const char *subscribe_key, const char *uuid);
bool send(const char *channel, const char *message);
String connect(const char *channel);
};
bool iotbridge::init(const char *publish_key, const char *subscribe_key, const char *uuid){
PubNub.begin(publish_key,subscribe_key);
PubNub.set_uuid(uuid);
}
bool iotbridge::send(const char *channel, const char *message){
EthernetClient *client;
client = PubNub.publish(channel,message);
return client;
}
String iotbridge::connect(const char *channel){
String message = "";
int i = 0;
PubSubClient *pclient = PubNub.subscribe(channel);
if (!pclient) {
return 0;
}
while (pclient->wait_for_data()) {
char c = pclient->read();
message += c;
}
pclient->stop();
Serial.println();
return message;
}
@Yop1403
Copy link

Yop1403 commented Oct 23, 2015

It should be mentioned that the 1st line in "iotconnector.h" wants to include "pubnub.h".
It can be found here: https://github.com/pubnub/arduino

@hafizmaster93
Copy link

I try to compile my code on Arduino IDE by using this library, however I got this error:-

Arduino: 1.6.5 (Windows 8.1), Board: "Arduino Nano, ATmega328"

In file included from testiot.ino:4:0:
In file included from testiot.ino:4:0:
C:\Users\Asus\Documents\Arduino\libraries\iotconnector/iotconnector.h: In member function 'String iotbridge::connect(const char_)':
C:\Users\Asus\Documents\Arduino\libraries\iotconnector/iotconnector.h: In member function 'String iotbridge::connect(const char_)':
C:\Users\Asus\Documents\Arduino\libraries\iotconnector/iotconnector.h:39:10: error: converting to 'String' from initializer list would use explicit constructor 'String::String(int, unsigned char)'
C:\Users\Asus\Documents\Arduino\libraries\iotconnector/iotconnector.h:39:10: error: converting to 'String' from initializer list would use explicit constructor 'String::String(int, unsigned char)'
return 0;
return 0;
^
^
Error compiling.
Error compiling.

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

can anyone give me some suggestion to fix this issue? thanks

@andriyudatama
Copy link

I have the same problem with @hafizmaster93, can somebody help us?

@millesm
Copy link

millesm commented Aug 29, 2016

same problem....

In file included from C:\Arduino\pub_nub_test\sketch_aug29a\sketch_aug29a.ino:4:0:

C:\Arduino\libraries\PubNub/iotconnector.h: In member function 'String iotbridge::connect(const char*)':

C:\Arduino\libraries\PubNub/iotconnector.h:39:10: error: converting to 'String' from initializer list would use explicit constructor 'String::String(int, unsigned char)'

return 0;

      ^

exit status 1

Error compiling.

no idea what it actually means but cannot compile code.

@mskoczen
Copy link

mskoczen commented Apr 3, 2018

You are attempting to return an integer in a function with a return type of String.
It should compile if you change line 39 of iotconnector.h to return "";

NOTE: I have not looked at the rest of the code to verify that this will not cause other problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment