Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Libraries
- #include <DHT.h>
- #include <ESP8266WiFi.h>
- #include <ArduinoJson.h>
- #define DHTPIN 2 // what digital pin we're connected to
- #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
- DHT dht(DHTPIN, DHTTYPE);
- // ACCOUNT SETUP CONSTANTS
- const int nodeID = ESP.getChipId();
- const char* ssid = "Belkin.32C7"; // WiFi Router SSID *Usually on side or bottom of router*
- const char* password = "44579397"; // WiFi Router Password *Usually on side or bottom of router*
- const char* apiHost = "192.168.2.3"; // Local cannacle API server *the PCduino's assigned Hostname*
- const int apiPort = 80;
- void setup()
- {
- Serial.begin(57600);
- dht.begin();
- delay(2000);
- // We start by connecting to a WiFi network...
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- }
- Serial.print("connecting to ");
- Serial.println(apiHost);
- // Variables
- String url;
- String request;
- // Use WiFiClient class to create TCP connections
- WiFiClient client;
- if (!client.connect(apiHost, apiPort)) {
- Serial.println(String(apiHost) + " : connection failed");
- return;
- }
- Serial.print("Requesting URL: ");
- Serial.println(url);
- // Read Temp & Humdity data and store it to variables...
- float humidity = dht.readHumidity();
- float airTempC = dht.readTemperature();
- float airTempF = dht.readTemperature(true);
- StaticJsonBuffer<200> jsonBuffer;
- JsonObject& device = jsonBuffer.createObject();
- device["node_id"] = nodeID;
- device["sensors"] = "DHT22";
- JsonObject& data = device.createNestedObject("data");
- data["humidity"] = String(humidity); // 6 is the number of decimals to print
- data["air_tempC"] = String(airTempC); // if not specified, 2 digits are printed
- data["air_tempF"] = String(airTempF);
- device.printTo(request);
- // We now create a URI for the request
- url = "/?request=" + urlencode(request);
- // This will send the request to the server
- client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + apiHost + "\r\n" + "Connection: close\r\n\r\n");
- delay(1000);
- // Read all the lines of the reply from server and print them to Serial
- String response;
- long sleepTime = 0;
- while(client.available()){
- response = client.readString();
- }
- Serial.print(response);
- JsonObject& responseObj = jsonBuffer.parseObject(response);
- if (!responseObj.success()){Serial.println("B0rken!");}
- sleepTime = responseObj["sleep_interval"];
- if (sleepTime == 0) {
- sleepTime = 600000000;
- }
- ESP.deepSleep(sleepTime);
- delay(1000);
- }
- //
- //
- void loop()
- {
- }
- //
- //
- String urldecode(String str)
- {
- String encodedString="";
- char c;
- char code0;
- char code1;
- for (int i =0; i < str.length(); i++){
- c=str.charAt(i);
- if (c == '+'){
- encodedString+=' ';
- }else if (c == '%') {
- i++;
- code0=str.charAt(i);
- i++;
- code1=str.charAt(i);
- c = (h2int(code0) << 4) | h2int(code1);
- encodedString+=c;
- } else{
- encodedString+=c;
- }
- yield();
- }
- return encodedString;
- }
- String urlencode(String str)
- {
- String encodedString="";
- char c;
- char code0;
- char code1;
- char code2;
- for (int i =0; i < str.length(); i++){
- c=str.charAt(i);
- if (c == ' '){
- encodedString+= '+';
- } else if (isalnum(c)){
- encodedString+=c;
- } else{
- code1=(c & 0xf)+'0';
- if ((c & 0xf) >9){
- code1=(c & 0xf) - 10 + 'A';
- }
- c=(c>>4)&0xf;
- code0=c+'0';
- if (c > 9){
- code0=c - 10 + 'A';
- }
- code2='\0';
- encodedString+='%';
- encodedString+=code0;
- encodedString+=code1;
- //encodedString+=code2;
- }
- yield();
- }
- return encodedString;
- }
- unsigned char h2int(char c)
- {
- if (c >= '0' && c <='9'){
- return((unsigned char)c - '0');
- }
- if (c >= 'a' && c <='f'){
- return((unsigned char)c - 'a' + 10);
- }
- if (c >= 'A' && c <='F'){
- return((unsigned char)c - 'A' + 10);
- }
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement