Сборка типового набора Интернета вещей на Ардуино


Полезный ролик для тех, кто делает приложения Интернета вещей на Ардуино, а также готовиться в соревнованиям WorldSkills/JuniorSkills в данной компетенции по теме "Умное сельское хозяйство"


Пример скетча для Ардуино:

--

#include "DHT.h" #include "pitches.h" #include <Ethernet.h> #include <OneWire.h> #include <DallasTemperature.h>

const String APIKEY = "97f31f8321a8df31ed5efbb4f3e22072d5732d1b5d075f5d3ee85f74115d1716"; const String DEVICE = "cactusDevice@vrxfile.vrxfile";

#define SERVER_UPDATE_TIME 60000 // Update Carriots data server every 60000 ms (1 minute)

#define TIMEOUT 1000 // 1 second timout

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Local IP if DHCP fails IPAddress ip(192, 168, 1, 250); IPAddress dnsServerIP(192, 168, 1, 1); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 255, 0);

IPAddress carriots_server(82, 223, 244, 60);

EthernetClient client;

long timer_main = 0; long timer_carriots = 0;

#define DHTPIN 3 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE);

#define ONE_WIRE_BUS 8 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature ds_sensors(&oneWire);

#define LightSensorPIN A1 #define TemperatureSensorPIN A2 #define MoistureSensorPIN A3

#define LEDPIN 13

float h1 = 0; float t1 = 0; float t2 = 0; float t3 = 0; float light1 = 0; float moisture1 = 0;

#define BUZZERPIN 5

int melody[] = { NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4 }; int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 };

// Main setup void setup() { // Serial port Serial.begin(19200); Serial.println("/* Carriots data client by Rostislav Varzar */\n");

// Init analog PINS pinMode(A1, INPUT); pinMode(A2, INPUT); pinMode(A3, INPUT);

// Ethernet shield if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); Ethernet.begin(mac, ip, dnsServerIP, gateway, subnet); } Serial.print("LocalIP: "); Serial.println(Ethernet.localIP()); Serial.print("SubnetMask: "); Serial.println(Ethernet.subnetMask()); Serial.print("GatewayIP: "); Serial.println(Ethernet.gatewayIP()); Serial.print("dnsServerIP: "); Serial.println(Ethernet.dnsServerIP()); Serial.println("");

// DHT11 dht.begin();

// DS18B20 ds_sensors.begin(); }

// Main loop void loop() { // Main timeout if (millis() > timer_main + SERVER_UPDATE_TIME) { // Read data from sensors // DHT11 h1 = dht.readHumidity(); t1 = dht.readTemperature(); if (isnan(h1) || isnan(t1)) { Serial.println("Failed to read from DHT sensor!"); } // Analog sensors float sens1 = analogRead(LightSensorPIN); float sens2 = analogRead(TemperatureSensorPIN); float sens3 = analogRead(MoistureSensorPIN); t2 = sens2 / 1023.00 * 5 * 100.00 * 1.2; light1 = sens1 / 1023.00 * 100.00; moisture1 = sens3 / 1023.00 * 100.00; // DS18B20 ds_sensors.requestTemperatures(); t3 = ds_sensors.getTempCByIndex(0); // If moisture less than 30, play music if (moisture1 <= 30) { for (int thisNote = 0; thisNote < 8; thisNote++) { int noteDuration = 1000 / noteDurations[thisNote]; tone(BUZZERPIN, melody[thisNote], noteDuration); int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); noTone(BUZZERPIN); } } // Print data from sensors printAllSenors(); // Send data to servser sendCarriotsStream(); // Reset timeout timer timer_main = millis(); } }

// Send IoT packet void sendCarriotsStream() {

