Easy Iot read data from ESP8266

8 years 7 months ago - 8 years 7 months ago #2195 by Release12
Hi there,

I hope this isn't too stupid. However I'm trying to make some code by sort of piecing together bits and pieces from different examples and tutorials. This is how I tend to learn languages while still building devices. What I am trying to do is just generate a number on the ESP8266 and then graph that in EasyIoT.


Here is my code, any help would be appreciated:

#include <ESP8266WiFi.h>
#include <Base64.h>
#include <DHT.h>

//AP definitions
#define AP_SSID "*******"
#define AP_PASSWORD "*******"

// EasyIoT server definitions
#define EIOT_USERNAME "admin"
#define EIOT_PASSWORD "test"
#define EIOT_IP_ADDRESS "10.0.0.113"
#define EIOT_PORT 80
#define EIOT_NODE "N13S0"

#define REPORT_INTERVAL 15 // in sec


#define USER_PWD_LEN 40
char unameenc[USER_PWD_LEN];
int test;


void setup() {
Serial.begin(115200);

wifiConnect();

char uname[USER_PWD_LEN];
String str = String(EIOT_USERNAME)+":"+String(EIOT_PASSWORD);
str.toCharArray(uname, USER_PWD_LEN);
memset(unameenc,0,sizeof(unameenc));
base64_encode(unameenc, uname, strlen(uname));
}

void loop() {

delay (5000);
test = test + 1;
Serial.print(test);

WiFiClient client;

while(!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
Serial.println("connection failed");
wifiConnect();
}

String url ("");
url += "/API/EasyIoT/Control/Module/Virtual/"+String(EIOT_NODE) + "/ControlLevel/"+int (test);

Serial.print("Post data to URL:");
Serial.println(url);

client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + String(EIOT_IP_ADDRESS) + "\r\n" +
"Connection: close\r\n" +
"Authorization: Basic " + unameenc + " \r\n" +
"Content-Length: 0\r\n" +
"\r\n");

delay(100);

while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}

Serial.println();
Serial.println("Connection closed");
}

void wifiConnect()
{
Serial.print("Connecting to AP");
WiFi.begin(AP_SSID, AP_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
}
The following user(s) said Thank You: osalval

Please Log in or Create an account to join the conversation.

8 years 7 months ago - 8 years 7 months ago #2229 by gordonendersby
Maybe this will be helpful.
Its the code i put together quickly to test if it works.
Bunged together from several examples.
The key bit that helped was outputting plenty of debug to the serial console.
So the whole http post and response is shown.
This posts the temperature to a virtual device using http post.
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <Base64.h>
 

const char* ssid = "xxxxxxxxxxxx";
const char* password  = "xxxxxxxxxxxxx";

// EasyIoT server definitions
#define EIOT_USERNAME    "admin"
#define EIOT_PASSWORD    "test"
#define EIOT_IP_ADDRESS  "192.168.0.52"
#define EIOT_PORT        80
#define EIOT_NODE        "N5S0"
 
#define DHTPIN 2 // what pin we're connected to


#define USER_PWD_LEN 40
char unameenc[USER_PWD_LEN];
float oldTemp;

DHT dht(DHTPIN, DHT22,15);
WiFiClient client;
   
 
void setup() {                
  Serial.begin(57600);
  delay(10);
  dht.begin();
  
  WiFi.begin(ssid, password);
 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
   
  WiFi.begin(ssid, password);
   
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  char uname[USER_PWD_LEN];
  String str = String(EIOT_USERNAME)+":"+String(EIOT_PASSWORD);  
  str.toCharArray(uname, USER_PWD_LEN); 
  memset(unameenc,0,sizeof(unameenc));
  base64_encode(unameenc, uname, strlen(uname));
  oldTemp = -1;
  
}
 
 
void loop() {
   
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  sendTeperature(t);
 
  
  delay(1000*60*10);  //millis * 60seconds * 10mins
  //delay(1000*20);  //millis * 60seconds * 10mins
}

void sendTeperature(float temp)
{  
   WiFiClient client;
   
   while(!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
    Serial.println("connection failed");
    //wifiConnect(); 
  }
 
  String url = "";
  url += "/Api/EasyIoT/Control/Module/Virtual/"+ String(EIOT_NODE) + "/ControlLevel/"+String(temp); // generate EasIoT server node URL

  Serial.print("POST data to URL: ");
  Serial.println(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + String(EIOT_IP_ADDRESS) + "\r\n" + 
               "Connection: close\r\n" + 
               "Authorization: Basic " + unameenc + " \r\n" + 
               "Content-Length: 0\r\n" + 
               "\r\n");
  
  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + String(EIOT_IP_ADDRESS) + "\r\n" + 
               "Connection: close\r\n" + 
               "Authorization: Basic " + unameenc + " \r\n" + 
               "Content-Length: 0\r\n" + 
               "\r\n");

  delay(100);
    while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("Connection closed");
}

Please Log in or Create an account to join the conversation.

Time to create page: 0.257 seconds

Forum latest

  • No posts to display.