Difference between revisions of "Note to mitul(eastern)"

From Wiki2
(Created page with "===notes on esp8266=== Hi Mitul, My best esp8266 code is in https://github.com/mckennatim/demiot. A couple of notes on it. You can always get it online by hard coding your S...")
 
 
(3 intermediate revisions by the same user not shown)
Line 6: Line 6:
You can always get it online by hard coding your SSID info ala...
You can always get it online by hard coding your SSID info ala...


#include <ESP8266WiFi.h>
    #include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
    #include <ESP8266WebServer.h>
   
const char *ssid = "street_no_vale2";
    const char *ssid = "street_no_vale2";
const char *pwd = "jjjjjjjj";
    const char *pwd = "jjjjjjjj";
   
ESP8266WebServer server(80);
    ESP8266WebServer server(80);
   
void handleRoot() {
    void handleRoot() {
server.send(200, "text/html", "<h1>root of espAPsb AP server</h1>");
      server.send(200, "text/html", "h1 root of espAPsb AP server /h1");
}
    }
   
void getOnline(){
    void getOnline(){
WiFi.begin(ssid, pwd);
      WiFi.begin(ssid, pwd);
  int tries =0;
      int tries =0;
  int success=1;
      int success=1;
  while (WiFi.status() != WL_CONNECTED ) {
      while (WiFi.status() != WL_CONNECTED ) {
    delay(500);
        delay(500);
    Serial.print(".");
        Serial.print(".");
    tries++;
        tries++;
    if (tries==15){
        if (tries==15){
      success=0;
          success=0;
      scan();
          scan();
      setupAP();
          setupAP();
      break;
          break;
    }
        }
  }
      }
  if (success){
      if (success){
    Serial.println("");
        Serial.println("");
    Serial.println("WiFi connected");
        Serial.println("WiFi connected");
    Serial.print("IP address: ");
        Serial.print("IP address: ");
    Serial.println(WiFi.localIP());     
        Serial.println(WiFi.localIP());     
  }
      }  
}
    }


void setup(){
    void setup(){
Serial.begin(115200);
      Serial.begin(115200);
Serial.println();
      Serial.println();
Serial.println("--------------------------");
      Serial.println("--------------------------");
  Serial.println("ESP8266 webconfig");
      Serial.println("ESP8266 webconfig");
  Serial.println("--------------------------");
      Serial.println("--------------------------");
  getOnline();
      getOnline();
}
    }


void loop(){
    void loop(){
server.handleClient();
      server.handleClient();
}
    }
 
but that is not at all interesting for a couple of reasons
# you shouldn't have to be reconnect and upload over USB every time you move to a new SSID
# this sets you up as a server instead of client. There are lots of downsides to that. (your customers have to open ports on their routers ala xbox, an outside server&clients needs to keep track of its IP...)
 
So you need to be able to webconfig the thing and then run it as a client.
 
In denmiot/essp8266/mqttall I broke out webconfig in `#include "config.h"` (excuse the `extern` globals).
 
setup calls getOnline() which reads a config from the EEPROM and connects but if that fails it jumps into webconfig mode, turning itself into and access point server with an SSID of `espAPsb` and an ip of 192.168.4.1 where you can send it a get string like
 
  http://192.168.4.1/config?ssid=street_no_vale2&pwd=jjjjjjjj&devid=CYURD001&ip=10.0.1.100&port=3332
 
once you send it that it reboots itself (sometimes you need to hit the reset or powerdown) as a client on your local wifi.
 
====mqtt====
mqtt is a very cool pu/sub/ protocol. For some reason though it won't automatically reconnect when you do a webconfig. You have to power down first

Latest revision as of 10:18, 5 May 2016

notes on esp8266

Hi Mitul,

My best esp8266 code is in https://github.com/mckennatim/demiot. A couple of notes on it.

You can always get it online by hard coding your SSID info ala...

   #include <ESP8266WiFi.h>
   #include <ESP8266WebServer.h>
   
   const char *ssid = "street_no_vale2";
   const char *pwd = "jjjjjjjj";
   
   ESP8266WebServer server(80);
   
   void handleRoot() {
     server.send(200, "text/html", "h1 root of espAPsb AP server /h1");
   }
   
   void getOnline(){
     WiFi.begin(ssid, pwd);
     int tries =0;
     int success=1;
     while (WiFi.status() != WL_CONNECTED ) {
       delay(500);
       Serial.print(".");
       tries++;
       if (tries==15){
         success=0;
         scan();
         setupAP();
         break;
       }
     }
     if (success){
       Serial.println("");
       Serial.println("WiFi connected");
       Serial.print("IP address: ");
       Serial.println(WiFi.localIP());    
     }   
   }
   void setup(){
     Serial.begin(115200);
     Serial.println();
     Serial.println("--------------------------");
     Serial.println("ESP8266 webconfig");
     Serial.println("--------------------------");
     getOnline();
   }
   void loop(){
     server.handleClient();
   }

but that is not at all interesting for a couple of reasons

  1. you shouldn't have to be reconnect and upload over USB every time you move to a new SSID
  2. this sets you up as a server instead of client. There are lots of downsides to that. (your customers have to open ports on their routers ala xbox, an outside server&clients needs to keep track of its IP...)

So you need to be able to webconfig the thing and then run it as a client.

In denmiot/essp8266/mqttall I broke out webconfig in `#include "config.h"` (excuse the `extern` globals).

setup calls getOnline() which reads a config from the EEPROM and connects but if that fails it jumps into webconfig mode, turning itself into and access point server with an SSID of `espAPsb` and an ip of 192.168.4.1 where you can send it a get string like

  http://192.168.4.1/config?ssid=street_no_vale2&pwd=jjjjjjjj&devid=CYURD001&ip=10.0.1.100&port=3332

once you send it that it reboots itself (sometimes you need to hit the reset or powerdown) as a client on your local wifi.

mqtt

mqtt is a very cool pu/sub/ protocol. For some reason though it won't automatically reconnect when you do a webconfig. You have to power down first