Code Monkey home page Code Monkey logo

advaita-internet-of-things--2015's People

Contributors

utkg avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

advaita-internet-of-things--2015's Issues

Phase2_IOT_Receiving and toggling_DHCP code

include <SPI.h>

include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
// Initialize the Ethernet client library with the IP address and port of the server that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// this check is only needed on the Leonardo:
Figure 8
while (!Serial) {
;
// wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
void loop() {
}

Phase2_IOT_Receiving ang Toggling_Main Code

include <SPI.h>

include <Ethernet.h>

boolean incoming = 0; //to check wheather the data is coming from the client is valid according to the code
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };// mac address of the arduino
IPAddress ip(192,168,1,34); //ip address of the arduino
// Initialize the Ethernet server library
// (port 80 is default for HTTP we can change this port but at the every connect we will have to specify that port so we are using 80):
EthernetServer server(80);
void setup()
{
pinMode(2, OUTPUT); //configuring the pin mode on the arduino board where we can connect the led to be toggled
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.begin(9600);
}
void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true; //used to differentiate wheather line is blank or usdesired character is there on the url
//loop until the connection is made
while (client.connected()) {
if (client.available()) {
char c = client.read(); //reads URL string from $ to first blank space
if(incoming && c == ' '){ //used to validate the data
incoming = 0;
}
if(c == '$'){
incoming = 1;
}
//Checks for the URL string $1 or $2
if(incoming == 1){
Serial.println(c); //print the value on serial monitor
if(c == '1'){
Serial.println("ON");
digitalWrite(2, HIGH); //used to toggle(switch on) the led
}
if(c == '2'){
Serial.println("OFF");
digitalWrite(2, LOW); //used to toggle(switch off) the led
}
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}

Phase 1_IOT_sending through ethernet shield

include <SPI.h>

include <Ethernet.h>

// Local Network Settings
byte mac[] = {0x00, 0x16, 0x3E, 0x49, 0xCF, 0x3A }; //Paste here the mac address
// GroveStreams Settings
char gsApiKey[] = "a981ad00-b167-39da-8441-4d8e477abc57"; //Paste here the api key
char gsComponentName[] = "Temperature"; //Name of the component to be used.
Figure 15
char gsDomain[] = "grovestreams.com"; //Domain name of the server where all the data is to be uploaded.
char gsComponentTemplateId[] = "temp"; //Tells Grovestreams what template to use when the feed initially arrives and a new component needs to be created.
//GroveStreams Stream IDs.
char gsStreamId1[] = "s1"; //Stream ID tell GroveStreams which component streams the values will be assigned to.
char gsStreamId2[] = "s2"; //In this case it is s1->Temp C and s2->Temp F.
const unsigned long updateFrequency = 20000UL; // Update frequency in milliseconds (20000 = 20 seconds).
const int temperaturePin = 0;
char samples[35];
char myIPAddress[20];
char myMac[20];
unsigned long lastSuccessfulUploadTime = 0;
int failedCounter = 0
// Initialize Arduino Ethernet Client
EthernetClient client; //object of EthernetClient is created as client
void setup()
{ Serial.begin(9600);
startEthernet();
}
void loop()
{
// Update sensor data to GroveStreams
if(millis() - lastSuccessfulUploadTime > updateFrequency)
{
updateGroveStreams();
}
}
void updateGroveStreams()
{ unsigned long connectAttemptTime = millis();
if (client.connect(gsDomain, 80))
{ char urlBuf[175];
sprintf(urlBuf,"PUT/api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%sHTTP/1.1",gsComponentTemplateId,myMac,gsComponentName, gsApiKey, getSamples()); // stores the formatted data as a string
Serial.println(urlBuf);
client.println(urlBuf);
client.print(F("Host: "));
client.println();
client.println(F("Connection: close"));
client.print(F("X-Forwarded-For: "));
client.println(myIPAddress);
client.println(F("Content-Type: application/json"));
client.println();
if (client.connected())
{
//Begin Report Response
while(!client.available())
{
delay(1);
}
while(client.available())
{
char c = client.read();
Serial.print(c);
}
//End Report Response
//Client is now disconnected; stop it to cleannup.
client.stop();
lastSuccessfulUploadTime = connectAttemptTime;
failedCounter = 0;
}
else
{
handleConnectionFailure();
}
}
else
{
handleConnectionFailure();
}
}
void handleConnectionFailure()
{
//Connection failed. Increase failed counter
failedCounter++;
Serial.print(F("Connection to GroveStreams Failed "));
Serial.print(failedCounter);
Serial.println(F(" times"));
delay(1000);
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 )
{
//Too many failures. Restart Ethernet.
startEthernet();
}
}
void startEthernet()
{
//Start or restart the Ethernet connection.
client.stop();
Serial.println(F("Connecting Arduino to network..."));
Serial.println();
//Wait for the connection to finish stopping
delay(2000);
//Connect to the network and obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println(F("DHCP Failed, reset your Arduino and try again"));
Serial.println();
}
else
{
Serial.println(F("Arduino connected to network using DHCP"));
//Set the mac and ip variables so that they can be used during sensor uploads later
Serial.print(F(" MAC: "));
Serial.println(getMacReadable());
Serial.print(F(" IP address: "));
Serial.println(getIpReadable(Ethernet.localIP()));
Serial.println();
}
}
char* getMacReadable()
{
//Convert the mac address to a readable string
sprintf(myMac, "%02x:%02x:%02x:%02x:%02x:%02x\0", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return myMac;
}
char* getIpReadable(IPAddress ipAddress)
{
//Convert the ip address to a readable string
unsigned char octet[4] = {0,0,0,0};
for (int i=0; i<4; i++)
{
octet[i] = ( ipAddress >> (i_8) ) & 0xFF;
}
sprintf(myIPAddress,"%d.%d.%d.%d\0",octet[0],octet[1],octet[2],octet[3]); //Taking the ip address in 4 array storing 8 bit in each according to their significance
return myIPAddress;
}
char_ getSamples()
{
//Get the temperature analog reading and convert it to a string
float voltage, degreesC, degreesF,temp;
voltage = (voltage - 0.5) _100; //used for correction of the voltage
temp =analogRead(temperaturePin);
temp=temp_0.048828125; //correction factor for getting result in desired range
degreesC = temp; //storing that value in variable
degreesF = degreesC * (9.0/5.0) + 32.0; //converting degreeC to degreeF
delay(1000);
char tempC[15] = {0}; //Initialize buffer to nulls
dtostrf(degreesC, 12, 3, tempC); //Convert float to string.
char tempF[15] = {0};
dtostrf(degreesF, 12, 3, tempF); //12 indicate the min width & 3 the precision
//Assemble the samples into URL parameters which are seperated with the "&" character
sprintf(samples, "&%s=%s&%s=%s", gsStreamId1, trim(tempC), gsStreamId2, trim(tempF));
return samples; //return the value of temperature to ethernet shield
}
char* trim(char* input) //used to remove whitespace and other predefined characters from both sides of a string without changing the actual string
{
int i,j;
char *output=input;
for (i = 0, j = 0; i<strlen(input); i++,j++)
{
if (input[i]!=' ')
output[j]=input[i];
else
j--;
}
output[j]=0;
return output;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.