Skip to content

Instantly share code, notes, and snippets.

@liz-miller
Created July 10, 2018 16:06
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/d17a63ca722497f4b703f57dcf1cdb48 to your computer and use it in GitHub Desktop.
Save liz-miller/d17a63ca722497f4b703f57dcf1cdb48 to your computer and use it in GitHub Desktop.
Arduino to dweet.io Sample Code for Wemos D1 Mini
#include <Arduino.h>
#include <ESP8266WiFi.h>
/*
* Setup code for publishing Arduino sensors to dweet.io
* using Wemos D1 Mini
* Full Code Sample
* 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
//THIS WAY WE REFER TO EACH VARIABLE AS AN INDEX IN THIS GLOBAL ARRAY.
String arrayVariableNames[]={"Brightness"};
int arrayVariableValues[]={0};
//tells the number of arguments inside each array
int numberVariables=sizeof(arrayVariableValues)/sizeof(arrayVariableValues[0]);
//INPUTS
int ldrData = 0; //initialize LDR sensor
int ldr = A0; //pin # located on Wemos d1 mini
/*
* Configuration - runs once
*/
void setup() {
// put your setup code here, to run once:
pinMode(ldr,INPUT);
//wemos d1 mini runs @ 115200 baud
Serial.begin(115200);
//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");
}
/*
* 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