Created
October 14, 2015 16:22
-
-
Save AgustinPelaez/42c41633d39b4409a55e to your computer and use it in GitHub Desktop.
Read a value from Ubidots and display it an LED Matrix using a chipKIT Uno32.
This file contains 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 <Wprogram.h> | |
#include <Wire.h> | |
#include <WiFiShieldOrPmodWiFi_G.h> | |
#include <DNETcK.h> | |
#include <DWIFIcK.h> | |
#include <LedControl.h> | |
//------------------------------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<devices;address++) | |
{ | |
//The MAX72XX is in power-saving mode on startup, we have to do a WAKEUP call | |
lc1.shutdown(address,false); | |
// Set the brightness values (0 to 15) | |
lc1.setIntensity(address,MAX72xxbrightnessValue); | |
// and clear the display | |
lc1.clearDisplay(address); | |
} | |
} | |
//********************************************************************************************************************************************************** | |
void loop() | |
{ | |
int i,j,k,second=0; | |
char va[20]; | |
get_value(idvariable); | |
check(); | |
strcpy(va, v.c_str()); | |
for(i=0;i<20;i++) | |
{ | |
if ((va[i] > 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<20;i++) | |
{ | |
if ((va[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<8;j++) // Set up rows on current display | |
{ | |
byte outputbyte = 0; | |
curchar = msg[curcharix]; | |
curcharixsave = curcharix; | |
curcharbitsave = curcharbit; | |
for (k=7;k>=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<<curcharbit)) | |
outputbyte |= (1<<k); | |
// advance the current character bit of current character | |
curcharbit ++; | |
if (curcharbit > 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<8) //First Set of 4 x Bi-color LED Matrix (need 8 x MAX7219) | |
{ | |
if (colorMode == '1') //Red | |
{ | |
lc1.setRow(i-1, j, outputbyte); //Even number devices - Red | |
} | |
else if (colorMode == '2') //Green | |
{ | |
lc1.setRow(i, j, outputbyte); //Odd number devices - Green | |
} | |
else if (colorMode == '3') //Orange | |
{ | |
lc1.setRow(i-1, j, outputbyte); //Even number devices - Red | |
lc1.setRow(i, j, outputbyte); //Odd number devices - Green | |
} | |
} | |
if (j != 7) // if this is not the last row, roll back advancement, if it is, leave the counters advanced. | |
{ | |
curcharix = curcharixsave; | |
curcharbit = curcharbitsave; | |
} | |
} | |
} | |
curcharix = curcharixsave2; | |
curcharbit = curcharbitsave2; | |
// Prepare new text message | |
if (newMsg == true) | |
{ | |
newMsg = false; | |
MAXbrightnessCount = 0; | |
MAX72xxbrightnessValue = 0; | |
if (newCmd == true) | |
{ | |
newCmd = false; | |
if (temp_msg[5]=='*') // To use current text message | |
{ | |
//do Nothing | |
} | |
else | |
{ | |
for (j=0;j<temp_msgsize;j++) // Discard command part from message data | |
temp_msg[j]=temp_msg[j+5]; | |
temp_msgsize = strlen(temp_msg); | |
for (j=0;j<temp_msgsize;j++) | |
msg[j]=temp_msg[j]; | |
msgsize = strlen(temp_msg); | |
} | |
} | |
else | |
{ | |
for (j=0;j<temp_msgsize;j++) | |
msg[j]=temp_msg[j]; | |
msgsize = strlen(temp_msg); | |
} | |
colorMode = temp_colorMode; | |
} | |
curchar = msg[curcharix]; | |
// advance the current character bit of current character | |
curcharbit ++; | |
if (curcharbit > 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<devices;address++) | |
lc1.setIntensity(address,MAX72xxbrightnessValue); | |
} | |
else if (MAX72xxbrightnessValue < SetbrightnessValue) | |
{ | |
MAX72xxbrightnessValue = MAX72xxbrightnessValue+1; | |
if (MAX72xxbrightnessValue > SetbrightnessValue) | |
MAX72xxbrightnessValue = SetbrightnessValue; | |
for(int address=0;address<devices;address++) | |
lc1.setIntensity(address,MAX72xxbrightnessValue); | |
} | |
} | |
} | |
} | |
//********************************************************************************************************************************************************** | |
void maxAll(int cmd) | |
{ | |
byte reg = 0x0c; //max7219_reg_shutdown | |
byte col = 0x01; //shutdown false | |
byte col2 = 0x00; //shutdown true | |
int c = 0; | |
digitalWrite(lc1_Load_Pin, LOW); | |
if (cmd == offREDoffGREEN) | |
{ | |
//for ( c =1; c<= maxInUse; c++) { | |
for ( c =1; c<= 4; c++) { | |
putByte(reg);// specify register | |
putByte(col2);//((data & 0x01) * 256) + data >> 1); // put data | |
putByte(reg);// specify register | |
putByte(col2);//((data & 0x01) * 256) + data >> 1); // put data | |
} | |
} | |
else if (cmd == offREDonGREEN) | |
{ | |
//for ( c =1; c<= maxInUse; c++) { | |
for ( c =1; c<= 4; c++) { | |
putByte(reg);// specify register | |
putByte(col);//((data & 0x01) * 256) + data >> 1); // put data | |
putByte(reg);// specify register | |
putByte(col2);//((data & 0x01) * 256) + data >> 1); // put data | |
} | |
} | |
else if (cmd == onREDoffGREEN) | |
{ | |
//for ( c =1; c<= maxInUse; c++) { | |
for ( c =1; c<= 4; c++) | |
{ | |
putByte(reg);// specify register | |
putByte(col2);//((data & 0x01) * 256) + data >> 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])&&(flag<9)) | |
{ | |
flag++; | |
} | |
else if(((t[i]>47)&&(t[i]<58)||(t[i]=='.')||(t[i]=='}'))&&(flag>8)) | |
{ | |
if(t[i]=='}') | |
{ | |
break; | |
} | |
else | |
{ | |
v+=t[i]; | |
temp_msg[i] = t[i]; | |
msg[i] = t[i]; | |
flag++; | |
} | |
} | |
else | |
{ | |
flag=0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment