Advertisement
Scouris

16-relay code - updated.

Jan 24th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.46 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <PubSubClient.h>
  4.  
  5. #define datapin 2
  6. #define clockpin 3
  7. #define latchpin 4
  8. int current = 0;        // 00000000
  9. const int relayValues[]={
  10.   1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
  11. int relayCheck[] = {
  12.   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  13. int changeVal = 0;
  14. //char OnOrOff[] = {"off","on"};
  15. //char DataResult[100]="Publishing data - relay "; //24
  16. String OnOrOff;
  17. String DataMash;
  18. String DataResult;
  19. byte mac[] = {  
  20.   0xDE, 0xC0, 0xDE, 0xC0, 0xFF, 0xEE };
  21. byte mqttserver[] = {
  22.   192, 168, 2, 7 };
  23. byte ip[] = {
  24.   192, 168, 2, 101 };
  25.  
  26. //char DataMash[30]="                             ";
  27.  
  28. EthernetClient ethClient;
  29. PubSubClient clientr(mqttserver, 1883, callback);
  30.  
  31. /////////////////// calculateRelayVal ///////////////////
  32. int calculateRelayVal(int newVal) {
  33.  
  34.   // if newVal==0 then return value as 11111111
  35.   // newVal has relay we need to toggle
  36.   // referenced in relayCheck as newVal-1
  37.   int intRelayToWorkWith = relayValues[(newVal-1)];
  38.  
  39.   if(relayCheck[(newVal-1)]==0) {
  40.     // relay is currently off, turn on
  41.     changeVal = current | intRelayToWorkWith;
  42.     relayCheck[(newVal-1)]=1;
  43.   }
  44.   else {
  45.     // relay is currently on, turn off
  46.     changeVal = current ^ intRelayToWorkWith;
  47.     relayCheck[(newVal-1)]=0;
  48.   }
  49.  
  50.   current = changeVal;
  51.   return current;
  52. }
  53.  
  54. /////////////////// doWork ///////////////////
  55. void doWork(int val) {
  56.  
  57.   // as relays need a '0' to turn on and a '1' to turn off, need to NOT this value
  58.   unsigned int realVal = ~val;
  59.  
  60.   uint8_t rVlow = realVal & 0xff;
  61.   uint8_t rVhigh = (realVal >> 8);
  62.  
  63.   digitalWrite(latchpin,LOW);
  64.   shiftOut(datapin,clockpin,MSBFIRST,rVhigh);
  65.   shiftOut(datapin,clockpin,MSBFIRST,rVlow);
  66.   digitalWrite(latchpin,HIGH);
  67.  
  68. }
  69.  
  70. /////////////////// callback ///////////////////
  71. static void callback(char* topic, byte* payload, unsigned int length) {
  72.   char PublishData[24];
  73.   char tina[4];
  74.   String tp = (char*)payload;
  75.   (tp.substring(0,length)).toCharArray(tina,4);
  76.   unsigned int readVar = (unsigned int)atoi(tina);
  77.  
  78.   if(readVar<0 || (readVar>16 && readVar!=999)) {
  79.     // if input is outside of specification (greater than 16 and not 999 (status check) or less than zero then make -1
  80.     // -1 is used in the switch below to indicate invalid input
  81.     readVar=-1;
  82.   }
  83.   if(readVar==999){
  84.     String strGetStatus="S:";
  85.     for(int i=0;i<16;i++){
  86.       strGetStatus+=relayCheck[i];
  87.     }
  88.     strGetStatus.toCharArray(PublishData,100);
  89.     //    clientr.publish("RelayControlReport",PublishData);
  90.     clientr.publish("WebControl",PublishData);
  91.   }
  92.   else {
  93.     if(readVar>=0 && readVar<=16) {
  94.       int registerValue=0;
  95.       if(readVar > 0) {
  96.         registerValue = calculateRelayVal(readVar);
  97.       }
  98.       else {
  99.         current=0;
  100.         for(int i=0;i<16;i++){
  101.           relayCheck[i]=0;
  102.         }
  103.       }
  104.       doWork(registerValue);
  105.  
  106.       if(relayCheck[(readVar-1)]==1){
  107.         OnOrOff=" on";
  108.       }
  109.       else {
  110.         OnOrOff=" off";
  111.       }
  112.  
  113.       if(readVar!=0){
  114.         DataMash="Relay: ";
  115.         DataResult = DataMash + readVar + OnOrOff;
  116.         DataResult.toCharArray(PublishData,100);
  117.       }
  118.       else {
  119.         DataResult="All relays off";
  120.         DataResult.toCharArray(PublishData,100);
  121.       }
  122.       clientr.publish("RelayControlReport",PublishData);
  123.     }
  124.   }
  125. }
  126.  
  127. /////////////////// setup ///////////////////
  128. void setup()
  129. {
  130.   pinMode(datapin,OUTPUT);
  131.   pinMode(clockpin,OUTPUT);
  132.   pinMode(latchpin,OUTPUT);
  133.  
  134.   digitalWrite(latchpin,LOW);
  135.   shiftOut(datapin,clockpin,MSBFIRST,-1);
  136.   shiftOut(datapin,clockpin,MSBFIRST,-1);
  137.   digitalWrite(latchpin,HIGH);
  138.  
  139.   delay(500);
  140.   Serial.begin(9600);
  141.   Serial.println(F("Scouris - MQTT Control, booting up."));
  142.   Serial.print(F("Eth start .."));
  143.   Ethernet.begin(mac,ip);
  144.  
  145.   // below is for when running with DHCP
  146.   Serial.print(F("up, IP: "));
  147.   for (byte thisByte = 0; thisByte < 4; thisByte++) {
  148.     // print the value of each byte of the IP address:
  149.     Serial.print(Ethernet.localIP()[thisByte], DEC);
  150.     Serial.print(F("."));
  151.   }
  152.   Serial.println("");
  153.  
  154.   if(clientr.connect("arduinoClient")) {
  155.     clientr.publish("checkInTopic","Relay Controller Online");
  156.     clientr.publish("RelayControlReport","Relay Controller Online");
  157.     clientr.subscribe("RelayControl");
  158.   }
  159.   doWork(0);
  160.   //  delay(500);
  161. }
  162.  
  163. /////////////////// loop ///////////////////
  164. void loop()
  165. {
  166.   clientr.loop();
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement