// ARDUINO HOME II by D-H-S // EXAMPLE SKETCH FOR ARDUINO MEGA WITH ETHERNET SHIELD AND DS18B20 SENSOR ON PIN A6. // The Sketch may be changed and shared. // ------------------------------------------------------------------------------------------------ //*************************** DS18B20 ****************************** #include OneWire ds(A6); //Data pin of the DS18B20 sensor on pin A6 float temperature; unsigned long ds18B20_interval = 2000; //check ds18B20 every 2 seconds unsigned long ds18B20_millis = millis();//without using a timer //****************************************************************** #include #include #include #define MAX_PIN 53 //MEGA // IP AND MAC ADRESS #define PORT 7777 //-> Type this under "TCP-Port" in the App byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0xC1, 0x9C}; //If you're using more then one Arduino, set different Mac adresses for each board! IPAddress ip(192, 168,1, 168); String inputstream = ""; String answer = ""; String text = ""; String command = ""; String serialInput = ""; int pin = 0; unsigned long value = 0; unsigned long ticks[MAX_PIN]; //TICKS FOR EACH OUTPUT. USED IF AN OFFTIMER FOR AN OUTPUT HAS BEEN SET. 1 TICK = 100ms unsigned long ticks_at_command; //IF WE GET NO COMMAND FOR XX SECONDS, THEN STOP CLIENT. char charBuf[250]; EthernetServer server = (PORT); boolean verbunden = false; EthernetClient client; boolean pwm[MAX_PIN]; //Variables for the PWM-Values. The PWM-Values are always between 0 and 255, no matter, what Min/Max-Values you have set in the App. //NEVER CHANGE!!! The Position of the slider depends on that value. //(You can't read pwm values directly from a pin.) int pwm_value[MAX_PIN]; //________________________________________________________________________________________________________________________________________________ void setup() { /* Arduino communicates with the shield using the SPI bus. This is on digital pins 11, 12, and 13 on the Uno and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used as SS. On the Mega, the hardware SS pin, 53, is not used to select the W5500, but it must be kept as an output or the SPI interface won't work. */ //SET ALL USEABLE DIGITAL PINS AS OUTPUT for (int i = 0;i 100) inputstream = ""; while (inputstream.indexOf(';')>0){ inputstream.trim(); //ALL COMMANDS COMING FROM THE APP ARE CONSTRUCTED AS FOLLOWS: // 4 CHARS COMMAND (SETD = Set digital Pin ,GETD = Get digital Pin, OFFT = Set Offtimer,SETP = Set PWM, GETP = Get PWM) // x CHARS PIN (0,1,2,3...51,52,53) // 1 POINT . // x CHARS VALUE (0 = Off, 1 = On, 0-255 = PWM Value) // 1 SEMICOLON ; //FOR EXAMPLE: //SETD1.1; Set digital Pin 1 on //SETD3.0; Set digital Pin 3 off //SETP15.255; Set PWM for Pin 15 to 255 //OFFT17.200; Set offtimer for pin 17 to 200ticks (20 seconds) //GETA5; Get value of analogue Pin 5 command = inputstream.substring(0,4); value = 0; pin = -1; value = inputstream.substring(inputstream.lastIndexOf('.')+1).toInt(); pin = inputstream.substring(4,inputstream.lastIndexOf('.')).toInt(); //UNKNOWN COMMAND? if ((command != "SETD" && command != "GETD" && command != "SETP" && command != "GETA" && command != "OFFT" && command != "GETP") || pin == -1 ){ inputstream = ""; command = ""; value = 0; pin = 0; } //GET STATE OF DIGITAL OUTPUT (GETDXX). ANSWER: "DXX.0;" OR "DXX.1;" if (command == "GETD"){ answer += "D" + String(pin) + "." + digitalRead(pin) + ";"; } //GET STATE OF ANALOG INPUT (GETAXX). ANSWER: "AXX.YYY;" YYY = 0 to 1023 else if (command == "GETA"){ if (pin == 6){//The pin you have set in the app for the ds18b20 temperature. //It can be the pin where the sensor is connected to, but it doesn't has to be! answer += "A" + String(pin) + "." + String(temperature,2) + ";"; //2 decimal places } else{ answer += "A" + String(pin) + "." + analogRead(pin) + ";"; } } //SET DIGITAL OUTPUT ON (SETDXX.1) OR OFF (SETDXX.0). ANSWER: "DXX.0;" OR "DXX.1;" else if (command == "SETD"){ ticks[pin] = 0; digitalWrite(pin,value); answer += "D" + String(pin) + "." + digitalRead(pin)+";"; } //SET OFFTIMER FOR A DIGITAL OUTPUT (OFFTXX.SS) <- XX = PIN, SS = 1/10Sec (1 SECOND = 10), ANSWER: "DXX.1;" else if (command == "OFFT"){ digitalWrite(pin,1); ticks[pin] = millis() + (value*100); answer += "D" + String(pin) + ".1;";//digitalRead(pin); } //SET PWM VALUE (SETPXX.YYY) else if (command == "SETP"){ analogWrite(pin,value); pwm_value[pin] = value; answer += "P" + String(pin) + "." + String(pwm_value[pin], DEC) + ";"; } //GET PWM VALUE (GETPXX). ANSWER: "PXX.YYY;" else if (command == "GETP"){ answer += "P" + String(pin) + "." + String(pwm_value[pin], DEC) + ";"; } inputstream = inputstream.substring(inputstream.indexOf(';')+1); }loop;//<-(While inputstream Contains ';') //clear Input Buffer client.flush(); }//<-End if(client.available()) }//<-End if(client.connected) //NOT CONNECTED. RESTART else { client.stop(); if ((unsigned long)(millis()- ticks_at_command) > 60000) { ticks_at_command = millis(); answer = ""; } } //CHECK FOR OFFTIMERS for (int i = 0; i< MAX_PIN; i++){ if ((millis() >= ticks[i]) && (ticks[i] > 0)){ ticks[i] = 0; digitalWrite(i,0); text = String(i); answer +="D" + text + ".0;"; } } //SEND ANSWER TO PHONE if (answer != ""){ answer.toCharArray(charBuf, answer.length()+1); server.write(charBuf,answer.length()); //Writes to all Clients connected answer = ""; } //CHECK DS18B20 every xx milliseconds if ((unsigned long)(millis()- ds18B20_millis) > ds18B20_interval){ ds18B20_millis = millis(); temperature = getTemp(); } }//<-MAIN LOOP //_____________________________________________________________________________________________________________________________________ float getTemp(){ //returns the temperature from DS18S20 in DEG Celsius byte data[12]; byte addr[8]; if ( !ds.search(addr)) { //no more sensors on chain, reset search ds.reset_search(); return -1000; } if ( OneWire::crc8( addr, 7) != addr[7]) { // Serial.println("CRC is not valid!"); return -1000; } if ( addr[0] != 0x10 && addr[0] != 0x28) { // Serial.print("Device is not recognized"); return -1000; } ds.reset(); ds.select(addr); ds.write(0x44,1); // start conversion, with parasite power on at the end byte present = ds.reset(); ds.select(addr); ds.write(0xBE); // Read Scratchpad for (int i = 0; i < 9; i++) { // we need 9 bytes data[i] = ds.read(); } ds.reset_search(); byte MSB = data[1]; byte LSB = data[0]; float tempRead = ((MSB << 8) | LSB); //using two's compliment float TemperatureSum = tempRead / 16; return TemperatureSum; } //___________________________________________________________________________________________________________________________________