c_cpp 无线样反esp8266.ino
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 无线样反esp8266.ino相关的知识,希望对你有一定的参考价值。
#define DEBUG_MODE
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <MqttWrapper.h>
#include <WiFiHelper.h>
#include <PubSubClient.h>
const char* ssid = "CMMC.47";
const char* pass = "guestnetwork";
MqttWrapper *mqtt;
WiFiHelper *wifi;
#include "Wire.h" // enable I2C bus
byte saa1064 = 0x70 >> 1; // define the I2C bus address for our SAA1064 (pin 1 to GND)
int digits[17] = {
63, 6, 91, 79, 102, 109, 125, 7, 127, 111, 119, 124, 57, 94, 121, 113, 0
};
void callback(const MQTT::Publish& pub)
{
if (pub.payload_string() == "0")
{
Serial.print(" => ");
Serial.println(pub.payload_string());
}
else if (pub.payload_string() == "1")
{
Serial.print(" => ");
Serial.println(pub.payload_string());
}
else
{
Serial.print(pub.topic());
Serial.print(" => ");
displayInteger(atoi(pub.payload_string().c_str()), 0);
}
}
void hook_prepare_data(JsonObject** root)
{
JsonObject& data = (*(*root))["d"];
data["myName"] = "LIKE COUNTER";
}
void hook_configuration(MqttWrapper::Config config)
{
uint8_t mac[6];
WiFi.macAddress(mac);
String result;
for (int i = 0; i < 6; ++i)
{
result += String(mac[i], 16);
}
*(config.clientId) = String("d:quickstart:arduino:") + result;
*(config.channelId) = String("esp8266/cmmakerclub.com/likes");
//*(config.topicPub) = "esp8266/cmmakerclub.com/likes/status";
//*(config.topicSub) = "esp8266/cmmakerclub.com/likes/count";
}
void hook_publish_data(char* data)
{
Serial.print("PUBLISH: ->");
Serial.println(data);
}
void init_wifi()
{
wifi = new WiFiHelper(ssid, pass);
wifi->on_connected([](const char* message)
{
Serial.println (message);
});
wifi->on_disconnected([](const char* message)
{
Serial.println (message);
});
wifi->begin();
}
void init_mqtt()
{
mqtt = new MqttWrapper("128.199.104.122", 1883, hook_configuration);
mqtt->connect(callback);
mqtt->set_prepare_data_hook(hook_prepare_data, 5000);
mqtt->set_publish_data_hook(hook_publish_data);
}
void initDisplay()
// turns on dynamic mode and adjusts segment current to 12mA
{
Wire.beginTransmission(saa1064);
Wire.write(B00000000); // this is the instruction byte. Zero means the next byte is the control byte
Wire.write(B01000111); // control byte (dynamic mode on, digits 1+3 on, digits 2+4 on, 12mA segment current
Wire.endTransmission();
}
void clearDisplay()
{
Wire.beginTransmission(saa1064);
Wire.write(1); // start with digit 1 (right-hand side)
Wire.write(0); // blank digit 1
Wire.write(0); // blank digit 2
Wire.write(0); // blank digit 3
Wire.write(0); // blank digit 4
Wire.endTransmission();
}
void displayInteger(int num, int zero)
// displays the number 'num'
// zero = 0 - no leading zero
// zero = 1 - leading zero
{
int thousand, hundred, ten, one;
// breakdown number into columns
thousand = num / 1000;
hundred = (num - (thousand * 1000)) / 100;
ten = (num - ((thousand * 1000) + (hundred * 100))) / 10;
one = num - ((thousand * 1000) + (hundred * 100) + (ten * 10));
if (zero == 1) // yes to leading zero
{
Wire.beginTransmission(saa1064);
Wire.write(1);
Wire.write(digits[one]);
Wire.write(digits[ten]);
Wire.write(digits[hundred]);
Wire.write(digits[thousand]);
Wire.endTransmission();
delay(10);
}
else if (zero == 0) // no to leading zero
{
if (thousand == 0) {
thousand = 16;
}
if (hundred == 0 && num < 100) {
hundred = 16;
}
if (ten == 0 && num < 10) {
ten = 16;
}
Wire.beginTransmission(saa1064);
Wire.write(1);
Wire.write(digits[one]);
Wire.write(digits[ten]);
Wire.write(digits[hundred]);
Wire.write(digits[thousand]);
Wire.endTransmission();
delay(10);
}
}
void init_hardware()
{
Serial.begin(115200);
pinMode(0, INPUT_PULLUP);
delay(10);
Serial.println();
Serial.println();
Wire.begin(); // start up I2C bus
delay(500);
initDisplay();
pinMode(15, OUTPUT); //direction
pinMode(16, OUTPUT); //pwm
}
void setup()
{
init_hardware();
init_wifi();
init_mqtt();
}
void loop()
{
wifi->loop();
mqtt->loop();
}
以上是关于c_cpp 无线样反esp8266.ino的主要内容,如果未能解决你的问题,请参考以下文章