Skip to content

Instantly share code, notes, and snippets.

@vindolin

vindolin/wifisensor.ino

Last active Aug 29, 2015
Embed
What would you like to do?
#include <SoftwareSerial.h>
#include <DHT.h>
#define DHTTYPE DHT22
#define DHTPIN 12
#define LED_PIN 13
DHT dht(DHTPIN, DHTTYPE);
SoftwareSerial dbgSerial(2, 3); // RX, TX
char URL[] = "\"64.94.18.120\",80";
int request_len = 0;
float temperature;
float humidity;
char CRLF[] = "\r\n";
bool led_state = false;
void init_wifi() {
send_cmd("AT+RST");
if(Serial.find("Ready")) {
dbgSerial.println("ready!");
digitalWrite(LED_PIN, LOW);
}
delay(5000);
send_cmd("AT+CIFSR");
dbgSerial.println(Serial.readString());
}
// copy chars from one char array into another
void tcpy(char dest[], char source[], int offset) {
for(int i = 0; i < strlen(source); i++) {
dest[offset + i] = source[i];
}
}
bool xively_put(int temperature, int humidity) {
char temperature_c[3];
itoa(temperature, temperature_c, 10);
char humidity_c[3];
itoa(humidity, humidity_c, 10);
char json[] = "{\"version\":\"1.0.0\", \"datastreams\": ["
"{\"id\": \"temperature\", \"current_value\": \"00\"}"
", {\"id\": \"humidity\", \"current_value\": \"00\"}"
"]}\n";
tcpy(json, temperature_c, 76);
tcpy(json, humidity_c, 119);
// dbgSerial.println(json);
char head[] = "PUT /v2/feeds/897714818 HTTP/1.1\r\n"
"User-Agent: ESP8266\r\n"
"Host: 64.94.18.120\r\n"
"Content-Type: application/json\r\n"
"Content-Length: \r\n" // the spaces are a placeholder for the real length number.
"X-ApiKey: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\r\n"
"\r\n";
char content_length[4];
itoa(strlen(json), content_length, 10);
tcpy(head, content_length, 120);
char request_length[4];
itoa(strlen(head) + strlen(json), request_length, 10);
send_part("AT+CIPSTART=\"TCP\",");
send_part(URL);
send_part(CRLF);
if(Serial.find("Linked")) {
dbgSerial.println("Linked!");
}
send_part("AT+CIPSEND=");
send_part(request_length);
send_part(CRLF);
send_part(head);
send_part(json);
if(Serial.find("X-Request-Id: ")) {
dbgSerial.println("found X-Request-Id");
String request_id = Serial.readStringUntil('\n');
dbgSerial.println(request_id);
return true;
} else {
dbgSerial.println("nope :(");
return false;
}
}
void send_cmd(char cmd[]) {
dbgSerial.print(cmd);
dbgSerial.print("\r\n");
Serial.print(cmd);
Serial.print("\r\n");
}
void send_part(char cmd[]) {
dbgSerial.print(cmd);
Serial.print(cmd);
}
void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
dbgSerial.begin(9600);
dbgSerial.println("Init");
Serial.begin(9600);
Serial.setTimeout(4000);
dht.begin();
init_wifi();
}
void loop() {
humidity = (int) dht.readHumidity();
temperature = (int) dht.readTemperature();
if(! xively_put(temperature, humidity)) {
init_wifi();
}
led_state != led_state;
digitalWrite(LED_PIN, led_state);
delay(30000);
}
@spencerkoch

This comment has been minimized.

Copy link

@spencerkoch spencerkoch commented Dec 1, 2014

any hope of getting a pared down version without software serial running? I'm trying to run this on a trinket pro (native 3.3V) but I think software serial doesn't run at the clock speed that my board runs at.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.