if (client.connect(carriots_server, 80)) { if (client.connected()) { Serial.println("Sending data to Carriots server...\n");

// Calculating packet size int thisLength = 0; String json_data = ""; json_data = String("{\"protocol\":\"v2\",\"device\":\""); thisLength = thisLength + json_data.length(); json_data = String(DEVICE); thisLength = thisLength + json_data.length(); json_data = String("\",\"at\":\"now\",\"data\":{"); thisLength = thisLength + json_data.length(); json_data = String("\"temperature1\":"); thisLength = thisLength + json_data.length(); json_data = String("\"" + String(t1, 2) + "\","); thisLength = thisLength + json_data.length(); json_data = String("\"temperature2\":"); thisLength = thisLength + json_data.length(); json_data = String("\"" + String(t2, 2) + "\","); thisLength = thisLength + json_data.length(); json_data = String("\"temperature3\":"); thisLength = thisLength + json_data.length(); json_data = String("\"" + String(t3, 2) + "\","); thisLength = thisLength + json_data.length(); json_data = String("\"humidity1\":"); thisLength = thisLength + json_data.length(); json_data = String("\"" + String(h1, 2) + "\","); thisLength = thisLength + json_data.length(); json_data = String("\"light1\":"); thisLength = thisLength + json_data.length(); json_data = String("\"" + String(light1, 2) + "\","); thisLength = thisLength + json_data.length(); json_data = String("\"moisture1\":"); thisLength = thisLength + json_data.length(); json_data = String("\"" + String(moisture1, 2) + "\"}}"); thisLength = thisLength + json_data.length();

Serial.println("Size of data: " + String(thisLength)); Serial.println();

client.println("POST /streams HTTP/1.1"); client.println("Host: api.carriots.com"); client.println("Accept: application/json"); client.println("User-Agent: Arduino-Carriots"); client.println("Content-Type: application/json"); client.print("carriots.apikey: "); client.println(APIKEY); client.print("Content-Length: "); //thisLength = json_data.length(); client.println(thisLength); client.println("Connection: close"); client.println(); //client.println(json_data);

client.print("{\"protocol\":\"v2\",\"device\":\""); client.print(DEVICE); client.print("\",\"at\":\"now\",\"data\":{"); client.print("\"temperature1\":"); client.print("\"" + String(t1, 2) + "\","); client.print("\"temperature2\":"); client.print("\"" + String(t2, 2) + "\","); client.print("\"temperature3\":"); client.print("\"" + String(t3, 2) + "\","); client.print("\"humidity1\":"); client.print("\"" + String(h1, 2) + "\","); client.print("\"light1\":"); client.print("\"" + String(light1, 2) + "\","); client.print("\"moisture1\":"); client.println("\"" + String(moisture1, 2) + "\"}}");

delay(1000);

timer_carriots = millis(); while ((client.available() == 0) && (millis < timer_carriots + TIMEOUT));

while (client.available() > 0) { char inData = client.read(); Serial.print(inData); } Serial.println("\n");

client.stop(); } } }

// Print sensors data to terminal void printAllSenors() { Serial.print("Temperature1: "); Serial.print(t1); Serial.println(" *C"); Serial.print("Temperature2: "); Serial.print(t2); Serial.println(" *C"); Serial.print("Temperature3: "); Serial.print(t3); Serial.println(" *C"); Serial.print("Humidity1: "); Serial.print(h1); Serial.println(" %"); Serial.print("Light detection : "); Serial.print(light1); Serial.println(" %"); Serial.print("Moisture detection : "); Serial.print(moisture1); Serial.println(" %"); Serial.println(""); }

==================

Содержание файла pitches.h:

--

/************************************************* * Public Constants *************************************************/

#define NOTE_B0 31 #define NOTE_C1 33 #define NOTE_CS1 35 #define NOTE_D1 37 #define NOTE_DS1 39 #define NOTE_E1 41 #define NOTE_F1 44 #define NOTE_FS1 46 #define NOTE_G1 49 #define NOTE_GS1 52 #define NOTE_A1 55 #define NOTE_AS1 58 #define NOTE_B1 62 #define NOTE_C2 65 #define NOTE_CS2 69 #define NOTE_D2 73 #define NOTE_DS2 78 #define NOTE_E2 82 #define NOTE_F2 87 #define NOTE_FS2 93 #define NOTE_G2 98 #define NOTE_GS2 104 #define NOTE_A2 110 #define NOTE_AS2 117 #define NOTE_B2 123 #define NOTE_C3 131 #define NOTE_CS3 139 #define NOTE_D3 147 #define NOTE_DS3 156 #define NOTE_E3 165 #define NOTE_F3 175 #define NOTE_FS3 185 #define NOTE_G3 196 #define NOTE_GS3 208 #define NOTE_A3 220 #define NOTE_AS3 233 #define NOTE_B3 247 #define NOTE_C4 262 #define NOTE_CS4 277 #define NOTE_D4 294 #define NOTE_DS4 311 #define NOTE_E4 330 #define NOTE_F4 349 #define NOTE_FS4 370 #define NOTE_G4 392 #define NOTE_GS4 415 #define NOTE_A4 440 #define NOTE_AS4 466 #define NOTE_B4 494 #define NOTE_C5 523 #define NOTE_CS5 554 #define NOTE_D5 587 #define NOTE_DS5 622 #define NOTE_E5 659 #define NOTE_F5 698 #define NOTE_FS5 740 #define NOTE_G5 784 #define NOTE_GS5 831 #define NOTE_A5 880 #define NOTE_AS5 932 #define NOTE_B5 988 #define NOTE_C6 1047 #define NOTE_CS6 1109 #define NOTE_D6 1175 #define NOTE_DS6 1245 #define NOTE_E6 1319 #define NOTE_F6 1397 #define NOTE_FS6 1480 #define NOTE_G6 1568 #define NOTE_GS6 1661 #define NOTE_A6 1760 #define NOTE_AS6 1865 #define NOTE_B6 1976 #define NOTE_C7 2093 #define NOTE_CS7 2217 #define NOTE_D7 2349 #define NOTE_DS7 2489 #define NOTE_E7 2637 #define NOTE_F7 2794 #define NOTE_FS7 2960 #define NOTE_G7 3136 #define NOTE_GS7 3322 #define NOTE_A7 3520 #define NOTE_AS7 3729 #define NOTE_B7 3951 #define NOTE_C8 4186 #define NOTE_CS8 4435 #define NOTE_D8 4699 #define NOTE_DS8 4978

#Интернетвещей #Arduino

Просмотров: 41