Skip to content

Instantly share code, notes, and snippets.

@jenschr
Last active March 1, 2024 08:04
Show Gist options
  • Save jenschr/69e1827cba44f892483a5ec0cf189f70 to your computer and use it in GitHub Desktop.
Save jenschr/69e1827cba44f892483a5ec0cf189f70 to your computer and use it in GitHub Desktop.
Basic code for setting a Dweet with a GET request on an ESP32
#include <WiFi.h>
/*
* Basic code for setting a Dweet with a GET request on an ESP32
*
* Original by Liz Miller (www.learnrobotics.org)
* Updated for ESP32 by https://gist.github.com/jenschr
*/
// WiFi parameters
const char* ssid = "your-ssid"; // Replace with your ssid
const char* password = "your-password"; // Replace with your pw
String thingName = "nameOfYourTing"; // The name you want to use for this device
const char* host = "dweet.io"; // The host for the tcp connection
const int httpPort = 80; // The port to use
// data to send
String arrayVariableNames[] = {"fruit"};
String arrayVariableValues[] = {"apricotzzz"};
// Amount of items to send
int numberVariables = sizeof(arrayVariableValues) / sizeof(arrayVariableValues[0]);
void setup() {
Serial.begin(115200);
delay(1000);
// Connect to WiFi Network
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 connection 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() {
String queryString = "";
for (int i = 0; i < (numberVariables); i++) {
if (i == numberVariables - 1)
{
//the lastone doesnt have a "&" at the end
queryString += String(arrayVariableNames[i]) + "=" + String(arrayVariableValues[i]);
}
else
{
queryString += String(arrayVariableNames[i]) + "=" + String(arrayVariableValues[i]) + "&";
}
}
String dweetHttpGet = "GET /dweet/for/" + String(thingName) + "?"+queryString+" HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n";
return dweetHttpGet;
}
/*
Sends the dweet to dweet.io
*/
void sendDweet() {
WiFiClient client;
//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() {
// 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(22000); //refresh rate (send a dweet every 22 seconds...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment