Created
February 17, 2016 14:32
-
-
Save lsilvs/834def427e7b6e3ab840 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"). | |
* You may not use this file except in compliance with the License. | |
* A copy of the License is located at | |
* | |
* http://aws.amazon.com/apache2.0 | |
* | |
* or in the "license" file accompanying this file. This file is distributed | |
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | |
* express or implied. See the License for the specific language governing | |
* permissions and limitations under the License. | |
*/ | |
#include <LiquidCrystal.h> | |
#include <aws_iot_mqtt.h> | |
#include <aws_iot_version.h> | |
#include <ArduinoJson.h> | |
#include "aws_iot_config.h" | |
/* Pins configuration */ | |
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); | |
int inPin = 9; // button | |
int outPin = 13; // led | |
/* Button and led logics */ | |
int state = HIGH; // current state of the output pin | |
int reading; // current reading from the input pin | |
int previous = LOW; // previous reading from the input pin | |
long time = 0; // the last time the output pin was toggled | |
long debounce = 200; // the debounce time, increase if the output flickers | |
aws_iot_mqtt_client myClient; // init iot_mqtt_client | |
int cnt = 0; // loop counts | |
int rc = -100; // return value placeholder | |
bool success_connect = false; // whether it is connected | |
void setup() { | |
// Start Serial for print-out and wait until it's ready | |
Serial.begin(115200); | |
while(!Serial); | |
pinMode(inPin, INPUT); // button pin setup | |
pinMode(outPin, OUTPUT); // led pin setup | |
char curr_version[80]; | |
sprintf(curr_version, "AWS IoT SDK Version(dev) %d.%d.%d-%s\n", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG); | |
Serial.println(AWS_IOT_CLIENT_ID); | |
// Set up the client | |
if((rc = myClient.setup(AWS_IOT_CLIENT_ID)) == 0) { | |
// Load user configuration | |
if((rc = myClient.config(AWS_IOT_MQTT_HOST, AWS_IOT_MQTT_PORT, AWS_IOT_ROOT_CA_PATH, AWS_IOT_PRIVATE_KEY_PATH, AWS_IOT_CERTIFICATE_PATH)) == 0) { | |
// Use default connect: 60 sec for keepalive | |
if((rc = myClient.connect()) == 0) { | |
success_connect = true; | |
Serial.println("success_connect = true"); | |
// set up the LCD's number of columns and rows: | |
lcd.begin(16, 2); | |
lcd.print("hello, world!"); | |
rc = myClient.shadow_init(AWS_IOT_MY_THING_NAME); | |
rc = myClient.shadow_get(AWS_IOT_MY_THING_NAME, callback_get_shadow, 5); | |
rc = myClient.shadow_register_delta_func(AWS_IOT_MY_THING_NAME, msg_callback_delta); | |
} | |
else { | |
Serial.println("Connect failed!"); | |
Serial.println(rc); | |
} | |
} | |
else { | |
Serial.println("Config failed!"); | |
Serial.println(rc); | |
} | |
} | |
else { | |
Serial.println("Setup failed!"); | |
Serial.println(rc); | |
} | |
// Delay to make sure SUBACK is received, delay time could vary according to the server | |
delay(2000); | |
} | |
void loop() { | |
if(success_connect) { | |
reading = digitalRead(inPin); // read the button state | |
// if the input just went from LOW and HIGH and we've waited long enough to ignore | |
// any noise on the circuit, toggle the output pin and remember the time | |
if (reading == HIGH && previous == LOW && millis() - time > debounce) { | |
if (state == LOW) { | |
Serial.println("[PHYSICAL] Led turned on"); | |
state = HIGH; | |
} else { | |
Serial.println("[PHYSICAL] Led turned off"); | |
state = LOW; | |
} | |
// effectively update the light status | |
digitalWrite(outPin, state); | |
// update the light status on AWS IoT | |
if (state == HIGH) { | |
report_state(String("{\"state\":{\"desired\": {\"LED\": null},\"reported\":{\"LED\":\"1\"}}}")); | |
} else { | |
report_state(String("{\"state\":{\"desired\": {\"LED\": null},\"reported\":{\"LED\":\"0\"}}}")); | |
} | |
time = millis(); | |
} | |
previous = reading; | |
// Get a chance to run a callback | |
if((rc = myClient.yield()) != 0) { | |
Serial.println("Yield failed!"); | |
Serial.println(rc); | |
} | |
delay(200); | |
} | |
} | |
void callback_get_shadow(char* src, int len) { | |
char JSON_buf[100]; | |
String data = String(src); | |
int st = data.indexOf("\"reported\":") + strlen("\"reported\":"); | |
int ed = data.indexOf(",\"metadata\":") - 1; | |
String reported = data.substring(st, ed); | |
String payload = "{\"state\":{\"reported\":"; | |
payload += reported; | |
payload += "}}"; | |
int has_status = reported.indexOf("\"LED\":"); | |
int has_display = reported.indexOf("\"Display\":"); | |
StaticJsonBuffer<200> jsonBuffer; | |
JsonObject& root = jsonBuffer.parseObject(reported); | |
if (has_status > -1) { | |
const char* objStatus = root["LED"]; | |
if (String(objStatus) == String("1")) { | |
Serial.println("[VIRTUAL] Led turned on"); | |
state = HIGH; | |
} else { | |
Serial.println("[VIRTUAL] Led turned off"); | |
state = LOW; | |
} | |
digitalWrite(outPin, state); | |
} | |
if (has_display > -1) { | |
const char* objDisplay = root["Display"]; | |
lcd.setCursor(0, 1); | |
lcd.print(String(objDisplay)); | |
} | |
report_state(payload); | |
} | |
void msg_callback_delta(char* src, int len) { | |
char JSON_buf[100]; | |
String data = String(src); | |
int st = data.indexOf("\"state\":") + strlen("\"state\":"); | |
int ed = data.indexOf(",\"metadata\":"); | |
String delta = data.substring(st, ed); | |
String payload = "{\"state\":{\"reported\":"; | |
payload += delta; | |
payload += "}}"; | |
int has_status = payload.indexOf("\"LED\":"); | |
int has_display = payload.indexOf("\"Display\":"); | |
StaticJsonBuffer<200> jsonBuffer; | |
JsonObject& root = jsonBuffer.parseObject(delta); | |
if (has_status > -1) { | |
const char* objStatus = root["LED"]; | |
if (String(objStatus) == String("1")) { | |
Serial.println("[VIRTUAL] Led turned on"); | |
state = HIGH; | |
} else { | |
Serial.println("[VIRTUAL] Led turned off"); | |
state = LOW; | |
} | |
digitalWrite(outPin, state); | |
} | |
if (has_display > -1) { | |
const char* objDisplay = root["Display"]; | |
lcd.setCursor(0, 1); | |
lcd.print(String(objDisplay)); | |
} | |
report_state(payload); | |
} | |
void report_state(String msg) { | |
char JSON_report[100]; | |
msg.toCharArray(JSON_report, 100); | |
myClient.shadow_update(AWS_IOT_MY_THING_NAME, JSON_report, strlen(JSON_report), NULL, 5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment