Skip to content

Instantly share code, notes, and snippets.

@liz-miller
Created July 10, 2018 16:04
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/023498b003f84291d23113c62747c4b3 to your computer and use it in GitHub Desktop.
Save liz-miller/023498b003f84291d23113c62747c4b3 to your computer and use it in GitHub Desktop.
Publish from ESP8266 to dweet.io (part 1)
#include <Arduino.h>
#include <ESP8266WiFi.h>
/*
* Setup code for publishing Arduino sensors to dweet.io
* Part 1
* 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.
*/
// WiFi parameters
const char* ssid = "#"; //replace with your ssid
const char* password = "#";//replace with your pw
String thingName="ardumini";//thing for grouping all the data together
const char* host = "dweet.io"; //host :) for tcp connection
//Initialize the names & values of our metrics for dweet.io
String arrayVariableNames[]={"Brightness", "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: 1 = staffed, 0 = unstaffed
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; //storage for LDR data
/*
* 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"));
}
// OLD CODE BEGINS HERE ---
/*
* 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");
}
/*
* Main loop - runs forever
*/
void loop(){
ldrData = analogRead(ldr);
Serial.println(ldrData);
arrayVariableValues[0] = ldrData;
arrayVariableNames[0] = "Brightness";
// 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(2000); //refresh rate (send a dweet every 2 seconds...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment