Commit 05e868d1 authored by JackV's avatar JackV
Browse files

First implementation, testing needed

parents
Copyright 2017 Giacomo Vercesi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ESQTT
This is a sketch for the esp8266 microcontroller that allows its control
from a mqtt server messages on specific topics (see doc/topics.md for details)
## Building
In order to compile this sketch some special care must be taken
### Steps:
1. Download arduino ide
2. [Install the esp8266 core, git version](https://github.com/esp8266/Arduino#using-git-version)
3. navigate to the \<arduino directory\>/hardware/esp8266com/esp8266 and run
```wget -q https://patch-diff.githubusercontent.com/raw/esp8266/Arduino/pull/2745.diff -O - | patch -p1```
4. Compile the sketch with the arduino ide and upload it to the esp
=== EsQtt Channels ===
<root> is defined as MQTT_ROOT plus "esp_<ID>"
| Node | Description | Possible Values |
| --- | --- | --- |
| `<root>/gpio/<#>/setmode` | Sets the mode of the pin | 0 = INPUT, 1 = OUTPUT, 2 = INPUT_PULLUP, 3 = OUTPUT_OPEN_DRAIN |
| `<root>/gpio/<#>/writevalue` | if pin is OUTPUT, write value | 0,1 |
| `<root>/gpio/<#>/readvalue` | read the value on pin | * |
| `<root>/gpio/<#>/value` | reading from readvalue are sent here | 0,1 |
| `<root>/gpio/<#>/enableisr` | enables ISR on pin | 0 = DISABLE, 1 = RISING, 2 = FALLING, 3 = CHANGE, 4 = ONLOW, 5 = ONHIGH |
| `<root>/gpio/<#>/interrupt` | Message sent on this topic when interrupt occurs | * |
| `<root>/adc/readvalue` | Read the value of the ADC pin | * |
| `<root>/adc/value` | values read on adc are sent here | 0-1023 |
| `<root>/hello` | ESP sends a hello on this topic on connection | * |
| `<root>/debug` | ESP sends debug messages (if enabled) on this channel | * |
#include <MQTT.h>
#include <ESP8266WiFi.h>
#include <HardwareSerial.h>
#include "settings.h"
#include <stdio.h>
#include <stdint.h>
#include <functional>
#include <FunctionalInterrupt.h>
void mqtt_onConnected();
void mqtt_onDisconnected();
void mqtt_onMessage(String& topic, String& data);
bool serial_debug = false;
MQTT mqtt("",MQTT_IP,MQTT_PORT);
String id = String(ESP.getChipId(), HEX);
String mqtt_root = String("") + MQTT_ROOT + "esp_" + String(id);
void print_debug(String Message){
if(serial_debug)
Serial.println(Message);
if(mqtt.isConnected() && MQTT_DEBUG)
mqtt.publish(mqtt_root + "/debug", Message, 2);
}
void setup() {
pinMode(4, INPUT);
if( digitalRead(4) ){
Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY);
serial_debug = true;
}
mqtt.setClientId(String("esp_"+id).c_str());
WiFi.begin(W_SSID, W_PASS);
while( WiFi.status() != WL_CONNECTED );
if(serial_debug)
Serial.println('Connected');
mqtt.onConnected(mqtt_onConnect);
mqtt.onDisconnected(mqtt_onDisconnect);
mqtt.onData(mqtt_onMessage);
mqtt.connect();
}
void loop() {}
void mqtt_onConnect(){
print_debug("Connected!");
mqtt.publish(mqtt_root + "/hello","Hello from ESP " + id);
mqtt.subscribe(String(mqtt_root + "/#"));
}
void mqtt_onDisconnect(){
if( WiFi.status() != WL_CONNECTED)
while( WiFi.status() != WL_CONNECTED );
if( !mqtt.isConnected() ){
mqtt.connect();
delay(100);
}
}
void mqtt_onMessage(String& topic, String& data){
String subtopic = topic.substring(mqtt_root.length() + 1);
if( subtopic.startsWith("gpio") ){
int gpio;
String op;
char _op[21];
int _sr = sscanf(subtopic.c_str(),"gpio/%d/%20s",&gpio,&op);
if( _sr < 2 || gpio < 0 || gpio > MAX_GPIO ){
print_debug(String("Malformed message\n"));
return;
}
op = String(_op);
if( op == "setmode" ){
int mode = data.toInt();
if( mode <= 3 ){
pinMode(gpio, data.toInt());
}
}
if( op == "writevalue" ){
digitalWrite(gpio, data.toInt());
}
if( op == "readvalue" ){
String topic = String(mqtt_root + "/gpio/" + gpio + "/value");
String value = String(digitalRead(gpio));
mqtt.publish(topic, value);
}
if( op == "enableisr" ){
int mode = data.toInt();
if( mode > 0 && mode <= 5 ){
attachInterrupt((uint8_t) gpio, std::bind(isr_handler, gpio), data.toInt());
}
}
}
if( subtopic.startsWith("adc/readvalue") ){
String topic = String(mqtt_root + "/adc/value");
String reading = String(analogRead(A0));
mqtt.publish(topic, reading);
}
}
void isr_handler(int gpio){
String topic = String(mqtt_root + "/gpio/" + gpio + "/interrupt");
mqtt.publish(topic,"", 1);
}
#ifndef __SETTINGS_H
#define __SETTINGS_H
#define MQTT_IP "192.168.0.1"
#define MQTT_PORT 1883
#define MQTT_ROOT ""
#define MQTT_DEBUG true
#define W_SSID "test"
#define W_PASS "testpass"
#define MAX_GPIO 16
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment