summaryrefslogtreecommitdiff
path: root/datasources/arduino/arduinoAnalogHTTP/analogethernet.ino
blob: 5a0b2413223327b5a57a7a8d8381653a91069840 (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
// vim: filetype=cpp
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {  0x02, 0xEE, 0x00, 0xEF, 0xFE, 0xED };
const int ledPin = 13;      // select the pin for the LED

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

EthernetClient client;

void sendKeyValue(const char *key, const char *value) {
    char hdrbuf[512];
    snprintf(hdrbuf, 512, "Content-Length: %d\r\n", strlen(value));

  if ( ! client.connect("10.1.0.1", 8080)) {
    Serial.println("Failed to connect");
    delay(500);
    return;
  }
  Serial.print("Send ");
  Serial.print(key);
  Serial.print("=");
  Serial.println(value);

  client.write("PUT /api/value/");
  client.write(key);
  client.write(" HTTP/1.1\r\n");
  client.write(hdrbuf);
  client.write("\r\n");
  client.write(value);

  client.stop();
}

void sendPinValue() {
  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);
    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);
    sendKeyValue(sensorKeyMV, buf);
    
    sensorValueDBA = sensorValueMV / OV_FACTOR; 
    sensorKeyDBA[9] = '0' + pinnr;
    snprintf(buf, 12, "%ld.%04ld", sensorValueDBA/1000, sensorValueDBA%1000);
    sendKeyValue(sensorKeyDBA, buf);
  }
}


void setup() {
  Serial.begin(9600);
  Serial.println("Begin init");

  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
  
  // start the Ethernet and UDP: Use DHCP for IP-Configuration
  Ethernet.begin(mac);
  Serial.println("Ethernet init done");
  
  analogReference(EXTERNAL);
  delay(1000);
  Serial.println("Start loop()");
}



char buf[512];

void loop() {
  unsigned long dt = millis();
  digitalWrite(ledPin, HIGH);

  sendPinValue();

  digitalWrite(ledPin, LOW);
  
  dt = (millis() - dt);
  if (dt < 1000) delay(1000 - dt);
}