Skip to content

Instantly share code, notes, and snippets.

@liz-miller
Created July 10, 2018 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liz-miller/f7b19227b53dff978d37b1bc57c02a54 to your computer and use it in GitHub Desktop.
Save liz-miller/f7b19227b53dff978d37b1bc57c02a54 to your computer and use it in GitHub Desktop.
Publish from ESP8266 to dweet.io (part 1)
/*
* Debouncing + LED's for publishing Arduino sensors to dweet.io
* Part 2
* Written by Liz Miller
* Provided by Learn Robotics (www.learnrobotics.org)
* Version 1.0
* 7-10-18
* ---
* The Software is provided "AS IS" and "WITH ALL FAULTS,"
* without warranty of any kind, including without limitation
* the warranties of merchantability, fitness for a particular
* purpose and non-infringement.
*
* NOT FOR USE TO CONTROL PRODUCTION EQUIPMENT.
* SERIOUS INJURY COULD RESULT.
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
// WiFi parameters
const char* ssid = "#your ssid";
const char* password = "#your password";
String thingName="Learn_Robotics_NodeMCU";//thing for grouping all the data together
const char* host = "dweet.io"; //host :) for tcp connection
//THIS WAY WE REFER TO EACH VARIABLE AS AN INDEX IN THIS GLOBAL ARRAY.
String arrayVariableNames[]={"Temperature","Status", "Staff"};
int arrayVariableValues[]={0, 0, 0};
//tells the number of arguments inside each array
int numberVariables=sizeof(arrayVariableValues)/sizeof(arrayVariableValues[0]);
int staff; //operator present
int status; //machine status 1 - running, 0 - down
// Button & LED Pin Numbers
const int yellowButton = 15; // the number of the pushbutton pin
const int yellowLED = 14; // the number of the LED pin
const int redButton = 4; // the number of the pushbutton pin
const int redLED = 5; // the number of the LED pin
const int ldr = A0; // the number of the ldr pin
int ldrData;
// Debouncing Variables
int ledStateY = LOW; // the current state of the output pin
int buttonStateY; // the current reading from the input pin
int lastButtonStateY = LOW; // the previous reading from the input pin
int ledStateR = LOW; // the current state of the output pin
int buttonStateR; // the current reading from the input pin
int lastButtonStateR = LOW; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 5; // the debounce time; increase if the output flickers
/*
* Configuration - runs once
*/
void setup() {
// put your setup code here, to run once:
pinMode(ldr,INPUT);
pinMode(redButton,INPUT);
pinMode(yellowButton,INPUT);
pinMode(redLED,OUTPUT);
pinMode(yellowLED,OUTPUT);
// set initial LED state
digitalWrite(yellowLED, ledStateY);
digitalWrite(redLED, ledStateR);
//NodeMCU runs @ 9600 baud
Serial.begin(9600);
//Connect to WiFi Network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
WiFi.begin(ssid, password);
int retries = 0;
while ((WiFi.status() != WL_CONNECTED) && (retries < 15)){
retries++;
delay(500);
Serial.print(".");
}
if(retries>14){
Serial.println(F("WiFi conenction FAILED"));
}
if (WiFi.status() == WL_CONNECTED){
Serial.println(F("WiFi connected!"));
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
Serial.println(F("Setup ready"));
}
/*
* Get the string URL to post dweet
*/
String getDweetString(){
int i = 0;
//use the dweet GET to post to dweet
String dweetHttpGet="GET /dweet/for/";
dweetHttpGet=dweetHttpGet+String(thingName)+"?";
for(i=0;i<(numberVariables);i++){
if(i==numberVariables-1){
//the lastone doesnt have a "&" at the end
dweetHttpGet=dweetHttpGet + String(arrayVariableNames[i]) + "=" + String(arrayVariableValues[i]);
}
else
dweetHttpGet=dweetHttpGet + String(arrayVariableNames[i]) + "="+ String(arrayVariableValues[i]) + "&";
}
dweetHttpGet=dweetHttpGet+" HTTP/1.1\r\n"+
"Host: " +
host +
"\r\n" +
"Connection: close\r\n\r\n";
return dweetHttpGet;//this is our freshly made http string request
}
/*
* Sends the dweet to dweet.io
*/
void sendDweet(){
WiFiClient client;
const int httpPort = 80;
//connect to dweet.io
if (!client.connect(host, httpPort)){
Serial.println("connection failed");
return;
}
client.print(getDweetString());
delay(10); //wait...
while (client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
/*
* Turn all LED's ON
*/
void allLED(int status){
if(status == 1){
digitalWrite(redLED,HIGH);
digitalWrite(yellowLED,HIGH);
Serial.println("LED's on!");
}
else if(status == 0){
digitalWrite(redLED,LOW);
digitalWrite(yellowLED,LOW);
Serial.println("LED's off!");
}
}
/*
* Read LDR brightness from sensor
*/
void getLDRdata(){
ldrData = analogRead(ldr);
//Serial.println(ldrData);
//delay(rate);
}
/*
* Publish dweets (original configuration)
*/
void pubDweet(){
ldrData = 1024-analogRead(ldr);
Serial.print("LDR Data: ");
Serial.println(ldrData);
arrayVariableValues[0] = ldrData;
// display the readings to your serial monitor (locally)
Serial.print("Sending dweet to ");
Serial.print(host);
Serial.print("/follow/");
Serial.print(thingName);
Serial.println();
sendDweet(); //send data to dweet.io
delay(20); //refresh rate (send a dweet every 2 seconds...)
}
/*
* Debouncing code for yellow button
*/
void yellowCtrl(){
// read the state of the switch into a local variable:
int yellowReading = digitalRead(yellowButton);
Serial.print("yellow button state = ");
Serial.println(yellowReading);
// If the switch changed, due to noise or pressing:
if (yellowReading != lastButtonStateY) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (yellowReading != buttonStateY) {
buttonStateY = yellowReading;
// only toggle the LED if the new button state is HIGH
if (buttonStateY == HIGH) {
ledStateY = !ledStateY;
}
if (ledStateY == 1){
staff = 1;
}
else{
staff = 0;
}
arrayVariableValues[2] = staff;
}
}
// set the LED:
digitalWrite(yellowLED, ledStateY);
Serial.print("Yellow LED State = ");
Serial.println(ledStateY);
Serial.print("Yellow Button State = ");
Serial.println(buttonStateY);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonStateY = yellowReading;
}
/*
* Debouncing code for red button
*/
void redCtrl(){
// read the state of the switch into a local variable:
int redReading = digitalRead(redButton);
Serial.print("red button state = ");
Serial.println(redReading);
// If the switch changed, due to noise or pressing:
if (redReading != lastButtonStateR) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (redReading != buttonStateR) {
buttonStateR = redReading;
// only toggle the LED if the new button state is HIGH
if (buttonStateR == HIGH) {
ledStateR = !ledStateR;
}
if (ledStateR == 1){
status = 1;
}
else if(ledStateR == 0){
status = 0;
}
//dweet.io stuff
arrayVariableValues[1] = status;
}
}
// set the LED:
digitalWrite(redLED, ledStateR);
Serial.print("Red LED State = ");
Serial.println(ledStateR);
Serial.print("Red Button State = ");
Serial.println(buttonStateR);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonStateR = redReading;
}
/*
* Main loop - runs forever
*/
void loop(){
yellowCtrl();
redCtrl();
pubDweet();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment