.PS
# ColdControlDPV.m4
#https://www.researchgate.net/journal/Sensors-1424-8220/publication/361484962_Design_of_a_Smart_IoT-Based_Control_System_for_Remotely_Managing_Cold_Storage_Facilities/links/6806de36df0e3f544f442241/Design-of-a-Smart-IoT-Based-Control-System-for-Remotely-Managing-Cold-Storage-Facilities.pdf

log_init
sinclude(ics.m4)
sinclude(../ics.m4)


divert(-1)

define(`ESP8266',`[ Chip: box wid 9*lg_pinsep ht 16*lg_pinsep
  foreach_(`x',`Pin`'eval(m4Lx+15): line from Chip.ne+(0,(m4Lx-16)*lg_pinsep) \
    right pinlen "eval(m4Lx+15)" above; "x" rjust at last line.start',
    3V3,GND,TX(GPIO1),RX(GPIO3),D8(GPIO15),D7(GPIO13),D6(GPIO12),
    D5(GPIO14),GND,3V3,D4(GPIO2),D3(GPIO0),D2(GPIO4),D1(GPIO5),D0(GPIO16))
  foreach_(`x',`Pin`'m4Lx: line from Chip.nw+(0,-m4Lx*lg_pinsep) \
    left pinlen "m4Lx" above; "x" ljust at last line.start',
    AO(ADC0),RSV,RSV,SD3(GPIO10),SD2(GPIO9),SD1(MOSI), CMD(CS),SDO(MISO),
    CLK(SCLK),GND,3V3,EN,RST,GND,VIN) ]')

define(`LEDQ',`[ B:dot; resistor(up_ dimen_,,E); rlabel(,R)
  diode(up_ dimen_/2 from Here+(0,-dimen_/5),L)
  { em_arrows(,135,dimen_/3) with .Tail at last [].nw}; dot
  {ground(,,S,0); "GND" at last line.s below ljust}
 Q: bi_tr(up_ dimen_*5/4,,,E) with .E at Here; "BC547" at last [].e ljust
  line from Q.B left_ dimen_/3
  resistor(down_ to (Here,B),,E); llabel(,R); line to B
 T: Q.C ]')

define(`Relay',`[ C: relaycoil(S,,,ifelse(`$2',,U,D))
 B: box wid elen_ ht C.ht at C ifelse(`$2',,+,-)(elen_/4,0)
  `"Relay"' at B.n above
 ifelse(`$2',,`C1: 1/4 < B.nw, B.ne >; C2: 1/4 < B.sw, B.se >
               S1: 5/6 < B.nw, B.ne >; S2: 5/6 < B.sw, B.se >
 S: dswitch(ifinstr(`$1',R,`down_ from S1 to S2,R',`up_ from S2 to S1,'),WBCD)',
              `C1: 3/4 < B.nw, B.ne >; C2: 3/4 < B.sw, B.se >
               S1: 1/6 < B.nw, B.ne >; S2: 1/6 < B.sw, B.se >
 S: dswitch(ifinstr(`$1',R,`up_ from S2 to S1,R',`down_ from S1 to S2,'),WBCD)')
   "G5Q-1A" at 1/2<C2,S2> below ]')

define(`Fan',`[ C: circle at (0,0) ifelse(`$1',,,`diam `$1'')
 arc from (Rect_(C.rad,-30)) to (Rect_(C.rad,210)) with .c at (0,-C.rad) 
 arc to (Rect_(C.rad,90)) with .c at (Rect_(C.rad,150))
 arc to (Rect_(C.rad,-30)) with .c at (Rect_(C.rad,30)) ]')

define(`pins',`for_(1,`$1',1,
`ifinstr(`$2',L,`L`'m4x:line left_ del \
   from ((m4x*2-1)/(2*(`$1')))<last box.nw, last box.sw>')
 ifinstr(`$2',R,`R`'m4x:line right_ del \
   from ((m4x*2-1)/(2*(`$1')))<last box.ne,last box.se>') ')')

define(`thermometer',`[ eleminit_(`$1')
   tx = rp_len; ty = tx/6
  {box ht_ ty rad ty/2 wid_ rp_len-ty}
  {circle diam 2*ty fill_(0) at rvec_(rp_len-ty,0)}
  {box ht_ last box.ht_ wid_ last box.wid_/4 \
     at rvec_(rp_len-ty*5/2,0) fill_(0)}
  for i=1 to 5 do {{ move to rvec_(ty*i/2,0)
    line from rvec_(0,-ty/2) to Here }} ]')

define(`thsen',`[ B: box ht boxwid*5/6
   pins(4,L)
   "T svg_amp RH" above at B.n;  "DHT22" below rjust at B.se
 for j=1 to 5 do {
   box wid B.wid/10 ht B.wid/15 at B.nw+((j-1/2)*B.wid/5,-(1/2)*B.ht/4)
   for i=2 to 4 do {
     box wid B.wid/10 ht B.wid/12 at B.nw+((j-1/2)*B.wid/5,-(i-1/2)*B.ht/4)
     } } ]')

define(`S8',`[ B: box wid dimen_*3.2 ht dimen_*2
 foreach_(`x',`move to B.nw-(0,(m4Lx-1/2)*B.ht/5)
   ifelse(x,nul,,{"x" ljust}; L`'m4Lx: line left_ del)',
   UART_R/T,UART_TXD,UART_RXD,nul,BCAL_IN)
 foreach_(`x',`move to B.ne-(0,(m4Lx-1/2)*B.ht/5)
   ifelse(x,nul,,{"x" rjust}; R`'m4Lx: line right_ del)',
   ALARMLOC,nul,PWM_1KHZ,nul,DVCC_OUT) ]')

#                           `RC1602A rotated 90 deg'
 define(`XC1602A_R',`[ Chip: box ht 6*lg_pinsep wid 14*lg_pinsep
  foreach_(`xx',`pushdef(`x1',patsubst(xx,:.*)) pushdef(`x2',patsubst(xx,.*:))
    Pin`'x1: line from Chip.ne-(m4Lx*lg_pinsep,0) up_ pinlen
    svg_rot(90,"x1",at last line.c-(textoffset+textht/2,0))
    svg_rot(90,"x2",at last line.start-(0,textht*(len(x2)+1/2)/2))',
    4:RS,5:R/W,6:E,3:Vo) popdef(`x1',`x2')
  line from Pin6.start-(textht/2+2bp__,textoffset) down_ textht*3/4
  line from Pin5.start-(textht/2+2bp__,textoffset) down_ textht
  foreach_(`xx',`Pin`'eval(m4Lx+6): line from Chip.ne-((m4Lx+5)*lg_pinsep,0) \
    up_ pinlen
    svg_rot(90,"eval(m4Lx+6)",at last line.c-(textoffset+textht/2,0))
    svg_rot(90,"xx",at last line.start-(0,textht*(len(xx)+1/2)/2)) ',
    DB0,DB1,DB2,DB3,DB4,DB5,DB6,DB7)
  Pin1: line from Chip.w left_ pinlen
    svg_rot(90,"1",with .s at Pin1)
    svg_rot(90,"VSS",at Chip.w+(textht,0))
  Pin2: line from Chip.e right_ pinlen
    svg_rot(90,"2",at Pin2+(0,textht/2+textoffset))
    svg_rot(90,"VDD",at Chip.e-(textht/2+textoffset,0))
  foreach_(`xx',`pushdef(`x1',patsubst(xx,:.*))
    pushdef(`x2',patsubst(xx,^[^:]*:\|:.*)) pushdef(`x3',patsubst(xx,.*:))
  Pin`'x2: line from Chip.se-(x1*lg_pinsep,0) down_ pinlen
    svg_rot(90,"x2",at last line.c-(textoffset+textht/2,0))
    svg_rot(90,"x3",at last line.start+(0,textht*(len(x3)+1/2)/2))',
    6:16:K,8:A:A1,9:K1:K) popdef(`x1',`x2',`x3')
  Pin15: line from Chip.se-(7*lg_pinsep,0) down_ pinlen
    svg_rot(90,"15",at last line.c-(textoffset+textht/2,0))
    svg_rot(90,"A/VEE",at last line.start+(0,textht*5/2)) ]')

define(`Ammonia',`[ B: box; "MQ-3" at B.se rjust below
  line left_ del then up_ del from 3/4<B.sw,B.nw>; "5 V" above
  "VCC" at last line.start ljust
  line left_ del from 1/4<B.sw,B.nw>
  {"GND" at last line.start ljust}
  ground(,,S); "GND" at last line.s ljust
 C: circle diam B.ht*7/8 at B.e-(B.ht/2,0)
  circle diam B.ht*5/8 at C
  circle diam B.ht*3/8 at C
  circle diam B.ht/4 at C
 Out: 1/6<B.se,B.ne>; "out" at Out below ljust
  "C`'svg_sub(2)H`'svg_sub(4)" at B.n+(0,2bp__) above ]')

divert(0)dnl
svg_font(sans-serif,8.5bp__)
#vg_rot_init(test)
svg_rot_init(ColdControlDPV)

 define(`pinlen',`20bp__')
 define(`lg_pinsep',(1.2*lg_pinsep))
 del = lg_pinsep

 ESP: ESP8266; `"ESP8266"' at ESP.Chip.se below rjust
  line from ESP.Pin24.end right del then down ESP.Pin24.y-ESP.Pin16.y
  ground(,,S); "GND" at last line.s below
  dot(at ESP.Pin17.end+(del,0)); line to ESP.Pin17.end
  "3.3 V" at ESP.Pin25.end ljust
 LEQ1: LEDQ with .B at ESP.Chip.nw+(dimen_+del,dimen_/2)
  line from LEQ1.B to (ESP.Pin30.end,LEQ1.B) then to ESP.Pin30.end 
 LEQ2: LEDQ with .B at ESP.Chip.ne+(del*3/2,dimen_/2+del)
  line from LEQ2.B to (ESP.Pin29.end.x+del,LEQ1.B.y+del)
    continue to (Here,ESP.Pin29) then to ESP.Pin29.end
  line from LEQ1.T up 2*del then right_ ESP.Pin30.end.x-LEQ1.T.x +6*del \
    then down_ del; right_
 R1: Relay(R) with .C1 at Here
 R2: Relay with .B.n at R1.B.s+(0,-dimen_)
  line from R1.S.end to R2.S.end; dot(at last line.c); line right_ del
  "220 V" ljust
  line from R1.C.V1 to R2.C.V2; dot(at last line.c); line left_ del
  "5 V" rjust
  line from LEQ2.T right_ R2.C2.x-LEQ2.T.x-3*del
  continue to (Here.x,R2.C2.y-del) then right_ R2.C2.x-Here.x then to R2.C2
  line from R1.S1 up del then right_ 2*del

 Hum: [F: Fan; box wid F.wid ht F.ht at F; pins(3,LR)] with .L1.end at Here
  "Hum. unit" at Hum.s below
  line from R2.S2 down del then right_ 2*del

 Con: [B: box; pins(3,LR)
       circle diam B.ht/3 with .n at 1/4 < B.nw, B.ne >
       circle diam B.ht/3 with .n at 3/4 < B.nw, B.ne >
       circle diam B.ht/3 with .s at 3/4 < B.sw, B.se >
       circle diam B.ht/3 with .s at 1/4 < B.sw, B.se > ] \
     with .L3.end at Here
    "Control unit" at Con.s below

   line from ESP.Pin23.end to (R2.w.x+4*del,ESP.Pin23.y)
 T1: box wid boxht with .w at Here; "T1" at T1.e ljust "DS1822" ljust
   "out" at T1.w ljust
 T2: box wid boxht with .w at (T1.w,ESP.Pin28)
   "T2" at T2.e ljust "DS1822" ljust; "out" at T2.w ljust
   line from ESP.Pin26.end right del then up T2.w.y-ESP.Pin26.y then to T2.w
   line from T2.s to T1.n; dot(at last line.c); ground(,,S,0)
    "GND" at Here+(dimen_*5/6,0)
   line from 1/4 <T2.nw,T2.sw> left del; "5 V" above
   line from 1/4 <T1.nw,T1.sw> left del; "5 V" above

   thermometer(up_ T2.ht*3/4) at 3/4<T2.w,T2.e>
   thermometer(down_ T1.ht*3/4) at 3/4<T1.w,T1.e>

 M1: thsen with .L2.start at (T1.w.x,ESP.Pin21.y-2*del)
   ground(at M1.L4.end,,S); "GND" at last line.s below
   "5 V" above at M1.L1.end
   line from M1.L2.end left del
   continue to (Here,ESP.Pin22) then to ESP.Pin22.end
 M2: thsen with .n at M1.s+(0,-2*del)
   ground(at M2.L4.end,,S); "GND" at last line.s below
   "5 V" rjust at M2.L1.end
   line from M2.L2.end left 2*del
   continue to (Here,ESP.Pin21) then to ESP.Pin21.end

 CO2: S8 with .B.nw at (M2.L1.end.x-2*del,M2.s.y-dimen_)
   "G0" at CO2.s above; "G+" at CO2.n below
  `"Senseair S8"' at CO2.B.se rjust below
   line up del from CO2.n; "5 V" above
   line down del from CO2.s; ground(,T,S); "GND" at last line.s below
   line from CO2.R1.end up del
 Spkr:speaker(U,dimen_*2/3) with .In1 at Here
   "CO`'svg_sub(2)" at Spkr.n+(0,10bp__) "Alarm"
   line down del*3/2 from Spkr.In3; ground(,T,S)
   "GND" at last line.s below
   line from CO2.L1.end to (CO2.L1.end,ESP.Pin20) then to ESP.Pin20.end
   line from CO2.L2.end left del
   continue to (Here,ESP.Pin19) then to ESP.Pin19.end
   line from CO2.L3.end left 2*del
   continue to (Here,ESP.Pin18) then to ESP.Pin18.end

#   svg_rot(90,"X",at (ESP.Pin7.end.x+4*del,ESP.s.y))
 LCD: XC1602A_R with .Pin14.start \
   at (ESP.Pin7.end.x-4*del,ESP.s.y-dimen_-del*3/2)
   {"LCD" at 1/2<LCD.Chip.sw,LCD.PinK1.start> below
   `"RC1602A-GHW-ESX"' at LCD.Chip.se+(10bp__,0) rjust below}
 RV: potentiometer(right_ dimen_) with .T1 at LCD.Pin15.end
   {"RV" at RV.R.c+(0,-dimen_/12) below
    "1" at RV.R.c+(dimen_/4,0) above ljust} 
   line from RV.End to (LCD.Pin2.end,RV.End); dot 
   line from LCD.Pin2.end to Here chop 0 chop -del; "5 V" ljust
   line from RV.Start to (LCD.Pin1.end,RV.Start); dot 
   line from LCD.Pin1.end to Here; ground(,,S); "GND" at last line.s below

   line from ESP.Pin27.end right_ 2*del;continue to (Here.x,LCD.Pin12.end.y+del)
   continue to (LCD.Pin12,Here) then to LCD.Pin12.end
   line from ESP.Pin11.end left del then down ESP.Pin11.y-ESP.s.y; "3.3 V" ljust
   line from ESP.Pin15.end left del; dot
   line from ESP.Pin10.end left 2*del then down ESP.Pin10.y-ESP.s.y
   ground(,T,S); "GND" at last line.s below
   line from ESP.Pin14.end left 2*del; dot
   line from ESP.Pin9.end to (LCD.Pin13,ESP.Pin9) then to LCD.Pin13.end
   line from ESP.Pin7.end to (LCD.Pin14,ESP.Pin7) then to LCD.Pin14.end

 LEQ3: LEDQ with .B at (ESP.Pin1.end.x-del,LEQ1.B.y)
   line from LEQ3.B to (LEQ3.B,ESP.Pin4) then to ESP.Pin4.end
 R3: Relay(,R) with .se at LEQ3.Q.B-(2*del,0)
   line from R3.C1 up del; continue to (LEQ3.T,Here) then to LEQ3.T
   line from R3.S2 down_ del; "220 V" below
   line from R3.C2 down_ del; "5 V" below
   line from R3.S1 up_ del then left_ R3.S1.x-R3.w.x+del then down R3.ht+3*del
 F1: Fan with .n at Here
   ground(at F1.s,,S); "GND" at last line.s below
   "Ventilation" at F1.e ljust "unit" ljust

   line from ESP.Pin5.end left_ 7*del
 A1: Ammonia with .Out at Here
 A2: Ammonia with .Out at A1.Out-(0,dimen_*2)
   line from A2.Out right_ del; continue to (Here,ESP.Pin6) then to ESP.Pin6.end

   line from ESP.Pin8.end left_ 5*del; continue to (Here,ESP.s) \
     then left_ del
 LightS: [ B: box ht boxwid; "BH1750" at B.se rjust below
   circle diam B.ht/5 at B.nw+(B.ht/5,-B.ht/5)
   circle diam B.ht/5 at B.sw+(B.ht/5, B.ht/5)
  foreach_(`x',
   `Pin`'m4Lx: line right_ pinlen from B.se+(0,(m4Lx-1/2)*B.ht/5) "m4Lx" above
   "x" at last line.start rjust',ADDR,SDA,SCL,GND,VCC)
  ] with .Pin2.end at Here
   {"Light" at LightS.B.n above}
   line from LightS.Pin3.end to (LCD.Pin13,LightS.Pin3); dot
   line from LightS.Pin5.end up del; "5 V" above
   line from LightS.Pin4.end down LightS.Pin4.y-LightS.s.y
   {dot(at (Here,LightS.Pin1))}
   ground(,,S); "GND" at last line.s below

 command "</g>"
.PE
