summaryrefslogtreecommitdiff
path: root/arduinoAnalogStomp/analogethernet.ino
blob: feefbd812695724beb43c872f88bb37cfec4929c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {  0xAD, 0xEE, 0x00, 0xEF, 0xFE, 0xED };
const char *serverHost = "stomp.xapek.org";
const int serverPort = 61613;
const char *serverLogin = "arduino1";
const char *serverPasscode = "arduino1";
const char *serverQueue = "/queue/queue-put";

const int ledPin = 13;      // select the pin for the LED

struct StompHeader {
  const char *name;
  const char *value;
};

class StompClient; // Forward

struct StompFrame {
  const char *command;
  struct StompHeader *headers = 0;
  int headersPos = -1;
  const char *body = 0;
  void (*bodyFunc)(class StompClient&) = NULL;
  void addHeader(const char *name, const char *value) {
    headersPos++;
    headers = (StompHeader*)realloc(headers, sizeof(StompHeader) * (headersPos+1));
    memset(headers + headersPos, 0, sizeof(StompHeader));
    (headers+(sizeof(StompHeader)*headersPos))->name = name;
    (headers+(sizeof(StompHeader)*headersPos))->value = value;
  }
  void setBody(const char *b) {
    body = strdup(b);
  }
  StompFrame(const char *command) : command(strdup(command)) {};
  ~StompFrame() {
    if (headers != NULL) 
      delete headers;
    delete command;
    if (body)
      delete body;
  }
};

class StompClient : public EthernetClient {
  const char *serverHost;
  int serverPort;
  const char *serverLogin = NULL;
  const char *serverPasscode = NULL;
public:
  StompClient(const char *serverHost, int serverPort, const char *serverLogin, const char *serverPasscode) :
      serverHost(serverHost), serverPort(serverPort), serverLogin(serverLogin), serverPasscode(serverPasscode) {};
  int connect() {
    int result = EthernetClient::connect(serverHost, serverPort);
    if (result) {
      StompFrame frame("CONNECT");
      frame.addHeader("host", serverHost);
      if (serverLogin) frame.addHeader("login", serverLogin);
      if (serverPasscode) frame.addHeader("passcode", serverPasscode);
      
      send(frame);
      
      StompFrame *answer = readFrame();
      if (strcmp(answer->command, "CONNECTED") != 0) {
        Serial.print("Got no CONNECTED Frame, read: ");
        Serial.println(answer->command);
        stop();
        delete answer;
        return false;
      } else {
        delete answer;
        return true;
      }
    } else {
      return result;
    }
  };
  void send(StompFrame &frame) {
    EthernetClient::print(frame.command);
    EthernetClient::print("\n");
    for (int i = 0; i<=frame.headersPos; i++) {
      StompHeader *header = frame.headers+sizeof(StompHeader)*i;
      EthernetClient::print(header->name);
      EthernetClient::print(":");
      EthernetClient::print(header->value);
      EthernetClient::print("\n");
    }
    EthernetClient::print("\n");
    if (frame.bodyFunc) {
      frame.bodyFunc(*this);
    } if (frame.body) {
      EthernetClient::print(frame.body);
    }
    EthernetClient::write((byte)0);
  };

  void sendKeyValue(const char *key, const char *value) {
      EthernetClient::print(key);
      EthernetClient::print("=");
      EthernetClient::print(value);
      EthernetClient::print("\n");
  }
  StompFrame *readFrame() {
    char buf[255];
    int i = readBytesUntil('\n', buf, 254);
    buf[i] = '\0';
    StompFrame *frame = new StompFrame(buf);
    
    while (readBytesUntil('\n', buf, 255)) {
      //read header
      //ignore 
    }
    readBytesUntil('\0', buf, 255);
    frame->setBody(buf);
    
    flush();
    return frame;
  }
};





const long ADC_REF_MV = 3300;
const long ADC_MAX_STEP = 1024;
const long OV_FACTOR = 4; // Op. Verstärker Faktor = 4; 10mV = 1dB

void sendPinValue(class StompClient& c) {
  long sensorValue,
      sensorValueMV, 
      sensorValueDBA,
      pinnr;
  char buf[12];
  char *sensorKey = "arduino.aX", 
       *sensorKeyMV = "arduino.aX.mv",
       *sensorKeyDBA = "arduino.aX.dba";
  for (int i = A0; i <= A5; i++) {
    pinnr = i - A0;
    
    sensorValue = analogRead(i);
    sensorKey[9] = '0' + pinnr;
    snprintf(buf, 8, "%u", sensorValue);
    c.sendKeyValue(sensorKey, buf);
    
    sensorValueMV = sensorValue * (ADC_REF_MV*100/ADC_MAX_STEP);
    sensorKeyMV[9] = '0' + pinnr;
    snprintf(buf, 8, "%ld.%03ld", sensorValueMV/100, sensorValue%100);
    c.sendKeyValue(sensorKeyMV, buf);
    
    sensorValueDBA = sensorValueMV / OV_FACTOR; 
    sensorKeyDBA[9] = '0' + pinnr;
    snprintf(buf, 12, "%ld.%04ld", sensorValueDBA/1000, sensorValueDBA%1000);
    c.sendKeyValue(sensorKeyDBA, buf);
  }
}


void setup() {
  Serial.begin(9600);

  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
  
  // start the Ethernet and UDP: Use DHCP for IP-Configuration
  Ethernet.begin(mac);
  
  analogReference(EXTERNAL);
}



StompClient client(serverHost, serverPort, serverLogin, serverPasscode);

void loop() {
  unsigned long dt = millis();
  
  digitalWrite(ledPin, HIGH);
  if (client.connected()) {
    Serial.println("Send packet");
    StompFrame frame("SEND");
    frame.addHeader("destination", serverQueue);
    frame.bodyFunc = &sendPinValue;
    client.send(frame);
  } else {
    Serial.println("connecting");
    client.stop();
    if (client.connect()) {
      Serial.println("connected");
    } else {
      Serial.println("connection failed");
    }
  }
  digitalWrite(ledPin, LOW);
  
  dt = (millis() - dt);

  if (dt < 500) {
    Serial.println(String("sleep: ") + String((500-dt)));
    delay(500 - dt);
  }
}