This project shows how to read a Ubidots variable using an HTTP GET request and the chipKIT Uno board.
It serves as an example on how data can be read from an embedded system using the short polling technique. There are other methods and protocols to talk to a remote device which can help you achieve real time push notifications; long polling, MQTT or CoAP. For more information about these methods, check out our article "Choosing the Right Protocol to Send Data to Remote Devices".
To get started you'll only need five wires. Please keep in mind that an external power source will be needed if handling more than two (2) LED Matrix:
This is a modification of the LED Matrix code provided by JolliFactory. Please remember to put the corresponding TOKEN and Variable ID of your Ubidots account.
Also, refer to our chipKIT WiFi Shield example to download the required networking libraries so the code can compile.
#include
#include
#include
#include
#include
#include
//------------------------------End Libraries----------------------------------
#define GREEN 0
#define RED 1
#define offREDoffGREEN 0
#define offREDonGREEN 1
#define onREDoffGREEN 2
int MAXbrightnessCount = 0;
int maxInShutdown=RED; // indicates which LED Matrix color is currently off
int colorMode = '2'; // default color (1 = RED, 2 = GREEN, 3 = ORANGE, 4 = blank off)
int lastcolorMode = '0';
int temp_colorMode = '2';
int brightnessLevel = '9'; // default brightness level (1 to 9)
int SetbrightnessValue = (15 * (brightnessLevel-48))/9; //Re-adjust values to meet MAX7219 Brightness Level range between 0 to 15
int MAX72xxbrightnessValue = 0;
int scrollDelay = 30; // default scroll delay
boolean newMsg = false;
boolean newCmd = false;
boolean fadeInFlag = true;
// You may change the pin assignments according to your requirements below
// First set of 4 Bi-color LED Matrices
int devices;
int lc1_DataIn_Pin = 7;
int lc1_CLK_Pin = 6;
int lc1_Load_Pin = 5;
int lc1_LEDMatrixQty = 4; // No. of Bi-color LED Matrices daisy-chained
//Create a new LedControl for first set of 4 Bi-color LED Matrices with 2 MAX7219 chips each
LedControl lc1=LedControl(lc1_DataIn_Pin, lc1_CLK_Pin, lc1_Load_Pin, lc1_LEDMatrixQty * 2);
char msg[1024];
int msgsize = strlen(msg);
char temp_msg[255];
int temp_msgsize = strlen(temp_msg);
// The character set courtesy of cosmicvoid.
byte Font8x5[104*8] =
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x02, 0x00,
0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0A, 0x0A, 0x1F, 0x0A, 0x1F, 0x0A, 0x0A, 0x00,
0x0E, 0x15, 0x05, 0x0E, 0x14, 0x15, 0x0E, 0x00,
0x13, 0x13, 0x08, 0x04, 0x02, 0x19, 0x19, 0x00,
0x06, 0x09, 0x05, 0x02, 0x15, 0x09, 0x16, 0x00,
0x02, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x04, 0x02, 0x01, 0x01, 0x01, 0x02, 0x04, 0x00,
0x01, 0x02, 0x04, 0x04, 0x04, 0x02, 0x01, 0x00,
0x00, 0x0A, 0x15, 0x0E, 0x15, 0x0A, 0x00, 0x00,
0x00, 0x04, 0x04, 0x1F, 0x04, 0x04, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x01,
0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
0x10, 0x10, 0x08, 0x04, 0x02, 0x01, 0x01, 0x00,
0x0E, 0x11, 0x19, 0x15, 0x13, 0x11, 0x0E, 0x00,
0x04, 0x06, 0x04, 0x04, 0x04, 0x04, 0x0E, 0x00,
0x0E, 0x11, 0x10, 0x0C, 0x02, 0x01, 0x1F, 0x00,
0x0E, 0x11, 0x10, 0x0C, 0x10, 0x11, 0x0E, 0x00,
0x08, 0x0C, 0x0A, 0x09, 0x1F, 0x08, 0x08, 0x00,
0x1F, 0x01, 0x01, 0x0E, 0x10, 0x11, 0x0E, 0x00,
0x0C, 0x02, 0x01, 0x0F, 0x11, 0x11, 0x0E, 0x00,
0x1F, 0x10, 0x08, 0x04, 0x02, 0x02, 0x02, 0x00,
0x0E, 0x11, 0x11, 0x0E, 0x11, 0x11, 0x0E, 0x00,
0x0E, 0x11, 0x11, 0x1E, 0x10, 0x08, 0x06, 0x00,
0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x02, 0x01,
0x08, 0x04, 0x02, 0x01, 0x02, 0x04, 0x08, 0x00,
0x00, 0x00, 0x0F, 0x00, 0x0F, 0x00, 0x00, 0x00,
0x01, 0x02, 0x04, 0x08, 0x04, 0x02, 0x01, 0x00,
0x0E, 0x11, 0x10, 0x08, 0x04, 0x00, 0x04, 0x00,
0x0E, 0x11, 0x1D, 0x15, 0x0D, 0x01, 0x1E, 0x00,
0x04, 0x0A, 0x11, 0x11, 0x1F, 0x11, 0x11, 0x00,
0x0F, 0x11, 0x11, 0x0F, 0x11, 0x11, 0x0F, 0x00,
0x0E, 0x11, 0x01, 0x01, 0x01, 0x11, 0x0E, 0x00,
0x07, 0x09, 0x11, 0x11, 0x11, 0x09, 0x07, 0x00,
0x1F, 0x01, 0x01, 0x0F, 0x01, 0x01, 0x1F, 0x00,
0x1F, 0x01, 0x01, 0x0F, 0x01, 0x01, 0x01, 0x00,
0x0E, 0x11, 0x01, 0x0D, 0x11, 0x19, 0x16, 0x00,
0x11, 0x11, 0x11, 0x1F, 0x11, 0x11, 0x11, 0x00,
0x07, 0x02, 0x02, 0x02, 0x02, 0x02, 0x07, 0x00,
0x1C, 0x08, 0x08, 0x08, 0x08, 0x09, 0x06, 0x00,
0x11, 0x09, 0x05, 0x03, 0x05, 0x09, 0x11, 0x00,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x0F, 0x00,
0x11, 0x1B, 0x15, 0x15, 0x11, 0x11, 0x11, 0x00,
0x11, 0x13, 0x13, 0x15, 0x19, 0x19, 0x11, 0x00,
0x0E, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0E, 0x00,
0x0F, 0x11, 0x11, 0x0F, 0x01, 0x01, 0x01, 0x00,
0x0E, 0x11, 0x11, 0x11, 0x15, 0x09, 0x16, 0x00,
0x0F, 0x11, 0x11, 0x0F, 0x05, 0x09, 0x11, 0x00,
0x0E, 0x11, 0x01, 0x0E, 0x10, 0x11, 0x0E, 0x00,
0x1F, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0E, 0x00,
0x11, 0x11, 0x11, 0x11, 0x0A, 0x0A, 0x04, 0x00,
0x41, 0x41, 0x41, 0x49, 0x2A, 0x2A, 0x14, 0x00,
0x11, 0x11, 0x0A, 0x04, 0x0A, 0x11, 0x11, 0x00,
0x11, 0x11, 0x11, 0x0A, 0x04, 0x04, 0x04, 0x00,
0x1F, 0x10, 0x08, 0x04, 0x02, 0x01, 0x1F, 0x00,
0x07, 0x01, 0x01, 0x01, 0x01, 0x01, 0x07, 0x00,
0x01, 0x01, 0x02, 0x04, 0x08, 0x10, 0x10, 0x00,
0x07, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07, 0x00,
0x00, 0x04, 0x0A, 0x11, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x00,
0x01, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x06, 0x08, 0x0E, 0x09, 0x0E, 0x00,
0x01, 0x01, 0x0D, 0x13, 0x11, 0x13, 0x0D, 0x00,
0x00, 0x00, 0x06, 0x09, 0x01, 0x09, 0x06, 0x00,
0x10, 0x10, 0x16, 0x19, 0x11, 0x19, 0x16, 0x00,
0x00, 0x00, 0x06, 0x09, 0x07, 0x01, 0x0E, 0x00,
0x04, 0x0A, 0x02, 0x07, 0x02, 0x02, 0x02, 0x00,
0x00, 0x00, 0x06, 0x09, 0x09, 0x06, 0x08, 0x07,
0x01, 0x01, 0x0D, 0x13, 0x11, 0x11, 0x11, 0x00,
0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00,
0x04, 0x00, 0x06, 0x04, 0x04, 0x04, 0x04, 0x03,
0x01, 0x01, 0x09, 0x05, 0x03, 0x05, 0x09, 0x00,
0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00,
0x00, 0x00, 0x15, 0x2B, 0x29, 0x29, 0x29, 0x00,
0x00, 0x00, 0x0D, 0x13, 0x11, 0x11, 0x11, 0x00,
0x00, 0x00, 0x06, 0x09, 0x09, 0x09, 0x06, 0x00,
0x00, 0x00, 0x0D, 0x13, 0x13, 0x0D, 0x01, 0x01,
0x00, 0x00, 0x16, 0x19, 0x19, 0x16, 0x10, 0x10,
0x00, 0x00, 0x0D, 0x13, 0x01, 0x01, 0x01, 0x00,
0x00, 0x00, 0x0E, 0x01, 0x06, 0x08, 0x07, 0x00,
0x00, 0x02, 0x07, 0x02, 0x02, 0x02, 0x04, 0x00,
0x00, 0x00, 0x11, 0x11, 0x11, 0x19, 0x16, 0x00,
0x00, 0x00, 0x11, 0x11, 0x11, 0x0A, 0x04, 0x00,
0x00, 0x00, 0x11, 0x11, 0x15, 0x15, 0x0A, 0x00,
0x00, 0x00, 0x11, 0x0A, 0x04, 0x0A, 0x11, 0x00,
0x00, 0x00, 0x09, 0x09, 0x09, 0x0E, 0x08, 0x06,
0x00, 0x00, 0x0F, 0x08, 0x06, 0x01, 0x0F, 0x00,
0x04, 0x02, 0x02, 0x01, 0x02, 0x02, 0x04, 0x00,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00,
0x01, 0x02, 0x02, 0x04, 0x02, 0x02, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x1C, 0x2A, 0x49, 0x49, 0x41, 0x22, 0x1C, 0x00,
0x1C, 0x22, 0x51, 0x49, 0x41, 0x22, 0x1C, 0x00,
0x1C, 0x22, 0x41, 0x79, 0x41, 0x22, 0x1C, 0x00,
0x1C, 0x22, 0x41, 0x49, 0x51, 0x22, 0x1C, 0x00,
0x1C, 0x22, 0x41, 0x49, 0x49, 0x2A, 0x1C, 0x00,
0x1C, 0x22, 0x41, 0x49, 0x45, 0x22, 0x1C, 0x00,
0x1C, 0x22, 0x41, 0x4F, 0x41, 0x22, 0x1C, 0x00,
0x1C, 0x22, 0x45, 0x49, 0x41, 0x22, 0x1C, 0x00};
byte lentbl_S[104] =
{
2, 2, 3, 5, 5, 5, 5, 2,
3, 3, 5, 5, 2, 5, 1, 5,
5, 4, 5, 5, 5, 5, 5, 5,
5, 5, 1, 2, 4, 4, 4, 5,
5, 5, 5, 5, 5, 5, 5, 5,
5, 3, 5, 5, 4, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 7,
5, 5, 5, 3, 5, 3, 5, 5,
2, 4, 5, 4, 5, 4, 4, 4,
5, 2, 3, 4, 2, 6, 5, 4,
5, 5, 5, 4, 3, 5, 5, 5,
5, 4, 4, 3, 2, 3, 0, 0,
7, 7, 7, 7, 7, 7, 7, 7
};
int curcharix = 0;
int curcharbit = 0;
int curcharixsave = 0;
int curcharbitsave = 0;
int curcharixsave2 = 0;
int curcharbitsave2 = 0;
char curchar;
//---------------------------End initialization Matrix-------------------------------------
char * szIPServer = "things.ubidots.com"; // server to connect to
unsigned short portServer = 80; // port server
const char * szSsid = "Atom House Medellin"; // SSID
const char * szPassPhrase = "atommed2014"; // passphrase
#define WiFiConnectMacro() DWIFIcK::connect(szSsid, szPassPhrase, &status)
String token="pSZJt9W7v5W3fpUsRYNKiDMgJ770NK"; // your token
String idvariable="53e262127625421973e7cdb5"; // your variable ID
unsigned tStart = 0;
unsigned tWait = 5000;
byte rgbRead[1024];
int cbRead = 0;
char t[1024];
String v = " PEOPLE "; // concatenated phrase
typedef enum // States of the state machine
{
NONE = 0,
CONNECT,
TCPCONNECT,
WRITE,
READ,
CLOSE,
DONE,
} STATE;
STATE state = CONNECT;
TcpClient tcpClient;
//--------------------------
//**********************************************************************************************************************************************************
void setup()
{
Serial.begin (9600);
//we have already set the number of devices when we created the LedControl
devices=lc1.getDeviceCount();
//we have to init all devices in a loop
for(int address=0;address 32) or (va[i] < 122))
{
msg[i] = va[i];
}
else
{
break;
}
}
msgsize = strlen(msg);
while(1)
{
second++;
if (second>1000)
{
get_value(idvariable);
check();
strcpy(va, v.c_str());
for(i=0;i 32) or (va[i] < 122))
{
msg[i] = va[i];
}
else
{
break;
}
}
msgsize = strlen(msg);
second=0;
}
//**********************************************************************************************************************************************************
//Start of Serial available Check
//Change message via serial comm
//**********************************************************************************************************************************************************
//End of Serial available Check
curcharixsave2 = curcharix;
curcharbitsave2 = curcharbit;
for (i=7;i>=0;i=i-2) // Loop through our 4 Bi-color LED Matrices
{
//If ISR not used - just have the following instructions below to alternate shutdown of MAX7219 chips for RED and GREEN LEDs to prevent 'ghosting' display
altShutDown();
for (j=0;j=0;k--) // Copy over data for 8 columns to current row and send it to current display - scroll from right to left
{
byte currentcharbits;
// This byte is the bitmap of the current character for the current row
currentcharbits = Font8x5[((curchar-32)*8)+j];
if (currentcharbits & (1< lentbl_S[curchar-32]) // we are past the end of this character, so advance.
{
curcharbit = 0;
curcharix += 1;
if (curcharix+1 > msgsize) curcharix=0;
curchar = msg[curcharix];
}
}
if (i lentbl_S[curchar-32]) // we are past the end of this character, so advance.
{
curcharbit = 0;
curcharix += 1;
if (curcharix+1 > msgsize) curcharix=0;
{
curchar = msg[curcharix];
}
}
if (colorMode == '3') //ORANGE Display
delay(scrollDelay/10); //Scrolling in ORANGE is much slower than in RED and GREEN due to alternate shutdown of MAX 7219 chips required
else
delay(scrollDelay);
// Fade-in effect for new text message
if (fadeInFlag == true)
{
if (MAXbrightnessCount < 6)
{
MAXbrightnessCount = MAXbrightnessCount+1;
for(int address=0;address SetbrightnessValue)
MAX72xxbrightnessValue = SetbrightnessValue;
for(int address=0;address> 1); // put data
putByte(reg);// specify register
putByte(col2);//((data & 0x01) * 256) + data >> 1); // put data
}
}
else if (cmd == offREDonGREEN)
{
//for ( c =1; c> 1); // put data
putByte(reg);// specify register
putByte(col2);//((data & 0x01) * 256) + data >> 1); // put data
}
}
else if (cmd == onREDoffGREEN)
{
//for ( c =1; c> 1); // put data
putByte(reg);// specify register
putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
}
}
digitalWrite(lc1_Load_Pin, LOW);
digitalWrite(lc1_Load_Pin,HIGH);
}
//**********************************************************************************************************************************************************
void putByte(byte data_1)
{
byte i = 8;
byte mask;
while(i > 0)
{
mask = 0x01 << (i - 1); // get bitmask
digitalWrite(lc1_CLK_Pin, LOW); // tick
if (data_1 & mask) // choose bit
{
digitalWrite(lc1_DataIn_Pin, HIGH);// send 1
}
else
{
digitalWrite(lc1_DataIn_Pin, LOW); // send 0
}
digitalWrite(lc1_CLK_Pin, HIGH); // tock
--i; // move to lesser bit
}
}
//**********************************************************************************************************************************************************
void altShutDown() //Simple but not ideal method for alternate shutdown of MAX7219 chips for RED and GREEN LEDs if ISR Timer method is not employed
{
if (colorMode == '3') //Scrolling in ORANGE
{
lastcolorMode = '3';
if(maxInShutdown==RED){
maxAll(onREDoffGREEN);
maxInShutdown=GREEN;
}
else
{
maxAll(offREDonGREEN);
maxInShutdown=RED;
}
}
else if ((colorMode == '2') and (lastcolorMode != '2')) //Scrolling in GREEN
{
lastcolorMode = '2';
maxAll(offREDonGREEN);
maxInShutdown=RED;
}
else if ((colorMode == '1') and (lastcolorMode != '1')) //Scrolling in RED
{
lastcolorMode = '1';
maxAll(onREDoffGREEN);
maxInShutdown=GREEN;
}
else if (colorMode == '4') //Blank Display
{
lastcolorMode = '4';
maxAll(offREDoffGREEN);
maxInShutdown=GREEN;
}
}
boolean get_value(String idvariable)//Send value to Ubidots
{
String message = "GET /api/v1.6/variables/"+idvariable+" HTTP/1.1\nX-Auth-Token: "+token+"\nHost: things.ubidots.com\n\n";
byte rgbWriteStream[message.length()] ;
message.getBytes(rgbWriteStream,message.length() + 1);
int cbWriteStream = sizeof(rgbWriteStream);
state_machine(rgbWriteStream,cbWriteStream);
return true;
}
boolean state_machine(byte rgbWriteStream[], int cbWriteStream)//Handle the TCP connection
{
int conID = DWIFIcK::INVALID_CONNECTION_ID;
int rap = 1;
DNETcK::STATUS status;
while(rap>0)
{
switch(state)
{
case CONNECT:
if((conID = WiFiConnectMacro()) != DWIFIcK::INVALID_CONNECTION_ID)
{
Serial.print("Connection Created, ConID = ");
Serial.println(conID, DEC);
state = TCPCONNECT;
}
else
{
Serial.print("Unable to connection, status: ");
Serial.println(status, DEC);
state = CLOSE;
}
break;
case TCPCONNECT:
Serial.print("H1");
DNETcK::begin();
Serial.print("H2");
tcpClient.connect(szIPServer, portServer);
Serial.print("H3");
state = WRITE;
break;
case WRITE:
Serial.print("H4");
if(tcpClient.isConnected())
{
Serial.print("H5");
//Serial.println("Got Connection");
tcpClient.writeStream(rgbWriteStream, cbWriteStream);
//Serial.println("Bytes Read Back:");
state = READ;
tStart = (unsigned) millis();
}
break;
case READ:
// see if we got anything to read
if((cbRead = tcpClient.available()) > 0)
{
cbRead = cbRead < sizeof(rgbRead) ? cbRead : sizeof(rgbRead);
cbRead = tcpClient.readStream(rgbRead, cbRead);
for(int i=0; i < cbRead; i++)
{
t[i] = (char)rgbRead[i];
//Serial.print(t[i]);
}
}
else if( (((unsigned) millis()) - tStart) > tWait )
{
Serial.println("");
state = CLOSE;
}
// give us some time to get everything echo'ed back
break;
case CLOSE:
state = DONE;
break;
case DONE:
state = CONNECT;
tcpClient.close();
tcpClient.discardReadBuffer();
DNETcK::end();
DWIFIcK::disconnect(status);
rap = 0;
break;
default:
break;
}
// keep the stack alive each pass through the loop()
}
}
void check()
{
char TOKEN[9]={'"','v','a','l','u','e','"',':',' '};
int flag=0;
v = " PEOPLE ";
for(int i=0; i < 1000; i++)
{
if((TOKEN[flag]==t[i])&&(flag47)&&(t[i]8))
{
if(t[i]=='}')
{
break;
}
else
{
v+=t[i];
temp_msg[i] = t[i];
msg[i] = t[i];
flag++;
}
}
else
{
flag=0;
}
}
}
Comments