#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define LCD_I2C_ADDR 0x20
#define BACKLIGHT 7
#define LCD_EN 4
#define LCD_RW 5
#define LCD_RS 6
#define LCD_D4 0
#define LCD_D5 1
#define LCD_D6 2
#define LCD_D7 3
LiquidCrystal_I2C lcd(LCD_I2C_ADDR,LCD_EN,LCD_RW,LCD_RS,LCD_D4,LCD_D5,LCD_D6,LCD_D7);
#include <Wire.h>
#include <Adafruit_BMP085.h>
Adafruit_BMP085 bmp;
#include <IoTkit.h> // include IoTkit.h to use the Intel IoT Kit
#include <Ethernet.h> // must be included to use IoTkit
IoTkit iotkit;
float temperature1;
int pressure1;
int moisturevalue1;
bool led1,relay1;
void setup() {
iotkit.begin();
Serial.begin(9600);
bmp.begin();
lcd.begin (20,4);
lcd.setBacklightPin(BACKLIGHT,NEGATIVE);
lcd.setBacklight(HIGH);
lcd.home ();
lcd.setCursor ( 0, 0 );
lcd.print("Edison. Habrahabr");
pinMode(8, OUTPUT);
digitalWrite(8, !false);
pinMode(9, OUTPUT);
digitalWrite(9, false);
iotkit.send("led1", 0);
iotkit.send("relay1", 0);
}
void loop() {
lcd.setCursor ( 0, 1 );
lcd.print("Tempera. = ");
lcd.print(bmp.readTemperature());
lcd.print(" *C");
lcd.setCursor ( 0, 2 );
lcd.print("Pressure = ");
lcd.print(bmp.readPressure());
lcd.print(" Pa");
lcd.setCursor ( 0, 3 );
lcd.print("Moisture Value = ");
lcd.print(analogRead(0));
temperature1=bmp.readTemperature();
pressure1=bmp.readPressure();
moisturevalue1=analogRead(0);
Serial.println("Sensors");
Serial.print("temperature1=");
Serial.println(temperature1);
iotkit.send("temperature1", temperature1);
delay(2000);
Serial.print("pressure1=");
Serial.println(pressure1);
iotkit.send("pressure1", pressure1);
delay(2000);
Serial.print("moisturevalue1=");
Serial.println(moisturevalue1);
moisturevalue1=20;
iotkit.send("moisturevalue1", moisturevalue1);
iotkit.receive(callback);
delay(1000);
}
void callback(char* json) {
Serial.println(json);
aJsonObject* parsed = aJson.parse(json);
if (&parsed == NULL) {
Serial.println("recieved invalid JSON");
return;
}
aJsonObject* component = aJson.getObjectItem(parsed, "component");
aJsonObject* command = aJson.getObjectItem(parsed, "command");
aJsonObject* argv = aJson.getObjectItem(parsed, "argv");
aJsonObject* argvArray = argv->child;
aJsonObject* name = argvArray->child;
aJsonObject* value = name->next;
if ((component != NULL)) {
if (strcmp(component->valuestring, "led1") == 0) {
if ((command != NULL)) {
if (strcmp(command->valuestring, "LED.v1.0") == 0 && strcmp(value->valuestring, "0") == 0) {
Serial.println("Light Off!");
digitalWrite(8, !false);
iotkit.send("led1", 0);
}
if (strcmp(command->valuestring, "LED.v1.0") == 0 && strcmp(value->valuestring, "1") == 0) {
Serial.println("Light on!");
digitalWrite(8, !true);
iotkit.send("led1", 0);
}
}
}
}
if ((component != NULL)) {
if (strcmp(component->valuestring, "relay1") == 0) {
if ((command != NULL)) {
if (strcmp(command->valuestring, "RELAY.v1.0") == 0 && strcmp(value->valuestring, "0") == 0) {
Serial.println("Relay Off!");
digitalWrite(9, false);
iotkit.send("relay1", 0);
}
if (strcmp(command->valuestring, "RELAY.v1.0") == 0 && strcmp(value->valuestring, "1") == 0) {
Serial.println("Relay on!");
digitalWrite(9, true);
iotkit.send("relay1", 0);
}
}
}
}
}