Created
May 19, 2022 00:22
-
-
Save mike-douglas/96e64274ecb6f8ab4d61d034facf4807 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <WiFi101.h> | |
typedef struct { | |
int input; | |
char name[10]; | |
} sensor; | |
sensor sensors[2] = { | |
{ 0, "soil03" }, | |
{ 1, "soil08" } | |
}; | |
char ssid[] = "XXX"; | |
char pass[] = "XXX"; | |
char host[] = "XXX"; | |
int port = 9800; | |
int status = WL_IDLE_STATUS; | |
WiFiClient client; | |
IPAddress ipAddress; | |
char ip[] = "xxx.xxx.xxx.xxx"; | |
void setup() { | |
Serial.begin(9600); | |
// set the pins for the ATWINC1500 Feather | |
WiFi.setPins(8, 7, 4); | |
pinMode(4, OUTPUT); | |
digitalWrite(4, HIGH); | |
while (status != WL_CONNECTED) { | |
status = WiFi.begin(ssid, pass); | |
delay(5000); | |
} | |
ipAddress = WiFi.localIP(); | |
sprintf(ip, "%d.%d.%d.%d", ipAddress[0], ipAddress[1], ipAddress[2], ipAddress[3]); | |
} | |
void loop() { | |
for (byte i = 0; i < (sizeof(sensors) / sizeof(sensors[0])); i++) { | |
int val = analogRead(sensors[i].input); | |
sendSensorReading(host, port, val, sensors[i].name, ip); | |
Serial.print("sent "); | |
Serial.println(sensors[i].name); | |
} | |
delay(10000); | |
} | |
void sendSensorReading(char* host, int port, int reading, char* sensor, char* readerIP) { | |
client.stop(); | |
char buffer[40], payload[100]; | |
sprintf(payload, "{\"value\":%d,\"labels\":{\"sensor\": \"%s\", \"reader\": \"%s\"}}", reading, sensor, readerIP); | |
Serial.println(payload); | |
if (client.connect(host, port)) { | |
client.println("POST /api/v1/metric/soil_moisture HTTP/1.1"); | |
client.println("User-Agent: arduino-ethernet"); | |
client.println("Content-Type: application/json"); | |
sprintf(buffer, "Host: %s", host); | |
client.println(buffer); | |
sprintf(buffer, "Content-Length: %d", strlen(payload)); | |
client.println(buffer); | |
client.println(); | |
client.println(payload); | |
client.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment