First lets get started with nodemcu and arduino to send a temperature data to the thingspeak account. Lets begin with Installing the Esp8266 support for the Arduino and see how to blink an LED ( the hello world in the electronics ).
Firstly open the Arduino IDE and go to files and click on the preference in the Arduino IDE.
copy the below code in the Additional boards Manager
http://arduino.esp8266.com/stable/package_esp8266com_index.json
click OK to close the preference Tab.
After completing the above steps , go to Tools and board, and then select board Manager.
Navigate to esp8266 by esp8266 community and install the software for Arduino. Once all the above process been completed we are read to program our esp8266 with Arduino IDE.
below is the basic leb blink program.
Note: the pinouts is not the same as that of arduino. refer the pinouts in internet.
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Now lets start programing it for uploading the data ti thingspeak.
at first create an account in thingspeak and create a channel.
go to api keys section and copy the write api key.
now lets start programing nodemcu. below is the code for uploading temp data to thingspeak using arduino.
#include <ESP8266WiFi.h>
const char* server = "api.thingspeak.com";
String apiKey ="your API key here";
const char* MY_SSID = "your SSID here";
const char* MY_PWD = "your SSID password here";
int sent = 0;
void setup() {
Serial.begin(115200);
connectWifi();
}
void loop() {
float temp;
//char buffer[10];
temp = analogRead(A0);
//String tempC = dtostrf(temp, 4, 1, buffer);//handled in sendTemp()
Serial.print(String(sent)+" Temperature: ");
Serial.println(temp);
//if (temp != prevTemp)
//{
//sendTeperatureTS(temp);
//prevTemp = temp;
//}
sendTeperatureTS(temp);
int count = myPeriodic;
while(count--)
delay(1000);
}
void connectWifi()
{
Serial.print("Connecting to "+*MY_SSID);
WiFi.begin(MY_SSID, MY_PWD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected");
Serial.println("");
}//end connect
void sendTeperatureTS(float temp)
{
WiFiClient client;
if (client.connect(server, 80)) { // use ip 184.106.153.149 or api.thingspeak.com
Serial.println("WiFi Client connected ");
String postStr = apiKey;
postStr += "&field1=";
postStr += String(temp);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
delay(1000);
}//end if
sent++;
client.stop();
}//end send
change the api key with your account write api key, ssid with your wifi ssid and password with your wifi password. after editing the program upload your program into nodemcu.
connect the temperature sensor lm35 to your nodemcu. connect the middle pin to A0, vcc of lm35 to vin of nodemcu (for getting 5v out) and ground to ground.
now you can visualise the temperature data in the thingspeak channel.
lets start with alexa skill kit now. log in into the developer account first.
in the alexa section click alexa skill kit and start a new skill by clicking the add new skill section.
You are now in the Skill Information area. For “Skill Type” the radio button for “Custom Interaction Model” should be selected
Add the name and cool invocation name (what users will say to launch your skill)
click next and in the next section fill in the intents field and utterances field as below.
now download the fact skill template from the link
https://s3.amazonaws.com/alexatutorials/Fact/tutorial_assets/FactSkillTemplate.zip
we can edit the program for reading the data from thingspeak.
provide a name of your own in the configure section and upload the zip file that u hav downloaded .
Select “index.handler” from the Handler dropdown. Select “Create a custom role” to open a new tab in the IAM Management Console, then:
- Accept the defaults to create a new IAM Role called “lambda_basic_execution”
- Select “Allow” in the lower right corner and you will be returned to your Lambda function
- Keep the default Advanced settings. Select “Next” and review
leave the other section as default and click the create function.
now you got the ARN. copy paste the ARN in the previous tab of developer console and click next.
now in the lambda function click the code section and edit the program as follows.
'use strict';
var Alexa = require('alexa-sdk');
var request = require('aws-sdk/lib/request');
var url = 'https://api.thingspeak.com/channels/161182/fields/1.json?results=2';
var APP_ID = 'amzn1.ask.skill.dab4a8db-1c4c-4ab5-989f-0d2dbcff17cc'; //OPTIONAL: replace with "amzn1.echo-sdk-ams.app.[your-unique-value-here]";
var SKILL_NAME = 'Child health';
/**
* Array containing space facts.
*
*/
var output ;
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
var obj = JSON.parse(body);
var arr = obj.feeds;
output = arr[0].field1;
}
});
exports.handler = function(event, context, callback) {
var alexa = Alexa.handler(event, context);
alexa.APP_ID = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};
var handlers = {
'LaunchRequest': function () {
this.emit('GetTemp');
},
'GetNewAdviceIntent': function () {
this.emit('GetTemp');
},
'GetTemp': function () {
var speechOutput = output;
this.emit(':tellWithCard', speechOutput, SKILL_NAME, output)
},
'AMAZON.HelpIntent': function () {
var speechOutput = "You can say what is the temperature, or, you can say exit... What can I help you with?";
var reprompt = "What can I help you with?";
this.emit(':ask', speechOutput, reprompt);
},
'AMAZON.CancelIntent': function () {
this.emit(':tell', 'Goodbye!');
},
'AMAZON.PauseIntent': function () {
this.emit(':tell', 'paused');
},
'AMAZON.ResumeIntent': function () {
this.emit(':tell', 'app is resumed');
},
'AMAZON.StopIntent': function () {
this.emit(':tell', 'Goodbye!');
}
};
save the program in the lambda console and test the output in the developer console.
Comments