#include <OneWire.h>
OneWire ds(9);
int nPinWaterState = 8;
int nPinRelayState = 7;
byte addr[8];
bool bRelay=false;
bool bWater=true;
int nTemp=0;
int nFinishTemp=-1;
void setup()
void setup()
{
Serial.begin(9600);
InitDS18B20();
pinMode(nPinWaterState, INPUT);
pinMode(nPinRelayState, OUTPUT);
digitalWrite(nPinRelayState,HIGH);
sendData("AT+RST\r\n",1000);
sendData("AT+CWSAP=\"Kettle\",\"123\",3,0\r\n",200);
sendData("AT+CWMODE=2\r\n",200);
sendData("AT+CIPAP=\"192.168.0.100\"\r\n",200);
sendData("AT+CWDHCP=0,1\r\n",200);
sendData("AT+CIPMUX=1\r\n",200);
sendData("AT+CIPSERVER=1,8888\r\n",200);
}
void loop()
{
ReadSensors();
if(!bWater || nTemp>=nFinishTemp)
{
bRelay=false;
}
if(Serial.available())
{
if(Serial.find((char*)"+IPD,"))
{
String strRead =Serial.readStringUntil('\n');
int connectionId=strRead[0]-48;
if(Serial.find((char*)"AND:"))
{
strRead=Serial.readStringUntil('\n');
nFinishTemp=strRead.substring(1,4).toInt();
if(strRead[0]=='T')
{
if(bWater && nTemp<nFinishTemp)
{
bRelay=true;
}
}
else
{
bZap=false;
}
}
String strSend = "KET:";
if(bRelay)
{
strSend=strSend+'T';
}
else
{
strSend=strSend+'F';
}
if(bWater)
{
strSend=strSend+'T';
}
else
{
strSend=strSend+'F';
}
char strTemp[3];
sprintf (strTemp, "%03d",nTemp);
strSend=strSend+strTemp;
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=strSend.length();
cipSend +="\r\n";
sendData(cipSend,20);
sendData(strSend,20);
String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId;
closeCommand+="\r\n";
sendData(closeCommand,20);
}
}
if (bRelay)
{
digitalWrite(nPinRelayState,LOW);
}
else
{
digitalWrite(nPinRelayState,HIGH);
}
}
String sendData(String command, const int timeout)
{
String response = "";
Serial.print(command);
long int time = millis();
while( (time+timeout) > millis())
{
while(Serial.available())
{
char c = Serial.read();
response+=c;
}
}
return response;
}
void ReadSensors(void)
{
float tempC;
tempC=ReadDS18B20();
nTemp=(int)tempC;
bWater = !digitalRead(nPinWaterState);
}
void InitDS18B20(void)
{
if ( !ds.search(addr)) {
ds.reset_search();
delay(100);
return;
}
if (OneWire::crc8(addr, 7) != addr[7]) {
return;
}
}
float ReadDS18B20(void)
{
byte i;
byte present = 0;
byte data[12];
float celsius;
ds.reset();
ds.select(addr);
ds.write(0x44, 1);
delay(100);
present = ds.reset();
ds.select(addr);
ds.write(0xBE);
for ( i = 0; i < 9; i++) {
data[i] = ds.read();
}
int16_t raw = (data[1] << 8) | data[0];
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw & ~7;
else if (cfg == 0x20) raw = raw & ~3;
else if (cfg == 0x40) raw = raw & ~1;
celsius = (float)raw / 16.0;
return(celsius);
}
Moderator: igrr