product-iots/modules/distribution/src/sketches/arduino/Connect.ino
2015-06-09 20:21:48 +05:30

84 lines
2.0 KiB
C++

byte mac[6] = { 0x90, 0xA2, 0xDA, 0x0D, 0x30, 0xD7}; //mac - 90a2da0d30d7
byte dns2[] = { 8, 8, 8, 8 };
byte subnet[] = { 255, 255, 255, 0 };
byte gateway[] = { 192, 168, 1, 1 };
byte deviceIP[4] = { 192, 168, 1, 219 };
byte server[4] = { 192, 168, 1, 216 };
String connecting = "connecting.... ";
void connectHttp() {
if(DEBUG) Serial.println("-------------------------------");
Ethernet.begin(mac, deviceIP, dns2, gateway, subnet);
delay(2000);
if(DEBUG) {
Serial.print("My IP: ");
Serial.println(Ethernet.localIP());
}
connecting += httpClient.connect(server, SERVICE_PORT);
delay(2000);
if(DEBUG) Serial.println(connecting);
if (httpClient.connected()) {
if(DEBUG) Serial.println("connected");
} else {
if(DEBUG) Serial.println("connection failed");
while(!httpClient.connected()){
if(DEBUG) Serial.println("retrying to connect......");
httpClient.connect(server, SERVICE_PORT);
delay(2000);
}
if(DEBUG) Serial.println("connected to server!");
}
if(DEBUG) Serial.println("-------------------------------");
}
void setupResource(){
String hostIP = getHostIP(server);
String port = String(SERVICE_PORT);
host = "Host: " + hostIP + ":" + port;
if(DEBUG) Serial.println(host);
jsonPayLoad = String(OWNER_JSON);
jsonPayLoad += String(DEVICE_OWNER);
jsonPayLoad += String(DEVICE_ID_JSON);
jsonPayLoad += String(DEVICE_ID);
jsonPayLoad += String(REPLY_JSON);
if(DEBUG) {
Serial.print("JSON Payload: ");
Serial.println(jsonPayLoad);
Serial.println("-------------------------------");
}
}
String getMyIP(){
String myIP = "";
myIP = String(Ethernet.localIP()[0]);
for ( int index = 1; index < 4; index++) {
myIP += "." + String(Ethernet.localIP()[index]);
}
return myIP;
}
String getHostIP(byte server[4]){
String hostIP = String(server[0]);
for ( int index = 1; index < 4; index++) {
hostIP += "." + String(server[index]);
}
return hostIP;
}